diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-19 11:05:11 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-19 11:05:11 -0700 |
commit | 4a0bec74c7f59ee370f6622cacfad1638e255bcc (patch) | |
tree | 85fd4839c4388c05bd31477c78b8175e54095faa /cloudinit | |
parent | 50ec3b78686ae812c44b22835bf1203c92f5e5e3 (diff) | |
download | vyos-cloud-init-4a0bec74c7f59ee370f6622cacfad1638e255bcc.tar.gz vyos-cloud-init-4a0bec74c7f59ee370f6622cacfad1638e255bcc.zip |
Add the ability to check if the metadata service is up before actually using it.
1. This is very useful for testing (until we have boto timeout in a more sane manner)
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/sources/DataSourceCloudStack.py | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/cloudinit/sources/DataSourceCloudStack.py b/cloudinit/sources/DataSourceCloudStack.py index 27217e65..b1817654 100644 --- a/cloudinit/sources/DataSourceCloudStack.py +++ b/cloudinit/sources/DataSourceCloudStack.py @@ -30,6 +30,7 @@ import boto.utils as boto_utils from cloudinit import log as logging from cloudinit import sources +from cloudinit import url_helper as uhelp from cloudinit import util LOG = logging.getLogger(__name__) @@ -63,6 +64,48 @@ class DataSourceCloudStack(sources.DataSource): def __str__(self): return util.obj_name(self) + def _get_url_settings(self): + mcfg = self.ds_cfg + if not mcfg: + mcfg = {} + max_wait = 120 + try: + max_wait = int(mcfg.get("max_wait", max_wait)) + except Exception: + util.logexc(LOG, "Failed to get max wait. using %s", max_wait) + + if max_wait == 0: + return False + + timeout = 50 + try: + timeout = int(mcfg.get("timeout", timeout)) + except Exception: + util.logexc(LOG, "Failed to get timeout, using %s", timeout) + + return (max_wait, timeout) + + def wait_for_metadata_service(self): + mcfg = self.ds_cfg + if not mcfg: + mcfg = {} + + (max_wait, timeout) = self._get_url_settings() + + urls = [self.metadata_address] + start_time = time.time() + url = uhelp.wait_for_url(urls=urls, max_wait=max_wait, + timeout=timeout, status_cb=LOG.warn) + + if url: + LOG.info("Using metadata source: '%s'", url) + else: + LOG.critical(("Giving up on waiting for the metadata from %s" + " after %s seconds"), + urls, int(time.time() - start_time)) + + return bool(url) + def get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): @@ -71,13 +114,15 @@ class DataSourceCloudStack(sources.DataSource): LOG.debug("Using seeded cloudstack data from: %s", self.seed_dir) return True try: - start = time.time() + if not self.wait_for_metadata_service(): + return False + start_time = time.time() self.userdata_raw = boto_utils.get_instance_userdata(self.api_ver, None, self.metadata_address) self.metadata = boto_utils.get_instance_metadata(self.api_ver, self.metadata_address) - tot_time = (time.time() - start) - LOG.debug("Crawl of metadata service took %s", int(tot_time)) + LOG.debug("Crawl of metadata service took %s seconds", + int(time.time() - start_time)) return True except Exception: util.logexc(LOG, ('Failed fetching from metadata ' |