]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Rewritten validation of existing VMs
authorFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 09:03:58 +0000 (11:03 +0200)
committerFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 09:03:58 +0000 (11:03 +0200)
lib/cr_tf/handler.py
lib/cr_tf/terraform/vm.py

index 2a829e806f674e3dc5b853020594d45e785c5857..91ee8d00cdbddf98fe669687ada6368631dddb74 100644 (file)
@@ -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()
 
index 953a2b227e8ee909c22d487a4f9bfc93d56ad053..85d5f9144752c529aa40f84f8c41517d9d402a7f 100644 (file)
@@ -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'