Skip to content

Commit 113258d

Browse files
committed
Change default behavior of keys:add to upload as key_comment if available.
This edit will default to uploading the users key as identified by its key comment if the key comment is defined, and gracefully fall back to the older method if it is not. This should improve usability for some users working from multiple workstations.
1 parent 358d36b commit 113258d

1 file changed

Lines changed: 36 additions & 27 deletions

File tree

client/deis.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,38 +1191,47 @@ def keys_add(self, args):
11911191
"""
11921192
path = args.get('<key>')
11931193
if not path:
1194-
ssh_dir = os.path.expanduser('~/.ssh')
1195-
pubkeys = glob.glob(os.path.join(ssh_dir, '*.pub'))
1196-
if not pubkeys:
1197-
print('No SSH public keys found')
1198-
return
1199-
print('Found the following SSH public keys:')
1200-
for i, k in enumerate(pubkeys):
1201-
key = k.split(os.path.sep)[-1]
1202-
print("{0}) {1}".format(i + 1, key))
1203-
inp = raw_input('Which would you like to use with Deis? ')
1204-
try:
1205-
path = pubkeys[int(inp) - 1]
1206-
except:
1207-
print('Aborting')
1194+
ssh_dir = os.path.expanduser('~/.ssh')
1195+
pubkeys = glob.glob(os.path.join(ssh_dir, '*.pub'))
1196+
pubkeys_list = []
1197+
if not pubkeys:
1198+
print('No SSH public keys found')
1199+
return
1200+
print('Found the following SSH public keys:')
1201+
for i, k in enumerate(pubkeys):
1202+
key = k.split(os.path.sep)[-1]
1203+
with open(k) as f:
1204+
data = f.read()
1205+
match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data)
1206+
if not match:
1207+
print("Could not parse SSH public key {0}".format(key))
12081208
return
1209-
key_id = path.split(os.path.sep)[-1].replace('.pub', '')
1210-
with open(path) as f:
1211-
data = f.read()
1212-
match = re.match(r'^(ssh-...) ([^ ]+) ?(.*)', data)
1213-
if not match:
1214-
print('Could not parse public key material')
1209+
key_type, key_str, _key_comment = match.groups()
1210+
pubkeys_list.append([k, key, key_type, key_str, _key_comment])
1211+
print("{0}) {1} {2}".format(i + 1, key, _key_comment))
1212+
1213+
inp = raw_input('Which would you like to use with Deis? ')
1214+
try:
1215+
selected_key = pubkeys_list[int(inp) - 1]
1216+
path = selected_key[0]
1217+
except:
1218+
print('Aborting')
12151219
return
1216-
key_type, key_str, _key_comment = match.groups()
1217-
body = {'id': key_id, 'public': "{0} {1}".format(key_type, key_str)}
1218-
sys.stdout.write("Uploading {} to Deis... ".format(path))
1219-
sys.stdout.flush()
1220-
response = self._dispatch('post', '/api/keys', json.dumps(body))
1221-
if response.status_code == requests.codes.created: # @UndefinedVariable
1220+
1221+
if not selected_key[4]:
1222+
body = {'id': selected_key[1].replace('.pub', ''), 'public': "{0} {1}".format(selected_key[2], selected_key[3])}
1223+
sys.stdout.write("Uploading {} to Deis...".format(selected_key[1]))
1224+
else:
1225+
body = {'id': selected_key[4], 'public': "{0} {1}".format(selected_key[2], selected_key[3])}
1226+
sys.stdout.write("Uploading {} to Deis...".format(selected_key[4]))
1227+
sys.stdout.flush()
1228+
response = self._dispatch('post', '/api/keys', json.dumps(body))
1229+
if response.status_code == requests.codes.created: # @UndefinedVariable
12221230
print('done')
1223-
else:
1231+
else:
12241232
raise ResponseError(response)
12251233

1234+
12261235
def keys_list(self, args):
12271236
"""
12281237
List SSH keys for the logged in user

0 commit comments

Comments
 (0)