jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 1 | import os, sys, time, types, socket, traceback, shutil, glob |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2 | from autotest_lib.client.common_lib import error, debug |
jadmanski | 31c49b7 | 2008-10-27 20:44:48 +0000 | [diff] [blame] | 3 | from autotest_lib.server import utils, autotest |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 4 | from autotest_lib.server.hosts import site_host |
| 5 | |
| 6 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 7 | def make_ssh_command(user="root", port=22, opts='', connect_timeout=30): |
| 8 | base_command = ("/usr/bin/ssh -a -x %s -o BatchMode=yes " |
jadmanski | d9365e5 | 2008-10-22 16:55:31 +0000 | [diff] [blame] | 9 | "-o ConnectTimeout=%d -o ServerAliveInterval=300 " |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 10 | "-l %s -p %d") |
| 11 | assert isinstance(connect_timeout, (int, long)) |
| 12 | assert connect_timeout > 0 # can't disable the timeout |
| 13 | return base_command % (opts, connect_timeout, user, port) |
| 14 | |
| 15 | |
| 16 | class AbstractSSHHost(site_host.SiteHost): |
| 17 | """ This class represents a generic implementation of most of the |
| 18 | framework necessary for controlling a host via ssh. It implements |
| 19 | almost all of the abstract Host methods, except for the core |
| 20 | Host.run method. """ |
| 21 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 22 | def _initialize(self, hostname, user="root", port=22, password="", |
| 23 | *args, **dargs): |
| 24 | super(AbstractSSHHost, self)._initialize(hostname=hostname, |
| 25 | *args, **dargs) |
mbligh | 6369cf2 | 2008-10-24 17:21:57 +0000 | [diff] [blame] | 26 | self.ip = socket.getaddrinfo(self.hostname, None)[0][4][0] |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 27 | self.user = user |
| 28 | self.port = port |
| 29 | self.password = password |
| 30 | |
| 31 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 32 | def _encode_remote_path(self, path): |
| 33 | """ Given a file path, encodes it as a remote path, in the style used |
| 34 | by rsync and scp. """ |
| 35 | return '%s@%s:"%s"' % (self.user, self.hostname, |
| 36 | utils.scp_remote_escape(path)) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 37 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 38 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 39 | def _make_rsync_cmd(self, sources, dest, delete_dest): |
| 40 | """ Given a list of source paths and a destination path, produces the |
| 41 | appropriate rsync command for copying them. Remote paths must be |
| 42 | pre-encoded. """ |
| 43 | ssh_cmd = make_ssh_command(self.user, self.port) |
| 44 | if delete_dest: |
| 45 | delete_flag = "--delete" |
| 46 | else: |
| 47 | delete_flag = "" |
| 48 | command = "rsync -L %s --rsh='%s' -az %s %s" |
| 49 | return command % (delete_flag, ssh_cmd, " ".join(sources), dest) |
| 50 | |
| 51 | |
| 52 | def _make_scp_cmd(self, sources, dest): |
| 53 | """ Given a list of source paths and a destination path, produces the |
| 54 | appropriate scp command for encoding it. Remote paths must be |
| 55 | pre-encoded. """ |
| 56 | command = "scp -rpq -P %d %s '%s'" |
| 57 | return command % (self.port, " ".join(sources), dest) |
| 58 | |
| 59 | |
| 60 | def _make_rsync_compatible_globs(self, path, is_local): |
| 61 | """ Given an rsync-style path, returns a list of globbed paths |
| 62 | that will hopefully provide equivalent behaviour for scp. Does not |
| 63 | support the full range of rsync pattern matching behaviour, only that |
| 64 | exposed in the get/send_file interface (trailing slashes). |
| 65 | |
| 66 | The is_local param is flag indicating if the paths should be |
| 67 | interpreted as local or remote paths. """ |
| 68 | |
| 69 | # non-trailing slash paths should just work |
| 70 | if len(path) == 0 or path[-1] != "/": |
| 71 | return [path] |
| 72 | |
| 73 | # make a function to test if a pattern matches any files |
| 74 | if is_local: |
| 75 | def glob_matches_files(path): |
| 76 | return len(glob.glob(path)) > 0 |
| 77 | else: |
| 78 | def glob_matches_files(path): |
| 79 | result = self.run("ls \"%s\"" % utils.sh_escape(path), |
| 80 | ignore_status=True) |
| 81 | return result.exit_status == 0 |
| 82 | |
| 83 | # take a set of globs that cover all files, and see which are needed |
| 84 | patterns = ["*", ".[!.]*"] |
| 85 | patterns = [p for p in patterns if glob_matches_files(path + p)] |
| 86 | |
| 87 | # convert them into a set of paths suitable for the commandline |
| 88 | path = utils.sh_escape(path) |
| 89 | if is_local: |
| 90 | return ["\"%s\"%s" % (path, pattern) for pattern in patterns] |
| 91 | else: |
| 92 | return ["\"%s\"" % (path + pattern) for pattern in patterns] |
| 93 | |
| 94 | |
| 95 | def _make_rsync_compatible_source(self, source, is_local): |
| 96 | """ Applies the same logic as _make_rsync_compatible_globs, but |
| 97 | applies it to an entire list of sources, producing a new list of |
| 98 | sources, properly quoted. """ |
| 99 | return sum((self._make_rsync_compatible_globs(path, is_local) |
| 100 | for path in source), []) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 101 | |
| 102 | |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 103 | def get_file(self, source, dest, delete_dest=False): |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 104 | """ |
| 105 | Copy files from the remote host to a local path. |
| 106 | |
| 107 | Directories will be copied recursively. |
| 108 | If a source component is a directory with a trailing slash, |
| 109 | the content of the directory will be copied, otherwise, the |
| 110 | directory itself and its content will be copied. This |
| 111 | behavior is similar to that of the program 'rsync'. |
| 112 | |
| 113 | Args: |
| 114 | source: either |
| 115 | 1) a single file or directory, as a string |
| 116 | 2) a list of one or more (possibly mixed) |
| 117 | files or directories |
| 118 | dest: a file or a directory (if source contains a |
| 119 | directory or more than one element, you must |
| 120 | supply a directory dest) |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 121 | delete_dest: if this is true, the command will also clear |
| 122 | out any old files at dest that are not in the |
| 123 | source |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 124 | |
| 125 | Raises: |
| 126 | AutoservRunError: the scp command failed |
| 127 | """ |
| 128 | if isinstance(source, basestring): |
| 129 | source = [source] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 130 | dest = os.path.abspath(dest) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 131 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 132 | try: |
| 133 | remote_source = [self._encode_remote_path(p) for p in source] |
| 134 | local_dest = utils.sh_escape(dest) |
| 135 | rsync = self._make_rsync_cmd(remote_source, local_dest, |
| 136 | delete_dest) |
| 137 | utils.run(rsync) |
| 138 | except error.CmdError, e: |
| 139 | print "warning: rsync failed with: %s" % e |
| 140 | print "attempting to copy with scp instead" |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 141 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 142 | # scp has no equivalent to --delete, just drop the entire dest dir |
| 143 | if delete_dest and os.path.isdir(dest): |
| 144 | shutil.rmtree(dest) |
| 145 | os.mkdir(dest) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 146 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 147 | remote_source = self._make_rsync_compatible_source(source, False) |
| 148 | if remote_source: |
| 149 | local_dest = utils.sh_escape(dest) |
| 150 | scp = self._make_scp_cmd(remote_source, local_dest) |
| 151 | try: |
| 152 | utils.run(scp) |
| 153 | except error.CmdError, e: |
| 154 | raise error.AutoservRunError(e.args[0], e.args[1]) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 155 | |
| 156 | |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 157 | def send_file(self, source, dest, delete_dest=False): |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 158 | """ |
| 159 | Copy files from a local path to the remote host. |
| 160 | |
| 161 | Directories will be copied recursively. |
| 162 | If a source component is a directory with a trailing slash, |
| 163 | the content of the directory will be copied, otherwise, the |
| 164 | directory itself and its content will be copied. This |
| 165 | behavior is similar to that of the program 'rsync'. |
| 166 | |
| 167 | Args: |
| 168 | source: either |
| 169 | 1) a single file or directory, as a string |
| 170 | 2) a list of one or more (possibly mixed) |
| 171 | files or directories |
| 172 | dest: a file or a directory (if source contains a |
| 173 | directory or more than one element, you must |
| 174 | supply a directory dest) |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 175 | delete_dest: if this is true, the command will also clear |
| 176 | out any old files at dest that are not in the |
| 177 | source |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 178 | |
| 179 | Raises: |
| 180 | AutoservRunError: the scp command failed |
| 181 | """ |
| 182 | if isinstance(source, basestring): |
| 183 | source = [source] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 184 | remote_dest = self._encode_remote_path(dest) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 185 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 186 | try: |
| 187 | local_source = [utils.sh_escape(path) for path in source] |
| 188 | rsync = self._make_rsync_cmd(local_source, remote_dest, |
| 189 | delete_dest) |
| 190 | utils.run(rsync) |
| 191 | except error.CmdError, e: |
| 192 | print "warning: rsync failed with: %s" % e |
| 193 | print "attempting to copy with scp instead" |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 194 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 195 | # scp has no equivalent to --delete, just drop the entire dest dir |
| 196 | if delete_dest: |
| 197 | is_dir = self.run("ls -d %s/" % remote_dest, |
| 198 | ignore_status=True).exit_status == 0 |
| 199 | if is_dir: |
| 200 | cmd = "rm -rf %s && mkdir %s" |
| 201 | cmd %= (remote_dest, remote_dest) |
| 202 | self.run(cmd) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 203 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 204 | local_source = self._make_rsync_compatible_source(source, True) |
| 205 | if local_source: |
| 206 | scp = self._make_scp_cmd(local_source, remote_dest) |
| 207 | try: |
| 208 | utils.run(scp) |
| 209 | except error.CmdError, e: |
| 210 | raise error.AutoservRunError(e.args[0], e.args[1]) |
| 211 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 212 | self.run('find "%s" -type d -print0 | xargs -0r chmod o+rx' % dest) |
| 213 | self.run('find "%s" -type f -print0 | xargs -0r chmod o+r' % dest) |
| 214 | if self.target_file_owner: |
| 215 | self.run('chown -R %s %s' % (self.target_file_owner, dest)) |
| 216 | |
| 217 | |
| 218 | def ssh_ping(self, timeout=60): |
| 219 | try: |
| 220 | self.run("true", timeout=timeout, connect_timeout=timeout) |
mbligh | 5f66ed4 | 2008-11-24 17:16:14 +0000 | [diff] [blame] | 221 | print "ssh_ping of %s completed sucessfully" % self.hostname |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 222 | except error.AutoservSSHTimeout: |
| 223 | msg = "ssh ping timed out (timeout = %d)" % timeout |
| 224 | raise error.AutoservSSHTimeout(msg) |
| 225 | except error.AutoservRunError, e: |
| 226 | msg = "command true failed in ssh ping" |
| 227 | raise error.AutoservRunError(msg, e.result_obj) |
| 228 | |
| 229 | |
| 230 | def is_up(self): |
| 231 | """ |
| 232 | Check if the remote host is up. |
| 233 | |
| 234 | Returns: |
| 235 | True if the remote host is up, False otherwise |
| 236 | """ |
| 237 | try: |
| 238 | self.ssh_ping() |
| 239 | except error.AutoservError: |
| 240 | return False |
| 241 | else: |
| 242 | return True |
| 243 | |
| 244 | |
| 245 | def wait_up(self, timeout=None): |
| 246 | """ |
| 247 | Wait until the remote host is up or the timeout expires. |
| 248 | |
| 249 | In fact, it will wait until an ssh connection to the remote |
| 250 | host can be established, and getty is running. |
| 251 | |
| 252 | Args: |
| 253 | timeout: time limit in seconds before returning even |
| 254 | if the host is not up. |
| 255 | |
| 256 | Returns: |
| 257 | True if the host was found to be up, False otherwise |
| 258 | """ |
| 259 | if timeout: |
| 260 | end_time = time.time() + timeout |
| 261 | |
| 262 | while not timeout or time.time() < end_time: |
| 263 | if self.is_up(): |
| 264 | try: |
| 265 | if self.are_wait_up_processes_up(): |
| 266 | return True |
| 267 | except error.AutoservError: |
| 268 | pass |
| 269 | time.sleep(1) |
| 270 | |
| 271 | return False |
| 272 | |
| 273 | |
| 274 | def wait_down(self, timeout=None): |
| 275 | """ |
| 276 | Wait until the remote host is down or the timeout expires. |
| 277 | |
| 278 | In fact, it will wait until an ssh connection to the remote |
| 279 | host fails. |
| 280 | |
| 281 | Args: |
| 282 | timeout: time limit in seconds before returning even |
| 283 | if the host is not up. |
| 284 | |
| 285 | Returns: |
| 286 | True if the host was found to be down, False otherwise |
| 287 | """ |
| 288 | if timeout: |
| 289 | end_time = time.time() + timeout |
| 290 | |
| 291 | while not timeout or time.time() < end_time: |
| 292 | if not self.is_up(): |
| 293 | return True |
| 294 | time.sleep(1) |
| 295 | |
| 296 | return False |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 297 | |
| 298 | # tunable constants for the verify & repair code |
| 299 | AUTOTEST_GB_DISKSPACE_REQUIRED = 20 |
| 300 | HOURS_TO_WAIT_FOR_RECOVERY = 2.5 |
| 301 | |
| 302 | def verify(self): |
| 303 | super(AbstractSSHHost, self).verify() |
| 304 | |
| 305 | print 'Pinging host ' + self.hostname |
| 306 | self.ssh_ping() |
| 307 | |
jadmanski | 80deb75 | 2009-01-21 17:14:16 +0000 | [diff] [blame] | 308 | if self.is_shutting_down(): |
| 309 | raise error.AutoservHostError("Host is shutting down") |
| 310 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 311 | try: |
| 312 | autodir = autotest._get_autodir(self) |
| 313 | if autodir: |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 314 | self.check_diskspace(autodir, |
| 315 | self.AUTOTEST_GB_DISKSPACE_REQUIRED) |
| 316 | except error.AutoservHostError: |
| 317 | raise # only want to raise if it's a space issue |
| 318 | except Exception: |
| 319 | pass # autotest dir may not exist, etc. ignore |
| 320 | |
| 321 | |
| 322 | def repair_filesystem_only(self): |
| 323 | super(AbstractSSHHost, self).repair_filesystem_only() |
jadmanski | 80deb75 | 2009-01-21 17:14:16 +0000 | [diff] [blame] | 324 | |
| 325 | TIMEOUT = int(self.HOURS_TO_WAIT_FOR_RECOVERY * 3600) |
| 326 | if self.is_shutting_down(): |
| 327 | print 'Host is shutting down, waiting for a restart' |
| 328 | self.wait_for_restart(TIMEOUT) |
| 329 | else: |
| 330 | self.wait_up(TIMEOUT) |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 331 | self.reboot() |
| 332 | |
| 333 | |
| 334 | def repair_full(self): |
| 335 | super(AbstractSSHHost, self).repair_full() |
| 336 | try: |
| 337 | self.repair_filesystem_only() |
| 338 | self.verify() |
| 339 | except Exception: |
| 340 | # the filesystem-only repair failed, try something more drastic |
| 341 | print "Filesystem-only repair failed" |
| 342 | traceback.print_exc() |
| 343 | try: |
| 344 | self.machine_install() |
| 345 | except NotImplementedError, e: |
| 346 | sys.stderr.write(str(e) + "\n\n") |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 347 | |
| 348 | |
| 349 | class LoggerFile(object): |
| 350 | def write(self, data): |
jadmanski | fcc851b | 2009-01-07 17:31:12 +0000 | [diff] [blame] | 351 | if data: |
| 352 | debug.get_logger().debug(data.rstrip("\n")) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | def flush(self): |
| 356 | pass |