From 3ad0a9d6764109efa2d66351d676ec937ff44080 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 27 May 2020 17:15:56 +0200 Subject: [PATCH] Checking for availability of the given distro --- lib/cr_vmware_tpl/cobbler.py | 16 +++++++++++++++- lib/cr_vmware_tpl/config.py | 7 ++++++- lib/cr_vmware_tpl/handler.py | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index 8b6dac4..51ee93d 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -31,7 +31,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.2.3' +__version__ = '0.2.4' LOG = logging.getLogger(__name__) @@ -165,6 +165,20 @@ class Cobbler(BaseHandler): return cobbler_version + # ------------------------------------------------------------------------- + def get_distro_list(self): + """Trying to get a list of all configured distros.""" + + distro_list = [] + proc = self.exec_cobbler(('distro', 'list'), no_simulate=True) + lines = proc.stdout.splitlines() + for line in proc.stdout.splitlines(): + distro = line.strip() + if distro: + distro_list.append(distro) + distro_list.sort(key=str.lower) + return distro_list + # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index c1e60a0..f58aca7 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -19,7 +19,7 @@ from fb_tools.config import ConfigError, BaseConfiguration from .xlate import XLATOR -__version__ = '1.4.0' +__version__ = '1.4.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -70,6 +70,7 @@ class CrTplConfiguration(BaseConfiguration): default_cobbler_ssh_port = 22 default_cobbler_ssh_user = 'root' default_cobbler_ssh_timeout = 30 + default_cobbler_distro = 'CentOS-8.1-x86_64' ssh_privkey = 'id_rsa_cr_vmw_tpl' @@ -106,6 +107,7 @@ class CrTplConfiguration(BaseConfiguration): self.private_ssh_key = None + self.cobbler_distro = self.default_cobbler_distro self.cobbler_host = self.default_cobbler_host self.cobbler_ssh_port = self.default_cobbler_ssh_port self.cobbler_ssh_user = self.default_cobbler_ssh_user @@ -393,6 +395,9 @@ class CrTplConfiguration(BaseConfiguration): re_timeout_key = re.compile(r'^\s*ssh-_]?timeout\s*$', re.IGNORECASE) for (key, value) in config.items(section_name): + if key.lower() == 'distro' and value.strip() != '': + self.cobbler_distro = value.strip() + continue if key.lower() == 'host' and value.strip() != '': self.cobbler_host = value.strip().lower() continue diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 98a106c..e59e020 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.3' +__version__ = '1.4.4' LOG = logging.getLogger(__name__) TZ = pytz.timezone('Europe/Berlin') @@ -173,6 +173,7 @@ class CrTplHandler(BaseHandler): LOG.debug(_("Starting handling ...")) self.cobbler.get_cobbler_version() + self.check_for_cobbler_distro() return 0 @@ -232,6 +233,21 @@ class CrTplHandler(BaseHandler): return 0 + # ------------------------------------------------------------------------- + def check_for_cobbler_distro(self): + LOG.debug(_( + "Checking, whether distro {!r} is available " + "on the cobbler host.").format(self.config.cobbler_distro)) + + distro_list = self.cobbler.get_distro_list() + if self.config.cobbler_distro not in distro_list: + msg = _("Did not found distro {!r} on the cobbler host.").format( + self.config.cobbler_distro) + raise ExpectedHandlerError(msg) + if self.verbose > 1: + msg = _("Distro {!r} is available on the cobbler host.").format( + self.config.cobbler_distro) + # ------------------------------------------------------------------------- def check_for_temp_tpl_vm(self, no_error=False): -- 2.39.5