blob: ccc8057afb949736d149109c35c0ea45bbfa4eed [file] [log] [blame]
showardca572982009-09-18 21:20:01 +00001import os, time, types, socket, shutil, glob, logging, traceback
mblighefccc1b2010-01-11 19:08:42 +00002from autotest_lib.client.common_lib import autotemp, error, logging_manager
jadmanski31c49b72008-10-27 20:44:48 +00003from autotest_lib.server import utils, autotest
mblighe8b93af2009-01-30 00:45:53 +00004from autotest_lib.server.hosts import remote
mblighefccc1b2010-01-11 19:08:42 +00005from autotest_lib.client.common_lib.global_config import global_config
jadmanskica7da372008-10-21 16:26:52 +00006
7
mblighb86bfa12010-02-12 20:22:21 +00008get_value = global_config.get_config_value
9enable_master_ssh = get_value('AUTOSERV', 'enable_master_ssh', type=bool,
10 default=False)
mblighefccc1b2010-01-11 19:08:42 +000011
12
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070013def _make_ssh_cmd_default(user="root", port=22, opts='', hosts_file='/dev/null',
14 connect_timeout=30, alive_interval=300):
lmraf676f32010-02-04 03:36:26 +000015 base_command = ("/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no "
16 "-o UserKnownHostsFile=%s -o BatchMode=yes "
mblighefccc1b2010-01-11 19:08:42 +000017 "-o ConnectTimeout=%d -o ServerAliveInterval=%d "
jadmanskica7da372008-10-21 16:26:52 +000018 "-l %s -p %d")
19 assert isinstance(connect_timeout, (int, long))
20 assert connect_timeout > 0 # can't disable the timeout
lmraf676f32010-02-04 03:36:26 +000021 return base_command % (opts, hosts_file, connect_timeout,
22 alive_interval, user, port)
jadmanskica7da372008-10-21 16:26:52 +000023
24
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070025make_ssh_command = utils.import_site_function(
26 __file__, "autotest_lib.server.hosts.site_host", "make_ssh_command",
27 _make_ssh_cmd_default)
28
29
mblighe8b93af2009-01-30 00:45:53 +000030# import site specific Host class
31SiteHost = utils.import_site_class(
32 __file__, "autotest_lib.server.hosts.site_host", "SiteHost",
33 remote.RemoteHost)
34
35
36class AbstractSSHHost(SiteHost):
mblighbc9402b2009-12-29 01:15:34 +000037 """
38 This class represents a generic implementation of most of the
jadmanskica7da372008-10-21 16:26:52 +000039 framework necessary for controlling a host via ssh. It implements
40 almost all of the abstract Host methods, except for the core
mblighbc9402b2009-12-29 01:15:34 +000041 Host.run method.
42 """
jadmanskica7da372008-10-21 16:26:52 +000043
jadmanskif6562912008-10-21 17:59:01 +000044 def _initialize(self, hostname, user="root", port=22, password="",
45 *args, **dargs):
46 super(AbstractSSHHost, self)._initialize(hostname=hostname,
47 *args, **dargs)
mbligh6369cf22008-10-24 17:21:57 +000048 self.ip = socket.getaddrinfo(self.hostname, None)[0][4][0]
jadmanskica7da372008-10-21 16:26:52 +000049 self.user = user
50 self.port = port
51 self.password = password
showard6eafb492010-01-15 20:29:06 +000052 self._use_rsync = None
lmraf676f32010-02-04 03:36:26 +000053 self.known_hosts_file = os.tmpfile()
54 known_hosts_fd = self.known_hosts_file.fileno()
55 self.known_hosts_fd = '/dev/fd/%s' % known_hosts_fd
jadmanskica7da372008-10-21 16:26:52 +000056
mblighefccc1b2010-01-11 19:08:42 +000057 """
58 Master SSH connection background job, socket temp directory and socket
59 control path option. If master-SSH is enabled, these fields will be
60 initialized by start_master_ssh when a new SSH connection is initiated.
61 """
62 self.master_ssh_job = None
63 self.master_ssh_tempdir = None
64 self.master_ssh_option = ''
65
showard6eafb492010-01-15 20:29:06 +000066
67 def use_rsync(self):
68 if self._use_rsync is not None:
69 return self._use_rsync
70
mblighc9892c02010-01-06 19:02:16 +000071 # Check if rsync is available on the remote host. If it's not,
72 # don't try to use it for any future file transfers.
showard6eafb492010-01-15 20:29:06 +000073 self._use_rsync = self._check_rsync()
74 if not self._use_rsync:
mblighc9892c02010-01-06 19:02:16 +000075 logging.warn("rsync not available on remote host %s -- disabled",
76 self.hostname)
Eric Lie0493a42010-11-15 13:05:43 -080077 return self._use_rsync
mblighc9892c02010-01-06 19:02:16 +000078
79
80 def _check_rsync(self):
81 """
82 Check if rsync is available on the remote host.
83 """
84 try:
85 self.run("rsync --version", stdout_tee=None, stderr_tee=None)
86 except error.AutoservRunError:
87 return False
88 return True
89
jadmanskica7da372008-10-21 16:26:52 +000090
showard56176ec2009-10-28 19:52:30 +000091 def _encode_remote_paths(self, paths, escape=True):
mblighbc9402b2009-12-29 01:15:34 +000092 """
93 Given a list of file paths, encodes it as a single remote path, in
94 the style used by rsync and scp.
95 """
showard56176ec2009-10-28 19:52:30 +000096 if escape:
97 paths = [utils.scp_remote_escape(path) for path in paths]
98 return '%s@%s:"%s"' % (self.user, self.hostname, " ".join(paths))
jadmanskica7da372008-10-21 16:26:52 +000099
jadmanskica7da372008-10-21 16:26:52 +0000100
mbligh45561782009-05-11 21:14:34 +0000101 def _make_rsync_cmd(self, sources, dest, delete_dest, preserve_symlinks):
mblighbc9402b2009-12-29 01:15:34 +0000102 """
103 Given a list of source paths and a destination path, produces the
jadmanskid7b79ed2009-01-07 17:19:48 +0000104 appropriate rsync command for copying them. Remote paths must be
mblighbc9402b2009-12-29 01:15:34 +0000105 pre-encoded.
106 """
lmraf676f32010-02-04 03:36:26 +0000107 ssh_cmd = make_ssh_command(user=self.user, port=self.port,
108 opts=self.master_ssh_option,
109 hosts_file=self.known_hosts_fd)
jadmanskid7b79ed2009-01-07 17:19:48 +0000110 if delete_dest:
111 delete_flag = "--delete"
112 else:
113 delete_flag = ""
mbligh45561782009-05-11 21:14:34 +0000114 if preserve_symlinks:
115 symlink_flag = ""
116 else:
117 symlink_flag = "-L"
118 command = "rsync %s %s --timeout=1800 --rsh='%s' -az %s %s"
119 return command % (symlink_flag, delete_flag, ssh_cmd,
120 " ".join(sources), dest)
jadmanskid7b79ed2009-01-07 17:19:48 +0000121
122
Eric Li861b2d52011-02-04 14:50:35 -0800123 def _make_ssh_cmd(self, cmd):
124 """
125 Create a base ssh command string for the host which can be used
126 to run commands directly on the machine
127 """
128 base_cmd = make_ssh_command(user=self.user, port=self.port,
129 opts=self.master_ssh_option,
130 hosts_file=self.known_hosts_fd)
131
132 return '%s %s "%s"' % (base_cmd, self.hostname, utils.sh_escape(cmd))
133
jadmanskid7b79ed2009-01-07 17:19:48 +0000134 def _make_scp_cmd(self, sources, dest):
mblighbc9402b2009-12-29 01:15:34 +0000135 """
136 Given a list of source paths and a destination path, produces the
jadmanskid7b79ed2009-01-07 17:19:48 +0000137 appropriate scp command for encoding it. Remote paths must be
mblighbc9402b2009-12-29 01:15:34 +0000138 pre-encoded.
139 """
mblighc0649d62010-01-15 18:15:58 +0000140 command = ("scp -rq %s -o StrictHostKeyChecking=no "
lmraf676f32010-02-04 03:36:26 +0000141 "-o UserKnownHostsFile=%s -P %d %s '%s'")
142 return command % (self.master_ssh_option, self.known_hosts_fd,
mblighefccc1b2010-01-11 19:08:42 +0000143 self.port, " ".join(sources), dest)
jadmanskid7b79ed2009-01-07 17:19:48 +0000144
145
146 def _make_rsync_compatible_globs(self, path, is_local):
mblighbc9402b2009-12-29 01:15:34 +0000147 """
148 Given an rsync-style path, returns a list of globbed paths
jadmanskid7b79ed2009-01-07 17:19:48 +0000149 that will hopefully provide equivalent behaviour for scp. Does not
150 support the full range of rsync pattern matching behaviour, only that
151 exposed in the get/send_file interface (trailing slashes).
152
153 The is_local param is flag indicating if the paths should be
mblighbc9402b2009-12-29 01:15:34 +0000154 interpreted as local or remote paths.
155 """
jadmanskid7b79ed2009-01-07 17:19:48 +0000156
157 # non-trailing slash paths should just work
158 if len(path) == 0 or path[-1] != "/":
159 return [path]
160
161 # make a function to test if a pattern matches any files
162 if is_local:
showard56176ec2009-10-28 19:52:30 +0000163 def glob_matches_files(path, pattern):
164 return len(glob.glob(path + pattern)) > 0
jadmanskid7b79ed2009-01-07 17:19:48 +0000165 else:
showard56176ec2009-10-28 19:52:30 +0000166 def glob_matches_files(path, pattern):
167 result = self.run("ls \"%s\"%s" % (utils.sh_escape(path),
168 pattern),
169 stdout_tee=None, ignore_status=True)
jadmanskid7b79ed2009-01-07 17:19:48 +0000170 return result.exit_status == 0
171
172 # take a set of globs that cover all files, and see which are needed
173 patterns = ["*", ".[!.]*"]
showard56176ec2009-10-28 19:52:30 +0000174 patterns = [p for p in patterns if glob_matches_files(path, p)]
jadmanskid7b79ed2009-01-07 17:19:48 +0000175
176 # convert them into a set of paths suitable for the commandline
jadmanskid7b79ed2009-01-07 17:19:48 +0000177 if is_local:
showard56176ec2009-10-28 19:52:30 +0000178 return ["\"%s\"%s" % (utils.sh_escape(path), pattern)
179 for pattern in patterns]
jadmanskid7b79ed2009-01-07 17:19:48 +0000180 else:
showard56176ec2009-10-28 19:52:30 +0000181 return [utils.scp_remote_escape(path) + pattern
182 for pattern in patterns]
jadmanskid7b79ed2009-01-07 17:19:48 +0000183
184
185 def _make_rsync_compatible_source(self, source, is_local):
mblighbc9402b2009-12-29 01:15:34 +0000186 """
187 Applies the same logic as _make_rsync_compatible_globs, but
jadmanskid7b79ed2009-01-07 17:19:48 +0000188 applies it to an entire list of sources, producing a new list of
mblighbc9402b2009-12-29 01:15:34 +0000189 sources, properly quoted.
190 """
jadmanskid7b79ed2009-01-07 17:19:48 +0000191 return sum((self._make_rsync_compatible_globs(path, is_local)
192 for path in source), [])
jadmanskica7da372008-10-21 16:26:52 +0000193
194
mblighfeac0102009-04-28 18:31:12 +0000195 def _set_umask_perms(self, dest):
mblighbc9402b2009-12-29 01:15:34 +0000196 """
197 Given a destination file/dir (recursively) set the permissions on
198 all the files and directories to the max allowed by running umask.
199 """
mblighfeac0102009-04-28 18:31:12 +0000200
201 # now this looks strange but I haven't found a way in Python to _just_
202 # get the umask, apparently the only option is to try to set it
203 umask = os.umask(0)
204 os.umask(umask)
205
206 max_privs = 0777 & ~umask
207
208 def set_file_privs(filename):
Chris Masone567d0d92011-12-19 09:38:30 -0800209 """Sets mode of |filename|. Assumes |filename| exists."""
210 file_stat = os.stat(filename)
mblighfeac0102009-04-28 18:31:12 +0000211
212 file_privs = max_privs
213 # if the original file permissions do not have at least one
214 # executable bit then do not set it anywhere
215 if not file_stat.st_mode & 0111:
216 file_privs &= ~0111
217
218 os.chmod(filename, file_privs)
219
220 # try a bottom-up walk so changes on directory permissions won't cut
221 # our access to the files/directories inside it
222 for root, dirs, files in os.walk(dest, topdown=False):
223 # when setting the privileges we emulate the chmod "X" behaviour
224 # that sets to execute only if it is a directory or any of the
225 # owner/group/other already has execute right
226 for dirname in dirs:
227 os.chmod(os.path.join(root, dirname), max_privs)
228
Chris Masone567d0d92011-12-19 09:38:30 -0800229 # Filter out broken symlinks as we go.
230 for filename in filter(os.path.exists, files):
mblighfeac0102009-04-28 18:31:12 +0000231 set_file_privs(os.path.join(root, filename))
232
233
234 # now set privs for the dest itself
235 if os.path.isdir(dest):
236 os.chmod(dest, max_privs)
237 else:
238 set_file_privs(dest)
239
240
mbligh45561782009-05-11 21:14:34 +0000241 def get_file(self, source, dest, delete_dest=False, preserve_perm=True,
242 preserve_symlinks=False):
jadmanskica7da372008-10-21 16:26:52 +0000243 """
244 Copy files from the remote host to a local path.
245
246 Directories will be copied recursively.
247 If a source component is a directory with a trailing slash,
248 the content of the directory will be copied, otherwise, the
249 directory itself and its content will be copied. This
250 behavior is similar to that of the program 'rsync'.
251
252 Args:
253 source: either
254 1) a single file or directory, as a string
255 2) a list of one or more (possibly mixed)
256 files or directories
257 dest: a file or a directory (if source contains a
258 directory or more than one element, you must
259 supply a directory dest)
mbligh89e258d2008-10-24 13:58:08 +0000260 delete_dest: if this is true, the command will also clear
261 out any old files at dest that are not in the
262 source
mblighfeac0102009-04-28 18:31:12 +0000263 preserve_perm: tells get_file() to try to preserve the sources
264 permissions on files and dirs
mbligh45561782009-05-11 21:14:34 +0000265 preserve_symlinks: try to preserve symlinks instead of
266 transforming them into files/dirs on copy
jadmanskica7da372008-10-21 16:26:52 +0000267
268 Raises:
269 AutoservRunError: the scp command failed
270 """
mblighefccc1b2010-01-11 19:08:42 +0000271
272 # Start a master SSH connection if necessary.
273 self.start_master_ssh()
274
jadmanskica7da372008-10-21 16:26:52 +0000275 if isinstance(source, basestring):
276 source = [source]
jadmanskid7b79ed2009-01-07 17:19:48 +0000277 dest = os.path.abspath(dest)
jadmanskica7da372008-10-21 16:26:52 +0000278
mblighc9892c02010-01-06 19:02:16 +0000279 # If rsync is disabled or fails, try scp.
showard6eafb492010-01-15 20:29:06 +0000280 try_scp = True
281 if self.use_rsync():
mblighc9892c02010-01-06 19:02:16 +0000282 try:
283 remote_source = self._encode_remote_paths(source)
284 local_dest = utils.sh_escape(dest)
285 rsync = self._make_rsync_cmd([remote_source], local_dest,
286 delete_dest, preserve_symlinks)
287 utils.run(rsync)
showard6eafb492010-01-15 20:29:06 +0000288 try_scp = False
mblighc9892c02010-01-06 19:02:16 +0000289 except error.CmdError, e:
290 logging.warn("trying scp, rsync failed: %s" % e)
mblighc9892c02010-01-06 19:02:16 +0000291
292 if try_scp:
jadmanskid7b79ed2009-01-07 17:19:48 +0000293 # scp has no equivalent to --delete, just drop the entire dest dir
294 if delete_dest and os.path.isdir(dest):
295 shutil.rmtree(dest)
296 os.mkdir(dest)
jadmanskica7da372008-10-21 16:26:52 +0000297
jadmanskid7b79ed2009-01-07 17:19:48 +0000298 remote_source = self._make_rsync_compatible_source(source, False)
299 if remote_source:
showard56176ec2009-10-28 19:52:30 +0000300 # _make_rsync_compatible_source() already did the escaping
301 remote_source = self._encode_remote_paths(remote_source,
302 escape=False)
jadmanskid7b79ed2009-01-07 17:19:48 +0000303 local_dest = utils.sh_escape(dest)
jadmanski2583a432009-02-10 23:59:11 +0000304 scp = self._make_scp_cmd([remote_source], local_dest)
jadmanskid7b79ed2009-01-07 17:19:48 +0000305 try:
306 utils.run(scp)
307 except error.CmdError, e:
308 raise error.AutoservRunError(e.args[0], e.args[1])
jadmanskica7da372008-10-21 16:26:52 +0000309
mblighfeac0102009-04-28 18:31:12 +0000310 if not preserve_perm:
311 # we have no way to tell scp to not try to preserve the
312 # permissions so set them after copy instead.
313 # for rsync we could use "--no-p --chmod=ugo=rwX" but those
314 # options are only in very recent rsync versions
315 self._set_umask_perms(dest)
316
jadmanskica7da372008-10-21 16:26:52 +0000317
mbligh45561782009-05-11 21:14:34 +0000318 def send_file(self, source, dest, delete_dest=False,
319 preserve_symlinks=False):
jadmanskica7da372008-10-21 16:26:52 +0000320 """
321 Copy files from a local path to the remote host.
322
323 Directories will be copied recursively.
324 If a source component is a directory with a trailing slash,
325 the content of the directory will be copied, otherwise, the
326 directory itself and its content will be copied. This
327 behavior is similar to that of the program 'rsync'.
328
329 Args:
330 source: either
331 1) a single file or directory, as a string
332 2) a list of one or more (possibly mixed)
333 files or directories
334 dest: a file or a directory (if source contains a
335 directory or more than one element, you must
336 supply a directory dest)
mbligh89e258d2008-10-24 13:58:08 +0000337 delete_dest: if this is true, the command will also clear
338 out any old files at dest that are not in the
339 source
mbligh45561782009-05-11 21:14:34 +0000340 preserve_symlinks: controls if symlinks on the source will be
341 copied as such on the destination or transformed into the
342 referenced file/directory
jadmanskica7da372008-10-21 16:26:52 +0000343
344 Raises:
345 AutoservRunError: the scp command failed
346 """
mblighefccc1b2010-01-11 19:08:42 +0000347
348 # Start a master SSH connection if necessary.
349 self.start_master_ssh()
350
jadmanskica7da372008-10-21 16:26:52 +0000351 if isinstance(source, basestring):
352 source = [source]
jadmanski2583a432009-02-10 23:59:11 +0000353 remote_dest = self._encode_remote_paths([dest])
jadmanskica7da372008-10-21 16:26:52 +0000354
mblighc9892c02010-01-06 19:02:16 +0000355 # If rsync is disabled or fails, try scp.
showard6eafb492010-01-15 20:29:06 +0000356 try_scp = True
357 if self.use_rsync():
mblighc9892c02010-01-06 19:02:16 +0000358 try:
359 local_sources = [utils.sh_escape(path) for path in source]
360 rsync = self._make_rsync_cmd(local_sources, remote_dest,
361 delete_dest, preserve_symlinks)
362 utils.run(rsync)
showard6eafb492010-01-15 20:29:06 +0000363 try_scp = False
mblighc9892c02010-01-06 19:02:16 +0000364 except error.CmdError, e:
365 logging.warn("trying scp, rsync failed: %s" % e)
mblighc9892c02010-01-06 19:02:16 +0000366
367 if try_scp:
jadmanskid7b79ed2009-01-07 17:19:48 +0000368 # scp has no equivalent to --delete, just drop the entire dest dir
369 if delete_dest:
showard27160152009-07-15 14:28:42 +0000370 is_dir = self.run("ls -d %s/" % dest,
jadmanskid7b79ed2009-01-07 17:19:48 +0000371 ignore_status=True).exit_status == 0
372 if is_dir:
373 cmd = "rm -rf %s && mkdir %s"
mbligh5a0ca532009-08-03 16:44:34 +0000374 cmd %= (dest, dest)
jadmanskid7b79ed2009-01-07 17:19:48 +0000375 self.run(cmd)
jadmanskica7da372008-10-21 16:26:52 +0000376
jadmanski2583a432009-02-10 23:59:11 +0000377 local_sources = self._make_rsync_compatible_source(source, True)
378 if local_sources:
379 scp = self._make_scp_cmd(local_sources, remote_dest)
jadmanskid7b79ed2009-01-07 17:19:48 +0000380 try:
381 utils.run(scp)
382 except error.CmdError, e:
383 raise error.AutoservRunError(e.args[0], e.args[1])
384
jadmanskica7da372008-10-21 16:26:52 +0000385
386 def ssh_ping(self, timeout=60):
387 try:
388 self.run("true", timeout=timeout, connect_timeout=timeout)
389 except error.AutoservSSHTimeout:
mblighd0e94982009-07-11 00:15:18 +0000390 msg = "Host (ssh) verify timed out (timeout = %d)" % timeout
jadmanskica7da372008-10-21 16:26:52 +0000391 raise error.AutoservSSHTimeout(msg)
mbligh9d738d62009-03-09 21:17:10 +0000392 except error.AutoservSshPermissionDeniedError:
393 #let AutoservSshPermissionDeniedError be visible to the callers
394 raise
jadmanskica7da372008-10-21 16:26:52 +0000395 except error.AutoservRunError, e:
mblighc971c5f2009-06-08 16:48:54 +0000396 # convert the generic AutoservRunError into something more
397 # specific for this context
398 raise error.AutoservSshPingHostError(e.description + '\n' +
399 repr(e.result_obj))
jadmanskica7da372008-10-21 16:26:52 +0000400
401
402 def is_up(self):
403 """
404 Check if the remote host is up.
405
jadmanskic0354912010-01-12 15:57:29 +0000406 @returns True if the remote host is up, False otherwise
jadmanskica7da372008-10-21 16:26:52 +0000407 """
408 try:
409 self.ssh_ping()
410 except error.AutoservError:
411 return False
412 else:
413 return True
414
415
416 def wait_up(self, timeout=None):
417 """
418 Wait until the remote host is up or the timeout expires.
419
420 In fact, it will wait until an ssh connection to the remote
421 host can be established, and getty is running.
422
jadmanskic0354912010-01-12 15:57:29 +0000423 @param timeout time limit in seconds before returning even
424 if the host is not up.
jadmanskica7da372008-10-21 16:26:52 +0000425
jadmanskic0354912010-01-12 15:57:29 +0000426 @returns True if the host was found to be up, False otherwise
jadmanskica7da372008-10-21 16:26:52 +0000427 """
428 if timeout:
429 end_time = time.time() + timeout
430
431 while not timeout or time.time() < end_time:
432 if self.is_up():
433 try:
434 if self.are_wait_up_processes_up():
jadmanski7ebac3d2010-06-17 16:06:31 +0000435 logging.debug('Host %s is now up', self.hostname)
jadmanskica7da372008-10-21 16:26:52 +0000436 return True
437 except error.AutoservError:
438 pass
439 time.sleep(1)
440
jadmanski7ebac3d2010-06-17 16:06:31 +0000441 logging.debug('Host %s is still down after waiting %d seconds',
442 self.hostname, int(timeout + time.time() - end_time))
jadmanskica7da372008-10-21 16:26:52 +0000443 return False
444
445
jadmanskic0354912010-01-12 15:57:29 +0000446 def wait_down(self, timeout=None, warning_timer=None, old_boot_id=None):
jadmanskica7da372008-10-21 16:26:52 +0000447 """
448 Wait until the remote host is down or the timeout expires.
449
jadmanskic0354912010-01-12 15:57:29 +0000450 If old_boot_id is provided, this will wait until either the machine
451 is unpingable or self.get_boot_id() returns a value different from
452 old_boot_id. If the boot_id value has changed then the function
453 returns true under the assumption that the machine has shut down
454 and has now already come back up.
jadmanskica7da372008-10-21 16:26:52 +0000455
jadmanskic0354912010-01-12 15:57:29 +0000456 If old_boot_id is None then until the machine becomes unreachable the
457 method assumes the machine has not yet shut down.
jadmanskica7da372008-10-21 16:26:52 +0000458
jadmanskic0354912010-01-12 15:57:29 +0000459 @param timeout Time limit in seconds before returning even
460 if the host is still up.
461 @param warning_timer Time limit in seconds that will generate
462 a warning if the host is not down yet.
463 @param old_boot_id A string containing the result of self.get_boot_id()
464 prior to the host being told to shut down. Can be None if this is
465 not available.
466
467 @returns True if the host was found to be down, False otherwise
jadmanskica7da372008-10-21 16:26:52 +0000468 """
mblighe5e3cf22010-05-27 23:33:14 +0000469 #TODO: there is currently no way to distinguish between knowing
470 #TODO: boot_id was unsupported and not knowing the boot_id.
mbligh2ed998f2009-04-08 21:03:47 +0000471 current_time = time.time()
jadmanskica7da372008-10-21 16:26:52 +0000472 if timeout:
mbligh2ed998f2009-04-08 21:03:47 +0000473 end_time = current_time + timeout
jadmanskica7da372008-10-21 16:26:52 +0000474
mbligh2ed998f2009-04-08 21:03:47 +0000475 if warning_timer:
476 warn_time = current_time + warning_timer
477
jadmanskic0354912010-01-12 15:57:29 +0000478 if old_boot_id is not None:
479 logging.debug('Host %s pre-shutdown boot_id is %s',
480 self.hostname, old_boot_id)
481
mbligh2ed998f2009-04-08 21:03:47 +0000482 while not timeout or current_time < end_time:
jadmanskic0354912010-01-12 15:57:29 +0000483 try:
484 new_boot_id = self.get_boot_id()
mblighdbc7e4a2010-01-15 20:34:20 +0000485 except error.AutoservError:
jadmanskic0354912010-01-12 15:57:29 +0000486 logging.debug('Host %s is now unreachable over ssh, is down',
487 self.hostname)
jadmanskica7da372008-10-21 16:26:52 +0000488 return True
jadmanskic0354912010-01-12 15:57:29 +0000489 else:
490 # if the machine is up but the boot_id value has changed from
491 # old boot id, then we can assume the machine has gone down
492 # and then already come back up
493 if old_boot_id is not None and old_boot_id != new_boot_id:
494 logging.debug('Host %s now has boot_id %s and so must '
495 'have rebooted', self.hostname, new_boot_id)
496 return True
mbligh2ed998f2009-04-08 21:03:47 +0000497
498 if warning_timer and current_time > warn_time:
499 self.record("WARN", None, "shutdown",
500 "Shutdown took longer than %ds" % warning_timer)
501 # Print the warning only once.
502 warning_timer = None
mbligha4464402009-04-17 20:13:41 +0000503 # If a machine is stuck switching runlevels
504 # This may cause the machine to reboot.
505 self.run('kill -HUP 1', ignore_status=True)
mbligh2ed998f2009-04-08 21:03:47 +0000506
jadmanskica7da372008-10-21 16:26:52 +0000507 time.sleep(1)
mbligh2ed998f2009-04-08 21:03:47 +0000508 current_time = time.time()
jadmanskica7da372008-10-21 16:26:52 +0000509
510 return False
jadmanskif6562912008-10-21 17:59:01 +0000511
mbligha0a27592009-01-24 01:41:36 +0000512
jadmanskif6562912008-10-21 17:59:01 +0000513 # tunable constants for the verify & repair code
mblighb86bfa12010-02-12 20:22:21 +0000514 AUTOTEST_GB_DISKSPACE_REQUIRED = get_value("SERVER",
515 "gb_diskspace_required",
516 type=int,
517 default=20)
mbligha0a27592009-01-24 01:41:36 +0000518
jadmanskif6562912008-10-21 17:59:01 +0000519
showardca572982009-09-18 21:20:01 +0000520 def verify_connectivity(self):
521 super(AbstractSSHHost, self).verify_connectivity()
jadmanskif6562912008-10-21 17:59:01 +0000522
showardb18134f2009-03-20 20:52:18 +0000523 logging.info('Pinging host ' + self.hostname)
jadmanskif6562912008-10-21 17:59:01 +0000524 self.ssh_ping()
mbligh2ba7ab02009-08-24 22:09:26 +0000525 logging.info("Host (ssh) %s is alive", self.hostname)
jadmanskif6562912008-10-21 17:59:01 +0000526
jadmanski80deb752009-01-21 17:14:16 +0000527 if self.is_shutting_down():
mblighc971c5f2009-06-08 16:48:54 +0000528 raise error.AutoservHostIsShuttingDownError("Host is shutting down")
jadmanski80deb752009-01-21 17:14:16 +0000529
mblighb49b5232009-02-12 21:54:49 +0000530
showardca572982009-09-18 21:20:01 +0000531 def verify_software(self):
532 super(AbstractSSHHost, self).verify_software()
jadmanskif6562912008-10-21 17:59:01 +0000533 try:
showardad812bf2009-10-20 23:49:56 +0000534 self.check_diskspace(autotest.Autotest.get_install_dir(self),
535 self.AUTOTEST_GB_DISKSPACE_REQUIRED)
jadmanskif6562912008-10-21 17:59:01 +0000536 except error.AutoservHostError:
537 raise # only want to raise if it's a space issue
showardad812bf2009-10-20 23:49:56 +0000538 except autotest.AutodirNotFoundError:
showardca572982009-09-18 21:20:01 +0000539 # autotest dir may not exist, etc. ignore
540 logging.debug('autodir space check exception, this is probably '
541 'safe to ignore\n' + traceback.format_exc())
mblighefccc1b2010-01-11 19:08:42 +0000542
543
544 def close(self):
545 super(AbstractSSHHost, self).close()
546 self._cleanup_master_ssh()
lmraf676f32010-02-04 03:36:26 +0000547 self.known_hosts_file.close()
mblighefccc1b2010-01-11 19:08:42 +0000548
549
550 def _cleanup_master_ssh(self):
551 """
552 Release all resources (process, temporary directory) used by an active
553 master SSH connection.
554 """
555 # If a master SSH connection is running, kill it.
556 if self.master_ssh_job is not None:
557 utils.nuke_subprocess(self.master_ssh_job.sp)
558 self.master_ssh_job = None
559
560 # Remove the temporary directory for the master SSH socket.
561 if self.master_ssh_tempdir is not None:
562 self.master_ssh_tempdir.clean()
563 self.master_ssh_tempdir = None
564 self.master_ssh_option = ''
565
566
567 def start_master_ssh(self):
568 """
569 Called whenever a slave SSH connection needs to be initiated (e.g., by
570 run, rsync, scp). If master SSH support is enabled and a master SSH
571 connection is not active already, start a new one in the background.
572 Also, cleanup any zombie master SSH connections (e.g., dead due to
573 reboot).
574 """
575 if not enable_master_ssh:
576 return
577
578 # If a previously started master SSH connection is not running
579 # anymore, it needs to be cleaned up and then restarted.
580 if self.master_ssh_job is not None:
581 if self.master_ssh_job.sp.poll() is not None:
582 logging.info("Master ssh connection to %s is down.",
583 self.hostname)
584 self._cleanup_master_ssh()
585
586 # Start a new master SSH connection.
587 if self.master_ssh_job is None:
588 # Create a shared socket in a temp location.
589 self.master_ssh_tempdir = autotemp.tempdir(unique_id='ssh-master')
590 self.master_ssh_option = ("-o ControlPath=%s/socket" %
591 self.master_ssh_tempdir.name)
592
593 # Start the master SSH connection in the background.
mbligh5644c122010-01-29 17:43:26 +0000594 master_cmd = self.ssh_command(options="-N -o ControlMaster=yes")
mblighefccc1b2010-01-11 19:08:42 +0000595 logging.info("Starting master ssh connection '%s'" % master_cmd)
596 self.master_ssh_job = utils.BgJob(master_cmd)
mbligh0a883702010-04-21 01:58:34 +0000597
598
599 def clear_known_hosts(self):
600 """Clears out the temporary ssh known_hosts file.
601
602 This is useful if the test SSHes to the machine, then reinstalls it,
603 then SSHes to it again. It can be called after the reinstall to
604 reduce the spam in the logs.
605 """
606 logging.info("Clearing known hosts for host '%s', file '%s'.",
607 self.hostname, self.known_hosts_fd)
608 # Clear out the file by opening it for writing and then closing.
609 fh = open(self.known_hosts_fd, "w")
610 fh.close()