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