from .config import CrTplConfiguration
-__version__ = '0.2.1'
+__version__ = '0.2.2'
LOG = logging.getLogger(__name__)
A handler class for creating a vSphere template.
"""
+ max_depth = 10
+
# -------------------------------------------------------------------------
def __init__(
self, appname=None, verbose=0, version=__version__, base_dir=None,
self.config = config
self.server_instance = None
+ self.tpl_vm_folder = None
if initialized:
self.initialized = True
user=self.config.vsphere_user)
try:
- self.create_folder()
+ self.ensure_vm_folder()
+ #self.list_vms()
finally:
+ LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
+ h=self.config.vsphere_host, p=self.config.vsphere_port))
Disconnect(self.server_instance)
# -------------------------------------------------------------------------
t=ftype, f=folder_name, d=self.config.dc))
# -------------------------------------------------------------------------
- def create_folder(self):
+ def get_tpl_folder(self, vm_folder=None):
+
+ if not vm_folder:
+ content = self.server_instance.RetrieveContent()
+ dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
+ vm_folder = dc.vmFolder
+
+ for child in vm_folder.childEntity:
+ if not isinstance(child, vim.Folder):
+ continue
+ if self.verbose > 3:
+ LOG.debug("Found vim.Folder {!r} in root VM folder.".format(child.name))
+ if child.name == self.config.folder:
+ LOG.info("VM-Folder {!r} already exists.".format(self.config.folder))
+ return child
+
+ return None
+
+ # -------------------------------------------------------------------------
+ def ensure_vm_folder(self):
content = self.server_instance.RetrieveContent()
dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
- 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))
+ tpl_vm_folder = self.get_tpl_folder(dc.vmFolder)
+ if tpl_vm_folder:
+ self.tpl_vm_folder = tpl_vm_folder
return True
- self._create_folder(dc.hostFolder, self.config.folder, 'host')
+# 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()
+
return True
+ # -------------------------------------------------------------------------
+ def list_vms(self):
+
+ content = self.server_instance.RetrieveContent()
+
+ for child in content.rootFolder.childEntity:
+ if hasattr(child, 'vmFolder'):
+ datacenter = child
+ vmfolder = datacenter.vmFolder
+ for vm in vmfolder.childEntity:
+ self._printvminfo(vm)
+
+ # -------------------------------------------------------------------------
+ def _printvminfo(self, vm, depth=1):
+
+ if hasattr(vm, '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
+
# =============================================================================
if __name__ == "__main__":