showard | ca57298 | 2009-09-18 21:20:01 +0000 | [diff] [blame] | 1 | import os, time, types, socket, shutil, glob, logging, traceback |
showard | f1175bb | 2009-06-17 19:34:36 +0000 | [diff] [blame] | 2 | from autotest_lib.client.common_lib import error, logging_manager |
jadmanski | 31c49b7 | 2008-10-27 20:44:48 +0000 | [diff] [blame] | 3 | from autotest_lib.server import utils, autotest |
mbligh | e8b93af | 2009-01-30 00:45:53 +0000 | [diff] [blame] | 4 | from autotest_lib.server.hosts import remote |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 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 | |
mbligh | e8b93af | 2009-01-30 00:45:53 +0000 | [diff] [blame] | 16 | # import site specific Host class |
| 17 | SiteHost = utils.import_site_class( |
| 18 | __file__, "autotest_lib.server.hosts.site_host", "SiteHost", |
| 19 | remote.RemoteHost) |
| 20 | |
| 21 | |
| 22 | class AbstractSSHHost(SiteHost): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 23 | """ |
| 24 | This class represents a generic implementation of most of the |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 25 | framework necessary for controlling a host via ssh. It implements |
| 26 | almost all of the abstract Host methods, except for the core |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 27 | Host.run method. |
| 28 | """ |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 29 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 30 | def _initialize(self, hostname, user="root", port=22, password="", |
| 31 | *args, **dargs): |
| 32 | super(AbstractSSHHost, self)._initialize(hostname=hostname, |
| 33 | *args, **dargs) |
mbligh | 6369cf2 | 2008-10-24 17:21:57 +0000 | [diff] [blame] | 34 | self.ip = socket.getaddrinfo(self.hostname, None)[0][4][0] |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 35 | self.user = user |
| 36 | self.port = port |
| 37 | self.password = password |
| 38 | |
mbligh | c9892c0 | 2010-01-06 19:02:16 +0000 | [diff] [blame^] | 39 | # Check if rsync is available on the remote host. If it's not, |
| 40 | # don't try to use it for any future file transfers. |
| 41 | self.use_rsync = self._check_rsync() |
| 42 | if not self.use_rsync: |
| 43 | logging.warn("rsync not available on remote host %s -- disabled", |
| 44 | self.hostname) |
| 45 | |
| 46 | |
| 47 | def _check_rsync(self): |
| 48 | """ |
| 49 | Check if rsync is available on the remote host. |
| 50 | """ |
| 51 | try: |
| 52 | self.run("rsync --version", stdout_tee=None, stderr_tee=None) |
| 53 | except error.AutoservRunError: |
| 54 | return False |
| 55 | return True |
| 56 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 57 | |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 58 | def _encode_remote_paths(self, paths, escape=True): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 59 | """ |
| 60 | Given a list of file paths, encodes it as a single remote path, in |
| 61 | the style used by rsync and scp. |
| 62 | """ |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 63 | if escape: |
| 64 | paths = [utils.scp_remote_escape(path) for path in paths] |
| 65 | return '%s@%s:"%s"' % (self.user, self.hostname, " ".join(paths)) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 66 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 67 | |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 68 | def _make_rsync_cmd(self, sources, dest, delete_dest, preserve_symlinks): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 69 | """ |
| 70 | Given a list of source paths and a destination path, produces the |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 71 | appropriate rsync command for copying them. Remote paths must be |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 72 | pre-encoded. |
| 73 | """ |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 74 | ssh_cmd = make_ssh_command(self.user, self.port) |
| 75 | if delete_dest: |
| 76 | delete_flag = "--delete" |
| 77 | else: |
| 78 | delete_flag = "" |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 79 | if preserve_symlinks: |
| 80 | symlink_flag = "" |
| 81 | else: |
| 82 | symlink_flag = "-L" |
| 83 | command = "rsync %s %s --timeout=1800 --rsh='%s' -az %s %s" |
| 84 | return command % (symlink_flag, delete_flag, ssh_cmd, |
| 85 | " ".join(sources), dest) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def _make_scp_cmd(self, sources, dest): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 89 | """ |
| 90 | Given a list of source paths and a destination path, produces the |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 91 | appropriate scp command for encoding it. Remote paths must be |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 92 | pre-encoded. |
| 93 | """ |
mbligh | 29e1584 | 2009-12-23 22:02:54 +0000 | [diff] [blame] | 94 | command = "scp -rq -P %d %s '%s'" |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 95 | return command % (self.port, " ".join(sources), dest) |
| 96 | |
| 97 | |
| 98 | def _make_rsync_compatible_globs(self, path, is_local): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 99 | """ |
| 100 | Given an rsync-style path, returns a list of globbed paths |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 101 | that will hopefully provide equivalent behaviour for scp. Does not |
| 102 | support the full range of rsync pattern matching behaviour, only that |
| 103 | exposed in the get/send_file interface (trailing slashes). |
| 104 | |
| 105 | The is_local param is flag indicating if the paths should be |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 106 | interpreted as local or remote paths. |
| 107 | """ |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 108 | |
| 109 | # non-trailing slash paths should just work |
| 110 | if len(path) == 0 or path[-1] != "/": |
| 111 | return [path] |
| 112 | |
| 113 | # make a function to test if a pattern matches any files |
| 114 | if is_local: |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 115 | def glob_matches_files(path, pattern): |
| 116 | return len(glob.glob(path + pattern)) > 0 |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 117 | else: |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 118 | def glob_matches_files(path, pattern): |
| 119 | result = self.run("ls \"%s\"%s" % (utils.sh_escape(path), |
| 120 | pattern), |
| 121 | stdout_tee=None, ignore_status=True) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 122 | return result.exit_status == 0 |
| 123 | |
| 124 | # take a set of globs that cover all files, and see which are needed |
| 125 | patterns = ["*", ".[!.]*"] |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 126 | patterns = [p for p in patterns if glob_matches_files(path, p)] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 127 | |
| 128 | # convert them into a set of paths suitable for the commandline |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 129 | if is_local: |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 130 | return ["\"%s\"%s" % (utils.sh_escape(path), pattern) |
| 131 | for pattern in patterns] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 132 | else: |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 133 | return [utils.scp_remote_escape(path) + pattern |
| 134 | for pattern in patterns] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def _make_rsync_compatible_source(self, source, is_local): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 138 | """ |
| 139 | Applies the same logic as _make_rsync_compatible_globs, but |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 140 | applies it to an entire list of sources, producing a new list of |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 141 | sources, properly quoted. |
| 142 | """ |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 143 | return sum((self._make_rsync_compatible_globs(path, is_local) |
| 144 | for path in source), []) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 145 | |
| 146 | |
mbligh | feac010 | 2009-04-28 18:31:12 +0000 | [diff] [blame] | 147 | def _set_umask_perms(self, dest): |
mbligh | bc9402b | 2009-12-29 01:15:34 +0000 | [diff] [blame] | 148 | """ |
| 149 | Given a destination file/dir (recursively) set the permissions on |
| 150 | all the files and directories to the max allowed by running umask. |
| 151 | """ |
mbligh | feac010 | 2009-04-28 18:31:12 +0000 | [diff] [blame] | 152 | |
| 153 | # now this looks strange but I haven't found a way in Python to _just_ |
| 154 | # get the umask, apparently the only option is to try to set it |
| 155 | umask = os.umask(0) |
| 156 | os.umask(umask) |
| 157 | |
| 158 | max_privs = 0777 & ~umask |
| 159 | |
| 160 | def set_file_privs(filename): |
| 161 | file_stat = os.stat(filename) |
| 162 | |
| 163 | file_privs = max_privs |
| 164 | # if the original file permissions do not have at least one |
| 165 | # executable bit then do not set it anywhere |
| 166 | if not file_stat.st_mode & 0111: |
| 167 | file_privs &= ~0111 |
| 168 | |
| 169 | os.chmod(filename, file_privs) |
| 170 | |
| 171 | # try a bottom-up walk so changes on directory permissions won't cut |
| 172 | # our access to the files/directories inside it |
| 173 | for root, dirs, files in os.walk(dest, topdown=False): |
| 174 | # when setting the privileges we emulate the chmod "X" behaviour |
| 175 | # that sets to execute only if it is a directory or any of the |
| 176 | # owner/group/other already has execute right |
| 177 | for dirname in dirs: |
| 178 | os.chmod(os.path.join(root, dirname), max_privs) |
| 179 | |
| 180 | for filename in files: |
| 181 | set_file_privs(os.path.join(root, filename)) |
| 182 | |
| 183 | |
| 184 | # now set privs for the dest itself |
| 185 | if os.path.isdir(dest): |
| 186 | os.chmod(dest, max_privs) |
| 187 | else: |
| 188 | set_file_privs(dest) |
| 189 | |
| 190 | |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 191 | def get_file(self, source, dest, delete_dest=False, preserve_perm=True, |
| 192 | preserve_symlinks=False): |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 193 | """ |
| 194 | Copy files from the remote host to a local path. |
| 195 | |
| 196 | Directories will be copied recursively. |
| 197 | If a source component is a directory with a trailing slash, |
| 198 | the content of the directory will be copied, otherwise, the |
| 199 | directory itself and its content will be copied. This |
| 200 | behavior is similar to that of the program 'rsync'. |
| 201 | |
| 202 | Args: |
| 203 | source: either |
| 204 | 1) a single file or directory, as a string |
| 205 | 2) a list of one or more (possibly mixed) |
| 206 | files or directories |
| 207 | dest: a file or a directory (if source contains a |
| 208 | directory or more than one element, you must |
| 209 | supply a directory dest) |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 210 | delete_dest: if this is true, the command will also clear |
| 211 | out any old files at dest that are not in the |
| 212 | source |
mbligh | feac010 | 2009-04-28 18:31:12 +0000 | [diff] [blame] | 213 | preserve_perm: tells get_file() to try to preserve the sources |
| 214 | permissions on files and dirs |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 215 | preserve_symlinks: try to preserve symlinks instead of |
| 216 | transforming them into files/dirs on copy |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 217 | |
| 218 | Raises: |
| 219 | AutoservRunError: the scp command failed |
| 220 | """ |
| 221 | if isinstance(source, basestring): |
| 222 | source = [source] |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 223 | dest = os.path.abspath(dest) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 224 | |
mbligh | c9892c0 | 2010-01-06 19:02:16 +0000 | [diff] [blame^] | 225 | # If rsync is disabled or fails, try scp. |
| 226 | try_scp = not self.use_rsync |
| 227 | if self.use_rsync: |
| 228 | try: |
| 229 | remote_source = self._encode_remote_paths(source) |
| 230 | local_dest = utils.sh_escape(dest) |
| 231 | rsync = self._make_rsync_cmd([remote_source], local_dest, |
| 232 | delete_dest, preserve_symlinks) |
| 233 | utils.run(rsync) |
| 234 | except error.CmdError, e: |
| 235 | logging.warn("trying scp, rsync failed: %s" % e) |
| 236 | try_scp = True |
| 237 | |
| 238 | if try_scp: |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 239 | # scp has no equivalent to --delete, just drop the entire dest dir |
| 240 | if delete_dest and os.path.isdir(dest): |
| 241 | shutil.rmtree(dest) |
| 242 | os.mkdir(dest) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 243 | |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 244 | remote_source = self._make_rsync_compatible_source(source, False) |
| 245 | if remote_source: |
showard | 56176ec | 2009-10-28 19:52:30 +0000 | [diff] [blame] | 246 | # _make_rsync_compatible_source() already did the escaping |
| 247 | remote_source = self._encode_remote_paths(remote_source, |
| 248 | escape=False) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 249 | local_dest = utils.sh_escape(dest) |
jadmanski | 2583a43 | 2009-02-10 23:59:11 +0000 | [diff] [blame] | 250 | scp = self._make_scp_cmd([remote_source], local_dest) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 251 | try: |
| 252 | utils.run(scp) |
| 253 | except error.CmdError, e: |
| 254 | raise error.AutoservRunError(e.args[0], e.args[1]) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 255 | |
mbligh | feac010 | 2009-04-28 18:31:12 +0000 | [diff] [blame] | 256 | if not preserve_perm: |
| 257 | # we have no way to tell scp to not try to preserve the |
| 258 | # permissions so set them after copy instead. |
| 259 | # for rsync we could use "--no-p --chmod=ugo=rwX" but those |
| 260 | # options are only in very recent rsync versions |
| 261 | self._set_umask_perms(dest) |
| 262 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 263 | |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 264 | def send_file(self, source, dest, delete_dest=False, |
| 265 | preserve_symlinks=False): |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 266 | """ |
| 267 | Copy files from a local path to the remote host. |
| 268 | |
| 269 | Directories will be copied recursively. |
| 270 | If a source component is a directory with a trailing slash, |
| 271 | the content of the directory will be copied, otherwise, the |
| 272 | directory itself and its content will be copied. This |
| 273 | behavior is similar to that of the program 'rsync'. |
| 274 | |
| 275 | Args: |
| 276 | source: either |
| 277 | 1) a single file or directory, as a string |
| 278 | 2) a list of one or more (possibly mixed) |
| 279 | files or directories |
| 280 | dest: a file or a directory (if source contains a |
| 281 | directory or more than one element, you must |
| 282 | supply a directory dest) |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 283 | delete_dest: if this is true, the command will also clear |
| 284 | out any old files at dest that are not in the |
| 285 | source |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 286 | preserve_symlinks: controls if symlinks on the source will be |
| 287 | copied as such on the destination or transformed into the |
| 288 | referenced file/directory |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 289 | |
| 290 | Raises: |
| 291 | AutoservRunError: the scp command failed |
| 292 | """ |
| 293 | if isinstance(source, basestring): |
| 294 | source = [source] |
jadmanski | 2583a43 | 2009-02-10 23:59:11 +0000 | [diff] [blame] | 295 | remote_dest = self._encode_remote_paths([dest]) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 296 | |
mbligh | c9892c0 | 2010-01-06 19:02:16 +0000 | [diff] [blame^] | 297 | # If rsync is disabled or fails, try scp. |
| 298 | try_scp = not self.use_rsync |
| 299 | if self.use_rsync: |
| 300 | try: |
| 301 | local_sources = [utils.sh_escape(path) for path in source] |
| 302 | rsync = self._make_rsync_cmd(local_sources, remote_dest, |
| 303 | delete_dest, preserve_symlinks) |
| 304 | utils.run(rsync) |
| 305 | except error.CmdError, e: |
| 306 | logging.warn("trying scp, rsync failed: %s" % e) |
| 307 | try_scp = True |
| 308 | |
| 309 | if try_scp: |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 310 | # scp has no equivalent to --delete, just drop the entire dest dir |
| 311 | if delete_dest: |
showard | 2716015 | 2009-07-15 14:28:42 +0000 | [diff] [blame] | 312 | is_dir = self.run("ls -d %s/" % dest, |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 313 | ignore_status=True).exit_status == 0 |
| 314 | if is_dir: |
| 315 | cmd = "rm -rf %s && mkdir %s" |
mbligh | 5a0ca53 | 2009-08-03 16:44:34 +0000 | [diff] [blame] | 316 | cmd %= (dest, dest) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 317 | self.run(cmd) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 318 | |
jadmanski | 2583a43 | 2009-02-10 23:59:11 +0000 | [diff] [blame] | 319 | local_sources = self._make_rsync_compatible_source(source, True) |
| 320 | if local_sources: |
| 321 | scp = self._make_scp_cmd(local_sources, remote_dest) |
jadmanski | d7b79ed | 2009-01-07 17:19:48 +0000 | [diff] [blame] | 322 | try: |
| 323 | utils.run(scp) |
| 324 | except error.CmdError, e: |
| 325 | raise error.AutoservRunError(e.args[0], e.args[1]) |
| 326 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 327 | |
| 328 | def ssh_ping(self, timeout=60): |
| 329 | try: |
| 330 | self.run("true", timeout=timeout, connect_timeout=timeout) |
| 331 | except error.AutoservSSHTimeout: |
mbligh | d0e9498 | 2009-07-11 00:15:18 +0000 | [diff] [blame] | 332 | msg = "Host (ssh) verify timed out (timeout = %d)" % timeout |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 333 | raise error.AutoservSSHTimeout(msg) |
mbligh | 9d738d6 | 2009-03-09 21:17:10 +0000 | [diff] [blame] | 334 | except error.AutoservSshPermissionDeniedError: |
| 335 | #let AutoservSshPermissionDeniedError be visible to the callers |
| 336 | raise |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 337 | except error.AutoservRunError, e: |
mbligh | c971c5f | 2009-06-08 16:48:54 +0000 | [diff] [blame] | 338 | # convert the generic AutoservRunError into something more |
| 339 | # specific for this context |
| 340 | raise error.AutoservSshPingHostError(e.description + '\n' + |
| 341 | repr(e.result_obj)) |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 342 | |
| 343 | |
| 344 | def is_up(self): |
| 345 | """ |
| 346 | Check if the remote host is up. |
| 347 | |
| 348 | Returns: |
| 349 | True if the remote host is up, False otherwise |
| 350 | """ |
| 351 | try: |
| 352 | self.ssh_ping() |
| 353 | except error.AutoservError: |
| 354 | return False |
| 355 | else: |
| 356 | return True |
| 357 | |
| 358 | |
| 359 | def wait_up(self, timeout=None): |
| 360 | """ |
| 361 | Wait until the remote host is up or the timeout expires. |
| 362 | |
| 363 | In fact, it will wait until an ssh connection to the remote |
| 364 | host can be established, and getty is running. |
| 365 | |
| 366 | Args: |
| 367 | timeout: time limit in seconds before returning even |
| 368 | if the host is not up. |
| 369 | |
| 370 | Returns: |
| 371 | True if the host was found to be up, False otherwise |
| 372 | """ |
| 373 | if timeout: |
| 374 | end_time = time.time() + timeout |
| 375 | |
| 376 | while not timeout or time.time() < end_time: |
| 377 | if self.is_up(): |
| 378 | try: |
| 379 | if self.are_wait_up_processes_up(): |
| 380 | return True |
| 381 | except error.AutoservError: |
| 382 | pass |
| 383 | time.sleep(1) |
| 384 | |
| 385 | return False |
| 386 | |
| 387 | |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 388 | def wait_down(self, timeout=None, warning_timer=None): |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 389 | """ |
| 390 | Wait until the remote host is down or the timeout expires. |
| 391 | |
| 392 | In fact, it will wait until an ssh connection to the remote |
| 393 | host fails. |
| 394 | |
| 395 | Args: |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 396 | timeout: time limit in seconds before returning even |
| 397 | if the host is still up. |
| 398 | warning_timer: time limit in seconds that will generate |
| 399 | a warning if the host is not down yet. |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 400 | |
| 401 | Returns: |
| 402 | True if the host was found to be down, False otherwise |
| 403 | """ |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 404 | current_time = time.time() |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 405 | if timeout: |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 406 | end_time = current_time + timeout |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 407 | |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 408 | if warning_timer: |
| 409 | warn_time = current_time + warning_timer |
| 410 | |
| 411 | while not timeout or current_time < end_time: |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 412 | if not self.is_up(): |
| 413 | return True |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 414 | |
| 415 | if warning_timer and current_time > warn_time: |
| 416 | self.record("WARN", None, "shutdown", |
| 417 | "Shutdown took longer than %ds" % warning_timer) |
| 418 | # Print the warning only once. |
| 419 | warning_timer = None |
mbligh | a446440 | 2009-04-17 20:13:41 +0000 | [diff] [blame] | 420 | # If a machine is stuck switching runlevels |
| 421 | # This may cause the machine to reboot. |
| 422 | self.run('kill -HUP 1', ignore_status=True) |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 423 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 424 | time.sleep(1) |
mbligh | 2ed998f | 2009-04-08 21:03:47 +0000 | [diff] [blame] | 425 | current_time = time.time() |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 426 | |
| 427 | return False |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 428 | |
mbligh | a0a2759 | 2009-01-24 01:41:36 +0000 | [diff] [blame] | 429 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 430 | # tunable constants for the verify & repair code |
| 431 | AUTOTEST_GB_DISKSPACE_REQUIRED = 20 |
mbligh | a0a2759 | 2009-01-24 01:41:36 +0000 | [diff] [blame] | 432 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 433 | |
showard | ca57298 | 2009-09-18 21:20:01 +0000 | [diff] [blame] | 434 | def verify_connectivity(self): |
| 435 | super(AbstractSSHHost, self).verify_connectivity() |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 436 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 437 | logging.info('Pinging host ' + self.hostname) |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 438 | self.ssh_ping() |
mbligh | 2ba7ab0 | 2009-08-24 22:09:26 +0000 | [diff] [blame] | 439 | logging.info("Host (ssh) %s is alive", self.hostname) |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 440 | |
jadmanski | 80deb75 | 2009-01-21 17:14:16 +0000 | [diff] [blame] | 441 | if self.is_shutting_down(): |
mbligh | c971c5f | 2009-06-08 16:48:54 +0000 | [diff] [blame] | 442 | raise error.AutoservHostIsShuttingDownError("Host is shutting down") |
jadmanski | 80deb75 | 2009-01-21 17:14:16 +0000 | [diff] [blame] | 443 | |
mbligh | b49b523 | 2009-02-12 21:54:49 +0000 | [diff] [blame] | 444 | |
showard | ca57298 | 2009-09-18 21:20:01 +0000 | [diff] [blame] | 445 | def verify_software(self): |
| 446 | super(AbstractSSHHost, self).verify_software() |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 447 | try: |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 448 | self.check_diskspace(autotest.Autotest.get_install_dir(self), |
| 449 | self.AUTOTEST_GB_DISKSPACE_REQUIRED) |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 450 | except error.AutoservHostError: |
| 451 | raise # only want to raise if it's a space issue |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 452 | except autotest.AutodirNotFoundError: |
showard | ca57298 | 2009-09-18 21:20:01 +0000 | [diff] [blame] | 453 | # autotest dir may not exist, etc. ignore |
| 454 | logging.debug('autodir space check exception, this is probably ' |
| 455 | 'safe to ignore\n' + traceback.format_exc()) |