]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Ensuring creation of profile.
authorFrank Brehm <frank@brehm-online.com>
Wed, 29 Jun 2022 13:09:11 +0000 (15:09 +0200)
committerFrank Brehm <frank@brehm-online.com>
Wed, 29 Jun 2022 13:09:11 +0000 (15:09 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/handler.py

index a5a8462cd4315fcc5701140721ab866818330bd1..1d87fc79519945b00228ea0109ab9c179230b6aa 100644 (file)
@@ -46,7 +46,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.8.6'
+__version__ = '0.8.7'
 
 LOG = logging.getLogger(__name__)
 
@@ -223,16 +223,27 @@ class Cobbler(BaseHandler):
             if self.verbose > 1:
                 LOG.debug(_("Connecting to {h!r}, port {p} as {u!r} per SSH ...").format(
                     h=self.host, p=self.ssh_port, u=self.ssh_user))
-            ssh.connect(
-                self.host, port=self.ssh_port, timeout=self.ssh_timeout,
-                username=self.ssh_user, key_filename=self.private_ssh_key)
 
-            sftp = ssh.open_sftp()
+            if self.simulate:
+                if self.verbose > 1:
+                    LOG.debug(_(
+                        "Simulating SCP of {local!r} to {user}@{host}:{remote} ...").format(
+                        local=str(local_file), user=self.ssh_user,
+                        host=self.host, remote=str(remote_file)))
 
-            if self.verbose > 1:
-                LOG.debug(_("SCP of {local!r} to {host}@{remote} ...").format(
-                    local=str(local_file), host=self.host, remote=str(remote_file)))
-            sftp.put(str(local_file), str(remote_file))
+            else:
+                ssh.connect(
+                    self.host, port=self.ssh_port, timeout=self.ssh_timeout,
+                    username=self.ssh_user, key_filename=self.private_ssh_key)
+
+                sftp = ssh.open_sftp()
+
+                if self.verbose > 1:
+                    LOG.debug(_("SCP of {local!r} to {user}@{host}:{remote} ...").format(
+                        local=str(local_file), user=self.ssh_user,
+                        host=self.host, remote=str(remote_file)))
+
+                sftp.put(str(local_file), str(remote_file))
 
         except SSHException as e:
             msg = _("Could not connect via {w} to {user}@{host}: {e}").format(
@@ -487,10 +498,13 @@ class Cobbler(BaseHandler):
         return proc.stdout
 
     # -------------------------------------------------------------------------
-    def ensure_root_authkeys(self):
+    def ensure_root_authkeys(self, tmp_auth_keys_file=None):
 
         bname = 'auth_keys_pp_betrieb'
-        local_file = self.base_dir / 'keys' / bname
+        if tmp_auth_keys_file:
+            local_file = tmp_auth_keys_file
+        else:
+            local_file = self.base_dir / 'keys' / bname
         remote_file = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir / bname
 
         self.ensure_remote_file(local_file, remote_file)
@@ -578,6 +592,8 @@ class Cobbler(BaseHandler):
         LOG.debug(_("Checking existing profile {!r} ...").format(profile))
         remote_file = self.cfg.cobbler_profile_dir / (self.cfg.cobbler_profile + '.json')
 
+        return
+
         fcontent = self.get_remote_filecontent(remote_file)
         if self.verbose > 2:
             LOG.debug(
@@ -665,32 +681,45 @@ class Cobbler(BaseHandler):
         LOG.info(_("Creating new profile {!r} ...").format(profile))
 
         os_id = self.cfg.os_id
-        comment = "Profile for creating a {} profile.".format(os_id)
+        distro_info = self.cfg.current_distro
+        comment = "Profile for creating a {} VM.".format(distro_info.description)
+
+        LOG.debug("Using kickstart file {!r}".format(self.cfg.cobbler_profile_ks))
+
+        repos = []
+        if distro_info.repos:
+            repos = distro_info.repos.as_list()
 
         args = ['profile', 'add']
         args.append('--name')
         args.append(self.cfg.cobbler_profile)
         args.append('--distro')
-        args.append(self.cfg.cobbler_distro)
+        args.append(distro_info.distro)
         args.append('--enable-menu')
         args.append('1')
-        args.append('--kickstart')
-        args.append(str(self.cfg.cobbler_profile_ks))
-        if self.cfg.cobbler_profile_repos:
+        if self.cfg.cobbler_major_version == 3:
+            args.append('--autoinstall')
+            args.append(str(self.cfg.cobbler_profile_ks.name))
+        else:
+            args.append('--kickstart')
+            args.append(str(self.cfg.cobbler_profile_ks))
+        if repos:
             args.append('--repos')
-            args.append(' '.join(self.cfg.cobbler_profile_repos))
+            args.append(' '.join(repos))
         args.append('--comment')
         args.append(comment)
         args.append('--virt-cpus')
         args.append('2')
         args.append('--virt-file-size')
-        args.append('32')
+        args.append('20')
         args.append('--virt-ram')
         args.append('4096')
         args.append('--virt-type')
         args.append('vmware')
         args.append('--virt-bridge')
         args.append('br0')
+        args.append('--virt-disk-driver')
+        args.append('vmdk')
         args.append('--name-servers')
         args.append(' '.join(self.cfg.cobbler_nameservers))
         args.append('--name-servers-search')
@@ -698,6 +727,9 @@ class Cobbler(BaseHandler):
 
         proc = self.exec_cobbler(args)
 
+        if self.verbose > 1:
+            LOG.debug(_("Completed SSH process:") + "\n{}".format(proc))
+
         if proc.returncode:
             err = _('No error message')
             if proc.stderr:
@@ -708,6 +740,21 @@ class Cobbler(BaseHandler):
                 rc=proc.returncode, err=err)
             raise ExpectedCobblerError(msg)
 
+        if proc.stderr:
+            msg = _("There was an error message on creating profile {!r}:").format(
+                self.cfg.cobbler_profile)
+            msg += ' ' + proc.stderr
+            LOG.error(msg)
+
+        if self.simulate:
+            return
+
+        profile_list = self.get_profile_list()
+        if self.cfg.cobbler_profile not in profile_list:
+            msg = _("Did not found profile {!r} after trying creation.").format(
+                self.cfg.cobbler_profile)
+            raise ExpectedCobblerError(msg)
+
     # -------------------------------------------------------------------------
     def ensure_system_ks(self):
 
@@ -1042,6 +1089,16 @@ class Cobbler(BaseHandler):
             return assigments[mac]
         return None
 
+    # -------------------------------------------------------------------------
+    def ensure_webroot(self):
+
+        docroot = self.cfg.cobbler_ws_docroot / self.cfg.cobbler_ws_rel_filesdir
+        webroot = docroot / self.cfg.system_status
+        desc = _("Webroot directory")
+        LOG.info(_("Ensuring existence of {what} {dir!r}...").format(what=desc, dir=webroot))
+
+        self.ensure_remote_directory(webroot, desc)
+
 
 # =============================================================================
 if __name__ == "__main__":
index ed95409cb60ad4b6bb4851226b2906c5c55f0de7..cf2a15a340377a1d9d9378c740b339d211df3c08 100644 (file)
@@ -57,7 +57,7 @@ from .cobbler import Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '2.2.2'
+__version__ = '2.2.3'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -331,20 +331,29 @@ class CrTplHandler(BaseHandler):
             LOG.debug(_("Closing ..."))
             self.vsphere.disconnect()
             self.vsphere = None
+            self.disconnect_ldap()
+
+            if self.auth_keys_file:
+                if self.auth_keys_file.exists():
+                    LOG.debug(_("Removing {!r} ...").format(str(self.auth_keys_file)))
+                    self.auth_keys_file.unlink()
 
         return retval
 
     # -------------------------------------------------------------------------
     def run(self):
 
+        if self.verbose > 1:
+            LOG.debug(_("Current configuration:") + '\n' + pp(self.cfg.as_dict()))
+
         LOG.debug(_("Starting handling ..."))
         self.cobbler.get_cobbler_version()
         self.check_for_cobbler_distro()
         self.cobbler.ensure_profile_ks()
         self.create_root_authkeys()
-        return 0
         self.cobbler.ensure_profile()
-        self.cobbler.ensure_root_authkeys()
+        self.cobbler.ensure_webroot()
+        self.cobbler.ensure_root_authkeys(self.auth_keys_file)
         self.cobbler.ensure_rsyslog_cfg_files()
         self.cobbler.ensure_snippets()
 
@@ -355,6 +364,7 @@ class CrTplHandler(BaseHandler):
                 self.auth_keys_file.unlink()
                 self.auth_keys_file = None
 
+        return 0
         self.cobbler.ensure_system_ks()
         self.cobbler.ensure_repo_files()
         self.cobbler.ensure_bashrc()
@@ -1302,7 +1312,7 @@ class CrTplHandler(BaseHandler):
                     auth_keys += used_key + '\n'
                     auth_keys += line
 
-            if self.verbose > 1:
+            if self.verbose > 2:
                 msg = _("Generated authorized keys for root:") + '\n' + auth_keys
                 LOG.debug(msg)