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