from .config import CrTplConfiguration
-__version__ = '0.10.3'
+__version__ = '0.10.4'
LOG = logging.getLogger(__name__)
TZ = pytz.timezone('Europe/Berlin')
# -------------------------------------------------------------------------
def __init__(
self, appname=None, verbose=0, version=__version__, base_dir=None,
- config=None, initialized=False):
+ config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
super(CrTplHandler, self).__init__(
- appname=appname,
- verbose=verbose,
- version=version,
- base_dir=base_dir,
- initialized=False,
+ appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+ terminal_has_colors=terminal_has_colors, simulate=simulate,
+ force=force, initialized=False,
)
self.config = config
return 6
self.vsphere.ensure_vm_folder(self.config.folder)
+ self.check_for_temp_tpl_vm(no_error=True)
+ self.select_data_store()
return 0
try:
# self.get_cluster()
# self.ensure_vm_folder()
- self.check_for_temp_tpl_vm()
- self.select_data_store()
if self.rotate_only:
LOG.warn("Only executing of template rotating.")
else:
if pcfilter:
pcfilter.Destroy()
- # -------------------------------------------------------------------------
- def _create_folder(self, host_folder, folder_name, ftype=None):
-
- if not ftype:
- ftype = host_folder.__class__.__name__
-
- LOG.debug("Creating {t}-folder {f!r} in data center {d!r} ...".format(
- t=ftype, f=folder_name, d=self.config.dc))
- host_folder.CreateFolder(folder_name)
- LOG.info("Successfully created the {t}-folder {f!r} in data center {d!r}.".format(
- t=ftype, f=folder_name, d=self.config.dc))
-
# -------------------------------------------------------------------------
def get_tpl_folder(self, vm_folder=None):
return None
# -------------------------------------------------------------------------
- def check_for_temp_tpl_vm(self):
+ def check_for_temp_tpl_vm(self, no_error=False):
+
+ LOG.debug("First checking, whether {!r} exists ...".format(self.config.template_vm))
+ vm = self.vsphere.get_vm(self.config.template_vm, no_error=no_error)
- vm = self.get_temp_tpl_vm()
if vm:
if self.verbose > 1:
LOG.debug("Temporary VM {!r} exists, raising TempVmExistsError.".format(
"Selecting a SAN based datastore with at least {:0.1f} GiB available "
"space.").format(self.config.data_size_gb))
- content = self.service_instance.RetrieveContent()
- dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
+ self.vsphere.get_datastores()
- ds_list = []
+ usable_ds = []
+ for ds in self.vsphere.datastores.values():
+ if not ds.accessible:
+ if self.verbose > 1:
+ LOG.debug("Cannot use datastore {n!r} - not accessible.".format(n=ds.name))
+ continue
+ if ds.name not in self.cluster.datastores:
+ if self.verbose > 1:
+ LOG.debug("Cannot use datastore {n!r}, not in cluster {c!r}.".format(
+ n=ds.name, c=self.cluster.name))
+ continue
+ if self.verbose > 3:
+ LOG.debug("Checking datastore:\n{}".format(pp(ds.as_dict())))
+ if ds.storage_type not in ('SAS', 'SSD', 'SATA'):
+ if self.verbose > 1:
+ LOG.debug("Cannot use datastore {n!r}, is of type {t!r}.".format(
+ n=ds.name, t=ds.storage_type))
+ continue
+ if ds.free_space_gb <= self.config.data_size_gb:
+ if self.verbose > 1:
+ LOG.debug((
+ "Cannot use datastore {n!r}, free space "
+ "{free:0.1f} GiB is less than {min:0.1f} GiB.").format(
+ n=ds.name, free=ds.free_space_gb, min=self.config.data_size_gb))
+ continue
- for child in dc.datastoreFolder.childEntity:
- self._get_data_stores(child, ds_list)
+ usable_ds.append(ds)
- if not len(ds_list):
- raise NoDatastoreFoundError(self.config.data_size)
+ LOG.debug("Found {} usable datastores.".format(len(usable_ds)))
+ if len(usable_ds) < 1:
+ msg = "Did not found an usable datastore."
+ raise ExpectedHandlerError(msg)
- LOG.debug("Found {} usable datastores.".format(len(ds_list)))
- self.tpl_data_store = random.choice(ds_list)
+ self.tpl_data_store = random.choice(usable_ds)
LOG.info("Using datastore {!r} for volume of temporary VM to create.".format(
- self.tpl_data_store.summary.name))
-
- # -------------------------------------------------------------------------
- def _get_data_stores(self, child, ds_list, depth=1):
-
- if hasattr(child, 'childEntity'):
- if depth > self.max_depth:
- return
- for sub_child in child.childEntity:
- self._get_data_stores(sub_child, ds_list, depth + 1)
- return
-
- if isinstance(child, vim.Datastore):
- if self.re_local_ds.match(child.summary.name):
- if self.verbose > 2:
- LOG.debug("Datastore {!r} seems to be local.".format(child.summary.name))
- return
- if self.re_share_nfs_ds.search(child.summary.name):
- if self.verbose > 2:
- LOG.debug("Datastore {!r} seems to be a NFS share.".format(child.summary.name))
- return
- if child.summary.name in self.config.excluded_datastores:
- LOG.debug("Datastore {!r} is excluded.".format(child.summary.name))
- return
- free_bytes = child.summary.freeSpace
- free_gbytes = float(free_bytes) / 1024.0 / 1024.0 / 1024.0
- if free_bytes >= self.config.data_size:
- if self.verbose > 2:
- LOG.debug("Found datastore {n!r} with {f:0.1f} GiB free space.".format(
- n=child.summary.name, f=free_gbytes))
- ds_list.append(child)
- return
-
- LOG.debug("Datastore {n!r} has too less space ({f:0.1f} GiB available).".format(
- n=child.summary.name, f=free_gbytes))
-
- return
+ self.tpl_data_store.name))
# -------------------------------------------------------------------------
def create_vm(self):