From: Frank Brehm Date: Thu, 22 Mar 2018 12:11:28 +0000 (+0100) Subject: Checking existence of temporary VM X-Git-Tag: 0.1.1~97 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=cc2378da21258af327c8e9c1d47c62e64149c4cb;p=pixelpark%2Fcreate-vmware-tpl.git Checking existence of temporary VM --- diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 1edc615..6ff4b8b 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -32,7 +32,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -from .handler import CrTplHandler +from .handler import TempVmExistsError, CrTplHandler __version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -429,7 +429,12 @@ class CrTplApplication(PpBaseObject): """ LOG.info("Starting ...") - self.handler() + + try: + self.handler() + except TempVmExistsError as e: + self.handle_error(str(e), "Temporary VM") + self.exit(5) # ------------------------------------------------------------------------- def __call__(self): diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 9c6ba50..60eb9ac 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -28,7 +28,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.2.2' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -40,6 +40,22 @@ class HandlerError(PpError, RuntimeError): pass +# ============================================================================= +class TempVmExistsError(HandlerError): + """Special error class for the case, if the temporary VM is already existing.""" + + # ------------------------------------------------------------------------- + def __init__(self, vm_name): + + self.vm_name = vm_name + + # ------------------------------------------------------------------------- + def __str__(self): + + msg = "The temporary VM {!r} is already existing, cannot continue.".format(self.vm_name) + return msg + + # ============================================================================= class CannotConnectError(HandlerError): """Special error class for the case, it cannot connect @@ -120,7 +136,7 @@ class CrTplHandler(PpBaseObject): try: self.ensure_vm_folder() - #self.list_vms() + self.check_for_temp_tpl_vm() finally: LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format( h=self.config.vsphere_host, p=self.config.vsphere_port)) @@ -181,12 +197,6 @@ class CrTplHandler(PpBaseObject): self.tpl_vm_folder = tpl_vm_folder return True -# if self.get_obj(content, [vim.Folder], self.config.folder): -# LOG.info("vSphere folder {f!r} in data center {d!r} already exists.".format( -# f=self.config.folder, d=self.config.dc)) -# return True - - #self._create_folder(dc.hostFolder, self.config.folder, 'host') self._create_folder(dc.vmFolder, self.config.folder, 'VM') self.tpl_vm_folder = self.get_tpl_folder() @@ -194,34 +204,48 @@ class CrTplHandler(PpBaseObject): return True # ------------------------------------------------------------------------- - def list_vms(self): + def check_for_temp_tpl_vm(self): + + vm = self.get_temp_tpl_vm() + if vm: + if self.verbose > 1: + LOG.debug("Temporary VM {!r} exists, raising TempVmExistsError.".format( + self.config.template_vm)) + raise TempVmExistsError(self.config.template_vm) + + LOG.debug("Temporary VM {!r} does not exists, will be created.".format( + self.config.template_vm)) + + # ------------------------------------------------------------------------- + def get_temp_tpl_vm(self): content = self.server_instance.RetrieveContent() + dc = self.get_obj(content, [vim.Datacenter], self.config.dc) - for child in content.rootFolder.childEntity: - if hasattr(child, 'vmFolder'): - datacenter = child - vmfolder = datacenter.vmFolder - for vm in vmfolder.childEntity: - self._printvminfo(vm) + for child in dc.vmFolder.childEntity: + vm = self._get_temp_tpl_vm(child) + if vm: + return vm # ------------------------------------------------------------------------- - def _printvminfo(self, vm, depth=1): + def _get_temp_tpl_vm(self, child, depth=1): - if hasattr(vm, 'childEntity'): + if hasattr(child, 'childEntity'): if depth > self.max_depth: - return - for child in vm.childEntity: - self._printvminfo(child, depth + 1) - return - - if hasattr(vm, 'summary'): - summary = vm.summary - vm_type = 'Virtual Machine' - if summary.config.template: - vm_type = 'Template' - LOG.info("Found {t} {n!r}.".format(t=vm_type, n=summary.config.name)) - return + return None + for sub_child in child.childEntity: + vm = self._get_temp_tpl_vm(sub_child, depth + 1) + if vm: + return vm + return None + + if hasattr(child, 'summary'): + summary = child.summary + if summary.config.name == self.config.template_vm: + return child + + return None + # =============================================================================