summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorharlowja <harlowja@virtualbox.rhel>2013-02-23 21:23:24 -0800
committerharlowja <harlowja@virtualbox.rhel>2013-02-23 21:23:24 -0800
commiteacfc7ffbec3e6a0348ed484da895e2d2fc5ba10 (patch)
treee63c8f74a34c6250764136865c542af6ba78d989 /cloudinit
parent46a7a39775ed8f745ec8b63a9563f3ae6337d845 (diff)
downloadvyos-cloud-init-eacfc7ffbec3e6a0348ed484da895e2d2fc5ba10.tar.gz
vyos-cloud-init-eacfc7ffbec3e6a0348ed484da895e2d2fc5ba10.zip
Get tests working and further adjustments.
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/sources/DataSourceMAAS.py9
-rw-r--r--cloudinit/url_helper.py21
-rw-r--r--cloudinit/user_data.py8
-rw-r--r--cloudinit/util.py34
4 files changed, 48 insertions, 24 deletions
diff --git a/cloudinit/sources/DataSourceMAAS.py b/cloudinit/sources/DataSourceMAAS.py
index 6e1133b2..0c526305 100644
--- a/cloudinit/sources/DataSourceMAAS.py
+++ b/cloudinit/sources/DataSourceMAAS.py
@@ -223,9 +223,12 @@ def read_maas_seed_url(seed_url, header_cb=None, timeout=None,
else:
headers = {}
try:
- resp = util.read_file_or_url(url, headers=headers, timeout=timeout,
- ssl_details=util.fetch_ssl_details(paths))
- if resp.ok:
+ ssl_details = util.fetch_ssl_details(paths)
+ resp = util.read_file_or_url(url,
+ headers=headers,
+ timeout=timeout,
+ ssl_details=ssl_details)
+ if resp.ok():
md[name] = str(resp)
else:
LOG.warn(("Fetching from %s resulted in"
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
index 0839e63b..300e70c2 100644
--- a/cloudinit/url_helper.py
+++ b/cloudinit/url_helper.py
@@ -70,9 +70,14 @@ class UrlResponse(object):
def url(self):
return self._response.url
- @property
- def ok(self):
- return self._response.ok
+ def ok(self, redirects_ok=False):
+ upper = 300
+ if redirects_ok:
+ upper = 400
+ if self.code >= 200 and self.code < upper:
+ return True
+ else:
+ return False
@property
def headers(self):
@@ -158,11 +163,8 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
r = requests.request(**req_args)
if check_status:
r.raise_for_status()
- contents = r.content
- status = r.status_code
- headers = r.headers
LOG.debug("Read from %s (%s, %sb) after %s attempts", url,
- status, len(contents), (i + 1))
+ r.status_code, len(r.content), (i + 1))
# Doesn't seem like we can make it use a different
# subclass for responses, so add our own backward-compat
# attrs
@@ -256,8 +258,9 @@ def wait_for_url(urls, max_wait=None, timeout=None,
time_taken = int(time.time() - start_time)
status_msg = "Calling '%s' failed [%s/%ss]: %s" % (url,
- time_taken,
- max_wait, reason)
+ time_taken,
+ max_wait,
+ reason)
status_cb(status_msg)
if exception_cb:
exception_cb(msg=status_msg, exception=e)
diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py
index eaf448a7..df069ff8 100644
--- a/cloudinit/user_data.py
+++ b/cloudinit/user_data.py
@@ -59,6 +59,7 @@ EXAMINE_FOR_LAUNCH_INDEX = ["text/cloud-config"]
class UserDataProcessor(object):
def __init__(self, paths):
self.paths = paths
+ self.ssl_details = util.fetch_ssl_details(paths)
def process(self, blob):
accumulating_msg = MIMEMultipart()
@@ -172,10 +173,11 @@ class UserDataProcessor(object):
if include_once_on and os.path.isfile(include_once_fn):
content = util.load_file(include_once_fn)
else:
- resp = util.read_file_or_url(include_url)
- if include_once_on and resp.ok:
+ resp = util.read_file_or_url(include_url,
+ ssl_details=self.ssl_details)
+ if include_once_on and resp.ok():
util.write_file(include_once_fn, str(resp), mode=0600)
- if resp.ok:
+ if resp.ok():
content = str(resp)
else:
LOG.warn(("Fetching from %s resulted in"
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 42b3ab01..dc3c5639 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -70,18 +70,31 @@ FN_ALLOWED = ('_-.()' + string.digits + string.ascii_letters)
CONTAINER_TESTS = ['running-in-container', 'lxc-is-container']
-class FileResponse(object):
- def __init__(self, path, contents):
- self.code = 200
+# Made to have same accessors as UrlResponse so that the
+# read_file_or_url can return this or that object and the
+# 'user' of those objects will not need to know the difference.
+class StringResponse(object):
+ def __init__(self, contents, code=200):
+ self.code = code
self.headers = {}
self.contents = contents
- self.ok = True
- self.url = path
+ self.url = None
+
+ def ok(self, *args, **kwargs):
+ if self.code != 200:
+ return False
+ return True
def __str__(self):
return self.contents
+class FileResponse(StringResponse):
+ def __init__(self, path, contents, code=200):
+ StringResponse.__init__(self, contents, code=code)
+ self.url = path
+
+
class ProcessExecutionError(IOError):
MESSAGE_TMPL = ('%(description)s\n'
@@ -630,7 +643,7 @@ def read_optional_seed(fill, base="", ext="", timeout=5):
fill['user-data'] = ud
fill['meta-data'] = md
return True
- except OSError as e:
+ except IOError as e:
if e.errno == errno.ENOENT:
return False
raise
@@ -670,9 +683,12 @@ def fetch_ssl_details(paths=None):
def read_file_or_url(url, timeout=5, retries=10,
headers=None, data=None, sec_between=1, ssl_details=None):
+ url = url.lstrip()
if url.startswith("/"):
url = "file://%s" % url
if url.lower().startswith("file://"):
+ if data:
+ LOG.warn("Unable to post data to file resource %s", url)
file_path = url[len("file://"):]
return FileResponse(file_path, contents=load_file(file_path))
else:
@@ -724,13 +740,13 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0):
md_resp = read_file_or_url(md_url, timeout, retries, file_retries)
md = None
- if md_resp.ok:
+ if md_resp.ok():
md_str = str(md_resp)
md = load_yaml(md_str, default={})
ud_resp = read_file_or_url(ud_url, timeout, retries, file_retries)
ud = None
- if ud_resp.ok:
+ if ud_resp.ok():
ud_str = str(ud_resp)
ud = ud_str
@@ -900,7 +916,7 @@ def get_cmdline_url(names=('cloud-config-url', 'url'),
return (None, None, None)
resp = read_file_or_url(url)
- if resp.contents.startswith(starts) and resp.ok:
+ if resp.contents.startswith(starts) and resp.ok():
return (key, url, str(resp))
return (key, url, None)