From 1c7da352fbf1fadb7d9242cbc0593d9f8d1be9ad Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 18 Oct 2019 09:08:32 +0200 Subject: [PATCH] Start rewriting VSPhare code --- lib/cr_tf/handler.py | 79 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 6c4e5ae..2aacd84 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -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: -- 2.39.5