From ab6f166da7290928d56ff3c62a5280536e1d241f Mon Sep 17 00:00:00 2001 From: root Date: Wed, 2 Mar 2016 08:51:16 +0000 Subject: Added Bigstep datasource. --- cloudinit/sources/DataSourceBigstep.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 cloudinit/sources/DataSourceBigstep.py (limited to 'cloudinit/sources/DataSourceBigstep.py') diff --git a/cloudinit/sources/DataSourceBigstep.py b/cloudinit/sources/DataSourceBigstep.py new file mode 100644 index 00000000..67d43eb3 --- /dev/null +++ b/cloudinit/sources/DataSourceBigstep.py @@ -0,0 +1,48 @@ +# +# Copyright (C) 2015-2016 Bigstep Cloud Ltd. +# +# Author: Alexandru Sirbu +# + +import json + +from cloudinit import log as logging +from cloudinit import sources +from cloudinit import util +from cloudinit import url_helper + +LOG = logging.getLogger(__name__) + + +class DataSourceBigstep(sources.DataSource): + def __init__(self, sys_cfg, distro, paths): + sources.DataSource.__init__(self, sys_cfg, distro, paths) + self.metadata = {} + self.vendordata_raw = "" + self.userdata_raw = "" + + + def get_data(self, apply_filter=False): + url = get_url_from_file() + response = url_helper.readurl(url) + decoded = json.loads(response.contents) + self.metadata = decoded["metadata"] + self.vendordata_raw = decoded["vendordata_raw"] + self.userdata_raw = decoded["userdata_raw"] + return True + + +def get_url_from_file(): + content = util.load_file("/var/lib/cloud/data/seed/bigstep/url") + return content + +# Used to match classes to dependencies +datasources = [ + (DataSourceBigstep, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)), +] + + +# Return a list of data sources that match this set of dependencies +def get_datasource_list(depends): + return sources.list_from_depends(depends, datasources) + -- cgit v1.2.3 From d5d89cfb1e61e6cc3f732a18ec1aa4d2b288489d Mon Sep 17 00:00:00 2001 From: root Date: Wed, 2 Mar 2016 08:53:47 +0000 Subject: Pep8 changes to Bigstep datasource. --- cloudinit/sources/DataSourceBigstep.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'cloudinit/sources/DataSourceBigstep.py') diff --git a/cloudinit/sources/DataSourceBigstep.py b/cloudinit/sources/DataSourceBigstep.py index 67d43eb3..c22ffdb6 100644 --- a/cloudinit/sources/DataSourceBigstep.py +++ b/cloudinit/sources/DataSourceBigstep.py @@ -21,7 +21,6 @@ class DataSourceBigstep(sources.DataSource): self.vendordata_raw = "" self.userdata_raw = "" - def get_data(self, apply_filter=False): url = get_url_from_file() response = url_helper.readurl(url) @@ -45,4 +44,3 @@ datasources = [ # Return a list of data sources that match this set of dependencies def get_datasource_list(depends): return sources.list_from_depends(depends, datasources) - -- cgit v1.2.3 From 9ec6c876b72ccfa2ae590505fe6dbf7c0c561520 Mon Sep 17 00:00:00 2001 From: Alex Sirbu Date: Mon, 7 Mar 2016 09:33:40 +0000 Subject: Returning false if file does not exist, instead of throwing error --- cloudinit/sources/DataSourceBigstep.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'cloudinit/sources/DataSourceBigstep.py') diff --git a/cloudinit/sources/DataSourceBigstep.py b/cloudinit/sources/DataSourceBigstep.py index c22ffdb6..2d66c609 100644 --- a/cloudinit/sources/DataSourceBigstep.py +++ b/cloudinit/sources/DataSourceBigstep.py @@ -5,6 +5,7 @@ # import json +import errno from cloudinit import log as logging from cloudinit import sources @@ -22,7 +23,13 @@ class DataSourceBigstep(sources.DataSource): self.userdata_raw = "" def get_data(self, apply_filter=False): - url = get_url_from_file() + try: + url = get_url_from_file() + except IOError as e: + if e.errno == errno.ENOENT: + return False + else: + raise response = url_helper.readurl(url) decoded = json.loads(response.contents) self.metadata = decoded["metadata"] -- cgit v1.2.3 From d23868d6d3e35a91c348b94ce8416f56514aaf15 Mon Sep 17 00:00:00 2001 From: Alex Sirbu Date: Mon, 7 Mar 2016 12:30:08 +0000 Subject: Implemented review concerning position of try and more information about the caught exception. --- cloudinit/sources/DataSourceBigstep.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'cloudinit/sources/DataSourceBigstep.py') diff --git a/cloudinit/sources/DataSourceBigstep.py b/cloudinit/sources/DataSourceBigstep.py index 2d66c609..b5ee4129 100644 --- a/cloudinit/sources/DataSourceBigstep.py +++ b/cloudinit/sources/DataSourceBigstep.py @@ -23,13 +23,9 @@ class DataSourceBigstep(sources.DataSource): self.userdata_raw = "" def get_data(self, apply_filter=False): - try: - url = get_url_from_file() - except IOError as e: - if e.errno == errno.ENOENT: - return False - else: - raise + url = get_url_from_file() + if url is None: + return False response = url_helper.readurl(url) decoded = json.loads(response.contents) self.metadata = decoded["metadata"] @@ -39,7 +35,15 @@ class DataSourceBigstep(sources.DataSource): def get_url_from_file(): - content = util.load_file("/var/lib/cloud/data/seed/bigstep/url") + try: + content = util.load_file("/var/lib/cloud/data/seed/bigstep/url") + except IOError as e: + # If the file doesn't exist, then the server probably isn't a Bigstep + # instance; otherwise, another problem exists which needs investigation + if e.errno == errno.ENOENT: + return None + else: + raise return content # Used to match classes to dependencies -- cgit v1.2.3