From f3dc6021df1d07fc0a5b68b3377caf2a8ab1e400 Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Tue, 5 Feb 2013 17:35:10 +0000 Subject: [PATCH] Check that public hostname can be resolved before stopping IaaS poll This is a workaround for OpenStack, which returns the local hostname as the public hostname for some time during the launch, even though the instance is running. If this hostname cannot be resolved, we continue polling IaaS until we get a resolvable hostname. --- cloudinitd/pollables.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/cloudinitd/pollables.py b/cloudinitd/pollables.py index 7f1dd50..721b2e3 100644 --- a/cloudinitd/pollables.py +++ b/cloudinitd/pollables.py @@ -217,10 +217,21 @@ def poll(self): state = self._instance.get_state() cloudinitd.log(self._log, logging.DEBUG, "Current iaas state in poll for %s is %s" % (self.get_instance_id(), state)) if state == "running": - self._done = True - self._thread.join() - self._execute_done_cb() - return True + # Workaround for OpenStack sending the internal hostname as the + # public hostname: we wait until the hostname can be resolved + hostname = self.get_hostname() + try: + socket.gethostbyname(hostname) + except socket.gaierror as e: + cloudinitd.log(self._log, logging.WARN, "Hostname %s fails to resolve, continuing iaas poll: %s" % (hostname, e)) + return False + except Exception: + raise + else: + self._done = True + self._thread.join() + self._execute_done_cb() + return True if state not in self._ok_states: msg = "The current state is %s. Never reached state running" % (state) cloudinitd.log(self._log, logging.DEBUG, msg, tb=traceback) @@ -263,14 +274,9 @@ def _thread_poll(self, poll_period=1.0): done = False while not self._done and not done: try: - if self._instance.get_state() not in self._ok_states: - cloudinitd.log(self._log, logging.DEBUG, "%s polling thread done" % (self.get_instance_id())) - done = True - # because update is called in start we will sleep first - else: - time.sleep(poll_period) - self._update() - cloudinitd.log(self._log, logging.DEBUG, "Current iaas state in thread for %s is %s" % (self.get_instance_id(), self._instance.get_state())) + time.sleep(poll_period) + self._update() + cloudinitd.log(self._log, logging.DEBUG, "Current iaas state in thread for %s is %s" % (self.get_instance_id(), self._instance.get_state())) except Exception, ex: cloudinitd.log(self._log, logging.ERROR, str(ex), tb=traceback) self.exception = IaaSException(ex)