]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Retrieving and using IP address of created template VM
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jun 2020 10:34:36 +0000 (12:34 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jun 2020 10:34:36 +0000 (12:34 +0200)
lib/cr_vmware_tpl/handler.py

index f32783cf9cd1cfcc00b1c024b5d4c414d01f1c0a..ffe2f4e33cc83dcb7aa6d6b75717b1115566e4eb 100644 (file)
@@ -42,7 +42,7 @@ from .cobbler import CobblerError, Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '1.5.11'
+__version__ = '1.6.1'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -74,6 +74,7 @@ class CrTplHandler(BaseHandler):
     """
 
     max_depth = 10
+    vm_boot_delay_secs = 5
     re_local_ds = re.compile(r'^local[_-]', re.IGNORECASE)
     re_share_nfs_ds = re.compile(r'(?:share[_-]*nfs|nfs[_-]*share)', re.IGNORECASE)
 
@@ -101,6 +102,7 @@ class CrTplHandler(BaseHandler):
         self.tpl_vm = None
         self.tpl_vm_hostname = None
         self.tpl_macaddress = None
+        self.tpl_ip = None
         self.ts_start_install = None
         self.ts_finish_install = None
         self.initial_sleep = 60
@@ -288,7 +290,8 @@ class CrTplHandler(BaseHandler):
 
             self.vsphere.poweron_vm(self.tpl_vm, max_wait=self.config.max_wait_for_poweron_vm)
             self.ts_start_install = time.time()
-#            self.wait_for_finish_install()
+            self.eval_tpl_ip()
+            self.wait_for_finish_install()
 
 #            self.get_postinstall_error()
 #            if self.abort:
@@ -425,7 +428,7 @@ class CrTplHandler(BaseHandler):
         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=5, ram_mb=self.config.ram_mb,
+            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)
 
@@ -440,6 +443,36 @@ class CrTplHandler(BaseHandler):
             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)
 
+    # -------------------------------------------------------------------------
+    def eval_tpl_ip(self):
+
+        LOG.info(_("Trying to evaluate the IP address of the template VM ..."))
+
+        initial_delay = self.vm_boot_delay_secs + 10
+
+        LOG.debug(_("Waiting initially for {} seconds:").format(initial_delay))
+        print('   ==> ', end='', flush=True)
+
+        start_time = time.time()
+        cur_time = start_time
+        cur_duration = 0
+
+        while cur_duration <= initial_delay:
+            time.sleep(1)
+            cur_time = time.time()
+            print('.', end='', flush=True)
+            cur_duration = cur_time - start_time
+        print('', flush=True)
+
+        self.tpl_ip = self.cobbler.get_dhcp_ip(self.tpl_macaddress)
+        if not self.tpl_ip:
+            msg = _(
+                "Did not got the IP address of MAC address {mac!r} after "
+                "{delay} seconds.").format(mac=self.tpl_macaddress, delay=initial_delay)
+            raise ExpectedHandlerError(msg)
+
+        LOG.info(_("Got IP address {!r} for template VM.").format(self.tpl_ip))
+
     # -------------------------------------------------------------------------
     def wait_for_finish_install(self):
 
@@ -463,16 +496,15 @@ class CrTplHandler(BaseHandler):
 
         LOG.debug(_("Waiting for SSH available ..."))
 
-        addr_infos = socket.getaddrinfo(
-            self.config.template_vm, 22, socket.AF_INET, socket.SOCK_STREAM)
+        addr_infos = socket.getaddrinfo(self.tpl_ip, 22, socket.AF_INET, socket.SOCK_STREAM)
         if self.verbose > 1:
             msg = _("Got following address_infos for {h!r}, IPv4 TCP port {p}:").format(
-                h=self.config.template_vm, p=22)
+                h=self.tpl_ip, p=22)
             msg += '\n' + pp(addr_infos)
             LOG.debug(msg)
         if not addr_infos:
             raise HandlerError(_("Did not get address infos for {h!r}, IPv4 TCP port {p}.").format(
-                h=self.config.template_vm, p=22))
+                h=self.tpl_ip, p=22))
 
         addr_info = random.choice(addr_infos)
         LOG.debug(_("Using address info: {}").format(pp(addr_info)))