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