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