From 6756fdb987dc042546cee8f5f1396ad56ce0ae8d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 20 Jun 2022 16:20:25 +0200 Subject: [PATCH] Making changes on config because of Cobbler 3.X --- bin/create-vmware-template | 2 +- lib/cr_vmware_tpl/cobbler.py | 21 ++++++++--- lib/cr_vmware_tpl/config.py | 70 +++++++++++++++++++++++++++++++----- 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/bin/create-vmware-template b/bin/create-vmware-template index 38e7507..63745de 100755 --- a/bin/create-vmware-template +++ b/bin/create-vmware-template @@ -52,7 +52,7 @@ app.initialized = True if app.verbose > 2: print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) -# app() +app() sys.exit(0) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 8ed38c9..098e9f3 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -26,15 +26,15 @@ from pathlib import Path import paramiko from paramiko.ssh_exception import SSHException +from packaging.version import Version + # Own modules from fb_tools.common import pp, to_str, is_sequence - from fb_tools.errors import HandlerError, ExpectedHandlerError - from fb_tools.handling_obj import CompletedProcess - from fb_tools.handler import BaseHandler +from fb_tools.xlate import format_list from . import print_section_start, print_section_end @@ -42,7 +42,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.7.0' +__version__ = '0.7.1' LOG = logging.getLogger(__name__) @@ -87,6 +87,7 @@ class Cobbler(BaseHandler): self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout self.root_dir = CrTplConfiguration.default_cobbler_rootdir self.config = config + self.cobbler_version = None super(Cobbler, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -250,10 +251,22 @@ class Cobbler(BaseHandler): first_line = proc.stdout.splitlines()[0] cobbler_version = re.sub(r'^\s*cobbler\s+', '', first_line, flags=re.IGNORECASE).strip() + self.cobbler_version = Version(cobbler_version) + LOG.info(_("Version of {} is:").format("Cobbler") + " {!r}".format(cobbler_version)) + if self.cobbler_version.major not in (2, 3): + msg = _("Unsupported version {ver!r} of {co}, valid versions of {co} are {valid}.") + msg = msg.format(ver=cobbler_version, co='Cobbler', valid=format_list(['2.X', '3.X'])) + raise ExpectedCobblerError(msg) + + self.config.cobbler_major_version = self.cobbler_version.major + self.check_remote_directory(self.config.cobbler_rootdir, _('Cobbler root directory')) + if self.verbose > 3: + LOG.debug("Current configuration:\n" + pp(self.config.as_dict())) + return cobbler_version # ------------------------------------------------------------------------- diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index 260157b..a77757a 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -19,12 +19,13 @@ from pathlib import Path # Own modules from fb_tools.config import ConfigError, BaseConfiguration +from fb_tools.xlate import format_list from fb_vmware.config import VSPhereConfigInfo from .xlate import XLATOR -__version__ = '1.7.1' +__version__ = '1.7.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -83,8 +84,6 @@ class CrTplConfiguration(BaseConfiguration): default_cobbler_distro = 'CentOS-8.2-x86_64' default_cobbler_rootdir = Path('/var/lib/cobbler') default_cobbler_profile = 'vmware-template-' + default_os_id - default_cobbler_ks_dir = default_cobbler_rootdir / 'kickstarts' - default_cobbler_profile_ks = default_cobbler_ks_dir / (default_cobbler_profile + '.ks') default_cobbler_profile_repos = ['pp-centos8-baseos'] default_cobbler_nameservers = [ '93.188.109.11', @@ -100,6 +99,14 @@ class CrTplConfiguration(BaseConfiguration): default_cobbler_ws_docroot = Path('/var/www/html') default_cobbler_ws_rel_filesdir = Path('custom/vmware-template-files') + default_cobbler2_templates_dir_rel = 'kickstarts' + default_cobbler2_collections_dir_rel = 'config' + default_cobbler2_profiles_dir_rel = 'profiles.d' + + default_cobbler3_templates_dir_rel = 'templates' + default_cobbler3_collections_dir_rel = 'collections' + default_cobbler3_profiles_dir_rel = 'profiles' + ssh_privkey = 'id_rsa_cr_vmw_tpl' mac_address_template = "00:16:3e:53:{:02x}:{:02x}" @@ -150,6 +157,8 @@ class CrTplConfiguration(BaseConfiguration): self.private_ssh_key = None + self._cobbler_major_version = 3 + self.cobbler_bin = self.default_cobbler_bin self.cobbler_distro = self.default_cobbler_distro self.cobbler_host = self.default_cobbler_host @@ -158,8 +167,6 @@ class CrTplConfiguration(BaseConfiguration): self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout self.cobbler_rootdir = self.default_cobbler_rootdir self.cobbler_profile = self.default_cobbler_profile - self.cobbler_ks_dir = self.default_cobbler_ks_dir - self.cobbler_profile_ks = self.default_cobbler_profile_ks self.cobbler_profile_repos = copy.copy(self.default_cobbler_profile_repos) self.cobbler_nameservers = copy.copy(self.default_cobbler_nameservers) self.cobbler_dns_search = copy.copy(self.default_cobbler_dns_search) @@ -186,6 +193,51 @@ class CrTplConfiguration(BaseConfiguration): if initialized: self.initialized = True + # ------------------------------------------------------------------------- + @property + def cobbler_major_version(self): + """The major version of the used cobbler installation. Valid values are 2 and 3.""" + return self._cobbler_major_version + + @cobbler_major_version.setter + def cobbler_major_version(self, value): + if value not in (2, 3): + msg = _("Unsupported version {ver!r} of {co}, valid versions of {co} are {valid}.") + msg = msg.format(ver=value, co='Cobbler', valid=format_list(['2', '3'])) + raise CrTplConfigError(msg) + + self._cobbler_major_version = value + + # ------------------------------------------------------------------------- + @property + def cobbler_collections_dir(self): + """The absolute pathname of all Cobbler collections.""" + if self.cobbler_major_version == 2: + return self.cobbler_rootdir / self.default_cobbler2_collections_dir_rel + return self.cobbler_rootdir / self.default_cobbler3_collections_dir_rel + + # ------------------------------------------------------------------------- + @property + def cobbler_ks_dir(self): + """The absolute pathname of the directory of the kickstart/template/autoinstall files.""" + if self.cobbler_major_version == 2: + return self.cobbler_rootdir / self.default_cobbler2_templates_dir_rel + return self.cobbler_rootdir / self.default_cobbler3_templates_dir_rel + + # ------------------------------------------------------------------------- + @property + def cobbler_profile_dir(self): + """The absolute pathname of the directory of Cobbler profile configuration files.""" + if self.cobbler_major_version == 2: + return self.cobbler_collections_dir / self.default_cobbler2_profiles_dir_rel + return self.cobbler_collections_dir / self.default_cobbler3_profiles_dir_rel + + # ------------------------------------------------------------------------- + @property + def cobbler_profile_ks(self): + """The absolute pathname of the profile kickstart file.""" + return self.cobbler_ks_dir / (self.cobbler_profile + '.ks') + # ------------------------------------------------------------------------- @property def data_size_mb(self): @@ -242,6 +294,11 @@ class CrTplConfiguration(BaseConfiguration): """ res = super(CrTplConfiguration, self).as_dict(short=short) + res['cobbler_major_version'] = self.cobbler_major_version + res['cobbler_collections_dir'] = self.cobbler_collections_dir + res['cobbler_ks_dir'] = self.cobbler_ks_dir + res['cobbler_profile_dir'] = self.cobbler_profile_dir + res['cobbler_profile_ks'] = self.cobbler_profile_ks res['data_size_mb'] = self.data_size_mb res['data_size_kb'] = self.data_size_kb res['data_size'] = self.data_size @@ -282,9 +339,6 @@ class CrTplConfiguration(BaseConfiguration): if not self.cobbler_profile_given: self.cobbler_profile = 'vmware-template-' + self.os_id - self.cobbler_ks_dir = self.cobbler_rootdir / 'kickstarts' - self.cobbler_profile_ks = self.cobbler_ks_dir / (self.cobbler_profile + '.ks') - # ------------------------------------------------------------------------- def eval_config_section(self, config, section_name): -- 2.39.5