3 \me @sUdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z mZddlmZd Zd Zd ZejeZd d deddgedededededgedgdZe eeZee e eddddZddZedddZdd Zd!d"ZdS)#z"Write Files: write arbitrary filesN)dedent)util)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEiFz text/plainZcc_write_filesz Write Fileszwrite 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)idnametitle descriptionZdistrosZexamplesZ frequencyZactivate_by_schema_keys)r cfgcloudargsreturncCsB|jdg}dd|D}|s.tjd|dSt|||jjdS)Nr cSsg|]}tj|dts|qS)Zdefer)rget_cfg_option_bool DEFAULT_DEFER).0fr$/usr/lib/python3.6/cc_write_files.py |szhandle..zESkipping module named %s, no/empty 'write_files' key in configuration)getLOGdebugr ZdistroZ default_owner)r rrrZ file_listZfiltered_filesrrrhandleys rcCsf|sd}|jj}|d kr"dgS|dkr2d dgS|dkr@d gS|tkrNtgS|r`tjd |ttgS)Ngzgzipzapplication/x-gzip gz+base64 gzip+base64gz+b64gzip+b64zapplication/base64b64base64z%Unknown encoding type %s, assuming %s)rr )r!r"r#r$)r%r&)lowerstripTEXT_PLAIN_ENCrwarning)Z encoding_typerrrcanonicalize_extractions" r+)ownerc Cs|sdSxt|D]\}}|jd}|sZextraction_typesresulttrrrr9s  r9)__doc__r&Zloggingr7textwraprZ cloudinitrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarrZcloudinit.settingsrr;rr)Z getLogger__name__rmetarFlistrr+r r:r9rrrrsJ