From: Frank Brehm Date: Fri, 18 Oct 2019 09:03:58 +0000 (+0200) Subject: Rewritten validation of existing VMs X-Git-Tag: 1.3.4~25 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=f0fcc4a679fcd0e0b1d65ddb37b99156fa72d8b2;p=pixelpark%2Fcreate-terraform.git Rewritten validation of existing VMs --- diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 2a829e8..91ee8d0 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -61,7 +61,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '2.9.6' +__version__ = '2.9.7' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -97,7 +97,6 @@ class CreateTerraformHandler(BaseHandler): re_group = re.compile(r'^\s*groups?\s*$', re.IGNORECASE) re_group_name = re.compile(r'^\s*name\s*$', re.IGNORECASE) re_doublequote = re.compile(r'"') - re_vm_path = re.compile(r'^\s*\[\s*([^\s\]]+)') re_tf_version = re.compile(r'^\s*Terraform\s+v(\S+)', re.IGNORECASE) @@ -787,7 +786,7 @@ class CreateTerraformHandler(BaseHandler): raise ExpectedHandlerError(msg) self.get_all_vms() - # self.validate_vms() + self.validate_vms() LOG.info(_("Finished step {!r}.").format('validate-yaml')) if self.stop_at_step == 'validate-yaml': @@ -805,8 +804,8 @@ class CreateTerraformHandler(BaseHandler): if vs_name not in self.all_vms: self.all_vms[vs_name] = {} - vm_list = self.vsphere[vs_name].get_vms(re_vm, name_only=True) - for vm_tuple in vm_list: + vm_list = self.vsphere[vs_name].get_vms(re_vm, name_only=True) + for vm_tuple in vm_list: vm_name = vm_tuple[0] vm_path = vm_tuple[1] if vm_name in self.all_vms[vs_name]: @@ -817,6 +816,7 @@ class CreateTerraformHandler(BaseHandler): if self.verbose > 2: msg = _("All existing VMs and templates:") msg += '\n' + pp(self.all_vms) + LOG.debug(msg) # -------------------------------------------------------------------------· def exec_validate_storage(self): @@ -1229,11 +1229,12 @@ class CreateTerraformHandler(BaseHandler): if self.verbose: print() vs_name = vm.vsphere + vsphere = self.vsphere[vs_name] vm_paths = None if vs_name in self.all_vms: if vm.fqdn in self.all_vms[vs_name]: - vm_paths = self.all_vms[vs_name][vs_name] + vm_paths = self.all_vms[vs_name][vm.fqdn] if vm_paths: msg = _('[{m}] - VM is already existing in VSphere {v!r}, path {p!r}.').format( @@ -1242,29 +1243,20 @@ class CreateTerraformHandler(BaseHandler): if self.verbose: print() - - vm_info = self.vsphere.get_vm(vm.name, no_error=True) - if vm_info: - print( - _('[{}] - VM is already existing.').format( - self.colored('Existing', 'YELLOW')), end='', flush=True) - if self.verbose > 0: - print() + vm_info = vsphere.get_vm(vm.fqdn, vsphere_name=vs_name, as_obj=True) if self.verbose > 2: - LOG.debug(_("VM info:") + "\n" + pp(vm_info)) - match = self.re_vm_path.search(vm_info['vmPathName']) - if match: - ds = match.group(1) - LOG.debug(_("Datastore of VM {vm!r}: {ds!r}.").format( - vm=vm.name, ds=ds)) - vm.datastore = ds - vm.already_existing = True - self.existing_vms.append(vm_info) - else: - LOG.error(_("Did not found datastore of existing VM {!r}.").format(vm.name)) + LOG.debug(_("VM info:") + "\n" + pp(vm_info.as_dict(bare=True))) + ds = vm_info.config_path_storage + LOG.debug(_("Datastore of VM {vm!r}: {ds!r}.").format(vm=vm.name, ds=ds)) + vm.datastore = ds + vm.already_existing = True + self.existing_vms.append(vm_info) + else: + print('[{}] '.format(self.colored('OK', 'GREEN')), end='', flush=True) vm.already_existing = False + vms2perform.append(vm) print() diff --git a/lib/cr_tf/terraform/vm.py b/lib/cr_tf/terraform/vm.py index 953a2b2..85d5f91 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.4.1' +__version__ = '1.4.2' LOG = logging.getLogger(__name__) @@ -70,7 +70,7 @@ ngettext = XLATOR.ngettext class TerraformVm(HandlingObject): """A class encapsulating a VirtualMachine managed by Terraform.""" - default_vsphere = 'life' + default_vsphere = 'live' default_boot_delay = 5 default_customer = 'Pixelpark'