use context stuff for fetching the ssh keys

This commit is contained in:
Michael Hudson-Doyle 2019-12-20 00:29:57 +13:00
parent 8fe1bffea1
commit 95a36eff56
1 changed files with 26 additions and 28 deletions

View File

@ -76,37 +76,35 @@ class SSHController(BaseController):
async def _fetch_ssh_keys(self, user_spec): async def _fetch_ssh_keys(self, user_spec):
ssh_import_id = "{ssh_import_id}:{import_username}".format(**user_spec) ssh_import_id = "{ssh_import_id}:{import_username}".format(**user_spec)
log.debug( with self.context.child("ssh_import_id", ssh_import_id):
"User input: %s, fetching ssh keys for %s", try:
user_spec, ssh_import_id) cp = await self.run_cmd_checked(
try: ['ssh-import-id', '-o-', ssh_import_id],
cp = await self.run_cmd_checked( failmsg=_("Importing keys failed:"))
['ssh-import-id', '-o-', ssh_import_id], except subprocess.CalledProcessError:
failmsg=_("Importing keys failed:")) return
except subprocess.CalledProcessError: key_material = cp.stdout.replace('\r', '').strip()
return
key_material = cp.stdout.replace('\r', '').strip()
try: try:
cp = await self.run_cmd_checked( cp = await self.run_cmd_checked(
['ssh-keygen', '-lf-'], ['ssh-keygen', '-lf-'],
failmsg=_( failmsg=_(
"ssh-keygen failed to show fingerprint of downloaded " "ssh-keygen failed to show fingerprint of downloaded "
"keys:"), "keys:"),
input=key_material) input=key_material)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return return
fingerprints = cp.stdout.replace( fingerprints = cp.stdout.replace(
"# ssh-import-id {}".format(ssh_import_id), "# ssh-import-id {}".format(ssh_import_id),
"").strip().splitlines() "").strip().splitlines()
if 'ssh-import-id' in self.app.answers.get("Identity", {}): if 'ssh-import-id' in self.app.answers.get("Identity", {}):
user_spec['authorized_keys'] = key_material.splitlines() user_spec['authorized_keys'] = key_material.splitlines()
self.done(user_spec) self.done(user_spec)
else: else:
self.ui.body.confirm_ssh_keys( self.ui.body.confirm_ssh_keys(
user_spec, ssh_import_id, key_material, fingerprints) user_spec, ssh_import_id, key_material, fingerprints)
def fetch_ssh_keys(self, user_spec): def fetch_ssh_keys(self, user_spec):
self._fetch_task = schedule_task(self._fetch_ssh_keys(user_spec)) self._fetch_task = schedule_task(self._fetch_ssh_keys(user_spec))