From 2018de5d051e4dfd1a5a8b9c96a667ad4b88b49d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 13 Oct 2023 17:13:48 +0200 Subject: [PATCH] Implementing 'is_rhel' as a new property of a TerraformVm object. --- example/prd-repo03.yaml | 2 +- lib/cr_tf/terraform/vm.py | 55 +++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/example/prd-repo03.yaml b/example/prd-repo03.yaml index 8c8e470..cdafa37 100644 --- a/example/prd-repo03.yaml +++ b/example/prd-repo03.yaml @@ -1,4 +1,4 @@ --- +--- defaults: vsphere: live cluster: vmcc-l105-01 diff --git a/lib/cr_tf/terraform/vm.py b/lib/cr_tf/terraform/vm.py index 1744e4a..86362d4 100644 --- a/lib/cr_tf/terraform/vm.py +++ b/lib/cr_tf/terraform/vm.py @@ -37,7 +37,7 @@ from .disk import TerraformDisk, TerraformDiskDict from .interface import TerraformInterface -__version__ = '1.6.2' +__version__ = '1.6.3' LOG = logging.getLogger(__name__) @@ -53,8 +53,6 @@ PUPPET_ENVIRONMENTS = ( 'production', 'test', 'development', - 'pre_development', - 'dev_chronie' ) DS_TYPES = ( @@ -133,7 +131,9 @@ class TerraformVm(HandlingObject): re_key_interface = re.compile(r'^\s*interfaces?\s*$', re.IGNORECASE) re_key_has_backup = re.compile(r'^\s*has[_-]?backup\s*$', re.IGNORECASE) re_key_has_puppet = re.compile(r'^\s*has[_-]?puppet\s*$', re.IGNORECASE) + re_key_is_rhel = re.compile(r'^\s*is[_-]?rhel\s*$', re.IGNORECASE) re_memory_value = re.compile(r'^\s*(\d+(?:\.\d*)?)\s*(?:(\D+)\s*)?$') + re_rhel_template = re.compile(r'(?:rhel|red[_-\s]*hat[_-\s]*enterprise)', re.IGNORECASE) re_invalid_chars = re.compile(r'[^a-z0-9@\._-]', re.IGNORECASE) re_invalid_chars_role = re.compile(r'[^a-z0-9:@\._-]', re.IGNORECASE) @@ -156,7 +156,7 @@ class TerraformVm(HandlingObject): puppet_customer=None, puppet_project=None, puppet_tier=None, puppet_env=None, puppet_initial_install=True, vm_template=None, nameservers=None, searchdomains=None, dns_options=None, has_backup=True, has_puppet=True, already_existing=None, - vsphere=None): + vsphere=None, is_rhel=None): self._vsphere = self.default_vsphere self._is_template = bool(is_template) @@ -184,6 +184,7 @@ class TerraformVm(HandlingObject): self._has_backup = bool(has_backup) self._has_puppet = bool(has_puppet) self._already_existing = False + self._is_rhel = None self.disks = None self.interfaces = [] @@ -202,7 +203,7 @@ class TerraformVm(HandlingObject): name=name, fqdn=fqdn, num_cpus=num_cpus, memory=memory, folder=folder, boot_delay=boot_delay, vm_template=vm_template, puppet_contact=puppet_contact, puppet_customer=puppet_customer, puppet_tier=puppet_tier, puppet_env=puppet_env, - puppet_initial_install=puppet_initial_install, + puppet_initial_install=puppet_initial_install, is_rhel=is_rhel, cluster=cluster, rootdisk_size=rootdisk_size, nameservers=nameservers, searchdomains=searchdomains, dns_options=dns_options, purpose=purpose, customer=customer, ds_cluster=ds_cluster, datastore=datastore, ds_type=ds_type, @@ -294,6 +295,10 @@ class TerraformVm(HandlingObject): if vm.interfaces and vm.fqdn and not vm.interfaces[0].fqdn: vm.interfaces[0].fqdn = vm.fqdn + if not vm.is_template: + if vm.is_rhel is None: + vm.is_rhel = cls.guess_rhel(vm) + vm.initialized = True return vm @@ -424,6 +429,10 @@ class TerraformVm(HandlingObject): vm.ds_type = value return True + if cls.re_key_is_rhel.search(key) and value: + vm.is_rhel = value + return True + return False # ------------------------------------------------------------------------- @@ -697,6 +706,28 @@ class TerraformVm(HandlingObject): return domains + # ------------------------------------------------------------------------- + @classmethod + def guess_rhel(cls, vm): + """Trying to guess, whether the VM to deploy should be a RHEL instance.""" + + if not vm.vm_template: + msg = _( + "The VM {!r} was no VMware template assigned, assuming the VM " + "should become a RHEL instance.").format(vm.fqdn) + LOG.warn(msg) + return True + + ret = False + if cls.re_rhel_template.search(vm.vm_template): + ret = True + + msg = _("Guessing the VM {fqdn!r} should become a RHEL instance: {ret!r}").format( + fqdn=vm.fqdn, ret=ret) + LOG.debug(msg) + + return ret + # ----------------------------------------------------------- @property def is_template(self): @@ -1240,6 +1271,19 @@ class TerraformVm(HandlingObject): def already_existing(self, value): self._already_existing = to_bool(value) + # ----------------------------------------------------------- + @property + def is_rhel(self): + """A flag indicating, that the VM should ishould be managed by puppet.""" + return self._is_rhel + + @is_rhel.setter + def is_rhel(self, value): + if value is None: + self._is_rhel = None + return + self._is_rhel = bool(value) + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -1269,6 +1313,7 @@ class TerraformVm(HandlingObject): res['hiera_project'] = self.hiera_project res['hostname'] = self.hostname res['interfaces'] = [] + res['is_rhel'] = self.is_rhel res['is_template'] = self.is_template res['memory'] = self.memory res['name'] = self.name -- 2.39.5