diff options
author | Joshua Harlow <harlowja@gmail.com> | 2014-02-01 22:48:55 -0800 |
---|---|---|
committer | Joshua Harlow <harlowja@gmail.com> | 2014-02-01 22:48:55 -0800 |
commit | c104d6dfa464a8906c16b4f09b4b76ab5bf2e4e1 (patch) | |
tree | 2d77228d9706ce918b5fcac5cf830cb67b6e618c /cloudinit/url_helper.py | |
parent | b2ebae64a7b9738db6c6408cee4adc2bf2f178de (diff) | |
download | vyos-cloud-init-c104d6dfa464a8906c16b4f09b4b76ab5bf2e4e1.tar.gz vyos-cloud-init-c104d6dfa464a8906c16b4f09b4b76ab5bf2e4e1.zip |
Add a openstack specific datasource
Openstack has a unique derivative datasource
that is gaining usage. Previously the config
drive datasource provided part of this functionality
as well as the ec2 datasource, but since new
functionality is being added to openstack is
seems benefical to combine the used parts into
one datasource just made for handling openstack
deployments.
This patch factors out the common logic shared
between the config drive and the openstack
metadata datasource and places that in a shared
helper file and then creates a new openstack
datasource that readers from the openstack metadata
service and refactors the config drive datasource
to use this common logic.
Diffstat (limited to 'cloudinit/url_helper.py')
-rw-r--r-- | cloudinit/url_helper.py | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py index 19a30409..5c33d1e4 100644 --- a/cloudinit/url_helper.py +++ b/cloudinit/url_helper.py @@ -21,6 +21,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import time +import urllib import requests from requests import exceptions @@ -58,6 +59,23 @@ def _cleanurl(url): return urlunparse(parsed_url) +def combine_url(base, *add_ons): + + def combine_single(url, add_on): + url_parsed = list(urlparse(url)) + path = url_parsed[2] + if path and not path.endswith("/"): + path += "/" + path += urllib.quote(str(add_on), safe="/:") + url_parsed[2] = path + return urlunparse(url_parsed) + + url = base + for add_on in add_ons: + url = combine_single(url, add_on) + return url + + class UrlResponse(object): def __init__(self, response): self._response = response @@ -101,30 +119,52 @@ class UrlError(IOError): self.headers = {} -def readurl(url, data=None, timeout=None, retries=0, sec_between=1, - headers=None, headers_cb=None, ssl_details=None, - check_status=True, allow_redirects=True): - url = _cleanurl(url) - req_args = { - 'url': url, - } +def _get_ssl_args(url, ssl_details): + ssl_args = {} scheme = urlparse(url).scheme # pylint: disable=E1101 if scheme == 'https' and ssl_details: if not SSL_ENABLED: LOG.warn("SSL is not enabled, cert. verification can not occur!") else: if 'ca_certs' in ssl_details and ssl_details['ca_certs']: - req_args['verify'] = ssl_details['ca_certs'] + ssl_args['verify'] = ssl_details['ca_certs'] else: - req_args['verify'] = True + ssl_args['verify'] = True if 'cert_file' in ssl_details and 'key_file' in ssl_details: - req_args['cert'] = [ssl_details['cert_file'], + ssl_args['cert'] = [ssl_details['cert_file'], ssl_details['key_file']] elif 'cert_file' in ssl_details: - req_args['cert'] = str(ssl_details['cert_file']) + ssl_args['cert'] = str(ssl_details['cert_file']) + return ssl_args + +def existsurl(url, ssl_details=None, timeout=None): + r = _readurl(url, ssl_details=ssl_details, timeout=timeout, + method='HEAD', check_status=False) + return r.ok() + + +def readurl(url, data=None, timeout=None, retries=0, sec_between=1, + headers=None, headers_cb=None, ssl_details=None, + check_status=True, allow_redirects=True): + return _readurl(url, data=data, timeout=timeout, retries=retries, + sec_between=sec_between, headers=headers, + headers_cb=headers_cb, ssl_details=ssl_details, + check_status=check_status, + allow_redirects=allow_redirects) + + +def _readurl(url, data=None, timeout=None, retries=0, sec_between=1, + headers=None, headers_cb=None, ssl_details=None, + check_status=True, allow_redirects=True, method='GET'): + url = _cleanurl(url) + req_args = { + 'url': url, + } + req_args.update(_get_ssl_args(url, ssl_details)) + scheme = urlparse(url).scheme # pylint: disable=E1101 req_args['allow_redirects'] = allow_redirects - req_args['method'] = 'GET' + req_args['method'] = method if timeout is not None: req_args['timeout'] = max(float(timeout), 0) if data: |