← Back to team overview

ubuntu-bugcontrol team mailing list archive

[Merge] ~jslarraz/ubuntu-qa-tools:create-ssh-key into ubuntu-qa-tools:master

 

Jorge Sancho Larraz has proposed merging ~jslarraz/ubuntu-qa-tools:create-ssh-key into ubuntu-qa-tools:master.

Commit message:
uvt: automatically create ssh key if it does not exist

Requested reviews:
  Ubuntu Bug Control (ubuntu-bugcontrol)

For more details, see:
https://code.launchpad.net/~jslarraz/ubuntu-qa-tools/+git/ubuntu-qa-tools/+merge/460669

Currently uvt instruct the user to create an ssh key if it can not find one. This MR will create the key for the user. 
-- 
Your team Ubuntu Bug Control is requested to review the proposed merge of ~jslarraz/ubuntu-qa-tools:create-ssh-key into ubuntu-qa-tools:master.
diff --git a/vm-tools/uvt b/vm-tools/uvt
index c7b0143..f34aace 100755
--- a/vm-tools/uvt
+++ b/vm-tools/uvt
@@ -3355,9 +3355,20 @@ def check_ssh_key():
     '''Checks if the user has an ssh key'''
 
     if not os.path.exists(uvt_conf['vm_ssh_key']):
-        print("\nYour user must have an ssh key.\n" +
-              "Please create one now with 'ssh-keygen -t rsa' and try again.\n", file=sys.stderr)
-        sys.exit(1)
+        print("ssh key not found in " + uvt_conf['vm_ssh_key'] + ". Creating it...")
+        # Infer key type from name, defaults to rsa
+        for key_type in ["ed25519-sk", "ed25519", "ecdsa-sk", "ecdsa", "dsa", "rsa"]:
+            if key_type in os.path.basename(uvt_conf['vm_ssh_key']):
+                break
+        os.makedirs(os.path.dirname(uvt_conf['vm_ssh_key']), exist_ok=True)
+        rc, out = runcmd(["ssh-keygen", "-t", key_type, "-f", uvt_conf['vm_ssh_key'].split(".pub")[0], "-q", "-N", ""])
+        if rc != 0:
+            print("Error while creating ssh key, please create it manually with 'ssh-keygen -t rsa'.")
+            sys.exit(1)
+
+    # Ensure .ssh directory is initialized anyway
+    if not os.path.exists(os.path.expanduser("~/.ssh")):
+        os.makedirs(os.path.dirname("~/.ssh"), exist_ok=True)
 
 def parse_config_file(conf_file):
     '''Parses a config file'''

Follow ups