From afc229563c74c1b8fc764fcb89ea9d3d2f608590 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 22 Sep 2023 13:44:56 +0200 Subject: [PATCH] Separating cobbler dependend methods from cr_vmware_tpl.config to cr_vmware_tpl.config.cobbler. --- lib/cr_vmware_tpl/config/__init__.py | 181 +--------------------- lib/cr_vmware_tpl/config/cobbler.py | 223 +++++++++++++++++++++++++++ 2 files changed, 226 insertions(+), 178 deletions(-) create mode 100644 lib/cr_vmware_tpl/config/cobbler.py diff --git a/lib/cr_vmware_tpl/config/__init__.py b/lib/cr_vmware_tpl/config/__init__.py index 92f7313..aa32c04 100644 --- a/lib/cr_vmware_tpl/config/__init__.py +++ b/lib/cr_vmware_tpl/config/__init__.py @@ -34,10 +34,10 @@ from ..errors import CrTplConfigError from ..xlate import XLATOR -from .distro import CobblerDistroInfo +from .cobbler import ConfigCobbler from .ldap import LdapConnectionInfo, LdapConnectionDict -__version__ = '3.0.2' +__version__ = '3.1.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -45,7 +45,7 @@ ngettext = XLATOR.ngettext # ============================================================================= -class CrTplConfiguration(BaseMultiConfig): +class CrTplConfiguration(BaseMultiConfig, ConfigCobbler): """ A class for providing a configuration for the CrTplApplication class and methods to read it from configuration files. @@ -460,50 +460,6 @@ class CrTplConfiguration(BaseMultiConfig): return False - # ------------------------------------------------------------------------- - def verify_cobbler_distros(self): - - LOG.debug(_("Verifying cobbler distros ...")) - - if not len(self.cobbler_distros): - msg = _("Did not found configured Cobbler distros.") - raise CrTplConfigError(msg) - - for distro_id in self.cobbler_distros.keys(): - distro = self.cobbler_distros[distro_id] - - if not distro.distro: - msg = _("Did not found distro of configured Cobbler distro {!r}.").format( - distro_id) - raise CrTplConfigError(msg) - - if not distro.ks_repo_url: - msg = _( - "Did not found the base install repo URL of configured Cobbler " - "distro {!r}.").format(distro_id) - raise CrTplConfigError(msg) - - if not len(distro.repos): - msg = _( - "Did not found repo definitions for configured Cobbler " - "distro {!r}.").format(distro_id) - LOG.warn(msg) - - if not distro.description: - distro.description = distro_id - - LOG.debug(_("Searching for distro with ID {!r} ...").format(self.os_id)) - - if self.os_id not in self.cobbler_distros: - msg = _("Did not found distro {!r} in configured Cobbler distros.").format(self.os_id) - raise CrTplConfigError(msg) - - self.current_distro = self.cobbler_distros[self.os_id] - self.cobbler_distro = self.current_distro.distro - - LOG.info(_("Using OS {os!r} with cobbler distro {di!r}.").format( - os=self.os_id, di=self.cobbler_distro)) - # ------------------------------------------------------------------------- def eval_section(self, section_name): @@ -804,113 +760,6 @@ class CrTplConfiguration(BaseMultiConfig): default_val=self.default_max_wait_for_finish_install) continue - # ------------------------------------------------------------------------- - def _eval_config_cobbler(self, section_name, section): - - if self.verbose > 1: - LOG.debug(_("Checking config section {!r} ...").format(section_name)) - - 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_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE) - re_split_values = re.compile(r'[,;\s]+') - re_pr_repos_key = re.compile(r'^\s*profile[-_]?repos?\s*$', re.IGNORECASE) - re_nameserver_key = re.compile(r'^\s*nameservers?\s*$', re.IGNORECASE) - re_dns_search_key = re.compile(r'^\s*dns[-_]?search\s*$', re.IGNORECASE) - re_ws_base_url_key = re.compile( - r'^\s*(?:ws|webserver)[-_]?base[-_]?url\s*$', re.IGNORECASE) - re_ws_docroot_key = re.compile( - r'^\s*(?:ws|webserver)[-_]?docroot\s*$', re.IGNORECASE) - re_ws_rel_filesdir_key = re.compile( - r'^\s*(?:ws|webserver)[-_]?rel(?:ative)?[-_]?filesdir\s*$', re.IGNORECASE) - re_system_status = re.compile(r'^\s*system[-_]?status\s*$', re.IGNORECASE) - re_cobbler_bin_key = re.compile(r'^\s*(?:cobbler[_-]?)?bin\s*$', re.IGNORECASE) - - for key in section.keys(): - value = section[key] - - 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 - if re_port_key.match(key) and value.strip() != '': - self.cobbler_ssh_port = int(value) - continue - if re_user_key.match(key): - self.cobbler_ssh_user = value.strip().lower() - continue - if re_timeout_key.match(key): - self.cobbler_ssh_timeout = int(value) - continue - if re_cobbler_bin_key.match(key) and value.strip() != '': - self.cobbler_bin = value.strip() - continue - if re_rootdir_key.match(key): - dpath = Path(value) - if dpath.is_absolute(): - self.cobbler_rootdir = Path(value) - else: - msg = _("Path for {what} {path!r} is not absolute.").format( - what=_("Cobbler root directory"), path=str(dpath)) - LOG.error(msg) - continue - if key.lower() == 'profile' and value.strip() != '': - self.cobbler_profile = value.strip().lower() - self.cobbler_profile_given = True - continue - if re_pr_repos_key.match(key) and value.strip() != '': - self.cobbler_profile_repos = re_split_values.split(value.strip()) - continue - if re_nameserver_key.match(key) and value.strip() != '': - self.cobbler_nameservers = re_split_values.split(value.strip().lower()) - continue - if re_dns_search_key.match(key) and value.strip() != '': - self.cobbler_dns_search = re_split_values.split(value.strip().lower()) - continue - if re_ws_base_url_key.match(key) and value.strip() != '': - self.cobbler_ws_base_url = value.strip().lower() - continue - if re_ws_docroot_key.match(key) and value.strip() != '': - dpath = Path(value.strip()) - if dpath.is_absolute(): - self.cobbler_ws_docroot = dpath - else: - msg = _("Path for {what} {path!r} is not absolute.").format( - what=_("Webserver document root"), path=str(dpath)) - LOG.error(msg) - continue - if re_ws_rel_filesdir_key.match(key) and value.strip() != '': - self.cobbler_ws_rel_filesdir = Path(value.strip()) - continue - if re_system_status.match(key) and value.strip() != '': - val = value.strip().lower() - if val in self.valid_system_status: - self.system_status = val - else: - msg = _( - "The value of {what!r} must be one of {valid!r}, " - "but found {val!r}.").format( - what='system_status', - valid=self.valid_system_status, val=value) - LOG.error(msg) - continue - - # ------------------------------------------------------------------------- - def _eval_cobbler_distros(self, section_name, section): - - if self.verbose > 1: - LOG.debug(_("Checking config section {!r} ...").format(section_name)) - - for distro_id in section.keys(): - distro_info = section[distro_id] - distro_id = distro_id.lower() - distro = CobblerDistroInfo.init_from_config( - distro_id, distro_info, verbose=self.verbose) - self.cobbler_distros[distro_id] = distro - # ------------------------------------------------------------------------- def get_root_pwd_hash(self, method='sha256'): """Generates a password hash based on the root password.""" @@ -933,30 +782,6 @@ class CrTplConfiguration(BaseMultiConfig): return pwd_hash - # ------------------------------------------------------------------------- - def _eval_cobbler_repos(self, section_name, section): - - if self.verbose > 1: - LOG.debug(_("Checking config section {!r} ...").format(section_name)) - - for full_repo_name in section.keys(): - repo_data = section[full_repo_name] - if isinstance(repo_data, dict): - repo_info = {} - if self.verbose > 2: - LOG.debug(_("Found Cobbler repository {!r}.").format(full_repo_name)) - for key in repo_data.keys(): - value = repo_data[key] - if key.lower() == 'reponame' and value.strip() != '': - repo_info['reponame'] = value.strip() - continue - if key.lower() == 'filename' and value.strip() != '': - repo_info['filename'] = value.strip() - continue - self.cobbler_repos[full_repo_name] = repo_info - if self.verbose > 3: - LOG.debug(_("Evaluated Cobbler repositories:") + '\n' + pp(self.cobbler_repos)) - # ------------------------------------------------------------------------- def strip_unnessecary(self): """Stripping no more necessary stuff.""" diff --git a/lib/cr_vmware_tpl/config/cobbler.py b/lib/cr_vmware_tpl/config/cobbler.py new file mode 100644 index 0000000..c524272 --- /dev/null +++ b/lib/cr_vmware_tpl/config/cobbler.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2023 by Frank Brehm, Berlin +@summary: A mixin module for the CrTplConfiguration class for cobbler configuration stuff. +""" +from __future__ import absolute_import, print_function + +# Standard modules +import logging +import re + +from pathlib import Path + +# Third party modules +from fb_tools.common import pp + +# Own modules +from ..errors import CrTplConfigError + +from .distro import CobblerDistroInfo + +from ..xlate import XLATOR + +__version__ = '0.1.0' + +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + +# ============================================================================= +class ConfigCobbler(): + """ + A mixin class for extending the CrTplConfiguration class for cobbler dependent methods. + """ + + # ------------------------------------------------------------------------- + def verify_cobbler_distros(self): + + LOG.debug(_("Verifying cobbler distros ...")) + + if not len(self.cobbler_distros): + msg = _("Did not found configured Cobbler distros.") + raise CrTplConfigError(msg) + + for distro_id in self.cobbler_distros.keys(): + distro = self.cobbler_distros[distro_id] + + if not distro.distro: + msg = _("Did not found distro of configured Cobbler distro {!r}.").format( + distro_id) + raise CrTplConfigError(msg) + + if not distro.ks_repo_url: + msg = _( + "Did not found the base install repo URL of configured Cobbler " + "distro {!r}.").format(distro_id) + raise CrTplConfigError(msg) + + if not len(distro.repos): + msg = _( + "Did not found repo definitions for configured Cobbler " + "distro {!r}.").format(distro_id) + LOG.warn(msg) + + if not distro.description: + distro.description = distro_id + + LOG.debug(_("Searching for distro with ID {!r} ...").format(self.os_id)) + + if self.os_id not in self.cobbler_distros: + msg = _("Did not found distro {!r} in configured Cobbler distros.").format(self.os_id) + raise CrTplConfigError(msg) + + self.current_distro = self.cobbler_distros[self.os_id] + self.cobbler_distro = self.current_distro.distro + + LOG.info(_("Using OS {os!r} with cobbler distro {di!r}.").format( + os=self.os_id, di=self.cobbler_distro)) + + # ------------------------------------------------------------------------- + def _eval_config_cobbler(self, section_name, section): + + if self.verbose > 1: + LOG.debug(_("Checking config section {!r} ...").format(section_name)) + + 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_rootdir_key = re.compile(r'^\s*root[-_]?dir(?:ectory)?\s*$', re.IGNORECASE) + re_split_values = re.compile(r'[,;\s]+') + re_pr_repos_key = re.compile(r'^\s*profile[-_]?repos?\s*$', re.IGNORECASE) + re_nameserver_key = re.compile(r'^\s*nameservers?\s*$', re.IGNORECASE) + re_dns_search_key = re.compile(r'^\s*dns[-_]?search\s*$', re.IGNORECASE) + re_ws_base_url_key = re.compile( + r'^\s*(?:ws|webserver)[-_]?base[-_]?url\s*$', re.IGNORECASE) + re_ws_docroot_key = re.compile( + r'^\s*(?:ws|webserver)[-_]?docroot\s*$', re.IGNORECASE) + re_ws_rel_filesdir_key = re.compile( + r'^\s*(?:ws|webserver)[-_]?rel(?:ative)?[-_]?filesdir\s*$', re.IGNORECASE) + re_system_status = re.compile(r'^\s*system[-_]?status\s*$', re.IGNORECASE) + re_cobbler_bin_key = re.compile(r'^\s*(?:cobbler[_-]?)?bin\s*$', re.IGNORECASE) + + for key in section.keys(): + value = section[key] + + 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 + if re_port_key.match(key) and value.strip() != '': + self.cobbler_ssh_port = int(value) + continue + if re_user_key.match(key): + self.cobbler_ssh_user = value.strip().lower() + continue + if re_timeout_key.match(key): + self.cobbler_ssh_timeout = int(value) + continue + if re_cobbler_bin_key.match(key) and value.strip() != '': + self.cobbler_bin = value.strip() + continue + if re_rootdir_key.match(key): + dpath = Path(value) + if dpath.is_absolute(): + self.cobbler_rootdir = Path(value) + else: + msg = _("Path for {what} {path!r} is not absolute.").format( + what=_("Cobbler root directory"), path=str(dpath)) + LOG.error(msg) + continue + if key.lower() == 'profile' and value.strip() != '': + self.cobbler_profile = value.strip().lower() + self.cobbler_profile_given = True + continue + if re_pr_repos_key.match(key) and value.strip() != '': + self.cobbler_profile_repos = re_split_values.split(value.strip()) + continue + if re_nameserver_key.match(key) and value.strip() != '': + self.cobbler_nameservers = re_split_values.split(value.strip().lower()) + continue + if re_dns_search_key.match(key) and value.strip() != '': + self.cobbler_dns_search = re_split_values.split(value.strip().lower()) + continue + if re_ws_base_url_key.match(key) and value.strip() != '': + self.cobbler_ws_base_url = value.strip().lower() + continue + if re_ws_docroot_key.match(key) and value.strip() != '': + dpath = Path(value.strip()) + if dpath.is_absolute(): + self.cobbler_ws_docroot = dpath + else: + msg = _("Path for {what} {path!r} is not absolute.").format( + what=_("Webserver document root"), path=str(dpath)) + LOG.error(msg) + continue + if re_ws_rel_filesdir_key.match(key) and value.strip() != '': + self.cobbler_ws_rel_filesdir = Path(value.strip()) + continue + if re_system_status.match(key) and value.strip() != '': + val = value.strip().lower() + if val in self.valid_system_status: + self.system_status = val + else: + msg = _( + "The value of {what!r} must be one of {valid!r}, " + "but found {val!r}.").format( + what='system_status', + valid=self.valid_system_status, val=value) + LOG.error(msg) + continue + + # ------------------------------------------------------------------------- + def _eval_cobbler_repos(self, section_name, section): + + if self.verbose > 1: + LOG.debug(_("Checking config section {!r} ...").format(section_name)) + + for full_repo_name in section.keys(): + repo_data = section[full_repo_name] + if isinstance(repo_data, dict): + repo_info = {} + if self.verbose > 2: + LOG.debug(_("Found Cobbler repository {!r}.").format(full_repo_name)) + for key in repo_data.keys(): + value = repo_data[key] + if key.lower() == 'reponame' and value.strip() != '': + repo_info['reponame'] = value.strip() + continue + if key.lower() == 'filename' and value.strip() != '': + repo_info['filename'] = value.strip() + continue + self.cobbler_repos[full_repo_name] = repo_info + if self.verbose > 3: + LOG.debug(_("Evaluated Cobbler repositories:") + '\n' + pp(self.cobbler_repos)) + + # ------------------------------------------------------------------------- + def _eval_cobbler_distros(self, section_name, section): + + if self.verbose > 1: + LOG.debug(_("Checking config section {!r} ...").format(section_name)) + + for distro_id in section.keys(): + distro_info = section[distro_id] + distro_id = distro_id.lower() + distro = CobblerDistroInfo.init_from_config( + distro_id, distro_info, verbose=self.verbose) + self.cobbler_distros[distro_id] = distro + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5