diff options
-rw-r--r-- | cloudinit/handling.py | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/cloudinit/handling.py b/cloudinit/handling.py index 553abe4f..8f6424e3 100644 --- a/cloudinit/handling.py +++ b/cloudinit/handling.py @@ -7,29 +7,7 @@ from cloudinit.constants import (PER_INSTANCE, PER_ALWAYS) LOG = logging.getLogger(__name__) -class InternalPartHandler: - freq = PER_INSTANCE - mtypes = [] - handler_version = 1 - handler = None - - def __init__(self, handler, mtypes, frequency, version=2): - self.handler = handler - self.mtypes = mtypes - self.frequency = frequency - self.handler_version = version - - def __repr__(self): - return("InternalPartHandler: [%s]" % self.mtypes) - - def list_types(self): - return(self.mtypes) - - def handle_part(self, data, ctype, filename, payload, frequency): - return(self.handler(data, ctype, filename, payload, frequency)) - - -def handler_register(mod, part_handlers, data, frequency=PER_INSTANCE): +def handler_register(mod, part_handlers, data, frequency=per_instance): if not hasattr(mod, "handler_version"): setattr(mod, "handler_version", 1) @@ -37,7 +15,7 @@ def handler_register(mod, part_handlers, data, frequency=PER_INSTANCE): part_handlers[mtype] = mod handler_call_begin(mod, data, frequency) - return mod + return(mod) def handler_call_begin(mod, data, frequency): @@ -50,9 +28,9 @@ def handler_call_end(mod, data, frequency): def handler_handle_part(mod, data, ctype, filename, payload, frequency): # only add the handler if the module should run - modfreq = getattr(mod, "frequency", PER_INSTANCE) - if not (modfreq == PER_ALWAYS or - (frequency == PER_INSTANCE and modfreq == PER_INSTANCE)): + modfreq = getattr(mod, "frequency", per_instance) + if not (modfreq == per_always or + (frequency == per_instance and modfreq == per_instance)): return try: if mod.handler_version == 1: @@ -70,14 +48,15 @@ def partwalker_handle_handler(pdata, _ctype, _filename, payload): frequency = pdata['frequency'] modfname = modname + ".py" - util.write_file(os.path.join(pdata['handlerdir'], modfname), payload, 0600) + util.write_file("%s/%s" % (pdata['handlerdir'], modfname), payload, 0600) try: - mod = importer.import_module(modname) + mod = __import__(modname) handler_register(mod, pdata['handlers'], pdata['data'], frequency) pdata['handlercount'] = curcount + 1 except: - LOG.exception("Could not import module %s", modname) + util.logexc(log) + traceback.print_exc(file=sys.stderr) def partwalker_callback(pdata, ctype, filename, payload): @@ -95,7 +74,29 @@ def partwalker_callback(pdata, ctype, filename, payload): details = "starting '%s...'" % start.encode("string-escape") else: details = repr(payload) - LOG.warning("Unhandled non-multipart userdata %s", details) + log.warning("Unhandled non-multipart userdata %s", details) return handler_handle_part(pdata['handlers'][ctype], pdata['data'], ctype, filename, payload, pdata['frequency']) + + +class InternalPartHandler: + freq = per_instance + mtypes = [] + handler_version = 1 + handler = None + + def __init__(self, handler, mtypes, frequency, version=2): + self.handler = handler + self.mtypes = mtypes + self.frequency = frequency + self.handler_version = version + + def __repr__(self): + return("InternalPartHandler: [%s]" % self.mtypes) + + def list_types(self): + return(self.mtypes) + + def handle_part(self, data, ctype, filename, payload, frequency): + return(self.handler(data, ctype, filename, payload, frequency)) |