From: Frank Brehm Date: Wed, 27 May 2020 11:59:14 +0000 (+0200) Subject: Adding lib/cr_vmware_tpl/cobbler.py X-Git-Tag: 2.1.2^2~9^2~31^2~114 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=24f5b941c01efbb873c260d84ff474a8457c1794;p=pixelpark%2Fcreate-vmware-tpl.git Adding lib/cr_vmware_tpl/cobbler.py --- diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py new file mode 100644 index 0000000..b7fe275 --- /dev/null +++ b/lib/cr_vmware_tpl/cobbler.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2020 by Frank Brehm, Berlin +@summary: A handler module for executing cobbler actions +""" +from __future__ import absolute_import, print_function + +# Standard module +import logging +import re + +# Third party modules +import paramiko + +# Own modules + +from fb_tools.common import pp, to_str + +from fb_tools.errors import HandlerError, ExpectedHandlerError + +from fb_tools.handler import BaseHandler + +from .config import CrTplConfiguration + +from .xlate import XLATOR + +__version__ = '0.0.1' + +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + + +# ============================================================================= +class CobblerError(HandlerError): + """Exception class for unexpected exceptions.""" + pass + +# ============================================================================= +class Cobbler(BaseHandler): + """ + A handler class for executing cobbler actions. + """ + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False): + + self.host = CrTplConfiguration.default_cobbler_host + self.ssh_port = CrTplConfiguration.default_cobbler_ssh_port + self.ssh_user = CrTplConfiguration.default_cobbler_ssh_user + self.private_ssh_key = None + self.ssh = None + self.ssh_timeout = CrTplConfiguration.default_cobbler_ssh_timeout + + super(Cobbler, self).__init__( + appname=appname, verbose=verbose, version=version, base_dir=base_dir, + terminal_has_colors=terminal_has_colors, simulate=simulate, + force=force, initialized=False, + ) + + self.private_ssh_key = str(self.base_dir.joinpath('keys', CrTplConfiguration.ssh_privkey)) + + if config: + self.private_ssh_key = config.private_ssh_key + self.host = config.cobbler_host + self.ssh_port = config.cobbler_ssh_port + self.ssh_user = config.cobbler_ssh_user + self.ssh_timeout = config.cobbler_ssh_timeout + + if initialized: + self.initialized = True + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index fbd1792..c1e60a0 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.3.1' +__version__ = '1.4.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -66,6 +66,13 @@ class CrTplConfiguration(BaseConfiguration): max_max_wait_for_finish_install = 24 * 60 * 60 limit_max_nr_templates_stay = 100 + default_cobbler_host = 'cobbler.pixelpark.com' + default_cobbler_ssh_port = 22 + default_cobbler_ssh_user = 'root' + default_cobbler_ssh_timeout = 30 + + ssh_privkey = 'id_rsa_cr_vmw_tpl' + mac_address_template = "00:16:3e:53:{:02x}:{:02x}" # ------------------------------------------------------------------------- @@ -97,6 +104,13 @@ class CrTplConfiguration(BaseConfiguration): self.vmware_cfg_version = self.default_vmware_cfg_version self.os_version = self.default_os_version + self.private_ssh_key = None + + 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 + self.cobbler_ssh_timeout = self.default_cobbler_ssh_timeout + self.excluded_datastores = [] super(CrTplConfiguration, self).__init__( @@ -104,6 +118,8 @@ class CrTplConfiguration(BaseConfiguration): encoding=encoding, config_dir=config_dir, config_file=config_file, initialized=False, ) + self.private_ssh_key = str(self.base_dir.joinpath('keys', self.ssh_privkey)) + if initialized: self.initialized = True @@ -190,6 +206,9 @@ class CrTplConfiguration(BaseConfiguration): if section_name.lower() == 'timeouts': self._eval_config_timeouts(config, section_name) return + if section_name.lower() == 'cobbler': + self._eval_config_cobbler(config, section_name) + return if self.verbose > 1: LOG.debug(_("Unhandled configuration section {!r}.").format(section_name)) @@ -363,6 +382,28 @@ class CrTplConfiguration(BaseConfiguration): max_val=self.max_max_wait_for_finish_install, default_val=self.default_max_wait_for_finish_install) + # ------------------------------------------------------------------------- + def _eval_config_cobbler(self, config, section_name): + + 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) + + for (key, value) in config.items(section_name): + 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) # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index fbaac0b..d02f337 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -3,7 +3,7 @@ """ @author: Frank Brehm @contact: frank.brehm@pixelpark.com -@copyright: © 2018 by Frank Brehm, Berlin +@copyright: © 2020 by Frank Brehm, Berlin @summary: A handler module for creating the VMWare template """ from __future__ import absolute_import, print_function