PNG  IHDR* pHYs+ IDATx]n#; cdLb Ǚ[at¤_:uP}>!Usă cag޿ ֵNu`ݼTâabO7uL&y^wFٝA"l[|ŲHLN밪4*sG3|Dv}?+y߉{OuOAt4Jj.u]Gz*҉sP'VQKbA1u\`& Af;HWj hsO;ogTu uj7S3/QzUr&wS`M$X_L7r2;aE+ώ%vikDA:dR+%KzƉo>eOth$z%: :{WwaQ:wz%4foɹE[9<]#ERINƻv溂E%P1i01 |Jvҗ&{b?9g=^wζXn/lK::90KwrюO\!ջ3uzuGv^;騢wq<Iatv09:tt~hEG`v;3@MNZD.1]L:{ծI3`L(÷ba")Y.iljCɄae#I"1 `3*Bdz>j<fU40⨬%O$3cGt]j%Fߠ_twJ;ABU8vP3uEԑwQ V:h%))LfraqX-ۿX]v-\9I gl8tzX ]ecm)-cgʒ#Uw=Wlێn(0hPP/ӨtQ“&J35 $=]r1{tLuǮ*i0_;NƝ8;-vݏr8+U-kruȕYr0RnC]*ެ(M:]gE;{]tg(#ZJ9y>utRDRMdr9㪩̞zֹb<ģ&wzJM"iI( .ꮅX)Qw:9,i좜\Ԛi7&N0:asϓc];=ΗOӣ APqz93 y $)A*kVHZwBƺnWNaby>XMN*45~ղM6Nvm;A=jֲ.~1}(9`KJ/V F9[=`~[;sRuk]rєT!)iQO)Y$V ی ۤmzWz5IM Zb )ˆC`6 rRa}qNmUfDsWuˤV{ Pݝ'=Kֳbg,UҘVz2ﴻnjNgBb{? ߮tcsͻQuxVCIY۠:(V뺕 ٥2;t`@Fo{Z9`;]wMzU~%UA蛚dI vGq\r82iu +St`cR.6U/M9IENDB`#!/usr/bin/env python3 # This file is part of cloud-init. See LICENSE file for license information. """Generate multi-part mime messages for user-data.""" import argparse import logging import sys from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from cloudinit.handlers import INCLUSION_TYPES_MAP NAME = "make-mime" LOG = logging.getLogger(__name__) EPILOG = ( "Example: make-mime -a config.yaml:cloud-config " "-a script.sh:x-shellscript > user-data" ) def create_mime_message(files): sub_messages = [] errors = [] for i, (fh, filename, format_type) in enumerate(files): contents = fh.read() sub_message = MIMEText(contents, format_type, sys.getdefaultencoding()) sub_message.add_header( "Content-Disposition", 'attachment; filename="%s"' % (filename) ) content_type = sub_message.get_content_type().lower() if content_type not in get_content_types(): msg = ("content type %r for attachment %s may be incorrect!") % ( content_type, i + 1, ) errors.append(msg) sub_messages.append(sub_message) combined_message = MIMEMultipart() for msg in sub_messages: combined_message.attach(msg) return (combined_message, errors) def file_content_type(text): """Return file content type by reading the first line of the input.""" try: filename, content_type = text.split(":", 1) return (open(filename, "r"), filename, content_type.strip()) except ValueError as e: raise argparse.ArgumentError( text, "Invalid value for %r" % (text) ) from e def get_parser(parser=None): """Build or extend and arg parser for make-mime utility. @param parser: Optional existing ArgumentParser instance representing the subcommand which will be extended to support the args of this utility. @returns: ArgumentParser with proper argument configuration. """ if not parser: parser = argparse.ArgumentParser() # update the parser's doc and add an epilog to show an example parser.description = __doc__ parser.epilog = EPILOG parser.add_argument( "-a", "--attach", dest="files", type=file_content_type, action="append", default=[], metavar=":", help="attach the given file as the specified content-type", ) parser.add_argument( "-l", "--list-types", action="store_true", default=False, help="List support cloud-init content types.", ) parser.add_argument( "-f", "--force", action="store_true", default=False, help="Ignore unknown content-type warnings", ) return parser def get_content_types(strip_prefix=False): """Return a list of cloud-init supported content types. Optionally strip out the leading 'text/' of the type if strip_prefix=True. """ return sorted( [ ctype.replace("text/", "") if strip_prefix else ctype for ctype in INCLUSION_TYPES_MAP.values() ] ) def handle_args(name, args): """Create a multi-part MIME archive for use as user-data. Optionally print out the list of supported content types of cloud-init. Also setup CLI log handlers to report to stderr since this is a development utility which should be run by a human on the CLI. @return 0 on success, 1 on failure. """ if args.list_types: print("\n".join(get_content_types(strip_prefix=True))) return 0 combined_message, errors = create_mime_message(args.files) if errors: level = "WARNING" if args.force else "ERROR" for error in errors: sys.stderr.write(f"{level}: {error}\n") sys.stderr.write("Invalid content-types, override with --force\n") if not args.force: return 1 print(combined_message) return 0 def main(): args = get_parser().parse_args() return handle_args(NAME, args) if __name__ == "__main__": sys.exit(main())