[virt-tools-list] [virt-manager PATCH v2] cloudinit: Move password generation inside class CloudInitData

Athina Plaskasoviti athina.plaskasoviti at gmail.com
Sun Jul 14 14:07:03 UTC 2019


Function generate_password() provides random 16 digit password and
get_root_password() handling root_password assignment.

Signed-off-by: Athina Plaskasoviti <athina.plaskasoviti at gmail.com>
---
 virtinst/install/cloudinit.py | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/virtinst/install/cloudinit.py b/virtinst/install/cloudinit.py
index 91a131c1..158b8d4e 100644
--- a/virtinst/install/cloudinit.py
+++ b/virtinst/install/cloudinit.py
@@ -7,6 +7,19 @@ from ..logger import log
 class CloudInitData():
     disable = None
     root_password = None
+    generated_root_password = None
+
+    def generate_password(self):
+        self.generated_root_password = ""
+        for dummy in range(16):
+            self.generated_root_password += random.choice(string.ascii_letters + string.digits)
+        return self.generated_root_password
+
+    def get_root_password(self):
+        if self.root_password == "generate":
+            return self.generate_password()
+        else:
+            return self.root_password
 
 
 def create_metadata(scratchdir):
@@ -26,13 +39,7 @@ def create_metadata(scratchdir):
 def create_userdata(scratchdir, cloudinit_data):
     content = "#cloud-config\n"
 
-    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)
-
+    rootpass = cloudinit_data.get_root_password()
     if rootpass:
         content += "chpasswd:\n"
         content += "  list: |\n"
@@ -45,7 +52,6 @@ def create_userdata(scratchdir, cloudinit_data):
         content += "- [ sudo, touch, /etc/cloud/cloud-init.disabled ]\n"
     log.debug("Generated cloud-init userdata:\n%s", content)
 
-
     fileobj = tempfile.NamedTemporaryFile(
             prefix="virtinst-", suffix="-userdata",
             dir=scratchdir, delete=False)
-- 
2.20.1




More information about the virt-tools-list mailing list