cloudinit: Fix setting root password in user-data

This commit is contained in:
Cole Robinson
2019-07-01 16:33:28 -04:00
parent 19317024cc
commit ec47c9dafc
2 changed files with 18 additions and 9 deletions

View File

@@ -1632,7 +1632,9 @@ class ParserCloudInit(VirtCLIParser):
def parse_cloud_init(optstr):
ret = CloudInitData()
if optstr == 1:
# This means bare --cloud-init, so there's nothing to parse
# This means bare --cloud-init, so there's nothing to parse.
log.warning("Defaulting to --cloud-init root-password=generate")
ret.root_password = "generate"
return ret
parser = ParserCloudInit(optstr)

View File

@@ -29,19 +29,26 @@ def create_metadata(scratchdir, hostname=None):
def create_userdata(scratchdir, cloudinit_data, username=None, password=None):
if not password:
password = ""
for dummy in range(16):
password += random.choice(string.ascii_letters + string.digits)
content = "#cloud-config\n"
if username:
content += "name: %s\n" % username
if cloudinit_data.root_password == "generate":
pass
else:
if password:
content += "password: %s\n" % password
log.debug("Generated password for first boot: \n%s", password)
rootpass = cloudinit_data.root_password
if rootpass == "generate":
rootpass = ""
for dummy in range(16):
rootpass += random.choice(string.ascii_letters + string.digits)
log.warning("Generated password for first boot: %s", rootpass)
time.sleep(20)
if rootpass:
content += "chpasswd:\n"
content += " list: |\n"
content += " root:%s\n" % rootpass
content += " expire: True\n"
content += "runcmd:\n"
content += "- [ sudo, touch, /etc/cloud/cloud-init.disabled ]\n"
log.debug("Generated cloud-init userdata:\n%s", content)