]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Start rewriting VSPhare code
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 18 Oct 2019 07:08:32 +0000 (09:08 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 18 Oct 2019 07:08:32 +0000 (09:08 +0200)
lib/cr_tf/handler.py

index 6c4e5aeb8b282f162107441591b57a809fd0efc3..2aacd840d9378bb9c5c210e152f1a7de7becd727 100644 (file)
@@ -173,6 +173,7 @@ class CreateTerraformHandler(BaseHandler):
         self.project_dir = None
         self.project_name = None
 
+        self.all_vms = {}
         self.existing_vms = []
 
         self.start_dir = Path(os.getcwd())
@@ -782,12 +783,38 @@ class CreateTerraformHandler(BaseHandler):
                 self.eval_errors).format(n=self.eval_errors)
             raise ExpectedHandlerError(msg)
 
+        self.get_all_vms()
         self.validate_vms()
 
         LOG.info(_("Finished step {!r}.").format('validate-yaml'))
         if self.stop_at_step == 'validate-yaml':
             raise AbortExecution('validate-yaml')
 
+    # -------------------------------------------------------------------------·
+    def get_all_vms(self):
+
+        LOG.info(_("Got a list of all VMs and templates ..."))
+        self.all_vms = {}
+        re_vm = re.compile(r'.*')
+
+        for vs_name in self.vsphere:
+
+            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_name = vm_tuple[0]
+                vm_path = vm_tuple[1]
+                if vm_name in self.all_vms[vs_name]:
+                    self.all_vms[vs_name][vm_name].append(vm_path)
+                else:
+                    self.all_vms[vs_name][vm_name] = [vm_path]
+
+        if self.verbose > 2:
+            msg = _("All existing VMs and templates:")
+            msg += '\n' + pp(self.all_vms)
+
     # -------------------------------------------------------------------------·
     def exec_validate_storage(self):
 
@@ -1154,27 +1181,31 @@ class CreateTerraformHandler(BaseHandler):
 
         for vm in self.vms:
 
-            if vm.cluster in clusters:
-                clusters[vm.cluster].append(vm.name)
+            if vm.vsphere not in clusters:
+                clusters[vm.vsphere] = {}
+
+            if vm.cluster in clusters[vm.vsphere]:
+                clusters[vm.vsphere][vm.cluster].append(vm.name)
             else:
-                clusters[vm.cluster] = [vm.name]
+                clusters[vm.vsphere][vm.cluster] = [vm.name]
 
-        for cluster in clusters.keys():
+        for vsname in clusters.keys():
+            for cluster in clusters[vsname].keys():
 
-            cl = str(cluster)
-            LOG.debug(_("Checking existence of computing cluster {!r} ...").format(cl))
+                cl = str(cluster)
+                LOG.debug(_("Checking existence of computing cluster {!r} ...").format(cl))
 
-            vmw_cluster = self.vsphere.get_cluster_by_name(cl)
-            if vmw_cluster:
-                if self.verbose > 2:
-                    LOG.debug(
-                        _("Found computing cluster {cl!r} (defined for VMs {vms}).").format(
-                            cl=vmw_cluster.name, vms=pp(clusters[cluster])))
-            else:
-                LOG.error(
-                    _("Computing cluster {cl!r} (defined for VMs {vms}) not found.").format(
-                        cl=cl, vms=pp(clusters[cluster])))
-                self.eval_errors += 1
+                vmw_cluster = self.vsphere[vsname].get_cluster_by_name(cl)
+                if vmw_cluster:
+                    if self.verbose > 2:
+                        LOG.debug(
+                            _("Found computing cluster {cl!r} (defined for VMs {vms}).").format(
+                                cl=vmw_cluster.name, vms=pp(clusters[cluster])))
+                else:
+                    LOG.error(
+                        _("Computing cluster {cl!r} (defined for VMs {vms}) not found.").format(
+                            cl=cl, vms=pp(clusters[cluster])))
+                    self.eval_errors += 1
 
     # -------------------------------------------------------------------------·
     def validate_vms(self):
@@ -1188,6 +1219,20 @@ class CreateTerraformHandler(BaseHandler):
             print(" * {} ".format(vm.fqdn), end='', flush=True)
             if self.verbose:
                 print()
+            vs_name = vm.vsphere
+
+            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]
+
+            if vm_paths:
+                msg = _('[{m}]  - VM is already existing in VSphere {v!r}, path {p!r}.').format(
+                    m=self.colored('Existing', 'YELLOW'), v=vs_name, p=pp(vm_paths))
+                print(msg, end='', flush=True)
+                if self.verbose:
+                    print()
+
 
             vm_info = self.vsphere.get_vm(vm.name, no_error=True)
             if vm_info: