From bd6658643925d6958e036c4c5441a9a9ff5eb50d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 22 Sep 2023 10:33:57 +0200 Subject: [PATCH] Separating distro dependend methods from cr_vmware_tpl.cobbler to cr_vmware_tpl.cobbler.distro. --- lib/cr_vmware_tpl/cobbler/__init__.py | 57 ++-------------- lib/cr_vmware_tpl/cobbler/distro.py | 93 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 lib/cr_vmware_tpl/cobbler/distro.py diff --git a/lib/cr_vmware_tpl/cobbler/__init__.py b/lib/cr_vmware_tpl/cobbler/__init__.py index 031f5d6..19d9127 100644 --- a/lib/cr_vmware_tpl/cobbler/__init__.py +++ b/lib/cr_vmware_tpl/cobbler/__init__.py @@ -8,7 +8,7 @@ """ from __future__ import absolute_import, print_function -# Standard module +# Standard modules import logging import re import datetime @@ -38,6 +38,8 @@ from fb_tools.handling_obj import CompletedProcess from fb_tools.handler import BaseHandler from fb_tools.xlate import format_list +from .distro import CobblerDistro + from .. import print_section_start, print_section_end from ..config import CrTplConfiguration @@ -55,7 +57,7 @@ ngettext = XLATOR.ngettext # ============================================================================= -class Cobbler(BaseHandler): +class Cobbler(BaseHandler, CobblerDistro): """ A handler class for executing cobbler actions. """ @@ -353,57 +355,6 @@ class Cobbler(BaseHandler): dsc=dsc, rdir=str(rdir), host=self.host, err=err) raise ExpectedCobblerError(msg) - # ------------------------------------------------------------------------- - 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, show_output=False) - for line in proc.stdout.splitlines(): - distro = line.strip() - if distro: - distro_list.append(distro) - distro_list.sort(key=str.lower) - if self.verbose > 1: - LOG.debug(_("Sorted list of found distros:") + "\n{}".format(pp(distro_list))) - return distro_list - - # ------------------------------------------------------------------------- - def get_repo_list(self): - """Trying to get a list of all configured repositories.""" - - repo_list = [] - - proc = self.exec_cobbler(('repo', 'list'), no_simulate=True, show_output=False) - for line in proc.stdout.splitlines(): - repo = line.strip() - if repo: - repo_list.append(repo) - repo_list.sort(key=str.lower) - if self.verbose > 1: - LOG.debug(_("Sorted list of found repositories:") + "\n{}".format(pp(repo_list))) - return repo_list - - # ------------------------------------------------------------------------- - def verify_distro_repos(self, distro): - - repo_list = self.get_repo_list() - - LOG.debug(_("Checking existence of repos for distro {!r}.").format(distro.name)) - - all_ok = True - for repo in distro.repos: - if repo not in repo_list: - msg = _("Repo {r!r} for distro {d!r} not found on cobbler server.").format( - r=repo, d=distro.name) - LOG.warn(msg) - all_ok = False - elif self.verbose > 1: - msg = _("Found repo {r!r} for distro {d!r}.").format(r=repo, d=distro.name) - LOG.debug(msg) - - return all_ok - # ------------------------------------------------------------------------- def get_profile_list(self): """Trying to get a list of all configured cobbler profiles.""" diff --git a/lib/cr_vmware_tpl/cobbler/distro.py b/lib/cr_vmware_tpl/cobbler/distro.py new file mode 100644 index 0000000..c35233f --- /dev/null +++ b/lib/cr_vmware_tpl/cobbler/distro.py @@ -0,0 +1,93 @@ +#!/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 forthe Cobbler class +""" +from __future__ import absolute_import, print_function + +# Standard modules +import logging + +from fb_tools.common import pp, to_str, is_sequence, to_bool +from fb_tools.xlate import format_list + +# Own modules + +from ..xlate import XLATOR + +__version__ = '0.1.1' + +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + +# ============================================================================= +class CobblerDistro(): + """ + A mixin class for extending the Cobbler class for repo dependend methods. + """ + + # ------------------------------------------------------------------------- + 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, show_output=False) + for line in proc.stdout.splitlines(): + distro = line.strip() + if distro: + distro_list.append(distro) + distro_list.sort(key=str.lower) + if self.verbose > 1: + LOG.debug(_("Sorted list of found distros:") + "\n{}".format(pp(distro_list))) + return distro_list + + # ------------------------------------------------------------------------- + def get_repo_list(self): + """Trying to get a list of all configured repositories.""" + + repo_list = [] + + proc = self.exec_cobbler(('repo', 'list'), no_simulate=True, show_output=False) + for line in proc.stdout.splitlines(): + repo = line.strip() + if repo: + repo_list.append(repo) + repo_list.sort(key=str.lower) + if self.verbose > 1: + LOG.debug(_("Sorted list of found repositories:") + "\n{}".format(pp(repo_list))) + return repo_list + + # ------------------------------------------------------------------------- + def verify_distro_repos(self, distro): + + repo_list = self.get_repo_list() + + LOG.debug(_("Checking existence of repos for distro {!r}.").format(distro.name)) + + all_ok = True + for repo in distro.repos: + if repo not in repo_list: + msg = _("Repo {r!r} for distro {d!r} not found on cobbler server.").format( + r=repo, d=distro.name) + LOG.warn(msg) + all_ok = False + elif self.verbose > 1: + msg = _("Found repo {r!r} for distro {d!r}.").format(r=repo, d=distro.name) + LOG.debug(msg) + + return all_ok + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5