#!/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="