diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-07 12:48:22 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-07 12:48:22 -0700 |
commit | 58e73f5ab22c5dcd1c10ca873028e779ef2470a7 (patch) | |
tree | de37d1a427458b4c2a57778fac837a075616203c | |
parent | 869402301c9793cece24a9357ee3c13dcdafb6e2 (diff) | |
download | vyos-cloud-init-58e73f5ab22c5dcd1c10ca873028e779ef2470a7.tar.gz vyos-cloud-init-58e73f5ab22c5dcd1c10ca873028e779ef2470a7.zip |
Add a file that just deals with handling modules and part data
-rw-r--r-- | cloudinit/handling.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/cloudinit/handling.py b/cloudinit/handling.py new file mode 100644 index 00000000..553abe4f --- /dev/null +++ b/cloudinit/handling.py @@ -0,0 +1,101 @@ +import os + +from cloudinit import importer +from cloudinit import log as logging +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): + if not hasattr(mod, "handler_version"): + setattr(mod, "handler_version", 1) + + for mtype in mod.list_types(): + part_handlers[mtype] = mod + + handler_call_begin(mod, data, frequency) + return mod + + +def handler_call_begin(mod, data, frequency): + handler_handle_part(mod, data, "__begin__", None, None, frequency) + + +def handler_call_end(mod, data, frequency): + handler_handle_part(mod, data, "__end__", None, None, 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)): + return + try: + if mod.handler_version == 1: + mod.handle_part(data, ctype, filename, payload) + else: + mod.handle_part(data, ctype, filename, payload, frequency) + except: + util.logexc(log) + traceback.print_exc(file=sys.stderr) + + +def partwalker_handle_handler(pdata, _ctype, _filename, payload): + curcount = pdata['handlercount'] + modname = 'part-handler-%03d' % curcount + frequency = pdata['frequency'] + + modfname = modname + ".py" + util.write_file(os.path.join(pdata['handlerdir'], modfname), payload, 0600) + + try: + mod = importer.import_module(modname) + handler_register(mod, pdata['handlers'], pdata['data'], frequency) + pdata['handlercount'] = curcount + 1 + except: + LOG.exception("Could not import module %s", modname) + + +def partwalker_callback(pdata, ctype, filename, payload): + # data here is the part_handlers array and then the data to pass through + if ctype == "text/part-handler": + if 'handlercount' not in pdata: + pdata['handlercount'] = 0 + partwalker_handle_handler(pdata, ctype, filename, payload) + return + if ctype not in pdata['handlers']: + if ctype == "text/x-not-multipart": + # Extract the first line or 24 bytes for displaying in the log + start = payload.split("\n", 1)[0][:24] + if start < payload: + details = "starting '%s...'" % start.encode("string-escape") + else: + details = repr(payload) + LOG.warning("Unhandled non-multipart userdata %s", details) + return + handler_handle_part(pdata['handlers'][ctype], pdata['data'], + ctype, filename, payload, pdata['frequency']) |