o
    w7e                     @   s  U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ d
ZdZdZeeZdddeddgedededededgedgdZeed< eeZ dedede
deddf
dd Zd!d" Zd#efd$dZd%d& Zd'd( Z dS ))z"Write Files: write arbitrary files    N)dedent)log)util)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCEi  Fz
text/plaincc_write_fileszWrite Fileszwrite arbitrary filesax          Write out arbitrary content to files, optionally setting permissions.
        Parent folders in the path are created if absent.
        Content can be specified in plain text or binary. Data encoded with
        either base64 or binary gzip data can be specified and will be decoded
        before being written. For empty file creation, content can be omitted.

    .. note::
        If multiline data is provided, care should be taken to ensure that it
        follows yaml formatting standards. To specify binary data, use the yaml
        option ``!!binary``

    .. note::
        Do not write files under /tmp during boot because of a race with
        systemd-tmpfiles-clean that can cause temp files to get cleaned during
        the early boot process. Use /run/somedir instead to avoid race
        LP:1707222.

    .. warning::
       Existing files will be overridden.alla)          # Write out base64 encoded content to /etc/sysconfig/selinux
        write_files:
        - encoding: b64
          content: CiMgVGhpcyBmaWxlIGNvbnRyb2xzIHRoZSBzdGF0ZSBvZiBTRUxpbnV4...
          owner: root:root
          path: /etc/sysconfig/selinux
          permissions: '0644'
        z        # Appending content to an existing file
        write_files:
        - content: |
            15 * * * * root ship_logs
          path: /etc/crontab
          append: true
        a          # Provide gziped binary content
        write_files:
        - encoding: gzip
          content: !!binary |
              H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA=
          path: /usr/bin/hello
          permissions: '0755'
        zt        # Create an empty file on the system
        write_files:
        - path: /root/CLOUD_INIT_WAS_HERE
        a           # Defer writing the file until after the package (Nginx) is
        # installed and its user is created alongside
        write_files:
        - path: /etc/nginx/conf.d/example.com.conf
          content: |
            server {
                server_name example.com;
                listen 80;
                root /var/www;
                location / {
                    try_files $uri $uri/ $uri.html =404;
                }
            }
          owner: 'nginx:nginx'
          permissions: '0640'
          defer: true
        write_files)idnametitledescriptiondistrosexamples	frequencyactivate_by_schema_keysmetar   cfgcloudargsreturnc                 C   sB   | dg }dd |D }|std|  d S t| ||jj d S )Nr   c                 S   s   g | ]}t |d ts|qS )defer)r   get_cfg_option_boolDEFAULT_DEFER).0f r   A/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py
<listcomp>{   s    zhandle.<locals>.<listcomp>zESkipping module named %s, no/empty 'write_files' key in configuration)getLOGdebugr   distrodefault_owner)r   r   r   r   	file_listfiltered_filesr   r   r    handley   s   r)   c                 C   sf   | sd} |    } | dv rdgS | dv rddgS | dv r dgS | tkr'tgS | r0td| t tgS )N )gzgzipapplication/x-gzip)z	gz+base64zgzip+base64zgz+b64zgzip+b64application/base64)b64base64z%Unknown encoding type %s, assuming %s)lowerstripTEXT_PLAIN_ENCr#   warning)encoding_typer   r   r    canonicalize_extraction   s$   r6   ownerc              	   C   s   |sd S t |D ]Z\}}|d}|std|d |  qtj|}t|d}t|dd|}t	
|d|\}}	t|dt}
t	|d	rNd
nd}t	j||||
||	d t	|||	 qd S )Npathz3No path provided to write for entry %s in module %s   encodingcontentr*   r7   permissionsappendabwb)omodemodeusergroup)	enumerater"   r#   r4   osr8   abspathr6   extract_contentsr   extract_usergroupdecode_permsDEFAULT_PERMSr   
write_filechownbyname)r   filesr7   if_infor8   extractionscontentsugpermsr@   r   r   r    r      s,   
c                 C   s   | d u r|S zt | ttfrt| W S tt| dW S  ttfyR   g }| |fD ]}z	|d|  W q) tyD   |d|  Y q)w tjdg|R   | Y S w )N   z%oz%rz0Undecodable permissions %s, returning default %s)	
isinstanceintfloatstr	TypeError
ValueErrorr=   r#   r4   )permdefaultrepsrr   r   r    rI      s"   
rI   c                 C   sJ   | }|D ]}|dkrt j|ddd}q|dkrt|}q|tkr"	 q|S )Nr-   F)quietdecoder.   )r   decomp_gzipr0   	b64decoder3   )rQ   extraction_typesresulttr   r   r    rG      s   rG   )!__doc__r0   rE   textwrapr   	cloudinitr   loggingr   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.settingsr	   rJ   r   r3   	getLogger__name__r#   r   __annotations__rY   listr)   r6   r   rI   rG   r   r   r   r    <module>   s\   

=\