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