diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 18:24:40 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 18:24:40 -0700 |
commit | 7f94f5b510c2ed8f0d449a7eaa9b7f15e041bf44 (patch) | |
tree | 855674e0526000b7fce8876d99f7ed6e0ee87f01 /cloudinit/url_helper.py | |
parent | 7b3bf46487e599b375acfdf99176294810805ef0 (diff) | |
download | vyos-cloud-init-7f94f5b510c2ed8f0d449a7eaa9b7f15e041bf44.tar.gz vyos-cloud-init-7f94f5b510c2ed8f0d449a7eaa9b7f15e041bf44.zip |
1. Modify the check http code function to allow or not allow redirect status from being valid codes.
2. Log how much we will sleep for
3. If not headers are provided, add a set which will have a cloud init user agent + version
Diffstat (limited to 'cloudinit/url_helper.py')
-rw-r--r-- | cloudinit/url_helper.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index 1bf24c4f..7ae0226a 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -29,12 +29,16 @@ import urllib import urllib2 from cloudinit import log as logging +from cloudinit import version LOG = logging.getLogger(__name__) -def ok_http_code(st): - return st in xrange(200, 400) +def ok_http_code(st, redirects_ok=False): + if redirects_ok: + return st in xrange(200, 400) + else: + return st in xrange(200, 300) def readurl(url, data=None, timeout=None, @@ -44,8 +48,13 @@ def readurl(url, data=None, timeout=None, req_args['url'] = url if data is not None: req_args['data'] = urllib.urlencode(data) - if headers is not None: - req_args['headers'] = dict(headers) + + if not headers: + headers = { + 'User-Agent': 'Cloud-Init/%s' % (version.version_string()), + } + + req_args['headers'] = headers req = urllib2.Request(**req_args) retries = max(retries, 0) @@ -70,19 +79,17 @@ def readurl(url, data=None, timeout=None, return (content, status) except urllib2.HTTPError as e: last_excp = e - LOG.exception("Failed at reading from %s.", url) except urllib2.URLError as e: # This can be a message string or # another exception instance # (socket.error for remote URLs, OSError for local URLs). - if (isinstance(e.reason, OSError) and + if (isinstance(e.reason, (OSError)) and e.reason.errno == errno.ENOENT): last_excp = e.reason else: last_excp = e - LOG.exception("Failed at reading from %s", url) if i + 1 < attempts: - LOG.info("Please wait %s seconds while we wait to try again", + LOG.debug("Please wait %s seconds while we wait to try again", sec_between) time.sleep(sec_between) |