cloudinit: Add ssh-key=/path/to/key cli option

Login to VM with:
ssh root@vm_ip_address

Signed-off-by: AthinaPl <athina.plaskasoviti@gmail.com>
This commit is contained in:
AthinaPl 2019-08-02 13:20:04 +03:00 committed by Cole Robinson
parent 3014644f25
commit b4daeba6b5
4 changed files with 18 additions and 2 deletions

View File

@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDAuibybX5lw2G/LPIyqgQS5KwgbOnKMA9TZyQPtRmIfKSGypdMJmGQ+Gsf54A9VI8zoL2fnq2t66zQIPJU53XveXU0oqCm7PfsEcoYjnqDeUeiqvCfTye8bIbEmIkLriH0SaISNnzyN0JSfb0VDYIg8Za6iW3/PfPs+tV0PSYyVEm3pBNJ9bHat2liA1/Afk0UWNrhCQG9/5v9kR36aIxNU+4qI9cZ2npFWt61/7t2otz2GcygJDCUYEp6wDlmoW6DHXnaUUj1USjZ9uI1IrgmjZYxfjlt/UaB7AJOJP/3bOC1iPXBe9HKt30YUG23KaVfK9DDeGD/tlAeIklaYK8RZd4zUXSm3eZjeZCGiC3XFYIIyf7e7M/IZE+/DbD1jQEGxGd7AmdVBJZPBFtkAd4Blypaidykx7n1UcfF0WWISfFsae956PHnqnSBKM5OUDh2y5LowgXjWmr/BVJmnaiNbeMhgC3axZVL3EmFSVtvwnup+sRmDwYoHh/RbmInyns=

View File

@ -83,6 +83,7 @@ test_files = {
'COLLIDE': "/dev/default-pool/collidevol1.img",
'ADMIN-PASSWORD-FILE': "%s/admin-password.txt" % XMLDIR,
'USER-PASSWORD-FILE': "%s/user-password.txt" % XMLDIR,
'SSH-KEY-FILE': "%s/ssh-key.txt" % XMLDIR
}
@ -876,6 +877,7 @@ c.add_compare("--connect %s --os-variant fedora26 --pxe --print-xml" % (utils.UR
c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init", "cloud-init-default") # default --cloud-init behavior is root-password-generate=yes,disable=yes
c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init root-password-generate=yes,disable=no", "cloud-init-options") # --cloud-init options
c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init root-password-file=%(ADMIN-PASSWORD-FILE)s,disable=no", "cloud-init-options") # --cloud-init-options
c.add_compare("--disk %(EXISTIMG1)s --os-variant fedora28 --cloud-init ssh-key=%(SSH-KEY-FILE)s", "cloud-init-options") # --cloud-init-options
c.add_valid("--panic help --disk=? --check=help", grep="path_in_use") # Make sure introspection doesn't blow up
c.add_valid("--connect test:///default --test-stub-command", use_default_args=False) # --test-stub-command
c.add_valid("--nodisks --pxe", grep="VM performance may suffer") # os variant warning

View File

@ -1629,6 +1629,7 @@ class ParserCloudInit(VirtCLIParser):
cls.add_arg("root-password-generate", "root_password_generate", is_onoff=True)
cls.add_arg("root-password-file", "root_password_file")
cls.add_arg("disable", "disable", is_onoff=True)
cls.add_arg("ssh-key", "ssh_key")
def parse_cloud_init(optstr):

View File

@ -10,6 +10,7 @@ class CloudInitData():
root_password_generate = None
root_password_file = None
generated_root_password = None
ssh_key = None
def generate_password(self):
self.generated_root_password = ""
@ -27,6 +28,10 @@ class CloudInitData():
elif self.root_password_file:
return self._get_password(self.root_password_file)
def get_ssh_key(self):
if self.ssh_key:
return self._get_password(self.ssh_key)
def create_metadata(scratchdir):
fileobj = tempfile.NamedTemporaryFile(
@ -44,8 +49,8 @@ def create_metadata(scratchdir):
def create_userdata(scratchdir, cloudinit_data):
content = "#cloud-config\n"
if cloudinit_data.root_password_generate or cloudinit_data.root_password_file:
rootpass = cloudinit_data.get_root_password()
if rootpass:
content += "chpasswd:\n"
content += " list: |\n"
content += " root:%s\n" % rootpass
@ -55,6 +60,13 @@ def create_userdata(scratchdir, cloudinit_data):
elif cloudinit_data.root_password_file:
content += " expire: False\n"
if cloudinit_data.ssh_key:
rootpass = cloudinit_data.get_ssh_key()
content += "users:\n"
content += " - name: root\n"
content += " ssh-authorized-keys:\n"
content += " - %s\n" % rootpass
if cloudinit_data.disable:
content += "runcmd:\n"
content += "- [ sudo, touch, /etc/cloud/cloud-init.disabled ]\n"