def handle_part(self, data, ctype, filename, payload, frequency, headers): if ctype in handlers.CONTENT_SIGNALS: return jinja_json_file = os.path.join(self.paths.run_dir, INSTANCE_JSON_SENSITIVE_FILE) rendered_payload = render_jinja_payload_from_file( payload, filename, jinja_json_file) if not rendered_payload: return subtype = handlers.type_from_starts_with(rendered_payload) sub_handler = self.sub_handlers.get(subtype) if not sub_handler: LOG.warning( "Ignoring jinja template for %s. Could not find supported" " sub-handler for type %s", filename, subtype, ) return if sub_handler.handler_version == 3: sub_handler.handle_part(data, ctype, filename, rendered_payload, frequency, headers) elif sub_handler.handler_version == 2: sub_handler.handle_part(data, ctype, filename, rendered_payload, frequency)
def _explode_archive(self, archive, append_msg): entries = util.load_yaml(archive, default=[], allowed=(list, set)) for ent in entries: # ent can be one of: # dict { 'filename' : 'value', 'content' : # 'value', 'type' : 'value' } # filename and type not be present # or # scalar(payload) if isinstance(ent, six.string_types): ent = {"content": ent} if not isinstance(ent, (dict)): # TODO(harlowja) raise? continue content = ent.get("content", "") mtype = ent.get("type") if not mtype: default = ARCHIVE_UNDEF_TYPE if isinstance(content, six.binary_type): default = ARCHIVE_UNDEF_BINARY_TYPE mtype = handlers.type_from_starts_with(content, default) maintype, subtype = mtype.split("/", 1) if maintype == "text": if isinstance(content, six.binary_type): content = content.decode() msg = MIMEText(content, _subtype=subtype) else: msg = MIMEBase(maintype, subtype) msg.set_payload(content) if "filename" in ent: _set_filename(msg, ent["filename"]) if "launch-index" in ent: msg.add_header("Launch-Index", str(ent["launch-index"])) for header in list(ent.keys()): if header.lower() in ( "content", "filename", "type", "launch-index", "content-disposition", ATTACHMENT_FIELD.lower(), CONTENT_TYPE.lower(), ): continue msg.add_header(header, ent[header]) self._attach_part(append_msg, msg)
def _explode_archive(self, archive, append_msg): entries = util.load_yaml(archive, default=[], allowed=(list, set)) for ent in entries: # ent can be one of: # dict { 'filename' : 'value', 'content' : # 'value', 'type' : 'value' } # filename and type not be present # or # scalar(payload) if isinstance(ent, six.string_types): ent = {'content': ent} if not isinstance(ent, (dict)): # TODO(harlowja) raise? continue content = ent.get('content', '') mtype = ent.get('type') if not mtype: default = ARCHIVE_UNDEF_TYPE if isinstance(content, six.binary_type): default = ARCHIVE_UNDEF_BINARY_TYPE mtype = handlers.type_from_starts_with(content, default) maintype, subtype = mtype.split('/', 1) if maintype == "text": if isinstance(content, six.binary_type): content = content.decode() msg = MIMEText(content, _subtype=subtype) else: msg = MIMEBase(maintype, subtype) msg.set_payload(content) if 'filename' in ent: _set_filename(msg, ent['filename']) if 'launch-index' in ent: msg.add_header('Launch-Index', str(ent['launch-index'])) for header in list(ent.keys()): if header.lower() in ('content', 'filename', 'type', 'launch-index', 'content-disposition', ATTACHMENT_FIELD.lower(), CONTENT_TYPE.lower()): continue msg.add_header(header, ent[header]) self._attach_part(append_msg, msg)
def _explode_archive(self, archive, append_msg): entries = util.load_yaml(archive, default=[], allowed=(list, set)) for ent in entries: # ent can be one of: # dict { 'filename' : 'value', 'content' : # 'value', 'type' : 'value' } # filename and type not be present # or # scalar(payload) if isinstance(ent, (str, basestring)): ent = {'content': ent} if not isinstance(ent, (dict)): # TODO(harlowja) raise? continue content = ent.get('content', '') mtype = ent.get('type') if not mtype: mtype = handlers.type_from_starts_with(content, ARCHIVE_UNDEF_TYPE) maintype, subtype = mtype.split('/', 1) if maintype == "text": msg = MIMEText(content, _subtype=subtype) else: msg = MIMEBase(maintype, subtype) msg.set_payload(content) if 'filename' in ent: _set_filename(msg, ent['filename']) if 'launch-index' in ent: msg.add_header('Launch-Index', str(ent['launch-index'])) for header in list(ent.keys()): if header.lower() in ('content', 'filename', 'type', 'launch-index', 'content-disposition', ATTACHMENT_FIELD.lower(), CONTENT_TYPE.lower()): continue msg.add_header(header, ent[header]) self._attach_part(append_msg, msg)
def handle_part(self, data, ctype, filename, payload, frequency, headers): if ctype in handlers.CONTENT_SIGNALS: return jinja_json_file = os.path.join(self.paths.run_dir, INSTANCE_JSON_FILE) rendered_payload = render_jinja_payload_from_file( payload, filename, jinja_json_file) if not rendered_payload: return subtype = handlers.type_from_starts_with(rendered_payload) sub_handler = self.sub_handlers.get(subtype) if not sub_handler: LOG.warning( 'Ignoring jinja template for %s. Could not find supported' ' sub-handler for type %s', filename, subtype) return if sub_handler.handler_version == 3: sub_handler.handle_part( data, ctype, filename, rendered_payload, frequency, headers) elif sub_handler.handler_version == 2: sub_handler.handle_part( data, ctype, filename, rendered_payload, frequency)
def list_types(self): return [ handlers.type_from_starts_with(UPSTART_PREFIX), ]
def find_ctype(payload): return handlers.type_from_starts_with(payload)
# vi: ts=4 expandtab import shutil import subprocess from cloudinit import handlers from cloudinit import log as logging from cloudinit import util from cloudinit.settings import (PER_ALWAYS) LOG = logging.getLogger(__name__) TAUPAGE_AMI_CONFIG_PREFIX = "#taupage-ami-config" TAUPAGE_AMI_CONFIG_MIME_TYPE = handlers.type_from_starts_with(TAUPAGE_AMI_CONFIG_PREFIX) TAUPAGE_CONFIG = "/meta/taupage.yaml" TMP_TAUPAGE_CONFIG = "/tmp/taupage.yaml" class ZalandoAMIConfigPartHandler(handlers.Handler): def __init__(self, paths, **_kwargs): handlers.Handler.__init__(self, PER_ALWAYS) def list_types(self): return [TAUPAGE_AMI_CONFIG_MIME_TYPE] def handle_part(self, _data, ctype, filename, payload, frequency): if ctype == TAUPAGE_AMI_CONFIG_MIME_TYPE: LOG.info("Got Taupage AMI configuration; merging with {config}".format(config=TAUPAGE_CONFIG))
# vi: ts=4 expandtab import shutil import subprocess from cloudinit import handlers from cloudinit import log as logging from cloudinit import util from cloudinit.settings import (PER_ALWAYS) LOG = logging.getLogger(__name__) TAUPAGE_AMI_CONFIG_PREFIX = "#taupage-ami-config" TAUPAGE_AMI_CONFIG_MIME_TYPE = handlers.type_from_starts_with( TAUPAGE_AMI_CONFIG_PREFIX) TAUPAGE_CONFIG = "/meta/taupage.yaml" TMP_TAUPAGE_CONFIG = "/tmp/taupage.yaml" class ZalandoAMIConfigPartHandler(handlers.Handler): def __init__(self, paths, **_kwargs): handlers.Handler.__init__(self, PER_ALWAYS) def list_types(self): return [TAUPAGE_AMI_CONFIG_MIME_TYPE] def handle_part(self, _data, ctype, filename, payload, frequency): if ctype == TAUPAGE_AMI_CONFIG_MIME_TYPE: LOG.info(
def list_types(self): return [ handlers.type_from_starts_with(BOOTHOOK_PREFIX), ]
def list_types(self): return [ handlers.type_from_starts_with(SHELL_PREFIX), ]
# #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") self.file_names = [] def list_types(self): return list(CC_TYPES.values()) def _write_cloud_config(self):
# #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())