From aec449e1f9f39553464de9d5b372f44825fff7c8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 10 Jun 2020 17:39:05 +0200 Subject: [PATCH] Adding configuration of system_status and swap_size --- lib/cr_vmware_tpl/cobbler.py | 20 +++++++++++++------- lib/cr_vmware_tpl/config.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/cr_vmware_tpl/cobbler.py b/lib/cr_vmware_tpl/cobbler.py index fc4b31d..810c3ff 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.4' +__version__ = '0.4.5' LOG = logging.getLogger(__name__) @@ -61,8 +61,6 @@ 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, @@ -601,7 +599,16 @@ class Cobbler(BaseHandler): raise ExpectedCobblerError(msg) # ------------------------------------------------------------------------- - def add_system(self, name, fqdn, mac_address, comment=None, status=None): + def ensure_system_ks(self): + + local_ks_base = 'template-' + self.config.os_id + '.ks' + local_ks = self.base_dir / 'kickstart' / local_ks_base + remote_ks = self.config.system_ks + + self.ensure_remote_file(local_ks, remote_ks) + + # ------------------------------------------------------------------------- + def add_system(self, name, fqdn, mac_address, comment=None): """Creating a new system.""" profile = self.config.cobbler_profile @@ -611,12 +618,11 @@ class Cobbler(BaseHandler): if not comment: comment = "VMWare template for creating a {} system.".format(os_id) - if not status: - status = 'development' + status = self.config.system_status ks_meta_list = [] ks_meta_list.append("ROOT_PWD_HASH={}".format(self.config.get_root_pwd_hash())) - ks_meta_list.append("SWAP_SIZE_MB=512") + ks_meta_list.append("SWAP_SIZE_MB={}".format(self.config.swap_size_mb)) ks_meta = None if ks_meta_list: diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index d47341d..58ed6fc 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -22,7 +22,7 @@ from fb_tools.config import ConfigError, BaseConfiguration from .xlate import XLATOR -__version__ = '1.5.4' +__version__ = '1.5.5' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -107,6 +107,11 @@ class CrTplConfiguration(BaseConfiguration): mname = method.name.lower() method_list[mname] = method + valid_system_status = ('development', 'testing', 'acceptance', 'production') + default_system_status = 'development' + + default_swap_size_mb = 512 + # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, @@ -126,6 +131,7 @@ class CrTplConfiguration(BaseConfiguration): self.data_size_gb = self.default_data_size_gb self.num_cpus = self.default_num_cpus self.ram_mb = self.default_ram_mb + self.swap_size_mb = self.default_swap_size_mb self.network = self.default_network self.mac_address = self.default_mac_address self.max_wait_for_general = self.default_max_wait_for_general @@ -162,6 +168,7 @@ class CrTplConfiguration(BaseConfiguration): self.cobbler_ws_base_url = self.default_cobbler_ws_base_url self.cobbler_ws_docroot = self.default_cobbler_ws_docroot self.cobbler_ws_rel_filesdir = self.default_cobbler_ws_rel_filesdir + self.system_status = self.default_system_status self.excluded_datastores = [] @@ -205,6 +212,13 @@ class CrTplConfiguration(BaseConfiguration): """The root password of the VM to create.""" return self._root_password + # ------------------------------------------------------------------------- + @property + def system_ks(self): + """The path to the system kickstart file.""" + ks_base = 'template-' + self.os_id + '-' + self.system_status + '.ks' + return self.cobbler_ks_dir / ks_base + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -222,6 +236,7 @@ class CrTplConfiguration(BaseConfiguration): res['data_size_kb'] = self.data_size_kb res['data_size'] = self.data_size res['ram_gb'] = self.ram_gb + res['system_ks'] = self.system_ks res['root_password'] = None if self.root_password: @@ -349,6 +364,7 @@ class CrTplConfiguration(BaseConfiguration): 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) + re_swap_space = re.compile(r'^\s*swap[-_]?space(?:[-_]?mb)?\s*$', re.IGNORECASE) for (key, value) in config.items(section_name): if key.lower() == 'vm': @@ -405,6 +421,9 @@ class CrTplConfiguration(BaseConfiguration): elif re_root_pwd.match(key) and value.strip(): self._root_password = value.strip() continue + elif re_swap_space.match(key) and value.strip(): + self.swap_size_mb = int(value) + continue return @@ -512,6 +531,7 @@ class CrTplConfiguration(BaseConfiguration): 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) + rs_system_status = re.compile(r'^\s*system[-_]?status\s*$', re.IGNORECASE) for (key, value) in config.items(section_name): if key.lower() == 'distro' and value.strip() != '': @@ -565,6 +585,18 @@ class CrTplConfiguration(BaseConfiguration): continue if re_ws_rel_filesdir_key.match(key) and value.strip() != '': self.cobbler_ws_rel_filesdir = Path(value.strip()) + continue + if rs_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 get_root_pwd_hash(self, method='sha256'): -- 2.39.5