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