|
43 | 43 | """ |
44 | 44 |
|
45 | 45 | from __future__ import print_function |
| 46 | +from collections import namedtuple |
46 | 47 | from cookielib import MozillaCookieJar |
47 | 48 | from getpass import getpass |
48 | 49 | from itertools import cycle |
|
55 | 56 | import re |
56 | 57 | import subprocess |
57 | 58 | import sys |
| 59 | +import tempfile |
58 | 60 | import time |
59 | 61 | import urlparse |
60 | 62 | import webbrowser |
|
63 | 65 | from docopt import docopt |
64 | 66 | from docopt import DocoptExit |
65 | 67 | import requests |
66 | | -import tempfile |
67 | 68 |
|
68 | 69 | __version__ = '0.3.1' |
69 | 70 |
|
@@ -1214,57 +1215,60 @@ def keys_add(self, args): |
1214 | 1215 |
|
1215 | 1216 | Usage: deis keys:add [<key>] |
1216 | 1217 | """ |
| 1218 | + |
| 1219 | + Key = namedtuple('Key', 'path name type str comment') |
| 1220 | + |
| 1221 | + def parse_key(path): |
| 1222 | + """Parse an SSH public key path into a Key namedtuple.""" |
| 1223 | + name = path.split(os.path.sep)[-1] |
| 1224 | + with open(path) as f: |
| 1225 | + data = f.read() |
| 1226 | + match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data) |
| 1227 | + if not match: |
| 1228 | + print("Could not parse SSH public key {0}".format(name)) |
| 1229 | + return |
| 1230 | + key_type, key_str, key_comment = match.groups() |
| 1231 | + return Key(path, name, key_type, key_str, key_comment) |
| 1232 | + |
1217 | 1233 | path = args.get('<key>') |
1218 | 1234 | if not path: |
| 1235 | + # find public keys and prompt the user to pick one |
1219 | 1236 | ssh_dir = os.path.expanduser('~/.ssh') |
1220 | | - pubkeys = glob.glob(os.path.join(ssh_dir, '*.pub')) |
1221 | | - pubkeys_list = [] |
1222 | | - if not pubkeys: |
| 1237 | + pubkey_paths = glob.glob(os.path.join(ssh_dir, '*.pub')) |
| 1238 | + if not pubkey_paths: |
1223 | 1239 | print('No SSH public keys found') |
1224 | 1240 | return |
| 1241 | + pubkeys_list = [parse_key(k) for k in pubkey_paths] |
1225 | 1242 | print('Found the following SSH public keys:') |
1226 | | - for i, k in enumerate(pubkeys): |
1227 | | - key = k.split(os.path.sep)[-1] |
1228 | | - with open(k) as f: |
1229 | | - data = f.read() |
1230 | | - match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data) |
1231 | | - if not match: |
1232 | | - print("Could not parse SSH public key {0}".format(key)) |
1233 | | - return |
1234 | | - key_type, key_str, _key_comment = match.groups() |
1235 | | - pubkeys_list.append([k, key, key_type, key_str, _key_comment]) |
1236 | | - print("{0}) {1} {2}".format(i + 1, key, _key_comment)) |
1237 | | - |
| 1243 | + for i, key_ in enumerate(pubkeys_list): |
| 1244 | + print("{}) {} {}".format(i + 1, key_.name, key_.comment)) |
1238 | 1245 | inp = raw_input('Which would you like to use with Deis? ') |
1239 | 1246 | try: |
1240 | 1247 | selected_key = pubkeys_list[int(inp) - 1] |
1241 | | - path = selected_key[0] |
1242 | 1248 | except: |
1243 | 1249 | print('Aborting') |
1244 | 1250 | return |
1245 | | - |
1246 | | - if not selected_key[4]: |
1247 | | - body = { |
1248 | | - 'id': selected_key[1].replace('.pub', ''), |
1249 | | - 'public': "{0} {1}".format( |
1250 | | - selected_key[2], selected_key[3] |
1251 | | - ) |
1252 | | - } |
1253 | | - sys.stdout.write("Uploading {} to Deis...".format(selected_key[1])) |
1254 | | - else: |
1255 | | - body = { |
1256 | | - 'id': selected_key[4], |
1257 | | - 'public': "{0} {1}".format( |
1258 | | - selected_key[2], selected_key[3] |
1259 | | - ) |
1260 | | - } |
1261 | | - sys.stdout.write("Uploading {} to Deis...".format(selected_key[4])) |
1262 | | - sys.stdout.flush() |
1263 | | - response = self._dispatch('post', '/api/keys', json.dumps(body)) |
1264 | | - if response.status_code == requests.codes.created: # @UndefinedVariable |
1265 | | - print('done') |
1266 | | - else: |
1267 | | - raise ResponseError(response) |
| 1251 | + else: |
| 1252 | + # check the specified key format |
| 1253 | + selected_key = parse_key(path) |
| 1254 | + if not selected_key: |
| 1255 | + return |
| 1256 | + # Upload the key to Deis |
| 1257 | + if selected_key.comment: |
| 1258 | + key_id = selected_key.comment |
| 1259 | + else: |
| 1260 | + key_id = selected_key.name.replace('.pub', '') |
| 1261 | + body = { |
| 1262 | + 'id': key_id, |
| 1263 | + 'public': "{} {}".format(selected_key.type, selected_key.str) |
| 1264 | + } |
| 1265 | + sys.stdout.write("Uploading {} to Deis...".format(key_id)) |
| 1266 | + sys.stdout.flush() |
| 1267 | + response = self._dispatch('post', '/api/keys', json.dumps(body)) |
| 1268 | + if response.status_code == requests.codes.created: # @UndefinedVariable |
| 1269 | + print('done') |
| 1270 | + else: |
| 1271 | + raise ResponseError(response) |
1268 | 1272 |
|
1269 | 1273 | def keys_list(self, args): |
1270 | 1274 | """ |
|
0 commit comments