diff options
Diffstat (limited to 'cloudinit/handlers')
-rw-r--r-- | cloudinit/handlers/__init__.py | 274 | ||||
-rw-r--r-- | cloudinit/handlers/boot_hook.py | 70 | ||||
-rw-r--r-- | cloudinit/handlers/cloud_config.py | 163 | ||||
-rw-r--r-- | cloudinit/handlers/shell_script.py | 55 | ||||
-rw-r--r-- | cloudinit/handlers/upstart_job.py | 119 |
5 files changed, 0 insertions, 681 deletions
diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py deleted file mode 100644 index b6c43ce8..00000000 --- a/cloudinit/handlers/__init__.py +++ /dev/null @@ -1,274 +0,0 @@ -# vi: ts=4 expandtab -# -# Copyright (C) 2012 Canonical Ltd. -# Copyright (C) 2012, 2013 Hewlett-Packard Development Company, L.P. -# Copyright (C) 2012 Yahoo! Inc. -# -# Author: Scott Moser <scott.moser@canonical.com> -# Author: Juerg Haefliger <juerg.haefliger@hp.com> -# Author: Joshua Harlow <harlowja@yahoo-inc.com> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import abc -import os -import six - -from cloudinit.settings import (PER_ALWAYS, PER_INSTANCE, FREQUENCIES) - -from cloudinit import importer -from cloudinit import log as logging -from cloudinit import type_utils -from cloudinit import util - -LOG = logging.getLogger(__name__) - -# Used as the content type when a message is not multipart -# and it doesn't contain its own content-type -NOT_MULTIPART_TYPE = "text/x-not-multipart" - -# When none is assigned this gets used -OCTET_TYPE = 'application/octet-stream' - -# Special content types that signal the start and end of processing -CONTENT_END = "__end__" -CONTENT_START = "__begin__" -CONTENT_SIGNALS = [CONTENT_START, CONTENT_END] - -# Used when a part-handler type is encountered -# to allow for registration of new types. -PART_CONTENT_TYPES = ["text/part-handler"] -PART_HANDLER_FN_TMPL = 'part-handler-%03d' - -# For parts without filenames -PART_FN_TPL = 'part-%03d' - -# Different file beginnings to there content type -INCLUSION_TYPES_MAP = { - '#include': 'text/x-include-url', - '#include-once': 'text/x-include-once-url', - '#!': 'text/x-shellscript', - '#cloud-config': 'text/cloud-config', - '#upstart-job': 'text/upstart-job', - '#part-handler': 'text/part-handler', - '#cloud-boothook': 'text/cloud-boothook', - '#cloud-config-archive': 'text/cloud-config-archive', - '#cloud-config-jsonp': 'text/cloud-config-jsonp', -} - -# Sorted longest first -INCLUSION_SRCH = sorted(list(INCLUSION_TYPES_MAP.keys()), - key=(lambda e: 0 - len(e))) - - -@six.add_metaclass(abc.ABCMeta) -class Handler(object): - - def __init__(self, frequency, version=2): - self.handler_version = version - self.frequency = frequency - - def __repr__(self): - return "%s: [%s]" % (type_utils.obj_name(self), self.list_types()) - - @abc.abstractmethod - def list_types(self): - raise NotImplementedError() - - @abc.abstractmethod - def handle_part(self, *args, **kwargs): - raise NotImplementedError() - - -def run_part(mod, data, filename, payload, frequency, headers): - mod_freq = mod.frequency - if not (mod_freq == PER_ALWAYS or - (frequency == PER_INSTANCE and mod_freq == PER_INSTANCE)): - return - # Sanity checks on version (should be an int convertable) - try: - mod_ver = mod.handler_version - mod_ver = int(mod_ver) - except (TypeError, ValueError, AttributeError): - mod_ver = 1 - content_type = headers['Content-Type'] - try: - LOG.debug("Calling handler %s (%s, %s, %s) with frequency %s", - mod, content_type, filename, mod_ver, frequency) - if mod_ver == 3: - # Treat as v. 3 which does get a frequency + headers - mod.handle_part(data, content_type, filename, - payload, frequency, headers) - elif mod_ver == 2: - # Treat as v. 2 which does get a frequency - mod.handle_part(data, content_type, filename, - payload, frequency) - elif mod_ver == 1: - # Treat as v. 1 which gets no frequency - mod.handle_part(data, content_type, filename, payload) - else: - raise ValueError("Unknown module version %s" % (mod_ver)) - except Exception: - util.logexc(LOG, "Failed calling handler %s (%s, %s, %s) with " - "frequency %s", mod, content_type, filename, mod_ver, - frequency) - - -def call_begin(mod, data, frequency): - # Create a fake header set - headers = { - 'Content-Type': CONTENT_START, - } - run_part(mod, data, None, None, frequency, headers) - - -def call_end(mod, data, frequency): - # Create a fake header set - headers = { - 'Content-Type': CONTENT_END, - } - run_part(mod, data, None, None, frequency, headers) - - -def walker_handle_handler(pdata, _ctype, _filename, payload): - curcount = pdata['handlercount'] - modname = PART_HANDLER_FN_TMPL % (curcount) - frequency = pdata['frequency'] - modfname = os.path.join(pdata['handlerdir'], "%s" % (modname)) - if not modfname.endswith(".py"): - modfname = "%s.py" % (modfname) - # TODO(harlowja): Check if path exists?? - util.write_file(modfname, payload, 0o600) - handlers = pdata['handlers'] - try: - mod = fixup_handler(importer.import_module(modname)) - call_begin(mod, pdata['data'], frequency) - # Only register and increment after the above have worked, so we don't - # register if it fails starting. - handlers.register(mod, initialized=True) - pdata['handlercount'] = curcount + 1 - except Exception: - util.logexc(LOG, "Failed at registering python file: %s (part " - "handler %s)", modfname, curcount) - - -def _extract_first_or_bytes(blob, size): - # Extract the first line or upto X symbols for text objects - # Extract first X bytes for binary objects - try: - if isinstance(blob, six.string_types): - start = blob.split("\n", 1)[0] - else: - # We want to avoid decoding the whole blob (it might be huge) - # By taking 4*size bytes we guarantee to decode size utf8 chars - start = blob[:4 * size].decode(errors='ignore').split("\n", 1)[0] - if len(start) >= size: - start = start[:size] - except UnicodeDecodeError: - # Bytes array doesn't contain text so return chunk of raw bytes - start = blob[0:size] - return start - - -def _escape_string(text): - try: - return text.encode("string_escape") - except (LookupError, TypeError): - try: - # Unicode (and Python 3's str) doesn't support string_escape... - return text.encode('unicode_escape') - except TypeError: - # Give up... - pass - except AttributeError: - # We're in Python3 and received blob as text - # No escaping is needed because bytes are printed - # as 'b\xAA\xBB' automatically in Python3 - pass - return text - - -def walker_callback(data, filename, payload, headers): - content_type = headers['Content-Type'] - if content_type in data.get('excluded'): - LOG.debug('content_type "%s" is excluded', content_type) - return - - if content_type in PART_CONTENT_TYPES: - walker_handle_handler(data, content_type, filename, payload) - return - handlers = data['handlers'] - if content_type in handlers: - run_part(handlers[content_type], data['data'], filename, - payload, data['frequency'], headers) - elif payload: - # Extract the first line or 24 bytes for displaying in the log - start = _extract_first_or_bytes(payload, 24) - details = "'%s...'" % (_escape_string(start)) - if content_type == NOT_MULTIPART_TYPE: - LOG.warning("Unhandled non-multipart (%s) userdata: %s", - content_type, details) - else: - LOG.warning("Unhandled unknown content-type (%s) userdata: %s", - content_type, details) - else: - LOG.debug("Empty payload of type %s", content_type) - - -# Callback is a function that will be called with -# (data, content_type, filename, payload) -def walk(msg, callback, data): - partnum = 0 - for part in msg.walk(): - # multipart/* are just containers - if part.get_content_maintype() == 'multipart': - continue - - ctype = part.get_content_type() - if ctype is None: - ctype = OCTET_TYPE - - filename = part.get_filename() - if not filename: - filename = PART_FN_TPL % (partnum) - - headers = dict(part) - LOG.debug(headers) - headers['Content-Type'] = ctype - payload = util.fully_decoded_payload(part) - callback(data, filename, payload, headers) - partnum = partnum + 1 - - -def fixup_handler(mod, def_freq=PER_INSTANCE): - if not hasattr(mod, "handler_version"): - setattr(mod, "handler_version", 1) - if not hasattr(mod, 'frequency'): - setattr(mod, 'frequency', def_freq) - else: - freq = mod.frequency - if freq and freq not in FREQUENCIES: - LOG.warn("Handler %s has an unknown frequency %s", mod, freq) - return mod - - -def type_from_starts_with(payload, default=None): - try: - payload_lc = util.decode_binary(payload).lower() - except UnicodeDecodeError: - return default - payload_lc = payload_lc.lstrip() - for text in INCLUSION_SRCH: - if payload_lc.startswith(text): - return INCLUSION_TYPES_MAP[text] - return default diff --git a/cloudinit/handlers/boot_hook.py b/cloudinit/handlers/boot_hook.py deleted file mode 100644 index a4ea47ac..00000000 --- a/cloudinit/handlers/boot_hook.py +++ /dev/null @@ -1,70 +0,0 @@ -# vi: ts=4 expandtab -# -# Copyright (C) 2012 Canonical Ltd. -# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. -# Copyright (C) 2012 Yahoo! Inc. -# -# Author: Scott Moser <scott.moser@canonical.com> -# Author: Juerg Haefliger <juerg.haefliger@hp.com> -# Author: Joshua Harlow <harlowja@yahoo-inc.com> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import os - -from cloudinit import handlers -from cloudinit import log as logging -from cloudinit import util - -from cloudinit.settings import (PER_ALWAYS) - -LOG = logging.getLogger(__name__) -BOOTHOOK_PREFIX = "#cloud-boothook" - - -class BootHookPartHandler(handlers.Handler): - def __init__(self, paths, datasource, **_kwargs): - handlers.Handler.__init__(self, PER_ALWAYS) - self.boothook_dir = paths.get_ipath("boothooks") - self.instance_id = None - if datasource: - self.instance_id = datasource.get_instance_id() - - def list_types(self): - return [ - handlers.type_from_starts_with(BOOTHOOK_PREFIX), - ] - - def _write_part(self, payload, filename): - filename = util.clean_filename(filename) - filepath = os.path.join(self.boothook_dir, filename) - contents = util.strip_prefix_suffix(util.dos2unix(payload), - prefix=BOOTHOOK_PREFIX) - util.write_file(filepath, contents.lstrip(), 0o700) - return filepath - - def handle_part(self, data, ctype, filename, payload, frequency): - if ctype in handlers.CONTENT_SIGNALS: - return - - filepath = self._write_part(payload, filename) - try: - env = os.environ.copy() - if self.instance_id is not None: - env['INSTANCE_ID'] = str(self.instance_id) - util.subp([filepath], env=env) - except util.ProcessExecutionError: - util.logexc(LOG, "Boothooks script %s execution error", filepath) - except Exception: - util.logexc(LOG, "Boothooks unknown error when running %s", - filepath) diff --git a/cloudinit/handlers/cloud_config.py b/cloudinit/handlers/cloud_config.py deleted file mode 100644 index cad4dc0f..00000000 --- a/cloudinit/handlers/cloud_config.py +++ /dev/null @@ -1,163 +0,0 @@ -# vi: ts=4 expandtab -# -# Copyright (C) 2012 Canonical Ltd. -# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. -# Copyright (C) 2012 Yahoo! Inc. -# -# Author: Scott Moser <scott.moser@canonical.com> -# Author: Juerg Haefliger <juerg.haefliger@hp.com> -# Author: Joshua Harlow <harlowja@yahoo-inc.com> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import jsonpatch - -from cloudinit import handlers -from cloudinit import log as logging -from cloudinit import mergers -from cloudinit import util - -from cloudinit.settings import (PER_ALWAYS) - -LOG = logging.getLogger(__name__) - -MERGE_HEADER = 'Merge-Type' - -# Due to the way the loading of yaml configuration was done previously, -# where previously each cloud config part was appended to a larger yaml -# file and then finally that file was loaded as one big yaml file we need -# to mimic that behavior by altering the default strategy to be replacing -# keys of prior merges. -# -# -# For example -# #file 1 -# a: 3 -# #file 2 -# a: 22 -# #combined file (comments not included) -# a: 3 -# a: 22 -# -# This gets loaded into yaml with final result {'a': 22} -DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()') -CLOUD_PREFIX = "#cloud-config" -JSONP_PREFIX = "#cloud-config-jsonp" - -# The file header -> content types this module will handle. -CC_TYPES = { - JSONP_PREFIX: handlers.type_from_starts_with(JSONP_PREFIX), - CLOUD_PREFIX: handlers.type_from_starts_with(CLOUD_PREFIX), -} - - -class CloudConfigPartHandler(handlers.Handler): - def __init__(self, paths, **_kwargs): - handlers.Handler.__init__(self, PER_ALWAYS, version=3) - self.cloud_buf = None - self.cloud_fn = paths.get_ipath("cloud_config") - if 'cloud_config_path' in _kwargs: - self.cloud_fn = paths.get_ipath(_kwargs["cloud_config_path"]) - self.file_names = [] - - def list_types(self): - return list(CC_TYPES.values()) - - def _write_cloud_config(self): - if not self.cloud_fn: - return - # Capture which files we merged from... - file_lines = [] - if self.file_names: - file_lines.append("# from %s files" % (len(self.file_names))) - for fn in self.file_names: - if not fn: - fn = '?' - file_lines.append("# %s" % (fn)) - file_lines.append("") - if self.cloud_buf is not None: - # Something was actually gathered.... - lines = [ - CLOUD_PREFIX, - '', - ] - lines.extend(file_lines) - lines.append(util.yaml_dumps(self.cloud_buf)) - else: - lines = [] - util.write_file(self.cloud_fn, "\n".join(lines), 0o600) - - def _extract_mergers(self, payload, headers): - merge_header_headers = '' - for h in [MERGE_HEADER, 'X-%s' % (MERGE_HEADER)]: - tmp_h = headers.get(h, '') - if tmp_h: - merge_header_headers = tmp_h - break - # Select either the merge-type from the content - # or the merge type from the headers or default to our own set - # if neither exists (or is empty) from the later. - payload_yaml = util.load_yaml(payload) - mergers_yaml = mergers.dict_extract_mergers(payload_yaml) - mergers_header = mergers.string_extract_mergers(merge_header_headers) - all_mergers = [] - all_mergers.extend(mergers_yaml) - all_mergers.extend(mergers_header) - if not all_mergers: - all_mergers = DEF_MERGERS - return (payload_yaml, all_mergers) - - def _merge_patch(self, payload): - # JSON doesn't handle comments in this manner, so ensure that - # if we started with this 'type' that we remove it before - # attempting to load it as json (which the jsonpatch library will - # attempt to do). - payload = payload.lstrip() - payload = util.strip_prefix_suffix(payload, prefix=JSONP_PREFIX) - patch = jsonpatch.JsonPatch.from_string(payload) - LOG.debug("Merging by applying json patch %s", patch) - self.cloud_buf = patch.apply(self.cloud_buf, in_place=False) - - def _merge_part(self, payload, headers): - (payload_yaml, my_mergers) = self._extract_mergers(payload, headers) - LOG.debug("Merging by applying %s", my_mergers) - merger = mergers.construct(my_mergers) - self.cloud_buf = merger.merge(self.cloud_buf, payload_yaml) - - def _reset(self): - self.file_names = [] - self.cloud_buf = None - - def handle_part(self, data, ctype, filename, payload, frequency, headers): - if ctype == handlers.CONTENT_START: - self._reset() - return - if ctype == handlers.CONTENT_END: - self._write_cloud_config() - self._reset() - return - try: - # First time through, merge with an empty dict... - if self.cloud_buf is None or not self.file_names: - self.cloud_buf = {} - if ctype == CC_TYPES[JSONP_PREFIX]: - self._merge_patch(payload) - else: - self._merge_part(payload, headers) - # Ensure filename is ok to store - for i in ("\n", "\r", "\t"): - filename = filename.replace(i, " ") - self.file_names.append(filename.strip()) - except Exception: - util.logexc(LOG, "Failed at merging in cloud config part from %s", - filename) diff --git a/cloudinit/handlers/shell_script.py b/cloudinit/handlers/shell_script.py deleted file mode 100644 index b5087693..00000000 --- a/cloudinit/handlers/shell_script.py +++ /dev/null @@ -1,55 +0,0 @@ -# vi: ts=4 expandtab -# -# Copyright (C) 2012 Canonical Ltd. -# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. -# Copyright (C) 2012 Yahoo! Inc. -# -# Author: Scott Moser <scott.moser@canonical.com> -# Author: Juerg Haefliger <juerg.haefliger@hp.com> -# Author: Joshua Harlow <harlowja@yahoo-inc.com> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -import os - -from cloudinit import handlers -from cloudinit import log as logging -from cloudinit import util - -from cloudinit.settings import (PER_ALWAYS) - -LOG = logging.getLogger(__name__) -SHELL_PREFIX = "#!" - - -class ShellScriptPartHandler(handlers.Handler): - def __init__(self, paths, **_kwargs): - handlers.Handler.__init__(self, PER_ALWAYS) - self.script_dir = paths.get_ipath_cur('scripts') - if 'script_path' in _kwargs: - self.script_dir = paths.get_ipath_cur(_kwargs['script_path']) - - def list_types(self): - return [ - handlers.type_from_starts_with(SHELL_PREFIX), - ] - - def handle_part(self, data, ctype, filename, payload, frequency): - if ctype in handlers.CONTENT_SIGNALS: - # TODO(harlowja): maybe delete existing things here - return - - filename = util.clean_filename(filename) - payload = util.dos2unix(payload) - path = os.path.join(self.script_dir, filename) - util.write_file(path, payload, 0o700) diff --git a/cloudinit/handlers/upstart_job.py b/cloudinit/handlers/upstart_job.py deleted file mode 100644 index ab381e00..00000000 --- a/cloudinit/handlers/upstart_job.py +++ /dev/null @@ -1,119 +0,0 @@ -# vi: ts=4 expandtab -# -# Copyright (C) 2012 Canonical Ltd. -# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. -# Copyright (C) 2012 Yahoo! Inc. -# -# Author: Scott Moser <scott.moser@canonical.com> -# Author: Juerg Haefliger <juerg.haefliger@hp.com> -# Author: Joshua Harlow <harlowja@yahoo-inc.com> -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 3, as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - - -import os -import re - -from cloudinit import handlers -from cloudinit import log as logging -from cloudinit import util - -from cloudinit.settings import (PER_INSTANCE) - -LOG = logging.getLogger(__name__) -UPSTART_PREFIX = "#upstart-job" - - -class UpstartJobPartHandler(handlers.Handler): - def __init__(self, paths, **_kwargs): - handlers.Handler.__init__(self, PER_INSTANCE) - self.upstart_dir = paths.upstart_conf_d - - def list_types(self): - return [ - handlers.type_from_starts_with(UPSTART_PREFIX), - ] - - def handle_part(self, data, ctype, filename, payload, frequency): - if ctype in handlers.CONTENT_SIGNALS: - return - - # See: https://bugs.launchpad.net/bugs/819507 - if frequency != PER_INSTANCE: - return - - if not self.upstart_dir: - return - - filename = util.clean_filename(filename) - (_name, ext) = os.path.splitext(filename) - if not ext: - ext = '' - ext = ext.lower() - if ext != ".conf": - filename = filename + ".conf" - - payload = util.dos2unix(payload) - path = os.path.join(self.upstart_dir, filename) - util.write_file(path, payload, 0o644) - - if SUITABLE_UPSTART: - util.subp(["initctl", "reload-configuration"], capture=False) - - -def _has_suitable_upstart(): - # (LP: #1124384) - # a bug in upstart means that invoking reload-configuration - # at this stage in boot causes havoc. So, try to determine if upstart - # is installed, and reloading configuration is OK. - if not os.path.exists("/sbin/initctl"): - return False - try: - (version_out, _err) = util.subp(["initctl", "version"]) - except Exception: - util.logexc(LOG, "initctl version failed") - return False - - # expecting 'initctl version' to output something like: init (upstart X.Y) - if re.match("upstart 1.[0-7][)]", version_out): - return False - if "upstart 0." in version_out: - return False - elif "upstart 1.8" in version_out: - if not os.path.exists("/usr/bin/dpkg-query"): - return False - try: - (dpkg_ver, _err) = util.subp(["dpkg-query", - "--showformat=${Version}", - "--show", "upstart"], rcs=[0, 1]) - except Exception: - util.logexc(LOG, "dpkg-query failed") - return False - - try: - good = "1.8-0ubuntu1.2" - util.subp(["dpkg", "--compare-versions", dpkg_ver, "ge", good]) - return True - except util.ProcessExecutionError as e: - if e.exit_code is 1: - pass - else: - util.logexc(LOG, "dpkg --compare-versions failed [%s]", - e.exit_code) - except Exception as e: - util.logexc(LOG, "dpkg --compare-versions failed") - return False - else: - return True - -SUITABLE_UPSTART = _has_suitable_upstart() |