Commit 7f94f5b5 authored by Joshua Harlow's avatar Joshua Harlow

1. Modify the check http code function to allow or not allow redirect status...

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
parent 7b3bf464
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment