a 'Dg@sUdZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z ddl mZddlmZd Zd Zd ZeeZd d gedgdZeed<ee e eddddZddZdeeedddZddZddZ ddZ!dS)z"Write Files: write arbitrary filesN)Optional) url_helperutil)Cloud)Config) MetaSchema) PER_INSTANCEiFz text/plainZcc_write_filesall write_files)idZdistrosZ frequencyZactivate_by_schema_keysmeta)namecfgcloudargsreturncCsP|dg}dd|D}|s.td|dSt|j}t|||jj|dS)Nr cSsg|]}t|dts|qS)Zdefer)rget_cfg_option_bool DEFAULT_DEFER).0frC/usr/lib/python3.9/site-packages/cloudinit/config/cc_write_files.py $szhandle..zESkipping module named %s, no/empty 'write_files' key in configuration) getLOGdebugrZfetch_ssl_detailspathsr ZdistroZ default_owner)r rrrZ file_listZfiltered_files ssl_detailsrrrhandle"s  rcCsf|sd}|}|dvr"dgS|dvr2ddgS|dvr@dgS|tkrNtgS|r`td|ttgS)N)gzgzipapplication/x-gzip)z gz+base64z gzip+base64zgz+b64zgzip+b64application/base64)Zb64base64z%Unknown encoding type %s, assuming %s)lowerstripTEXT_PLAIN_ENCrwarning)Z encoding_typerrrcanonicalize_extraction4s$ r))ownerrc Cs|sdSt|D]\}}|d}|s:td|d|qtj|}t|dd||dd|dd}|durtd|d|qt |d|\}} t |d t } t |d rd nd } tj ||| | || d t||| qdS)Npathz3No path provided to write for entry %s in module %ssourcecontentencodingz>No content could be loaded for entry %s in module %s; skippingr*Z permissionsappendabwb)omodemodeusergroup) enumeraterrr(osr+abspathread_url_or_decoderZextract_usergroup decode_perms DEFAULT_PERMSrZ write_fileZ chownbyname) r filesr*riZf_infor+contentsugZpermsr3rrrr Ms@      c Cs|dur |Sz,t|ttfr&t|WStt|dWSWnrttfyg}||fD]8}z|d|WqVty|d|YqV0qVtjdg|R|YS0dS)Nz%oz%rz0Undecodable permissions %s, returning default %s) isinstanceintfloatstr TypeError ValueErrorr0rr()ZpermdefaultZrepsrrrrr;us   r;cCs|dur dn |dd}t|}|dur0|s0dSd}|rz"tj||dddd|dj}Wn$ty~ttd|d}Yn0|dur|st |}t ||}|S)NZurirheaders)rKZretriesZ sec_betweenrzVFailed to retrieve contents from source "%s"; falling back to data from "contents" keyF) rboolrZread_file_or_urlr? ExceptionrZlogexcrr)extract_contents)r-rr.r/urlZuse_urlresultZ extractionsrrrr:s2       r:cCsH|}|D]:}|dkr&tj|ddd}q|dkr:t|}q|tkrq|S)Nr"F)quietdecoder#)rZ decomp_gzipr$Z b64decoder')r?Zextraction_typesrQtrrrrOs rO)N)"__doc__r$Zloggingr8typingrZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarZcloudinit.settingsrr<rr'Z getLogger__name__rr __annotations__rFlistrr)dictr r;r:rOrrrrs0      (%