From 7f94f5b510c2ed8f0d449a7eaa9b7f15e041bf44 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 15 Jun 2012 18:24:40 -0700 Subject: 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 --- cloudinit/url_helper.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'cloudinit/url_helper.py') 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) -- cgit v1.2.3