From: Frank Brehm Date: Fri, 22 Sep 2023 09:50:11 +0000 (+0200) Subject: Making the flake8 happy X-Git-Tag: 3.0.0^2~23 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=434ee5e69c92d965889200198b3dc418e6af2baa;p=pixelpark%2Fcreate-vmware-tpl.git Making the flake8 happy --- diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 50117c3..896d42f 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -27,12 +27,10 @@ from fb_tools.common import pp from fb_tools.cfg_app import FbConfigApplication -from fb_tools.errors import FbAppError, ExpectedHandlerError +from fb_tools.errors import ExpectedHandlerError from .config import CrTplConfiguration -from .errors import CrTplAppError - from .handler import CrTplHandler from .xlate import __module_dir__ as __xlate_module_dir__ @@ -40,7 +38,7 @@ from .xlate import __base_dir__ as __xlate_base_dir__ from .xlate import __mo_file__ as __xlate_mo_file__ from .xlate import XLATOR, LOCALE_DIR, DOMAIN -__version__ = '1.5.3' +__version__ = '1.5.4' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/cr_vmware_tpl/cobbler/__init__.py b/lib/cr_vmware_tpl/cobbler/__init__.py index 39fee8e..274274b 100644 --- a/lib/cr_vmware_tpl/cobbler/__init__.py +++ b/lib/cr_vmware_tpl/cobbler/__init__.py @@ -13,11 +13,7 @@ import logging import re import datetime import pipes -import hashlib -import textwrap import ipaddress -import tempfile -import os from pathlib import Path @@ -27,13 +23,9 @@ from paramiko.ssh_exception import SSHException from packaging.version import Version -import jinja2 - -from six.moves import configparser - # Own modules -from fb_tools.common import pp, to_str, is_sequence, to_bool +from fb_tools.common import pp, to_str, is_sequence from fb_tools.handling_obj import CompletedProcess from fb_tools.handler import BaseHandler from fb_tools.xlate import format_list @@ -43,15 +35,13 @@ from .files import CobblerFiles from .profile import CobblerProfile from .system import CobblerSystem -from .. import print_section_start, print_section_end - from ..config import CrTplConfiguration from ..errors import CobblerError, ExpectedCobblerError from ..xlate import XLATOR -__version__ = '0.11.3' +__version__ = '0.11.4' LOG = logging.getLogger(__name__) diff --git a/lib/cr_vmware_tpl/cobbler/distro.py b/lib/cr_vmware_tpl/cobbler/distro.py index 811f909..1533940 100644 --- a/lib/cr_vmware_tpl/cobbler/distro.py +++ b/lib/cr_vmware_tpl/cobbler/distro.py @@ -12,14 +12,13 @@ from __future__ import absolute_import, print_function import logging # Third party modules -from fb_tools.common import pp, to_str, is_sequence, to_bool -from fb_tools.xlate import format_list +from fb_tools.common import pp # Own modules from ..xlate import XLATOR -__version__ = '0.1.1' +__version__ = '0.1.2' LOG = logging.getLogger(__name__) diff --git a/lib/cr_vmware_tpl/cobbler/files.py b/lib/cr_vmware_tpl/cobbler/files.py index a3b67a2..10e403a 100644 --- a/lib/cr_vmware_tpl/cobbler/files.py +++ b/lib/cr_vmware_tpl/cobbler/files.py @@ -17,9 +17,6 @@ import textwrap import paramiko from paramiko.ssh_exception import SSHException -from fb_tools.common import pp, to_str, is_sequence, to_bool -from fb_tools.xlate import format_list - # Own modules from .. import print_section_start, print_section_end @@ -28,7 +25,7 @@ from ..errors import ExpectedCobblerError from ..xlate import XLATOR -__version__ = '0.1.0' +__version__ = '0.1.1' LOG = logging.getLogger(__name__) diff --git a/lib/cr_vmware_tpl/cobbler/profile.py b/lib/cr_vmware_tpl/cobbler/profile.py index 7a85f0d..b8e649b 100644 --- a/lib/cr_vmware_tpl/cobbler/profile.py +++ b/lib/cr_vmware_tpl/cobbler/profile.py @@ -20,15 +20,14 @@ import jinja2 from six.moves import configparser -from fb_tools.common import pp, to_str, is_sequence, to_bool -from fb_tools.xlate import format_list +from fb_tools.common import pp, to_bool # Own modules from .. import print_section_start, print_section_end -from ..errors import CobblerError, ExpectedCobblerError +from ..errors import ExpectedCobblerError from ..xlate import XLATOR -__version__ = '0.1.0' +__version__ = '0.1.1' LOG = logging.getLogger(__name__) @@ -41,6 +40,8 @@ class CobblerProfile(): A mixin class for extending the Cobbler class for profile dependend methods. """ + profile_comment_tpl = "Profile for creating a {} VM." + # ------------------------------------------------------------------------- def get_profile_list(self): """Trying to get a list of all configured cobbler profiles.""" @@ -165,19 +166,43 @@ class CobblerProfile(): def _change_profile(self, profile_vars): profile = self.cfg.cobbler_profile - distro = self.cfg.cobbler_distro distro_info = self.cfg.current_distro - status = self.cfg.system_status LOG.debug(_("Checking existing profile {n!r} ({d}) ...").format( n=profile, d=distro_info.description)) - repos = [] - if distro_info.repos: - repos = distro_info.repos.as_list() - repos_str = ' '.join(repos) + args = self._get_profile_change_attribs(profile_vars) + if not args: + LOG.debug(_("No need for changing profile {!r}").format(profile)) + return + + args = ['profile', 'edit', '--name', profile] + args + + if self.verbose > 1: + LOG.debug('Arguments for changing profile:\n' + pp(args)) + return + + proc = self.exec_cobbler(args) + + if proc.returncode: + err = _('No error message') + if proc.stderr: + err = proc.stderr + elif proc.stdout: + err = proc.stdout + msg = _("Error editing a cobbler profile - returncode was {rc}: {err}").format( + rc=proc.returncode, err=err) + raise ExpectedCobblerError(msg) + + # ------------------------------------------------------------------------- + def _get_profile_change_attribs(self, profile_vars): + + distro = self.cfg.cobbler_distro + distro_info = self.cfg.current_distro + repos_str = distro_info.repos_string + comment = self.profile_comment_tpl.format(distro_info.description) + status = self.cfg.system_status - comment = "Profile for creating a {} VM.".format(distro_info.description) name_servers = '[' + ', '.join( map(lambda x: "'" + x + "'", self.cfg.cobbler_nameservers)) + ']' dns_search = '[' + ', '.join( @@ -197,14 +222,9 @@ class CobblerProfile(): args.append('--enable-menu') args.append('1') - if self.cfg.cobbler_major_version == 3: - if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name): - args.append('--autoinstall') - args.append(str(self.cfg.cobbler_profile_ks.name)) - else: - if profile_vars['kickstart'] != str(self.cfg.cobbler_profile_ks): - args.append('--kickstart') - args.append(str(self.cfg.cobbler_profile_ks)) + if profile_vars['autoinstall'] != str(self.cfg.cobbler_profile_ks.name): + args.append('--autoinstall') + args.append(str(self.cfg.cobbler_profile_ks.name)) if self.verbose > 1: msg = _("Checking for repos:") + ' ' + repos_str @@ -231,31 +251,7 @@ class CobblerProfile(): args.append('--name-servers-search') args.append(' '.join(self.cfg.cobbler_dns_search)) - ks_meta_ok = True - ks_meta_vars = {} - if 'autoinstall_meta' in profile_vars: - ks_meta_vars = self.xform_ks_meta(profile_vars['autoinstall_meta']) - if 'ROOT_PWD_HASH' not in ks_meta_vars: - LOG.debug(_('Profile ks_meta {!r} is not ok.').format('ROOT_PWD_HASH')) - ks_meta_ok = False - if ('SWAP_SIZE_MB' not in ks_meta_vars or - ks_meta_vars['SWAP_SIZE_MB'] != str(self.cfg.swap_size_mb)): - LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SWAP_SIZE_MB')) - ks_meta_ok = False - if ('SYSTEM_STATUS' not in ks_meta_vars or - ks_meta_vars['SYSTEM_STATUS'] != status): - LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SYSTEM_STATUS')) - ks_meta_ok = False - if ('WS_REL_FILESDIR' not in ks_meta_vars or - ks_meta_vars['WS_REL_FILESDIR'] != str(self.cfg.cobbler_ws_rel_filesdir)): - LOG.debug(_('Profile ks_meta {!r} is not ok.').format('WS_REL_FILESDIR')) - ks_meta_ok = False - if ('COBBLER_URL' not in ks_meta_vars or - ks_meta_vars['COBBLER_URL'] != "http://{}".format(self.cfg.cobbler_host)): - LOG.debug(_('Profile ks_meta {!r} is not ok.').format('COBBLER_URL')) - ks_meta_ok = False - - if not ks_meta_ok: + if not self._check_profile_ks_metavars(profile_vars): ks_meta_list = [] ks_meta_list.append("ROOT_PWD_HASH={}".format(self.cfg.get_root_pwd_hash())) ks_meta_list.append("SWAP_SIZE_MB={}".format(self.cfg.swap_size_mb)) @@ -269,29 +265,45 @@ class CobblerProfile(): args.append(ks_meta) if self.verbose: - LOG.debug("Args for 'profile edit:\n{}".format(pp(args))) + LOG.debug(_("Arguments for editing the profile:") + '\n' + pp(args)) - if not args: - LOG.debug(_("No need for changing profile {!r}").format(profile)) - return + return args - args = ['profile', 'edit', '--name', profile] + args + # ------------------------------------------------------------------------- + def _check_profile_ks_metavars(self, profile_vars): - if self.verbose > 1: - LOG.debug('Arguments for changing profile:\n' + pp(args)) - return + status = self.cfg.system_status - proc = self.exec_cobbler(args) + ks_meta_ok = True + ks_meta_vars = {} - if proc.returncode: - err = _('No error message') - if proc.stderr: - err = proc.stderr - elif proc.stdout: - err = proc.stdout - msg = _("Error editing a cobbler profile - returncode was {rc}: {err}").format( - rc=proc.returncode, err=err) - raise ExpectedCobblerError(msg) + if 'autoinstall_meta' in profile_vars: + ks_meta_vars = self.xform_ks_meta(profile_vars['autoinstall_meta']) + + if 'ROOT_PWD_HASH' not in ks_meta_vars: + LOG.debug(_('Profile ks_meta {!r} is not ok.').format('ROOT_PWD_HASH')) + ks_meta_ok = False + + if 'SWAP_SIZE_MB' not in ks_meta_vars or \ + ks_meta_vars['SWAP_SIZE_MB'] != str(self.cfg.swap_size_mb): + LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SWAP_SIZE_MB')) + ks_meta_ok = False + + if 'SYSTEM_STATUS' not in ks_meta_vars or \ + ks_meta_vars['SYSTEM_STATUS'] != status: + LOG.debug(_('Profile ks_meta {!r} is not ok.').format('SYSTEM_STATUS')) + ks_meta_ok = False + + if 'WS_REL_FILESDIR' not in ks_meta_vars or \ + ks_meta_vars['WS_REL_FILESDIR'] != str(self.cfg.cobbler_ws_rel_filesdir): + LOG.debug(_('Profile ks_meta {!r} is not ok.').format('WS_REL_FILESDIR')) + ks_meta_ok = False + if 'COBBLER_URL' not in ks_meta_vars or \ + ks_meta_vars['COBBLER_URL'] != "http://{}".format(self.cfg.cobbler_host): + LOG.debug(_('Profile ks_meta {!r} is not ok.').format('COBBLER_URL')) + ks_meta_ok = False + + return ks_meta_ok # ------------------------------------------------------------------------- def add_profile(self): @@ -302,7 +314,7 @@ class CobblerProfile(): LOG.info(_("Creating new profile {!r} ...").format(profile)) distro_info = self.cfg.current_distro - comment = "Profile for creating a {} VM.".format(distro_info.description) + comment = self.profile_comment_tpl.format(distro_info.description) status = self.cfg.system_status LOG.debug("Using kickstart file {!r}".format(self.cfg.cobbler_profile_ks)) diff --git a/lib/cr_vmware_tpl/cobbler/system.py b/lib/cr_vmware_tpl/cobbler/system.py index 407950c..4522e71 100644 --- a/lib/cr_vmware_tpl/cobbler/system.py +++ b/lib/cr_vmware_tpl/cobbler/system.py @@ -10,25 +10,16 @@ from __future__ import absolute_import, print_function # Standard modules import logging -import os -import tempfile - -from pathlib import Path # Third party modules import jinja2 -from six.moves import configparser - -from fb_tools.common import pp, to_str, is_sequence, to_bool -from fb_tools.xlate import format_list - # Own modules from .. import print_section_start, print_section_end -from ..errors import CobblerError, ExpectedCobblerError +from ..errors import ExpectedCobblerError from ..xlate import XLATOR -__version__ = '0.1.0' +__version__ = '0.1.1' LOG = logging.getLogger(__name__) diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index e799a57..53d27d3 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -35,7 +35,7 @@ from .errors import CrTplConfigError from .xlate import XLATOR -__version__ = '2.3.1' +__version__ = '2.3.3' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -332,7 +332,7 @@ class LdapConnectionInfo(FbBaseObject): new.admin_filter = value continue - if key.lower() in ['is_admin', 'readonly', 'tier', 'sync-source' ]: + if key.lower() in ['is_admin', 'readonly', 'tier', 'sync-source']: continue msg = _("Unknown LDAP configuration key {key} found in section {sec!r}.").format( @@ -543,6 +543,14 @@ class CobblerDistroInfo(FbGenericBaseObject): return self._ks_template = template + # ------------------------------------------------------------------------- + @property + def repos_string(self): + """Returns all repos as a string of their space concatinated names.""" + if self.repos: + return ' '.join(self.repos.as_list()) + return '' + # ------------------------------------------------------------------------- def __repr__(self): """Typecasting into a string for reproduction.""" @@ -583,6 +591,7 @@ class CobblerDistroInfo(FbGenericBaseObject): res['ks_repo_url'] = self.ks_repo_url res['ks_template'] = self.ks_template res['name'] = self.name + res['repos_string'] = self.repos_string res['shortname'] = self.shortname return res @@ -1661,7 +1670,7 @@ class CrTplConfiguration(BaseMultiConfig): def strip_unnessecary(self): """Stripping no more necessary stuff.""" LOG.debug(_("Stripping no more necessary stuff from configuration ...")) - + if self.verbose > 1: LOG.debug(_("Stripping {!r} ...").format('cfg.cobbler-distros')) if 'cobbler-distros' in self.cfg: diff --git a/lib/cr_vmware_tpl/errors.py b/lib/cr_vmware_tpl/errors.py index 4b89fee..d38ae96 100644 --- a/lib/cr_vmware_tpl/errors.py +++ b/lib/cr_vmware_tpl/errors.py @@ -14,7 +14,6 @@ from __future__ import absolute_import # Third party modules from fb_tools.errors import FbAppError -from fb_tools.errors import FbHandlerError from fb_tools.errors import HandlerError, ExpectedHandlerError from fb_tools.multi_config import MultiConfigError