diff options
-rw-r--r-- | cloudinit/sources/DataSourceMAAS.py | 20 | ||||
-rw-r--r-- | cloudinit/url_helper.py | 8 |
2 files changed, 19 insertions, 9 deletions
diff --git a/cloudinit/sources/DataSourceMAAS.py b/cloudinit/sources/DataSourceMAAS.py index 6c95c218..cfc59ca5 100644 --- a/cloudinit/sources/DataSourceMAAS.py +++ b/cloudinit/sources/DataSourceMAAS.py @@ -88,6 +88,10 @@ class DataSourceMAAS(sources.DataSource): return False try: + # doing this here actually has a side affect of + # getting oauth time-fix in place. As no where else would + # retry by default, so even if we could fix the timestamp + # we would not. if not self.wait_for_metadata_service(url): return False @@ -95,7 +99,7 @@ class DataSourceMAAS(sources.DataSource): (userdata, metadata) = read_maas_seed_url( self.base_url, read_file_or_url=self.oauth_helper.readurl, - paths=self.paths) + paths=self.paths, retries=1) self.userdata_raw = userdata self.metadata = metadata return True @@ -161,7 +165,7 @@ def read_maas_seed_dir(seed_d): def read_maas_seed_url(seed_url, read_file_or_url=None, timeout=None, - version=MD_VERSION, paths=None): + version=MD_VERSION, paths=None, retries=None): """ Read the maas datasource at seed_url. read_file_or_url is a method that should provide an interface @@ -193,13 +197,13 @@ def read_maas_seed_url(seed_url, read_file_or_url=None, timeout=None, for name in file_order: url = files.get(name) if name == 'user-data': - retries = 0 + item_retries = 0 else: - retries = None + item_retries = retries try: ssl_details = util.fetch_ssl_details(paths) - resp = read_file_or_url(url, retries=retries, + resp = read_file_or_url(url, retries=item_retries, timeout=timeout, ssl_details=ssl_details) if resp.ok(): if name in BINARY_FIELDS: @@ -306,7 +310,8 @@ if __name__ == "__main__": oauth_helper = url_helper.OauthUrlHelper(**creds) def geturl(url): - return oauth_helper.readurl(url).contents + # the retry is to ensure that oauth timestamp gets fixed + return oauth_helper.readurl(url, retries=1).contents def printurl(url): print("== %s ==\n%s\n" % (url, geturl(url).decode())) @@ -329,7 +334,8 @@ if __name__ == "__main__": if args.url[0] == "/" or args.url.startswith("file://"): readurl = None (userdata, metadata) = read_maas_seed_url( - args.url, version=args.apiver, read_file_or_url=readurl) + args.url, version=args.apiver, read_file_or_url=readurl, + retries=2) print("=== userdata ===") print(userdata.decode()) print("=== metadata ===") diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index a93847ce..f2e1390e 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -264,7 +264,9 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1, # ssl exceptions are not going to get fixed by waiting a # few seconds break - if exception_cb and not exception_cb(req_args.copy(), excps[-1]): + if exception_cb and exception_cb(req_args.copy(), excps[-1]): + # if an exception callback was given it should return None + # a true-ish value means to break and re-raise the exception break if i + 1 < manual_tries and sec_between > 0: LOG.debug("Please wait %s seconds while we wait to try again", @@ -404,7 +406,7 @@ class OauthUrlHelper(object): def read_skew_file(self): if self.skew_data_file and os.path.isfile(self.skew_data_file): with open(self.skew_data_file, mode="r") as fp: - return json.load(fp.read()) + return json.load(fp) return None def update_skew_file(self, host, value): @@ -412,6 +414,8 @@ class OauthUrlHelper(object): if not self.skew_data_file: return cur = self.read_skew_file() + if cur is None: + cur = {} cur[host] = value with open(self.skew_data_file, mode="w") as fp: fp.write(json.dumps(cur)) |