From 963765a5b3a79ed2635f20178580ff66eb9e2b30 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 9 Jun 2020 17:39:15 +0200 Subject: [PATCH] Start with creating Cobbler system --- lib/cr_vmware_tpl/cobbler.py | 43 ++++++++++++++++++++++++++++++++++- lib/cr_vmware_tpl/config.py | 44 +++++++++++++++++++++++++++++++++++- lib/cr_vmware_tpl/handler.py | 5 +++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index d244128..c9b0a7a 100644 --- a/lib/cr_vmware_tpl/cobbler.py +++ b/lib/cr_vmware_tpl/cobbler.py @@ -37,7 +37,7 @@ from .config import CrTplConfiguration from .xlate import XLATOR -__version__ = '0.4.2' +__version__ = '0.4.3' LOG = logging.getLogger(__name__) @@ -61,6 +61,8 @@ class Cobbler(BaseHandler): A handler class for executing cobbler actions. """ + valid_status = ('development', 'testing', 'acceptance', 'production') + # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, @@ -598,6 +600,45 @@ class Cobbler(BaseHandler): rc=proc.returncode, err=err) raise ExpectedCobblerError(msg) + # ------------------------------------------------------------------------- + def add_system(self, name, fqdn, mac_address, comment=None, status=None): + """Creating a new system.""" + + profile = self.config.cobbler_profile + os_id = self.config.os_id + + LOG.info(_("Creating new sytem {!r} ...").format(name)) + + if not comment: + comment = "VMWare template for creating a {} system.".format(os_id) + if not status: + status = 'development' + + ks_meta_list = [] + ks_meta_list.append("ROOT_PWD_HASH={}".format(self.config.get_root_pwd_hash())) + + ks_meta = None + if ks_meta_list: + ks_meta = ' '.join(ks_meta_list) + + args = ['system', 'add'] + args.append('--clobber') + args.append('--name') + args.append(name) + args.append('--profile') + args.append(profile) + args.append('--status') + args.append(status) + args.append('--kopts') + args.append('inst.sshd') + args.append('--comment') + args.append(comment) + if ks_meta: + args.append('--ksmeta') + args.append(ks_meta) + + if self.verbose > 1: + LOG.debug(_("Cobbler arguments for creating a new system:") + "\n" + pp(args)) # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index 9e96333..a7431ec 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -12,6 +12,7 @@ from __future__ import absolute_import import logging import re import copy +import crypt # Third party modules from pathlib import Path @@ -21,7 +22,7 @@ from fb_tools.config import ConfigError, BaseConfiguration from .xlate import XLATOR -__version__ = '1.5.2' +__version__ = '1.5.3' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -69,6 +70,7 @@ class CrTplConfiguration(BaseConfiguration): max_max_wait_for_finish_general = 60 * 60 max_max_wait_for_finish_install = 24 * 60 * 60 limit_max_nr_templates_stay = 100 + default_root_password = 'testtest' default_tpl_vm_domain = 'pixelpark.com' @@ -100,6 +102,11 @@ class CrTplConfiguration(BaseConfiguration): mac_address_template = "00:16:3e:53:{:02x}:{:02x}" + method_list = {} + for method in crypt.methods: + mname = method.name.lower() + method_list[mname] = method + # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, @@ -136,6 +143,8 @@ class CrTplConfiguration(BaseConfiguration): self._cobbler_profile_given = False self._template_name_given = False + self._root_password = self.default_root_password + self.private_ssh_key = None self.cobbler_distro = self.default_cobbler_distro @@ -190,6 +199,12 @@ class CrTplConfiguration(BaseConfiguration): """Size of RAM in GiB.""" return float(self.ram_mb) / 1024 + # ------------------------------------------------------------------------- + @property + def root_password(self): + """The root password of the VM to create.""" + return self._root_password + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -208,6 +223,13 @@ class CrTplConfiguration(BaseConfiguration): res['data_size'] = self.data_size res['ram_gb'] = self.ram_gb + res['root_password'] = None + if self.root_password: + if self.verbose > 4: + res['root_password'] = self.root_password + else: + res['root_password'] = '********' + res['password'] = None if self.password: if self.verbose > 4: @@ -326,6 +348,7 @@ class CrTplConfiguration(BaseConfiguration): re_os_id = re.compile(r'^\s*os[-_]?id\s*$', re.IGNORECASE) re_os_id_subst = re.compile(r'[^a-z0-9_-]+', re.IGNORECASE) re_vm_domain = re.compile(r'^\s*(?:vm[-_]?)?domain\s*$', re.IGNORECASE) + re_root_pwd = re.compile(r'^\s*root[-_]?password\s*$', re.IGNORECASE) for (key, value) in config.items(section_name): if key.lower() == 'vm': @@ -379,6 +402,9 @@ class CrTplConfiguration(BaseConfiguration): elif key.lower() == 'os_version': self.os_version = value.strip() continue + elif re_root_pwd.match(key) and value.strip(): + self._root_password = value.strip() + continue return @@ -540,6 +566,22 @@ class CrTplConfiguration(BaseConfiguration): if re_ws_rel_filesdir_key.match(key) and value.strip() != '': self.cobbler_ws_rel_filesdir = Path(value.strip()) + # ------------------------------------------------------------------------- + def get_root_pwd_hash(self, method='sha256'): + """Generates a password hash based on the root password.""" + + m = method.lower() + if m not in self.method_list: + msg = _("Given method {!r} is not a valid crypt method.").format(method) + raise ValueError(msg) + + salt = crypt.mksalt(self.method_list[m]) + pwd_hash = crypt.crypt(self.root_password, salt) + if self.verbose > 2: + LOG.debug(_("Hashed root password: {!r}").format(pwd_hash)) + + return pwd_hash + # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 3649017..f9f1686 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -42,7 +42,7 @@ from .cobbler import CobblerError, Cobbler from .xlate import XLATOR -__version__ = '1.5.5' +__version__ = '1.5.6' LOG = logging.getLogger(__name__) TZ = pytz.timezone('Europe/Berlin') @@ -276,6 +276,9 @@ class CrTplHandler(BaseHandler): LOG.info(_("Using MAC address of template VM: {!r}").format(self.tpl_macaddress)) + self.cobbler.add_system( + name=self.tpl_vm_hostname, fqdn=self.tpl_vm_fqdn, mac_address=self.tpl_macaddress) + # self.vsphere.poweron_vm(self.tpl_vm, max_wait=self.config.max_wait_for_poweron_vm) # self.ts_start_install = time.time() # self.wait_for_finish_install() -- 2.39.5