mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
mbligh | 63073c9 | 2008-03-31 16:49:32 +0000 | [diff] [blame] | 3 | # Copyright 2008 Google Inc. Released under the GPL v2 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | Miscellaneous small functions. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 9 | __author__ = """ |
| 10 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 11 | poirier@google.com (Benjamin Poirier), |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 12 | stutsman@google.com (Ryan Stutsman) |
| 13 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 14 | |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 15 | import atexit, os, re, shutil, textwrap, sys, tempfile, types |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 16 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import utils |
jadmanski | 6d2ada5 | 2008-12-15 17:22:57 +0000 | [diff] [blame] | 18 | from autotest_lib.server import subcommand |
mbligh | ea397bb | 2008-02-02 19:17:51 +0000 | [diff] [blame] | 19 | |
| 20 | |
mbligh | bea5682 | 2007-08-31 08:53:40 +0000 | [diff] [blame] | 21 | # A dictionary of pid and a list of tmpdirs for that pid |
| 22 | __tmp_dirs = {} |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 23 | |
| 24 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 25 | ############# we need pass throughs for the methods in client/common_lib/utils |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 26 | def run(command, timeout=None, ignore_status=False, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 27 | stdout_tee=None, stderr_tee=None, verbose=True, stdin=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | return utils.run(command, timeout, ignore_status, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 29 | stdout_tee, stderr_tee, verbose, stdin=stdin) |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 30 | |
| 31 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 32 | def system(command, timeout=None, ignore_status=False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 33 | return utils.system(command, timeout, ignore_status) |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def system_output(command, timeout=None, ignore_status=False, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 37 | retain_output=False): |
| 38 | return utils.system_output(command, timeout, ignore_status, |
| 39 | retain_output) |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 40 | |
| 41 | |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 42 | def urlopen(url, data=None, proxies=None, timeout=300): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 43 | return utils.urlopen(url, data=data, proxies=proxies, timeout=timeout) |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def urlretrieve(url, filename=None, reporthook=None, data=None, timeout=300): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 47 | return utils.urlretrieve(url, filename=filename, reporthook=reporthook, |
| 48 | data=data, timeout=timeout) |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 49 | |
| 50 | |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 51 | def read_keyval(path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 52 | return utils.read_keyval(path) |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def write_keyval(path, dictionary): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 56 | return utils.write_keyval(path, dictionary) |
mbligh | c25b58f | 2008-05-29 21:05:25 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | #################################################################### |
| 60 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 61 | def sh_escape(command): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 62 | """ |
| 63 | Escape special characters from a command so that it can be passed |
| 64 | as a double quoted (" ") string in a (ba)sh command. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 65 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 66 | Args: |
| 67 | command: the command string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 68 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 69 | Returns: |
| 70 | The escaped command string. The required englobing double |
| 71 | quotes are NOT added and so should be added at some point by |
| 72 | the caller. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 73 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 74 | See also: http://www.tldp.org/LDP/abs/html/escapingsection.html |
| 75 | """ |
| 76 | command = command.replace("\\", "\\\\") |
| 77 | command = command.replace("$", r'\$') |
| 78 | command = command.replace('"', r'\"') |
| 79 | command = command.replace('`', r'\`') |
| 80 | return command |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def scp_remote_escape(filename): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 84 | """ |
| 85 | Escape special characters from a filename so that it can be passed |
| 86 | to scp (within double quotes) as a remote file. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 87 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 88 | Bis-quoting has to be used with scp for remote files, "bis-quoting" |
| 89 | as in quoting x 2 |
| 90 | scp does not support a newline in the filename |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 91 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 92 | Args: |
| 93 | filename: the filename string to escape. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 94 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 95 | Returns: |
| 96 | The escaped filename string. The required englobing double |
| 97 | quotes are NOT added and so should be added at some point by |
| 98 | the caller. |
| 99 | """ |
| 100 | escape_chars= r' !"$&' "'" r'()*,:;<=>?[\]^`{|}' |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 101 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 102 | new_name= [] |
| 103 | for char in filename: |
| 104 | if char in escape_chars: |
| 105 | new_name.append("\\%s" % (char,)) |
| 106 | else: |
| 107 | new_name.append(char) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 108 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 109 | return sh_escape("".join(new_name)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 110 | |
| 111 | |
mbligh | 6e18dab | 2007-10-24 21:27:18 +0000 | [diff] [blame] | 112 | def get(location, local_copy = False): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 113 | """Get a file or directory to a local temporary directory. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 114 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 115 | Args: |
| 116 | location: the source of the material to get. This source may |
| 117 | be one of: |
| 118 | * a local file or directory |
| 119 | * a URL (http or ftp) |
| 120 | * a python file-like object |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 121 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 122 | Returns: |
| 123 | The location of the file or directory where the requested |
| 124 | content was saved. This will be contained in a temporary |
| 125 | directory on the local host. If the material to get was a |
| 126 | directory, the location will contain a trailing '/' |
| 127 | """ |
| 128 | tmpdir = get_tmp_dir() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 129 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 130 | # location is a file-like object |
| 131 | if hasattr(location, "read"): |
| 132 | tmpfile = os.path.join(tmpdir, "file") |
| 133 | tmpfileobj = file(tmpfile, 'w') |
| 134 | shutil.copyfileobj(location, tmpfileobj) |
| 135 | tmpfileobj.close() |
| 136 | return tmpfile |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 137 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 138 | if isinstance(location, types.StringTypes): |
| 139 | # location is a URL |
| 140 | if location.startswith('http') or location.startswith('ftp'): |
| 141 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 142 | utils.urlretrieve(location, tmpfile) |
| 143 | return tmpfile |
| 144 | # location is a local path |
| 145 | elif os.path.exists(os.path.abspath(location)): |
| 146 | if not local_copy: |
| 147 | if os.path.isdir(location): |
| 148 | return location.rstrip('/') + '/' |
| 149 | else: |
| 150 | return location |
| 151 | tmpfile = os.path.join(tmpdir, os.path.basename(location)) |
| 152 | if os.path.isdir(location): |
| 153 | tmpfile += '/' |
| 154 | shutil.copytree(location, tmpfile, symlinks=True) |
| 155 | return tmpfile |
| 156 | shutil.copyfile(location, tmpfile) |
| 157 | return tmpfile |
| 158 | # location is just a string, dump it to a file |
| 159 | else: |
| 160 | tmpfd, tmpfile = tempfile.mkstemp(dir=tmpdir) |
| 161 | tmpfileobj = os.fdopen(tmpfd, 'w') |
| 162 | tmpfileobj.write(location) |
| 163 | tmpfileobj.close() |
| 164 | return tmpfile |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 165 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 166 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 167 | def get_tmp_dir(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 168 | """Return the pathname of a directory on the host suitable |
| 169 | for temporary file storage. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 170 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 171 | The directory and its content will be deleted automatically |
| 172 | at the end of the program execution if they are still present. |
| 173 | """ |
jadmanski | 6d2ada5 | 2008-12-15 17:22:57 +0000 | [diff] [blame] | 174 | dir_name = tempfile.mkdtemp(prefix="autoserv-") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 175 | pid = os.getpid() |
| 176 | if not pid in __tmp_dirs: |
| 177 | __tmp_dirs[pid] = [] |
| 178 | __tmp_dirs[pid].append(dir_name) |
| 179 | return dir_name |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 180 | |
| 181 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 182 | def __clean_tmp_dirs(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 183 | """Erase temporary directories that were created by the get_tmp_dir() |
| 184 | function and that are still present. |
| 185 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 186 | pid = os.getpid() |
| 187 | if pid not in __tmp_dirs: |
| 188 | return |
| 189 | for dir in __tmp_dirs[pid]: |
| 190 | try: |
| 191 | shutil.rmtree(dir) |
| 192 | except OSError, e: |
| 193 | if e.errno == 2: |
| 194 | pass |
| 195 | __tmp_dirs[pid] = [] |
jadmanski | 6d2ada5 | 2008-12-15 17:22:57 +0000 | [diff] [blame] | 196 | atexit.register(__clean_tmp_dirs) |
| 197 | subcommand.subcommand.register_join_hook(lambda _: __clean_tmp_dirs()) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def unarchive(host, source_material): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 201 | """Uncompress and untar an archive on a host. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 202 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 203 | If the "source_material" is compresses (according to the file |
| 204 | extension) it will be uncompressed. Supported compression formats |
| 205 | are gzip and bzip2. Afterwards, if the source_material is a tar |
| 206 | archive, it will be untarred. |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 207 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 208 | Args: |
| 209 | host: the host object on which the archive is located |
| 210 | source_material: the path of the archive on the host |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 211 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 212 | Returns: |
| 213 | The file or directory name of the unarchived source material. |
| 214 | If the material is a tar archive, it will be extracted in the |
| 215 | directory where it is and the path returned will be the first |
| 216 | entry in the archive, assuming it is the topmost directory. |
| 217 | If the material is not an archive, nothing will be done so this |
| 218 | function is "harmless" when it is "useless". |
| 219 | """ |
| 220 | # uncompress |
| 221 | if (source_material.endswith(".gz") or |
| 222 | source_material.endswith(".gzip")): |
| 223 | host.run('gunzip "%s"' % (sh_escape(source_material))) |
| 224 | source_material= ".".join(source_material.split(".")[:-1]) |
| 225 | elif source_material.endswith("bz2"): |
| 226 | host.run('bunzip2 "%s"' % (sh_escape(source_material))) |
| 227 | source_material= ".".join(source_material.split(".")[:-1]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 228 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 229 | # untar |
| 230 | if source_material.endswith(".tar"): |
| 231 | retval= host.run('tar -C "%s" -xvf "%s"' % ( |
| 232 | sh_escape(os.path.dirname(source_material)), |
| 233 | sh_escape(source_material),)) |
| 234 | source_material= os.path.join(os.path.dirname(source_material), |
| 235 | retval.stdout.split()[0]) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 236 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 237 | return source_material |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 238 | |
| 239 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 240 | def get_server_dir(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 241 | path = os.path.dirname(sys.modules['autotest_lib.server.utils'].__file__) |
| 242 | return os.path.abspath(path) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 243 | |
| 244 | |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 245 | def find_pid(command): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 246 | for line in utils.system_output('ps -eo pid,cmd').rstrip().split('\n'): |
| 247 | (pid, cmd) = line.split(None, 1) |
| 248 | if re.search(command, cmd): |
| 249 | return int(pid) |
| 250 | return None |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 251 | |
| 252 | |
| 253 | def nohup(command, stdout='/dev/null', stderr='/dev/null', background=True, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 254 | env = {}): |
| 255 | cmd = ' '.join(key+'='+val for key, val in env.iteritems()) |
| 256 | cmd += ' nohup ' + command |
| 257 | cmd += ' > %s' % stdout |
| 258 | if stdout == stderr: |
| 259 | cmd += ' 2>&1' |
| 260 | else: |
| 261 | cmd += ' 2> %s' % stderr |
| 262 | if background: |
| 263 | cmd += ' &' |
| 264 | utils.system(cmd) |
mbligh | 34a3fd7 | 2007-12-10 17:16:22 +0000 | [diff] [blame] | 265 | |
| 266 | |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 267 | def default_mappings(machines): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 268 | """ |
| 269 | Returns a simple mapping in which all machines are assigned to the |
| 270 | same key. Provides the default behavior for |
| 271 | form_ntuples_from_machines. """ |
| 272 | mappings = {} |
| 273 | failures = [] |
| 274 | |
| 275 | mach = machines[0] |
| 276 | mappings['ident'] = [mach] |
| 277 | if len(machines) > 1: |
| 278 | machines = machines[1:] |
| 279 | for machine in machines: |
| 280 | mappings['ident'].append(machine) |
| 281 | |
| 282 | return (mappings, failures) |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 283 | |
| 284 | |
| 285 | def form_ntuples_from_machines(machines, n=2, mapping_func=default_mappings): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 286 | """Returns a set of ntuples from machines where the machines in an |
| 287 | ntuple are in the same mapping, and a set of failures which are |
| 288 | (machine name, reason) tuples.""" |
| 289 | ntuples = [] |
| 290 | (mappings, failures) = mapping_func(machines) |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 291 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 292 | # now run through the mappings and create n-tuples. |
| 293 | # throw out the odd guys out |
| 294 | for key in mappings: |
| 295 | key_machines = mappings[key] |
| 296 | total_machines = len(key_machines) |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 297 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 298 | # form n-tuples |
| 299 | while len(key_machines) >= n: |
| 300 | ntuples.append(key_machines[0:n]) |
| 301 | key_machines = key_machines[n:] |
mbligh | 0b4fe6e | 2008-05-06 20:41:37 +0000 | [diff] [blame] | 302 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 303 | for mach in key_machines: |
| 304 | failures.append((mach, "machine can not be tupled")) |
| 305 | |
| 306 | return (ntuples, failures) |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 307 | |
jadmanski | b0605d9 | 2008-06-06 16:12:34 +0000 | [diff] [blame] | 308 | |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 309 | def parse_machine(machine, user = 'root', port = 22, password = ''): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 310 | """ |
| 311 | Parse the machine string user:pass@host:port and return it separately, |
| 312 | if the machine string is not complete, use the default parameters |
| 313 | when appropriate. |
| 314 | """ |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 315 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 316 | user = user |
| 317 | port = port |
| 318 | password = password |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 319 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 320 | if re.search('@', machine): |
| 321 | machine = machine.split('@') |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 322 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 323 | if re.search(':', machine[0]): |
| 324 | machine[0] = machine[0].split(':') |
| 325 | user = machine[0][0] |
| 326 | password = machine[0][1] |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 327 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 328 | else: |
| 329 | user = machine[0] |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 330 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 331 | if re.search(':', machine[1]): |
| 332 | machine[1] = machine[1].split(':') |
| 333 | hostname = machine[1][0] |
| 334 | port = int(machine[1][1]) |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 335 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 336 | else: |
| 337 | hostname = machine[1] |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 338 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 339 | elif re.search(':', machine): |
| 340 | machine = machine.split(':') |
| 341 | hostname = machine[0] |
| 342 | port = int(machine[1]) |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 343 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 344 | else: |
| 345 | hostname = machine |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 346 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 347 | return hostname, user, password, port |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 348 | |
| 349 | |
| 350 | def get_public_key(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 351 | """ |
| 352 | Return a valid string ssh public key for the user executing autoserv or |
| 353 | autotest. If there's no DSA or RSA public key, create a DSA keypair with |
| 354 | ssh-keygen and return it. |
| 355 | """ |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 356 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 357 | ssh_conf_path = os.path.join(os.environ['HOME'], '.ssh') |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 358 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 359 | dsa_public_key_path = os.path.join(ssh_conf_path, 'id_dsa.pub') |
| 360 | dsa_private_key_path = os.path.join(ssh_conf_path, 'id_dsa') |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 361 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 362 | rsa_public_key_path = os.path.join(ssh_conf_path, 'id_rsa.pub') |
| 363 | rsa_private_key_path = os.path.join(ssh_conf_path, 'id_rsa') |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 364 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 365 | has_dsa_keypair = os.path.isfile(dsa_public_key_path) and \ |
| 366 | os.path.isfile(dsa_private_key_path) |
| 367 | has_rsa_keypair = os.path.isfile(rsa_public_key_path) and \ |
| 368 | os.path.isfile(rsa_private_key_path) |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 369 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 370 | if has_dsa_keypair: |
| 371 | print 'DSA keypair found, using it' |
| 372 | public_key_path = dsa_public_key_path |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 373 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 374 | elif has_rsa_keypair: |
| 375 | print 'RSA keypair found, using it' |
| 376 | public_key_path = rsa_public_key_path |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 377 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 378 | else: |
| 379 | print 'Neither RSA nor DSA keypair found, creating DSA ssh key pair' |
| 380 | system('ssh-keygen -t dsa -q -N "" -f %s' % dsa_private_key_path) |
| 381 | public_key_path = dsa_public_key_path |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 382 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 383 | public_key = open(public_key_path, 'r') |
| 384 | public_key_str = public_key.read() |
| 385 | public_key.close() |
mbligh | aa9e674 | 2008-06-05 21:14:05 +0000 | [diff] [blame] | 386 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 387 | return public_key_str |