from .config import CrTplConfiguration
-__version__ = '0.7.1'
+__version__ = '0.7.2'
LOG = logging.getLogger(__name__)
TZ = pytz.timezone('Europe/Berlin')
self.ssh_port = 22
self.ssh_user = 'root'
self.ssh_timeout = 30
+ self.max_wait_for_shutdown = 600
if initialized:
self.initialized = True
self.poweron_vm()
self.wait_for_finish_install()
self.post_install_tasks_ssh()
+ self.poweroff_vm()
finally:
LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
h=self.config.vsphere_host, p=self.config.vsphere_port))
printf "Current host FQDN: "
hostname -f
+ if [ -f /root/original-ks.cfg ] ; then
+ echo
+ echo "Kickstart configuration:"
+ echo
+ echo "-----------------------------------------------------------"
+ cat /root/original-ks.cfg
+ echo "-----------------------------------------------------------"
+ echo
+ echo "Removing /root/original-ks.cfg ..."
+ rm -v /root/original-ks.cfg
+ fi
+
+ if [ -v /root/anaconda-ks.cfg ] ; then
+ echo
+ echo "Removing /root/anaconda-ks.cfg ..."
+ rm -v /root/anaconda-ks.cfg
+ fi
+
if [ -f /var/log/post-install.log ] ; then
echo
echo "Post install log:"
echo
+ echo "-----------------------------------------------------------"
cat /var/log/post-install.log
+ echo "-----------------------------------------------------------"
echo
+ echo "Removing /var/log/post-install.log ..."
rm -v /var/log/post-install.log
fi
- # sleep 2
- # poweroff && logout
+ echo
+ echo "Cleaning up /root/.ssh/authorized_keys ..."
+ cat /root/.ssh/authorized_keys | grep -v create-vmware-tpl@pixelpark.com > /tmp/authorized_keys
+ mv -v /tmp/authorized_keys /root/.ssh/authorized_keys
+
+ echo "Removing SSH host keys ..."
+ rm -v /etc/ssh/ssh_host_*
+
""")
try:
LOG.debug("Closing SSH connection.")
ssh.close()
+ # -------------------------------------------------------------------------
+ def poweroff_vm(self):
+
+ LOG.info("Powering off VM {!r} ...".format(self.config.template_vm))
+
+ vm = self.get_temp_tpl_vm()
+ guest_state = vm.guest.guestState
+ LOG.debug("Current state of template VM is {!r}".format(guest_state))
+ if guest_state.strip().lower() == "notrunning":
+ LOG.info("Template VM is already shut off.")
+ return
+
+ if guest_state.strip().lower() != "running":
+ raise ExpectedHandlerError(
+ "Cannot shut down VM {h!r}, is currently in state {s!r}.".format(
+ h=self.config.template_vm, s=guest_state))
+
+ LOG.debug("Shutting down VM {!r} ...".format(self.config.template_vm))
+ start_shutdown = time.time()
+ cur_diff = 0
+ vm.ShutdownGuest()
+
+ LOG.debug("Waiting for successful shut down of VM ...")
+ while guest_state.strip().lower() != "notrunning":
+ time.sleep(0.2)
+ cur_time = time.time()
+ cur_diff = cur_time - start_shutdown
+ vm = self.get_temp_tpl_vm()
+ guest_state = vm.guest.guestState
+ if guest_state.strip().lower() == "notrunning":
+ 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
+
+ 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))
+
+
# =============================================================================
if __name__ == "__main__":