]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Generating and deploying profile kickstart file
authorFrank Brehm <frank@brehm-online.com>
Fri, 24 Jun 2022 14:58:11 +0000 (16:58 +0200)
committerFrank Brehm <frank@brehm-online.com>
Fri, 24 Jun 2022 14:58:11 +0000 (16:58 +0200)
lib/cr_vmware_tpl/cobbler.py
lib/cr_vmware_tpl/config.py
lib/cr_vmware_tpl/handler.py

index 52446441e19c528e4740eaac01c821eb4b0f0ef5..4cb4c618b7d5151d57feddd1ccb13317d5d403b5 100644 (file)
@@ -17,6 +17,8 @@ import json
 import hashlib
 import textwrap
 import ipaddress
+import tempfile
+import os
 
 from json import JSONDecodeError
 
@@ -44,7 +46,7 @@ from .config import CrTplConfiguration
 
 from .xlate import XLATOR
 
-__version__ = '0.8.3'
+__version__ = '0.8.4'
 
 LOG = logging.getLogger(__name__)
 
@@ -90,6 +92,7 @@ class Cobbler(BaseHandler):
         self.root_dir = CrTplConfiguration.default_cobbler_rootdir
         self.cfg = cfg
         self.cobbler_version = None
+        self.local_ks_file = None
 
         super(Cobbler, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
@@ -110,6 +113,13 @@ class Cobbler(BaseHandler):
         if initialized:
             self.initialized = True
 
+    # -------------------------------------------------------------------------
+    def __del__(self):
+
+        if self.local_ks_file:
+            if self.local_ks_file.exists():
+                self.local_ks_file.unlink()
+
     # -------------------------------------------------------------------------
     def exec_cobbler(self, cmd, no_simulate=False):
 
@@ -507,18 +517,38 @@ class Cobbler(BaseHandler):
     # -------------------------------------------------------------------------
     def ensure_profile_ks(self):
 
-        local_ks = self.base_dir / 'kickstart' / ('profile.' + self.cfg.cobbler_profile + '.ks')
+        prefix = 'tmp.' + self.cfg.cobbler_profile + '.'
+        (fh, tmp_ks) = tempfile.mkstemp(prefix=prefix, suffix='.ks', text=True)
+        os.close(fh)
+        self.local_ks_file = Path(tmp_ks)
+        LOG.debug(_("Using temporary kickstart file {!r}.").format(tmp_ks))
+
+        jinja_env = jinja2.Environment(
+            loader=jinja2.FileSystemLoader(str(self.base_dir / 'templates')),
+            autoescape=jinja2.select_autoescape(),
+        )
+        ks_template = jinja_env.get_template('el-standard.ks')
+        ks_content = ks_template.render(distro=self.cfg.current_distro) + '\n\n'
+        if self.verbose > 1:
+            LOG.debug(_("Generated kickstart file content:") + '\n' + ks_content)
+
+        self.local_ks_file.write_text(ks_content)
+
+        # local_ks = self.base_dir / 'kickstart' / ('profile.' + self.cfg.cobbler_profile + '.ks')
         remote_ks = self.cfg.cobbler_profile_ks
         LOG.info(_("Ensuring currentness of profile kickstart script {!r}.").format(
             str(remote_ks)))
 
-        self.ensure_remote_file(local_ks, remote_ks)
+        self.ensure_remote_file(self.local_ks_file, remote_ks)
+
+        LOG.debug(_("Removing {!r} ...").format(str(self.local_ks_file)))
+        self.local_ks_file.unlink()
+        self.local_ks_file = None
 
     # -------------------------------------------------------------------------
     def ensure_profile(self):
         """Ensure the existence and the correctnes of the given profile."""
 
-        self.ensure_profile_ks()
         profile = self.cfg.cobbler_profile
 
         LOG.info(_("Ensuring profile {!r} ...").format(profile))
index 7ca8a040d051948abc1317bd2224314c92234c15..7654c2e37b1cf95c7a8066e25ca36bb67d2e3536 100644 (file)
@@ -31,7 +31,7 @@ from . import DEFAULT_CONFIG_DIR, DEFAULT_DISTRO_ARCH
 
 from .xlate import XLATOR
 
-__version__ = '1.9.4'
+__version__ = '1.9.5'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -436,7 +436,7 @@ class CrTplConfiguration(BaseMultiConfig):
     valid_system_status = ('development', 'testing', 'acceptance', 'production')
     default_system_status = 'development'
 
-    default_cobbler_profile = 'vmware-template-' + default_os_id + '-' + default_system_status
+    default_cobbler_profile = 'vmware-template.' + default_os_id + '.' + default_system_status
 
     default_swap_size_mb = 512
 
@@ -688,7 +688,7 @@ class CrTplConfiguration(BaseMultiConfig):
             self.template_name = self.os_id + '-template'
 
         if not self.cobbler_profile_given:
-            self.cobbler_profile = 'vmware-template-' + self.os_id + '-' + self.system_status
+            self.cobbler_profile = 'vmware-template.' + self.os_id + '.' + self.system_status
 
         self.verify_cobbler_distros()
 
index 90143f72ab0d73ef2ec3987f87d1fe9325b20101..5a6e3f2abcd35c14ebde1d3f9a7f0527f45700da 100644 (file)
@@ -49,7 +49,7 @@ from .cobbler import Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '2.1.2'
+__version__ = '2.1.3'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -266,13 +266,14 @@ class CrTplHandler(BaseHandler):
         LOG.debug(_("Starting handling ..."))
         self.cobbler.get_cobbler_version()
         self.check_for_cobbler_distro()
-        self.cobbler.ensure_system_ks()
+        self.cobbler.ensure_profile_ks()
         return 0
         self.cobbler.ensure_profile()
         self.cobbler.ensure_root_authkeys()
         self.cobbler.ensure_rsyslog_cfg_files()
         self.cobbler.ensure_snippets()
         self.cobbler.ensure_keys()
+        self.cobbler.ensure_system_ks()
         self.cobbler.ensure_repo_files()
         self.cobbler.ensure_bashrc()
         self.cobbler.ensure_vimrc()