【问题标题】:cloud-init: Can write_files be merged?cloud-init:可以合并 write_files 吗?
【发布时间】:2020-07-17 18:48:12
【问题描述】:

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

谢谢

【问题讨论】:

    标签: cloud-init


    【解决方案1】:

    答案是肯定的。

    使用多部分 MIME 时,必须将以下内容添加到每个部分的标题中。

    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()
    

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 2020-12-20
      • 2021-12-16
      • 2017-07-11
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2019-07-09
      • 2011-03-30
      相关资源
      最近更新 更多