from .xlate import XLATOR
-__version__ = '2.0.0'
+__version__ = '2.1.0'
LOG = logging.getLogger(__name__)
TZ = pytz.timezone('Europe/Berlin')
# -------------------------------------------------------------------------
def __init__(
self, appname=None, verbose=0, version=__version__, base_dir=None,
- config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
+ cfg=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,
force=force, initialized=False,
)
- if not isinstance(config, CrTplConfiguration):
+ if not isinstance(cfg, CrTplConfiguration):
msg = _("{w} is not an instance of {c}, but an instance of {i} instead.").format(
- w='Parameter config', c='CrTplConfiguration', i=config.__class__.__name__)
+ w='Parameter cfg', c='CrTplConfiguration', i=cfg.__class__.__name__)
raise HandlerError(msg)
- self.config = config
+ self.cfg = cfg
self.service_instance = None
self.tpl_vm_folder = None
self.tpl_data_store = None
self.cobbler = None
self.vsphere = VsphereConnection(
- self.config.vsphere_info, cluster=self.config.vsphere_cluster,
+ self.cfg.vsphere_info, cluster=self.cfg.vsphere_cluster,
appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
auto_close=True, simulate=self.simulate, force=self.force, tz=TZ,
terminal_has_colors=self.terminal_has_colors, initialized=False)
# self.vsphere = VsphereServer(
# appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-# host=self.config.vsphere_host, port=self.config.vsphere_port,
-# user=self.config.vsphere_user, password=self.config.password,
-# dc=self.config.dc, cluster=self.config.vsphere_cluster,
+# host=self.cfg.vsphere_host, port=self.cfg.vsphere_port,
+# user=self.cfg.vsphere_user, password=self.cfg.password,
+# dc=self.cfg.dc, cluster=self.cfg.vsphere_cluster,
# auto_close=True, simulate=self.simulate, force=self.force, tz=TZ,
# terminal_has_colors=self.terminal_has_colors, initialized=False)
self.cluster = None
self.cobbler = Cobbler(
appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
- config=config, simulate=self.simulate, force=self.force,
+ cfg=cfg, simulate=self.simulate, force=self.force,
terminal_has_colors=self.terminal_has_colors, initialized=False)
self.cobbler.initialized = True
- if not self.config.os_id:
+ if not self.cfg.os_id:
msg = _("No ID for Operating system defined, please check the configuration.")
raise HandlerError(msg)
cur_ts = datetime.datetime.now()
cur_ts_str = cur_ts.strftime('%Y-%m-%d-%H-%M-%S')
- self.tpl_vm_hostname = self.config.os_id + '-' + cur_ts_str
+ self.tpl_vm_hostname = self.cfg.os_id + '-' + cur_ts_str
if initialized:
self.initialized = True
"""The FQDN of the template VM."""
if not self.tpl_vm_hostname:
return None
- if not self.config:
+ if not self.cfg:
return self.tpl_vm_hostname
- if not self.config.tpl_vm_domain:
+ if not self.cfg.tpl_vm_domain:
return self.tpl_vm_hostname
- return self.tpl_vm_hostname + '.' + self.config.tpl_vm_domain
+ return self.tpl_vm_hostname + '.' + self.cfg.tpl_vm_domain
# -------------------------------------------------------------------------
@property
def tpl_name(self):
"""The final name of the VSphere template."""
- if not self.config:
+ if not self.cfg:
return None
- if not self.config.os_id:
+ if not self.cfg.os_id:
return None
- return 'template-' + self.config.os_id
+ return 'template-' + self.cfg.os_id
# -------------------------------------------------------------------------
def as_dict(self, short=True):
if not self.initialized:
raise HandlerError(_("{}-object not initialized.").format(self.__class__.__name__))
- if not isinstance(self.config, CrTplConfiguration):
+ if not isinstance(self.cfg, CrTplConfiguration):
msg = _("{w} is not an instance of {c}, but an instance of {i} instead.").format(
- w='self.config', c='CrTplConfiguration', i=self.config.__class__.__name__)
+ w='self.cfg', c='CrTplConfiguration', i=self.cfg.__class__.__name__)
raise HandlerError(msg)
retval = 0
LOG.debug(_("Starting handling ..."))
self.cobbler.get_cobbler_version()
self.check_for_cobbler_distro()
+ return 0
self.cobbler.ensure_profile()
self.cobbler.ensure_root_authkeys()
self.cobbler.ensure_rsyslog_cfg_files()
print_section_start('vmw_info', 'Collecting VMWare info', collapsed=True)
self.vsphere.get_about()
self.vsphere.get_clusters()
- self.cluster = self.vsphere.get_cluster_by_name(self.config.vsphere_cluster)
+ self.cluster = self.vsphere.get_cluster_by_name(self.cfg.vsphere_cluster)
print_section_end('vmw_info')
if self.cluster:
LOG.debug(_("Found VSphere cluster {!r}.").format(self.cluster.name))
else:
LOG.error(_("Could not find VSphere cluster {!r}.").format(
- self.config.vsphere_cluster))
+ self.cfg.vsphere_cluster))
return 6
- if self.config.network not in self.cluster.networks:
+ if self.cfg.network not in self.cluster.networks:
LOG.error(_("Network {n!r} not available in cluster {c!r}.").format(
- n=self.config.network, c=self.cluster.name))
+ n=self.cfg.network, c=self.cluster.name))
return 6
print_section_start('vmw_networks', 'Collecting VSPhere networks ...', collapsed=True)
print_section_end('vmw_networks')
print_section_start('vmw_folder', 'Ensuring VSPhere folder ...', collapsed=True)
- self.vsphere.ensure_vm_folder(self.config.folder)
+ self.vsphere.ensure_vm_folder(self.cfg.folder)
print_section_end('vmw_folder')
self.check_for_temp_tpl_vm(no_error=True)
if self.tpl_vm:
LOG.debug(_("Created VM as {cls}: {vm!r}").format(
cls=self.tpl_vm.__class__.__name__, vm=self.tpl_vm))
- for device in self.tpl_vm.config.hardware.device:
+ for device in self.tpl_vm.cfg.hardware.device:
if isinstance(device, vim.vm.device.VirtualEthernetCard):
self.tpl_macaddress = device.macAddress
LOG.debug(_("Found Ethernet card as {}.").format(
else:
if self.simulate:
LOG.warn(_("Simulation mode - VM not created in real."))
- self.tpl_macaddress = self.config.default_mac_address
+ self.tpl_macaddress = self.cfg.default_mac_address
else:
raise HandlerError(_("Could not find VM after creating."))
self.cobbler.add_system(
name=tpl_sysname, fqdn=self.tpl_vm_fqdn, mac_address=self.tpl_macaddress)
- self.vsphere.poweron_vm(self.tpl_vm, max_wait=self.config.max_wait_for_poweron_vm)
+ self.vsphere.poweron_vm(self.tpl_vm, max_wait=self.cfg.max_wait_for_poweron_vm)
self.ts_start_install = time.time()
self.eval_tpl_ip()
self.wait_for_finish_install()
else:
self.post_install_tasks_ssh()
if self.postinstall_errors:
- self.vsphere.purge_vm(self.tpl_vm, max_wait=self.config.max_wait_for_purge_vm)
+ self.vsphere.purge_vm(self.tpl_vm, max_wait=self.cfg.max_wait_for_purge_vm)
return 10
else:
self.poweroff_vm()
def check_for_cobbler_distro(self):
LOG.debug(_(
"Checking, whether distro {!r} is available "
- "on the cobbler host.").format(self.config.cobbler_distro))
+ "on the cobbler host.").format(self.cfg.cobbler_distro))
distro_list = self.cobbler.get_distro_list()
- if self.config.cobbler_distro not in distro_list:
+ if self.cfg.cobbler_distro not in distro_list:
msg = _("Did not found distro {!r} on the cobbler host.").format(
- self.config.cobbler_distro)
+ self.cfg.cobbler_distro)
raise ExpectedHandlerError(msg)
if self.verbose > 1:
msg = _("Distro {!r} is available on the cobbler host.").format(
- self.config.cobbler_distro)
+ self.cfg.cobbler_distro)
LOG.debug(msg)
# -------------------------------------------------------------------------
LOG.info(_(
"Selecting a SAN based datastore with at least {:0.1f} GiB available "
- "space.").format(self.config.data_size_gb))
+ "space.").format(self.cfg.data_size_gb))
print_section_start('select_data_store', "Selecting data store ...", collapsed=True)
self.vsphere.get_ds_clusters()
self.vsphere.get_datastores()
ds_to_use = None
- if self.config.storage_cluster:
+ if self.cfg.storage_cluster:
ds_to_use = self.select_data_store_from_cluster()
if ds_to_use:
msg = _(
"Got datastore {n!r} as a member of datastore cluster {c!r}.").format(
- n=ds_to_use.name, c=self.config.storage_cluster)
+ n=ds_to_use.name, c=self.cfg.storage_cluster)
LOG.info(msg)
else:
msg = MSG_NO_CLUSTER.format(
- size=self.config.data_size_gb, c_name=self.config.storage_cluster)
+ size=self.cfg.data_size_gb, c_name=self.cfg.storage_cluster)
LOG.warn(msg)
if not ds_to_use:
ds_to_use = self.select_simple_data_store()
if not ds_to_use:
print_section_end('select_data_store')
- raise NoDatastoreFoundError(self.config.data_size_gb)
+ raise NoDatastoreFoundError(self.cfg.data_size_gb)
self.tpl_data_store = ds_to_use
LOG.info(_("Using datastore {!r} for volume of temporary VM to create.").format(
def select_data_store_from_cluster(self):
# Searching for the right storage cluster
- c_name = self.config.storage_cluster
+ c_name = self.cfg.storage_cluster
used_c_name = None
for cluster_name in self.vsphere.ds_clusters.keys():
if cluster_name.lower() == c_name.lower():
return None
cluster = self.vsphere.ds_clusters[used_c_name]
- if cluster.free_space_gb <= self.config.data_size_gb:
+ if cluster.free_space_gb <= self.cfg.data_size_gb:
msg = _(
"Cannot use datastore cluster {n!r}, free space "
"{free:0.1f} GiB is less than {min:0.1f} GiB.").format(
- n=used_c_name, free=cluster.free_space_gb, min=self.config.data_size_gb)
+ n=used_c_name, free=cluster.free_space_gb, min=self.cfg.data_size_gb)
LOG.warn(msg)
return None
podsel = vim.storageDrs.PodSelectionSpec()
podsel.storagePod = pod
- folder_obj = self.vsphere.get_vm_folder(self.config.folder)
+ folder_obj = self.vsphere.get_vm_folder(self.cfg.folder)
storagespec = vim.storageDrs.StoragePlacementSpec()
storagespec.podSelectionSpec = podsel
def _get_storage_pod_obj(self, used_c_name):
content = self.vsphere.service_instance.RetrieveContent()
- dc = self.vsphere.get_obj(content, [vim.Datacenter], self.config.dc)
+ dc = self.vsphere.get_obj(content, [vim.Datacenter], self.cfg.dc)
if not dc:
- raise VSphereDatacenterNotFoundError(self.config.dc)
+ raise VSphereDatacenterNotFoundError(self.cfg.dc)
for child in dc.datastoreFolder.childEntity:
pod = self._get_storage_pod_obj_rec(child, used_c_name)
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 ds.free_space_gb <= self.cfg.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))
+ n=ds.name, free=ds.free_space_gb, min=self.cfg.data_size_gb))
continue
usable_ds.append(ds)
# -------------------------------------------------------------------------
def create_vm(self):
- disk_size = self.config.data_size_gb
+ disk_size = self.cfg.data_size_gb
iface = VsphereVmInterface(
appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
- name='eth', network=self.tpl_network, network_name=self.config.network,
+ name='eth', network=self.tpl_network, network_name=self.cfg.network,
summary='Primary network device')
if self.verbose > 1:
vm_spec = self.vsphere.generate_vm_create_spec(
name=self.tpl_vm_fqdn, datastore=self.tpl_data_store.name,
disks=[disk_size], nw_interfaces=[iface], graphic_ram_mb=256,
- videao_ram_mb=32, boot_delay_secs=self.vm_boot_delay_secs, ram_mb=self.config.ram_mb,
- num_cpus=self.config.num_cpus, ds_with_timestamp=True,
- os_version=self.config.os_version, cfg_version=self.config.vmware_cfg_version)
+ videao_ram_mb=32, boot_delay_secs=self.vm_boot_delay_secs, ram_mb=self.cfg.ram_mb,
+ num_cpus=self.cfg.num_cpus, ds_with_timestamp=True,
+ os_version=self.cfg.os_version, cfg_version=self.cfg.vmware_cfg_version)
- tpl_vm_folder = self.vsphere.get_vm_folder(self.config.folder)
+ tpl_vm_folder = self.vsphere.get_vm_folder(self.cfg.folder)
if self.verbose > 1:
msg = _("VM-Folder object for template VM: {c} - {n!r}").format(
c=tpl_vm_folder, n=tpl_vm_folder.name)
self.vsphere.create_vm(
name=self.tpl_vm_fqdn, vm_folder=tpl_vm_folder, vm_config_spec=vm_spec,
- pool=self.cluster.resource_pool, max_wait=self.config.max_wait_for_create_vm)
+ pool=self.cluster.resource_pool, max_wait=self.cfg.max_wait_for_create_vm)
# -------------------------------------------------------------------------
def eval_tpl_ip(self):
last_dot = cur_time
i = 0
first = True
- while not ssh_available and cur_duration <= self.config.max_wait_for_finish_install:
+ while not ssh_available and cur_duration <= self.cfg.max_wait_for_finish_install:
if not first:
time.sleep(self.interval_poll)
"Template VM {h!r} was shutting down in {t:0.1f} seconds.").format(
h=self.tpl_ip, t=cur_diff))
return
- if cur_diff >= self.config.max_wait_for_shutdown_vm:
+ if cur_diff >= self.cfg.max_wait_for_shutdown_vm:
break
print('', flush=True)
last_tuple1 = random.randint(1, 254)
last_tuple2 = random.randint(1, 254)
- new_mac = self.config.mac_address_template.format(last_tuple1, last_tuple2)
+ new_mac = self.cfg.mac_address_template.format(last_tuple1, last_tuple2)
LOG.debug(_("New MAC address: {!r}.").format(new_mac))
vm = self.get_temp_tpl_vm()
print_section_start('rotate_templates', "Rotating templates ...", collapsed=True)
re_is_numeric = re.compile(r'^\s*(\d+)\s*$')
- pattern_tpl = r'^' + re.escape(self.config.template_name)
+ pattern_tpl = r'^' + re.escape(self.cfg.template_name)
re_tpl = re.compile(pattern_tpl, re.IGNORECASE)
templates = self.vsphere.get_vms(re_tpl, is_template=True, as_vmw_obj=True)
templates_to_remove = []
i = 0
for tpl_name in templates_sorted:
- if i > self.config.max_nr_templates_stay - 2:
+ if i > self.cfg.max_nr_templates_stay - 2:
templates_to_remove.append(tpl_name)
i += 1
templates_to_remove.reverse()
self.vsphere.purge_vm(template)
LOG.debug(_("Successful removed template {!r}.").format(tpl_name))
continue
- if tpl_name.strip().lower() == self.config.template_name.strip().lower():
+ if tpl_name.strip().lower() == self.cfg.template_name.strip().lower():
created = templates_ts[tpl_name]
ts_created = datetime.datetime.fromtimestamp(created, tz=TZ)
i = 0
def rename_and_change_vm(self):
LOG.info(_("Renaming VM {o!r} => {n!r} ...").format(
- o=self.tpl_vm_fqdn, n=self.config.template_name))
+ o=self.tpl_vm_fqdn, n=self.cfg.template_name))
print_section_start(
'rename_and_change_vm', "Renaming VM and mark as template ...", collapsed=True)
vm = self.get_temp_tpl_vm()
- task = vm.Rename_Task(self.config.template_name)
+ task = vm.Rename_Task(self.cfg.template_name)
self.vsphere.wait_for_tasks([task])
- LOG.debug(_("Successful renamed VM into {!r}.").format(self.config.template_name))
+ LOG.debug(_("Successful renamed VM into {!r}.").format(self.cfg.template_name))
LOG.info(_("Changing VM {!r} into a VMWare template ...").format(
- self.config.template_name))
+ self.cfg.template_name))
vm.MarkAsTemplate()
- LOG.debug(_("Object {!r} is now a VMWare template.").format(self.config.template_name))
+ LOG.debug(_("Object {!r} is now a VMWare template.").format(self.cfg.template_name))
print_section_end('rename_and_change_vm')