mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 5 | """ |
| 6 | This module defines the SSHHost class. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| 8 | Implementation details: |
| 9 | You should import the "hosts" package instead of importing each type of host. |
| 10 | |
| 11 | SSHHost: a remote machine with a ssh access |
| 12 | """ |
| 13 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 14 | __author__ = """ |
| 15 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | poirier@google.com (Benjamin Poirier), |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 17 | stutsman@google.com (Ryan Stutsman) |
| 18 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 19 | |
| 20 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 21 | import types, os, time, re |
| 22 | import base_classes, utils, errors, bootloader |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class SSHHost(base_classes.RemoteHost): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 26 | """ |
| 27 | This class represents a remote machine controlled through an ssh |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 28 | session on which you can run programs. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 29 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 30 | It is not the machine autoserv is running on. The machine must be |
| 31 | configured for password-less login, for example through public key |
| 32 | authentication. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 33 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 34 | Implementation details: |
| 35 | This is a leaf class in an abstract class hierarchy, it must |
| 36 | implement the unimplemented methods in parent classes. |
| 37 | """ |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 38 | |
mbligh | 137a05c | 2007-10-04 15:56:51 +0000 | [diff] [blame] | 39 | def __init__(self, hostname, user="root", port=22, initialize=True): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 40 | """ |
| 41 | Construct a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 42 | |
| 43 | Args: |
| 44 | hostname: network hostname or address of remote machine |
| 45 | user: user to log in as on the remote machine |
| 46 | port: port the ssh daemon is listening on on the remote |
| 47 | machine |
| 48 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 49 | self.hostname= hostname |
| 50 | self.user= user |
| 51 | self.port= port |
| 52 | self.tmp_dirs= [] |
mbligh | 137a05c | 2007-10-04 15:56:51 +0000 | [diff] [blame] | 53 | self.initialize = initialize |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 54 | |
| 55 | super(SSHHost, self).__init__() |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 56 | self.bootloader = bootloader.Bootloader(self) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 57 | |
| 58 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 59 | def __del__(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 60 | """ |
| 61 | Destroy a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 62 | """ |
| 63 | for dir in self.tmp_dirs: |
| 64 | try: |
| 65 | self.run('rm -rf "%s"' % (utils.sh_escape(dir))) |
| 66 | except errors.AutoservRunError: |
| 67 | pass |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 68 | |
| 69 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 70 | def run(self, command, timeout=None, ignore_status=False): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 71 | """ |
| 72 | Run a command on the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 73 | |
| 74 | Args: |
| 75 | command: the command line string |
| 76 | timeout: time limit in seconds before attempting to |
| 77 | kill the running process. The run() function |
| 78 | will take a few seconds longer than 'timeout' |
| 79 | to complete if it has to kill the process. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 80 | ignore_status: do not raise an exception, no matter |
| 81 | what the exit code of the command is. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 82 | |
| 83 | Returns: |
| 84 | a hosts.base_classes.CmdResult object |
| 85 | |
| 86 | Raises: |
| 87 | AutoservRunError: the exit code of the command |
| 88 | execution was not 0 |
| 89 | """ |
| 90 | #~ print "running %s" % (command,) |
| 91 | result= utils.run(r'ssh -l %s -p %d %s "%s"' % (self.user, |
| 92 | self.port, self.hostname, utils.sh_escape(command)), |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 93 | timeout, ignore_status) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 94 | return result |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 95 | |
| 96 | |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 97 | def reboot(self, timeout=600, label=None, kernel_args=None, wait=True): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 98 | """ |
| 99 | Reboot the remote host. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 100 | |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 101 | Args: |
| 102 | timeout |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 103 | """ |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 104 | if label or kernel_args: |
| 105 | self.bootloader.install_boottool() |
| 106 | if label: |
| 107 | self.bootloader.set_default(label) |
| 108 | if kernel_args: |
| 109 | if not label: |
| 110 | default = int(self.bootloader.get_default()) |
| 111 | label = self.bootloader.get_titles()[default] |
| 112 | self.bootloader.add_args(label, kernel_args) |
mbligh | d742a22 | 2007-09-30 01:27:06 +0000 | [diff] [blame] | 113 | print "Reboot: initiating reboot" |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 114 | self.run('reboot') |
| 115 | if wait: |
| 116 | self.wait_down(60) # Make sure he's dead, Jim |
| 117 | print "Reboot: machine has gone down" |
| 118 | self.wait_up(timeout) |
| 119 | time.sleep(2) # this is needed for complete reliability |
| 120 | self.wait_up(timeout) |
| 121 | print "Reboot complete" |
| 122 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 123 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 124 | def get_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 125 | """ |
| 126 | Copy files from the remote host to a local path. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 127 | |
| 128 | Directories will be copied recursively. |
| 129 | If a source component is a directory with a trailing slash, |
| 130 | the content of the directory will be copied, otherwise, the |
| 131 | directory itself and its content will be copied. This |
| 132 | behavior is similar to that of the program 'rsync'. |
| 133 | |
| 134 | Args: |
| 135 | source: either |
| 136 | 1) a single file or directory, as a string |
| 137 | 2) a list of one or more (possibly mixed) |
| 138 | files or directories |
| 139 | dest: a file or a directory (if source contains a |
| 140 | directory or more than one element, you must |
| 141 | supply a directory dest) |
| 142 | |
| 143 | Raises: |
| 144 | AutoservRunError: the scp command failed |
| 145 | """ |
| 146 | if isinstance(source, types.StringTypes): |
| 147 | source= [source] |
| 148 | |
| 149 | processed_source= [] |
| 150 | for entry in source: |
| 151 | if entry.endswith('/'): |
| 152 | format_string= '%s@%s:"%s*"' |
| 153 | else: |
| 154 | format_string= '%s@%s:"%s"' |
| 155 | entry= format_string % (self.user, self.hostname, |
| 156 | utils.scp_remote_escape(entry)) |
| 157 | processed_source.append(entry) |
| 158 | |
| 159 | processed_dest= os.path.abspath(dest) |
| 160 | if os.path.isdir(dest): |
| 161 | processed_dest= "%s/" % (utils.sh_escape(processed_dest),) |
| 162 | else: |
| 163 | processed_dest= utils.sh_escape(processed_dest) |
| 164 | |
| 165 | utils.run('scp -rpq %s "%s"' % ( |
| 166 | " ".join(processed_source), |
| 167 | processed_dest)) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 168 | |
| 169 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 170 | def send_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 171 | """ |
| 172 | Copy files from a local path to the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 173 | |
| 174 | Directories will be copied recursively. |
| 175 | If a source component is a directory with a trailing slash, |
| 176 | the content of the directory will be copied, otherwise, the |
| 177 | directory itself and its content will be copied. This |
| 178 | behavior is similar to that of the program 'rsync'. |
| 179 | |
| 180 | Args: |
| 181 | source: either |
| 182 | 1) a single file or directory, as a string |
| 183 | 2) a list of one or more (possibly mixed) |
| 184 | files or directories |
| 185 | dest: a file or a directory (if source contains a |
| 186 | directory or more than one element, you must |
| 187 | supply a directory dest) |
| 188 | |
| 189 | Raises: |
| 190 | AutoservRunError: the scp command failed |
| 191 | """ |
| 192 | if isinstance(source, types.StringTypes): |
| 193 | source= [source] |
| 194 | |
| 195 | processed_source= [] |
| 196 | for entry in source: |
| 197 | if entry.endswith('/'): |
| 198 | format_string= '"%s/"*' |
| 199 | else: |
| 200 | format_string= '"%s"' |
| 201 | entry= format_string % (utils.sh_escape(os.path.abspath(entry)),) |
| 202 | processed_source.append(entry) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 203 | |
mbligh | 47c5400 | 2007-09-10 18:31:02 +0000 | [diff] [blame] | 204 | result= utils.run('ssh -l %s %s rsync -h' % (self.user, |
| 205 | self.hostname), |
| 206 | ignore_status=True) |
mbligh | d566909 | 2007-08-27 19:01:05 +0000 | [diff] [blame] | 207 | |
| 208 | if result.exit_status == 0: |
mbligh | d6dc1fc | 2007-09-11 21:21:02 +0000 | [diff] [blame] | 209 | utils.run('rsync --rsh=ssh -az %s %s@%s:"%s"' % ( |
mbligh | d566909 | 2007-08-27 19:01:05 +0000 | [diff] [blame] | 210 | " ".join(processed_source), self.user, |
| 211 | self.hostname, utils.scp_remote_escape(dest))) |
| 212 | else: |
| 213 | utils.run('scp -rpq %s %s@%s:"%s"' % ( |
| 214 | " ".join(processed_source), self.user, |
| 215 | self.hostname, utils.scp_remote_escape(dest))) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 216 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 217 | def get_tmp_dir(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 218 | """ |
| 219 | Return the pathname of a directory on the host suitable |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 220 | for temporary file storage. |
| 221 | |
| 222 | The directory and its content will be deleted automatically |
| 223 | on the destruction of the Host object that was used to obtain |
| 224 | it. |
| 225 | """ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 226 | dir_name= self.run("mktemp -d /tmp/autoserv-XXXXXX").stdout.rstrip(" \n") |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 227 | self.tmp_dirs.append(dir_name) |
| 228 | return dir_name |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 229 | |
| 230 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 231 | def is_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 232 | """ |
| 233 | Check if the remote host is up. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 234 | |
| 235 | Returns: |
| 236 | True if the remote host is up, False otherwise |
| 237 | """ |
| 238 | try: |
| 239 | result= self.run("true", timeout=10) |
| 240 | except errors.AutoservRunError: |
| 241 | return False |
| 242 | else: |
| 243 | if result.exit_status == 0: |
| 244 | return True |
| 245 | else: |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 246 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 247 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 248 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 249 | def wait_up(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 250 | """ |
| 251 | Wait until the remote host is up or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 252 | |
| 253 | In fact, it will wait until an ssh connection to the remote |
| 254 | host can be established. |
| 255 | |
| 256 | Args: |
| 257 | timeout: time limit in seconds before returning even |
| 258 | if the host is not up. |
| 259 | |
| 260 | Returns: |
| 261 | True if the host was found to be up, False otherwise |
| 262 | """ |
| 263 | if timeout: |
| 264 | end_time= time.time() + timeout |
| 265 | |
| 266 | while not timeout or time.time() < end_time: |
| 267 | try: |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 268 | run_timeout= 10 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 269 | result= self.run("true", timeout=run_timeout) |
| 270 | except errors.AutoservRunError: |
| 271 | pass |
| 272 | else: |
| 273 | if result.exit_status == 0: |
| 274 | return True |
| 275 | time.sleep(1) |
| 276 | |
| 277 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 278 | |
| 279 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 280 | def wait_down(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 281 | """ |
| 282 | Wait until the remote host is down or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 283 | |
| 284 | In fact, it will wait until an ssh connection to the remote |
| 285 | host fails. |
| 286 | |
| 287 | Args: |
| 288 | timeout: time limit in seconds before returning even |
| 289 | if the host is not up. |
| 290 | |
| 291 | Returns: |
| 292 | True if the host was found to be down, False otherwise |
| 293 | """ |
| 294 | if timeout: |
| 295 | end_time= time.time() + timeout |
| 296 | |
| 297 | while not timeout or time.time() < end_time: |
| 298 | try: |
mbligh | 7e1e964 | 2007-07-31 18:00:45 +0000 | [diff] [blame] | 299 | run_timeout= 10 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 300 | result= self.run("true", timeout=run_timeout) |
| 301 | except errors.AutoservRunError: |
| 302 | return True |
| 303 | else: |
| 304 | if result.aborted: |
| 305 | return True |
| 306 | time.sleep(1) |
| 307 | |
| 308 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 309 | |
| 310 | |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 311 | def ensure_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 312 | """ |
| 313 | Ensure the host is up if it is not then do not proceed; |
| 314 | this prevents cacading failures of tests |
| 315 | """ |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 316 | print 'Ensuring that %s is up before continuing' % self.hostname |
| 317 | if hasattr(self, 'hardreset') and not self.wait_up(300): |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 318 | print "Performing a hardreset on %s" % self.hostname |
| 319 | self.hardreset() |
| 320 | self.wait_up() |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 321 | print 'Host up, continuing' |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 322 | |
| 323 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 324 | def get_num_cpu(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 325 | """ |
| 326 | Get the number of CPUs in the host according to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 327 | /proc/cpuinfo. |
| 328 | |
| 329 | Returns: |
| 330 | The number of CPUs |
| 331 | """ |
| 332 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 333 | proc_cpuinfo = self.run("cat /proc/cpuinfo").stdout |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 334 | cpus = 0 |
| 335 | for line in proc_cpuinfo.splitlines(): |
| 336 | if line.startswith('processor'): |
| 337 | cpus += 1 |
| 338 | return cpus |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 339 | |
| 340 | |
| 341 | def check_uptime(self): |
| 342 | """ |
| 343 | Check that uptime is available and monotonically increasing. |
| 344 | """ |
| 345 | if not self.ping(): |
| 346 | raise "Client is not pingable" |
| 347 | result = self.run("/bin/cat /proc/uptime", 30) |
| 348 | return result.stdout.strip().split()[0] |
| 349 | |
| 350 | |
| 351 | def get_arch(self): |
| 352 | """ |
| 353 | Get the hardware architecture of the remote machine |
| 354 | """ |
| 355 | arch = self.run('/bin/uname -m').stdout.rstrip() |
| 356 | if re.match(r'i\d86$', arch): |
| 357 | arch = 'i386' |
| 358 | return arch |
| 359 | |
| 360 | |
| 361 | def get_kernel_ver(self): |
| 362 | """ |
| 363 | Get the kernel version of the remote machine |
| 364 | """ |
| 365 | return self.run('/bin/uname -r').stdout.rstrip() |
| 366 | |
| 367 | |
| 368 | def get_cmdline(self): |
| 369 | """ |
| 370 | Get the kernel command line of the remote machine |
| 371 | """ |
| 372 | return self.run('cat /proc/cmdline').stdout.rstrip() |
| 373 | |
| 374 | |
| 375 | def ping(self): |
| 376 | """ |
| 377 | Ping the remote system, and return whether it's available |
| 378 | """ |
| 379 | fpingcmd = "%s -q %s" % ('/usr/bin/fping', self.hostname) |
| 380 | rc = utils.system(fpingcmd, ignore_status = 1) |
| 381 | return (rc == 0) |