From: Frank Brehm Date: Wed, 27 May 2020 16:31:35 +0000 (+0200) Subject: Start with ensuring profile X-Git-Tag: 2.1.2^2~9^2~31^2~102 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=0dc019334eb5a82da8a8bc9eb583ff37cc777b29;p=pixelpark%2Fcreate-vmware-tpl.git Start with ensuring profile --- diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index c493eda..6cc2b7c 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -13,6 +13,7 @@ import logging import re import datetime import pipes +import json # Third party modules import paramiko @@ -31,7 +32,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.2.4' +__version__ = '0.3.0' LOG = logging.getLogger(__name__) @@ -61,6 +62,7 @@ class Cobbler(BaseHandler): self.private_ssh_key = None self.ssh = None self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout + self.root_dir = CrTplConfiguration.default_cobbler_rootdir super(Cobbler, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -76,6 +78,7 @@ class Cobbler(BaseHandler): self.ssh_port = config.cobbler_ssh_port self.ssh_user = config.cobbler_ssh_user self.ssh_timeout = config.cobbler_ssh_timeout + self.root_dir = config.cobbler_rootdir if initialized: self.initialized = True @@ -181,6 +184,55 @@ class Cobbler(BaseHandler): LOG.debug(_("Sorted list of found distros:") + "\n{}".format(pp(distro_list))) return distro_list + # ------------------------------------------------------------------------- + def get_profile_list(self): + """Trying to get a list of all configured cobbler profiles.""" + + profile_list = [] + proc = self.exec_cobbler(('profile', 'list'), no_simulate=True) + lines = proc.stdout.splitlines() + for line in proc.stdout.splitlines(): + profile = line.strip() + if profile: + profile_list.append(profile) + profile_list.sort(key=str.lower) + if self.verbose > 1: + LOG.debug(_("Sorted list of found profiles:") + "\n{}".format(pp(profile_list))) + return profile_list + + # ------------------------------------------------------------------------- + def ensure_profile(self): + """Ensure the existence and the correctnes of the given profile.""" + + profile = self.config.cobbler_profile + + LOG.info(_("Ensuring profile {!r} ...").format(profile)) + + profile_list = self.get_profile_list() + + if profile in profile_list: + self.change_profile() + else: + self.add_profile() + + # ------------------------------------------------------------------------- + def change_profile(self): + """Ensure correctnes of an existing profile.""" + + profile = self.config.cobbler_profile + distro = self.config.cobbler_distro + + LOG.debug(_("Checking existing profile {!r} ...").format(profile)) + + # ------------------------------------------------------------------------- + def add_profile(self): + """Creating a new profile.""" + + profile = self.config.cobbler_profile + distro = self.config.cobbler_distro + + LOG.info(_("Creating new profile {!r} ...").format(profile)) + # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index f58aca7..153a6df 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -13,13 +13,14 @@ import logging import re # Third party modules +from pathlib import Path # Own modules from fb_tools.config import ConfigError, BaseConfiguration from .xlate import XLATOR -__version__ = '1.4.1' +__version__ = '1.4.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -71,6 +72,8 @@ class CrTplConfiguration(BaseConfiguration): default_cobbler_ssh_user = 'root' default_cobbler_ssh_timeout = 30 default_cobbler_distro = 'CentOS-8.1-x86_64' + default_cobbler_rootdir = Path('/var/lib/cobbler') + default_cobbler_profile = 'vmware-template_centos8' ssh_privkey = 'id_rsa_cr_vmw_tpl' @@ -112,6 +115,8 @@ class CrTplConfiguration(BaseConfiguration): self.cobbler_ssh_port = self.default_cobbler_ssh_port self.cobbler_ssh_user = self.default_cobbler_ssh_user self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout + self.cobbler_rootdir = self.default_cobbler_rootdir + self.cobbler_profile = self.default_cobbler_profile self.excluded_datastores = [] @@ -392,7 +397,8 @@ class CrTplConfiguration(BaseConfiguration): re_port_key = re.compile(r'^\s*ssh[-_]?port\s*$', re.IGNORECASE) re_user_key = re.compile(r'^\s*ssh[-_]?user\s*$', re.IGNORECASE) - re_timeout_key = re.compile(r'^\s*ssh-_]?timeout\s*$', re.IGNORECASE) + re_timeout_key = re.compile(r'^\s*ssh[-_]?timeout\s*$', re.IGNORECASE) + re_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE) for (key, value) in config.items(section_name): if key.lower() == 'distro' and value.strip() != '': @@ -409,6 +415,19 @@ class CrTplConfiguration(BaseConfiguration): continue if re_timeout_key.match(key): self.cobbler_ssh_timeout = int(value) + continue + if re_rootdir_key.match(key): + dpath = Path(value) + if dpath.is_absolute(): + self.cobbler_rootdir = Path(value) + else: + msg = _("Path for Cobbler root directory {!r} is not absolute.").format( + str(dpath)) + LOG.error(msg) + continue + if key.lower() == 'profile' and value.strip() != '': + self.cobbler_profile = value.strip().lower() + continue # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 1ac829b..665f5ea 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -40,7 +40,7 @@ from .cobbler import CobblerError, Cobbler from .xlate import XLATOR -__version__ = '1.4.4' +__version__ = '1.4.5' LOG = logging.getLogger(__name__) TZ = pytz.timezone('Europe/Berlin') @@ -174,6 +174,7 @@ class CrTplHandler(BaseHandler): LOG.debug(_("Starting handling ...")) self.cobbler.get_cobbler_version() self.check_for_cobbler_distro() + self.cobbler.ensure_profile() return 0