Skip to content

Commit

Permalink
OpenStack: Use timeout and retries from config in get_data.
Browse files Browse the repository at this point in the history
This modifies get_data in DataSourceOpenStack.py to get the timeout
and retries values from the data source configuration, rather than
from keyword arguments.  This permits get_data to use the same timeout
as other methods, and allows an operator to increase the timeout in
environments where the metadata service takes longer than five seconds
to respond.

LP: #1657130
Resolves: rhbz#1408589
  • Loading branch information
larsks authored and smoser committed Jan 17, 2017
1 parent 8ddb571 commit 4cf53f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions cloudinit/sources/DataSourceOpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _get_url_settings(self):
# max_wait < 0 indicates do not wait
max_wait = -1
timeout = 10
retries = 5

try:
max_wait = int(self.ds_cfg.get("max_wait", max_wait))
Expand All @@ -55,7 +56,13 @@ def _get_url_settings(self):
timeout = max(0, int(self.ds_cfg.get("timeout", timeout)))
except Exception:
util.logexc(LOG, "Failed to get timeout, using %s", timeout)
return (max_wait, timeout)

try:
retries = int(self.ds_cfg.get("retries", retries))
except Exception:
util.logexc(LOG, "Failed to get max wait. using %s", retries)

return (max_wait, timeout, retries)

def wait_for_metadata_service(self):
urls = self.ds_cfg.get("metadata_urls", [DEF_MD_URL])
Expand All @@ -76,7 +83,7 @@ def wait_for_metadata_service(self):
md_urls.append(md_url)
url2base[md_url] = url

(max_wait, timeout) = self._get_url_settings()
(max_wait, timeout, retries) = self._get_url_settings()
start_time = time.time()
avail_url = url_helper.wait_for_url(urls=md_urls, max_wait=max_wait,
timeout=timeout)
Expand All @@ -89,13 +96,15 @@ def wait_for_metadata_service(self):
self.metadata_address = url2base.get(avail_url)
return bool(avail_url)

def get_data(self, retries=5, timeout=5):
def get_data(self):
try:
if not self.wait_for_metadata_service():
return False
except IOError:
return False

(max_wait, timeout, retries) = self._get_url_settings()

try:
results = util.log_time(LOG.debug,
'Crawl of openstack metadata service',
Expand Down
8 changes: 4 additions & 4 deletions tests/unittests/test_datasource/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_datasource(self):
None,
helpers.Paths({}))
self.assertIsNone(ds_os.version)
found = ds_os.get_data(timeout=0.1, retries=0)
found = ds_os.get_data()
self.assertTrue(found)
self.assertEqual(2, ds_os.version)
md = dict(ds_os.metadata)
Expand All @@ -256,7 +256,7 @@ def test_bad_datasource_meta(self):
None,
helpers.Paths({}))
self.assertIsNone(ds_os.version)
found = ds_os.get_data(timeout=0.1, retries=0)
found = ds_os.get_data()
self.assertFalse(found)
self.assertIsNone(ds_os.version)

Expand All @@ -275,7 +275,7 @@ def test_no_datasource(self):
'timeout': 0,
}
self.assertIsNone(ds_os.version)
found = ds_os.get_data(timeout=0.1, retries=0)
found = ds_os.get_data()
self.assertFalse(found)
self.assertIsNone(ds_os.version)

Expand All @@ -298,7 +298,7 @@ def test_disabled_datasource(self):
'timeout': 0,
}
self.assertIsNone(ds_os.version)
found = ds_os.get_data(timeout=0.1, retries=0)
found = ds_os.get_data()
self.assertFalse(found)
self.assertIsNone(ds_os.version)

Expand Down

0 comments on commit 4cf53f1

Please sign in to comment.