From: Frank Brehm Date: Fri, 24 Jun 2022 14:58:11 +0000 (+0200) Subject: Generating and deploying profile kickstart file X-Git-Tag: 2.6.2~1^2~8^2~40 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=b4fe17c14e47ad3aa3a58b30773140e224b14fbd;p=pixelpark%2Fcreate-vmware-tpl.git Generating and deploying profile kickstart file --- diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 5244644..4cb4c61 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -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)) diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index 7ca8a04..7654c2e 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -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() diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 90143f7..5a6e3f2 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -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()