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