From 722bb4259bc81d738b30910f1666455ad53ebf99 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 12 Apr 2018 15:53:55 +0200 Subject: [PATCH] Implementing renaming VM and changing it into a template. --- lib/cr_vmware_tpl/handler.py | 39 +++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 249d838..650f1ba 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -38,7 +38,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.7.2' +__version__ = '0.7.3' LOG = logging.getLogger(__name__) TZ = pytz.timezone('Europe/Berlin') @@ -220,6 +220,7 @@ class CrTplHandler(PpBaseObject): self.wait_for_finish_install() self.post_install_tasks_ssh() self.poweroff_vm() + self.rename_and_change_vm() finally: LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format( h=self.config.vsphere_host, p=self.config.vsphere_port)) @@ -841,25 +842,61 @@ class CrTplHandler(PpBaseObject): start_shutdown = time.time() cur_diff = 0 vm.ShutdownGuest() + cur_time = start_shutdown + last_dot = cur_time + i = 0 LOG.debug("Waiting for successful shut down of VM ...") + if self.verbose <= 2: + print(' ==> ', end='', flush=True) + while guest_state.strip().lower() != "notrunning": + time.sleep(0.2) + cur_time = time.time() cur_diff = cur_time - start_shutdown + if (self.verbose <= 2) and ((cur_time - last_dot) >= self.interval_dot): + print('.', end='', flush=True) + i += 1 + if i >= 60: + print('', flush=True) + print(' ', end='', flush=True) + i = 0 + last_dot = cur_time + vm = self.get_temp_tpl_vm() guest_state = vm.guest.guestState + if self.verbose > 2: + LOG.debug("Still waiting for completing shutdown, current state is {!r}.".format( + guest_state)) if guest_state.strip().lower() == "notrunning": + print('', flush=True) LOG.info("Template VM {h!r} was shutting down in {t:0.1f} seconds.".format( h=self.config.template_vm, t=cur_diff)) return if cur_diff >= self.max_wait_for_shutdown: break + print('', flush=True) raise ExpectedHandlerError( "VM {h!r} was not shut down after {t:0.1f} seconds, current state is {s!r}.".format( h=self.config.template_vm, t=cur_diff, s=guest_state)) + # ------------------------------------------------------------------------- + def rename_and_change_vm(self): + + LOG.info("Renaming VM {o!r} => {n!r} ...".format( + o=self.config.template_vm, n=self.config.template_name) + + vm = self.get_temp_tpl_vm() + task = vm.Rename_Task(self.config.template_name) + self.wait_for_tasks([task]) + LOG.debug("Successful renamed VM into {!r}.".format(self.config.template_name)) + + LOG.info("Changing VM {!r} into a VMWare template ...".format(self.config.template_name)) + vm.MarkAsTemplate() + LOG.debug("Object {!r} is now a VMWare template.".format(self.config.template_name)) # ============================================================================= -- 2.39.5