From 0ff65aae0de76b706b8f5e2f649b5954c2577586 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 1 Oct 2019 11:29:36 +0200 Subject: [PATCH] Fix reading YAML file --- lib/cr_tf/handler.py | 8 +++----- lib/cr_tf/terraform/vm.py | 41 +++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index caff48b..4739628 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -59,7 +59,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '2.9.0' +__version__ = '2.9.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -886,8 +886,7 @@ class CreateTerraformHandler(BaseHandler): try: vm = TerraformVm.from_def( - vm_def, name=name, is_template=True, template_vm=template_vm, - default_cluster=self.config.vsphere_cluster, appname=self.appname, + vm_def, name=name, is_template=True, template_vm=template_vm, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, simulate=self.simulate, force=self.force, terminal_has_colors=self.terminal_has_colors) except Exception as e: @@ -911,8 +910,7 @@ class CreateTerraformHandler(BaseHandler): try: vm = TerraformVm.from_def( - vm_def, is_template=False, template_vm=template_vm, - default_cluster=self.config.vsphere_cluster, appname=self.appname, + vm_def, is_template=False, template_vm=template_vm, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, simulate=self.simulate, force=self.force, terminal_has_colors=self.terminal_has_colors) except Exception as e: diff --git a/lib/cr_tf/terraform/vm.py b/lib/cr_tf/terraform/vm.py index fa79142..953a2b2 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.3.1' +__version__ = '1.4.1' LOG = logging.getLogger(__name__) @@ -70,6 +70,8 @@ ngettext = XLATOR.ngettext class TerraformVm(HandlingObject): """A class encapsulating a VirtualMachine managed by Terraform.""" + default_vsphere = 'life' + default_boot_delay = 5 default_customer = 'Pixelpark' default_ds_type = 'sata' @@ -137,8 +139,9 @@ class TerraformVm(HandlingObject): customer=None, rootdisk_size=None, purpose=None, puppet_contact=None, puppet_role=None, puppet_customer=None, puppet_project=None, puppet_tier=None, puppet_env=None, vm_template=None, nameservers=None, has_backup=True, has_puppet=True, - already_existing=None): + already_existing=None, vsphere=None): + self._vsphere = self.default_vsphere self._is_template = bool(is_template) self._name = None self._fqdn = None @@ -159,7 +162,7 @@ class TerraformVm(HandlingObject): self._puppet_tier = self.default_puppet_tier self._puppet_env = None self._puppet_role = self.default_puppet_role - self._vm_template = CrTfConfiguration.default_template_name + self._vm_template = None self._has_backup = bool(has_backup) self._has_puppet = bool(has_puppet) self._already_existing = False @@ -182,7 +185,7 @@ class TerraformVm(HandlingObject): cluster=cluster, rootdisk_size=rootdisk_size, nameservers=nameservers, purpose=purpose, customer=customer, ds_cluster=ds_cluster, datastore=datastore, ds_type=ds_type, already_existing=already_existing, initialized=initialized, puppet_role=puppet_role, - puppet_project=puppet_project,) + puppet_project=puppet_project, vsphere=vsphere) # ------------------------------------------------------------------------- def _post_init(self, name=None, fqdn=None, nameservers=None, initialized=False, **kwargs): @@ -226,7 +229,7 @@ class TerraformVm(HandlingObject): @classmethod def from_def( cls, vm_def, name=None, is_template=False, template_vm=None, appname=None, - verbose=0, base_dir=None, simulate=False, force=False, default_cluster='', + verbose=0, base_dir=None, simulate=False, force=False, terminal_has_colors=False, initialized=False): if verbose > 2: @@ -250,7 +253,7 @@ class TerraformVm(HandlingObject): else: vm = cls( appname=appname, verbose=verbose, base_dir=base_dir, simulate=simulate, - force=force, is_template=is_template, name=name, cluster=default_cluster, + force=force, is_template=is_template, name=name, terminal_has_colors=terminal_has_colors) vm.initialized = False @@ -335,6 +338,10 @@ class TerraformVm(HandlingObject): vm.fqdn = value return True + if key.lower() == 'vsphere' and value: + vm.vsphere = value + return + if key.lower() == 'cluster': vm.cluster = value return True @@ -557,6 +564,7 @@ class TerraformVm(HandlingObject): puppet_env=self.puppet_env, puppet_role=self.puppet_role, nameservers=self.nameservers, rootdisk_size=self.rootdisk_size, has_backup=self.has_backup, has_puppet=self.has_puppet, puppet_project=self.puppet_project, + vsphere=self.vsphere, ) vm.disks = copy.copy(self.disks) @@ -676,6 +684,26 @@ class TerraformVm(HandlingObject): self._name = val + # ----------------------------------------------------------- + @property + def vsphere(self): + """The name of the VSPhere from configuration, in which + the VM should be created.""" + return self._vsphere + + @vsphere.setter + def vsphere(self, value): + if value is None: + msg = _("The name of the vsphere may not be None.") + raise TerraformVmDefinitionError(msg) + + val = str(value).strip() + if val == '': + msg = _("The name of the vsphere may not be empty.") + raise TerraformVmDefinitionError(msg) + + self._vsphere = val + # ----------------------------------------------------------- @property def tf_name(self): @@ -1150,6 +1178,7 @@ class TerraformVm(HandlingObject): res['rootdisk_size'] = self.rootdisk_size res['tf_name'] = self.tf_name res['vm_template'] = self.vm_template + res['vsphere'] = self.vsphere for interface in self.interfaces: res['interfaces'].append(interface.as_dict(short=short)) -- 2.39.5