]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Handling of an existing profile
authorFrank Brehm <frank@brehm-online.com>
Wed, 29 Jun 2022 14:17:10 +0000 (16:17 +0200)
committerFrank Brehm <frank@brehm-online.com>
Wed, 29 Jun 2022 14:17:10 +0000 (16:17 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/handler.py

index 1d87fc79519945b00228ea0109ab9c179230b6aa..09d6834dca861b8c65a97881184e08e6c9739b80 100644 (file)
@@ -32,9 +32,11 @@ from packaging.version import Version
 
 import jinja2
 
+from six.moves import configparser
+
 # Own modules
 
-from fb_tools.common import pp, to_str, is_sequence
+from fb_tools.common import pp, to_str, is_sequence, to_bool
 from fb_tools.errors import HandlerError, ExpectedHandlerError
 from fb_tools.handling_obj import CompletedProcess
 from fb_tools.handler import BaseHandler
@@ -46,7 +48,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.8.7'
+__version__ = '0.8.8'
 
 LOG = logging.getLogger(__name__)
 
@@ -590,64 +592,111 @@ class Cobbler(BaseHandler):
         profile = self.cfg.cobbler_profile
 
         LOG.debug(_("Checking existing profile {!r} ...").format(profile))
-        remote_file = self.cfg.cobbler_profile_dir / (self.cfg.cobbler_profile + '.json')
 
-        return
+        profile_vars = self.get_profile_vars(profile)
 
-        fcontent = self.get_remote_filecontent(remote_file)
-        if self.verbose > 2:
-            LOG.debug(
-                _("Got content of remote {!r}:").format(str(remote_file)) + '\n' + fcontent)
+        if self.verbose > 1:
+            LOG.debug(_("Got profile variables:") + '\n' + pp(profile_vars))
+        self._change_profile(profile_vars)
+
+    # -------------------------------------------------------------------------
+    def get_profile_vars(self, profile):
+
+        vars_out = '[main]\n'
+
+        kwargs = {
+            'allow_no_value': True,
+            'strict': False,
+        }
+
+        cmd = ('profile', 'dumpvars', '--name', profile)
 
+        proc = self.exec_cobbler(cmd, no_simulate=True)
+        vars_out += proc.stdout
+
+        parser = configparser.RawConfigParser(**kwargs)
         try:
-            js = json.loads(fcontent)
-        except JSONDecodeError as e:
-            msg = _("Error interpreting JS: {}").format(e)
+            parser.read_string(vars_out)
+        except Exception as e:
+            msg = _("Got {what} on reading and parsing of profile {p!r}:").format(
+                what=e.__class__.__name__, p=profile)
+            msg += ' ' + str(e)
             raise ExpectedCobblerError(msg)
 
-        if self.verbose > 1:
-            LOG.debug(_("Got json object for profile {!r}:").format(profile) + '\n' + pp(js))
+        data = {}
 
-        self._change_profile(js)
+        for (key, value) in parser.items('main'):
+            k = key.lower()
+            data[k] = value
+
+        return data
 
     # -------------------------------------------------------------------------
-    def _change_profile(self, js):
+    def _change_profile(self, profile_vars):
 
         profile = self.cfg.cobbler_profile
         distro = self.cfg.cobbler_distro
+        distro_info = self.cfg.current_distro
+
+        LOG.debug(_("Checking existing profile {n!r} ({d}) ...").format(
+            n=profile, d=distro_info.description))
+
+        repos = []
+        if distro_info.repos:
+            repos = distro_info.repos.as_list()
+        repos_str = ' '.join(repos)
+
+        comment = "Profile for creating a {} VM.".format(distro_info.description)
+        name_servers = '[' + ', '.join(
+            map(lambda x: "'" + x + "'", self.cfg.cobbler_nameservers)) + ']'
+        dns_search = '[' + ', '.join(
+            map(lambda x: "'" + x + "'", self.cfg.cobbler_dns_search)) + ']'
 
         args = []
 
-        if js['distro'] != distro:
+        if self.verbose > 1:
+            msg = _("Checking for distro:") + ' ' + distro
+            LOG.debug(msg)
+        if profile_vars['distro'] != distro:
             args.append('--distro')
             args.append(distro)
 
-        if not js['enable_menu']:
+        enable_menu = to_bool(profile_vars['enable_menu'])
+        if not enable_menu:
             args.append('--enable-menu')
             args.append('1')
 
-        if js['kickstart'] != str(self.cfg.cobbler_profile_ks):
-            if self.cfg.cobbler_major_version == 3:
+        if self.cfg.cobbler_major_version == 3:
+            if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name):
                 args.append('--autoinstall')
-            else:
+                args.append(str(self.cfg.cobbler_profile_ks.name))
+        else:
+            if profile_vars['kickstart'] != str(self.cfg.cobbler_profile_ks):
                 args.append('--kickstart')
-            args.append(str(self.cfg.cobbler_profile_ks))
+                args.append(str(self.cfg.cobbler_profile_ks))
 
-        if js['repos'] != self.cfg.cobbler_profile_repos:
+        if self.verbose > 1:
+            msg = _("Checking for repos:") + ' ' + repos_str
+            LOG.debug(msg)
+        if profile_vars['repos'] != repos_str:
             args.append('--repos')
-            args.append(' '.join(self.cfg.cobbler_profile_repos))
+            args.append(repos_str)
 
-        os_id = self.cfg.os_id
-        comment = "Kickstart file for creating a {} profile.".format(os_id)
-        if js['comment'] != comment:
+        if profile_vars['comment'] != comment:
             args.append('--comment')
             args.append(comment)
 
-        if js['name_servers'] != self.cfg.cobbler_nameservers:
+        if self.verbose > 1:
+            msg = _("Checking for nameservers:") + ' ' + name_servers
+            LOG.debug(msg)
+        if profile_vars['name_servers'] != name_servers:
             args.append('--name-servers')
             args.append(' '.join(self.cfg.cobbler_nameservers))
 
-        if js['name_servers_search'] != self.cfg.cobbler_dns_search:
+        if self.verbose > 1:
+            msg = _("Checking for DNS search domains:") + ' ' + dns_search
+            LOG.debug(msg)
+        if profile_vars['name_servers_search'] != dns_search:
             args.append('--name-servers-search')
             args.append(' '.join(self.cfg.cobbler_dns_search))
 
index cf2a15a340377a1d9d9378c740b339d211df3c08..8b08a6aecf47ba5894bc0923bf9bfd8352db34bd 100644 (file)
@@ -57,7 +57,7 @@ from .cobbler import Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '2.2.3'
+__version__ = '2.2.4'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -364,14 +364,14 @@ class CrTplHandler(BaseHandler):
                 self.auth_keys_file.unlink()
                 self.auth_keys_file = None
 
-        return 0
-        self.cobbler.ensure_system_ks()
+        # self.cobbler.ensure_system_ks()
         self.cobbler.ensure_repo_files()
         self.cobbler.ensure_bashrc()
         self.cobbler.ensure_vimrc()
         self.cobbler.ensure_create_motd()
         self.cobbler.ensure_postfix_files()
         self.cobbler.ensure_logrotate_files()
+        return 0
 
         print_section_start('vmw_info', 'Collecting VMWare info', collapsed=True)
         self.vsphere.get_about()