From c9d94f6621fc9da255c37e4505e16fbe6b2a9f8e Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 24 Oct 2018 16:46:45 +0200 Subject: [PATCH] Reaching method create_vm() --- etc/create-vmware-template.ini.default | 4 ++ lib/cr_vmware_tpl/config.py | 8 +++ lib/cr_vmware_tpl/handler.py | 68 +++++++++----------------- python_fb_tools | 2 +- 4 files changed, 35 insertions(+), 47 deletions(-) diff --git a/etc/create-vmware-template.ini.default b/etc/create-vmware-template.ini.default index b7387c5..b2373a3 100644 --- a/etc/create-vmware-template.ini.default +++ b/etc/create-vmware-template.ini.default @@ -18,6 +18,10 @@ ;vm = template.pixelpark.com +;vmware_cfg_version = vmx-13 + +;os_version = oracleLinux7_64Guest + ;name = oracle-linux-7-template ;data_size_gb = 32 diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index 1e688a6..d306f4f 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -53,6 +53,8 @@ class CrTplConfiguration(BaseConfiguration): default_mac_address = '00:16:3e:54:ab:2b' default_max_wait_for_finish_install = 60 * 60 default_max_nr_templates_stay = 4 + default_vmware_cfg_version = 'vmx-13' + default_os_version = 'oracleLinux7_64Guest' min_max_wait_for_finish_install = 3 * 60 max_max_wait_for_finish_install = 24 * 60 * 60 limit_max_nr_templates_stay = 100 @@ -80,6 +82,8 @@ class CrTplConfiguration(BaseConfiguration): self.mac_address = self.default_mac_address self.max_wait_for_finish_install = self.default_max_wait_for_finish_install self.max_nr_templates_stay = self.default_max_nr_templates_stay + self.vmware_cfg_version = self.default_vmware_cfg_version + self.os_version = self.default_os_version self.excluded_datastores = [] @@ -243,6 +247,10 @@ class CrTplConfiguration(BaseConfiguration): v = value.strip().lower() if v: self.mac_address = v + elif key.lower() == 'vmware_cfg_version': + self.vmware_cfg_version = value.strip() + elif key.lower() == 'os_version': + self.os_version = value.strip() elif key.lower() == 'max_wait_for_finish_install': v = float(value) if v < self.min_max_wait_for_finish_install: diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 970bba6..b73bc05 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -89,7 +89,6 @@ class CrTplHandler(BaseHandler): self.tpl_vm_folder = None self.tpl_data_store = None self.tpl_network = None - self.tpl_cluster = None self.tpl_vm = None self.ts_start_install = None self.ts_finish_install = None @@ -165,6 +164,11 @@ class CrTplHandler(BaseHandler): self.check_for_temp_tpl_vm(no_error=True) self.select_data_store() + if self.rotate_only: + LOG.warn("Only executing of template rotating.") + else: + self.create_vm() + return 0 @@ -192,8 +196,8 @@ class CrTplHandler(BaseHandler): if self.rotate_only: LOG.warn("Only executing of template rotating.") else: - self.check_network() - self.create_vm() +# self.check_network() +# self.create_vm() self.tpl_vm = self.get_temp_tpl_vm() if not self.tpl_vm: raise HandlerError( @@ -401,43 +405,6 @@ class CrTplHandler(BaseHandler): return tpl_list - # ------------------------------------------------------------------------- - def check_network(self): - - content = self.service_instance.RetrieveContent() - dc = self.get_obj(content, [vim.Datacenter], self.config.dc) - - LOG.debug("Checking existence of network {!r} ...".format(self.config.network)) - - net = None - for child in dc.networkFolder.childEntity: - net = self._get_network(child) - if net: - break - - if not net: - raise NetworkNotExistingError(self.config.network) - LOG.debug("Found network {!r}.".format(self.config.network)) - self.tpl_network = net - - # ------------------------------------------------------------------------- - def _get_network(self, child, depth=1): - - if hasattr(child, 'childEntity'): - if depth > self.max_depth: - return None - for sub_child in child.childEntity: - net = self._get_network(sub_child, depth + 1) - if net: - return net - return None - - if isinstance(child, (vim.Network, vim.DistributedVirtualSwitch)): - if child.summary.name == self.config.network: - return child - - return None - # ------------------------------------------------------------------------- def select_data_store(self): @@ -502,7 +469,7 @@ class CrTplHandler(BaseHandler): tstamp = datetime.datetime.now(tz=TZ).strftime('%Y-%m-%d_%H-%M') datastore_path = ( - '[' + self.tpl_data_store.summary.name + '] template-' + tstamp + '/') + '[' + self.tpl_data_store.name + '] template-' + tstamp + '/') LOG.debug("Datastore path: {!r}".format(datastore_path)) vm_path_name = datastore_path + self.config.template_vm + '.vmx' LOG.debug("VM path name: {!r}".format(vm_path_name)) @@ -595,17 +562,26 @@ class CrTplHandler(BaseHandler): memoryMB=self.config.ram_mb, memoryHotAddEnabled=True, numCPUs=self.config.num_cpus, cpuHotAddEnabled=True, cpuHotRemoveEnabled=True, files=vm_file_info, - guestId='oracleLinux64Guest', - version='vmx-11', + guestId=self.config.os_version, + version=self.config.vmware_cfg_version, bootOptions=boot_opts, ) - if self.verbose > 2: + if self.verbose > 1: LOG.debug("Generated VM config:\n{}".format(pp(config))) + tpl_vm_folder = self.vsphere.get_vm_folder(self.config.folder) + if self.verbose > 1: + LOG.debug("VM-Folder object for template VM: {c} - {n!r}\n{t}".format( + c=tpl_vm_folder, n=tpl_vm_folder.name, t=pp(tpl_vm_folder.childType))) + + if self.simulate: + LOG.info("Simulation mode - VM {!r} will not be created.".format( + self.config.template_vm)) + return + LOG.debug("Start Creating VM ...") - task = self.tpl_vm_folder.CreateVM_Task( - config=config, pool=self.tpl_cluster.resourcePool) + task = tpl_vm_folder.CreateVM_Task(config=config, pool=self.cluster.resource_pool) self.wait_for_tasks([task]) diff --git a/python_fb_tools b/python_fb_tools index b192448..1241c97 160000 --- a/python_fb_tools +++ b/python_fb_tools @@ -1 +1 @@ -Subproject commit b192448f42e85829d04a55044dfd9c244d9106be +Subproject commit 1241c9751b81c690aec9e33401cffc7cf2fc4946 -- 2.39.5