cloud-init: 可以合并 write_files 吗?

cloud-init: Can write_files be merged?

可以合并write_files吗?我似乎无法正确理解 merge_how 语法。仅创建最后 write_files 中的文件。

谢谢

答案是肯定的。

使用 multi-part MIME 时,必须将以下内容添加到每个部分的 header。

Merge-Type: list(append)+dict(no_replace,recurse_list)+str()

以下是 cloud-init 文档中提供的 helper script 的修改版本。

#!/usr/bin/env python3

import click
import sys
import gzip as gz

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


@click.command()
@click.option('--gzip', is_flag=True,
              help='gzip MIME message to current directory')
@click.argument('message', nargs=-1)
def main(message: str, gzip: bool) -> None:
    """This script creates a multi part MIME suitable for cloud-init

    A MESSAGE has following format <filename>:<type> where <filename> is the
    file that contains the data to add to the MIME message. <type> is the 
    MIME content-type.

    MIME content-type may be on of the following:
    text/cloud-boothook
    text/cloud-config
    text/cloud-config-archive
    text/jinja2
    text/part-handler
    text/upstart-job
    text/x-include-once-url
    text/x-include-url
    text/x-shellscript

    EXAMPLE:
    write-mime-multipart afile.cfg:cloud-config bfile.sh:x-shellscript
    """
    combined_message = MIMEMultipart()
    for message_part in message:
        (filename, format_type) = message_part.split(':', 1)
        with open(filename) as fh:
            contents = fh.read()
        sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())
        sub_message.add_header('Content-Disposition',
                               'attachment; filename="{}"'.format(filename))
        sub_message.add_header('Merge-Type',
                               'list(append)+dict(no_replace,recurse_list)+str()')
        combined_message.attach(sub_message)

    if gzip:
        with gz.open('combined-userdata.txt.gz', 'wb') as fd:
            fd.write(bytes(combined_message))
    else:
        print(combined_message)


if __name__ == '__main__':
    main()