3 \meQ @sXUddlZddlZddlZddlZddlmZddlmZmZddl m Z ddl m Z m Z ddlmZddlmZddlmZmZmZdd lmZmZeeydd lmZdd lmZWn"ek reZej d ZYnXej!e"Z#Gd ddeZ$GdddeZ%Gddde j&Z'dddZ(d ddZ)e*ee*dddZ+ddfdfddZ,dS)!N)EACCES)OptionalType)handlers)b64d json_dumps)Paths) PER_ALWAYS)MISSING_JINJA_PREFIXdetect_template render_string) load_file load_json)UndefinedError) operator_rez[-.]c@s eZdZdS)JinjaLoadErrorN)__name__ __module__ __qualname__rr$/usr/lib/python3.6/jinja_template.pyr!src@s eZdZdS) NotJinjaErrorN)rrrrrrrr%src@s(eZdZdgZedddZddZdS)JinjaTemplatePartHandlerz## template: jinja)pathscKsTtjj|tdd||_i|_x0|jdgD] }x|jD]}||j|<q:Wq,WdS)N)version sub_handlers)rHandler__init__r rrgetZ list_types)selfrZ_kwargsZhandlerctyperrrr-s z!JinjaTemplatePartHandler.__init__c Cs|tjkrdS|jjd}t|||}|s.dStj|} |jj| } | sZtj d|| dS| j dkrz| j ||||||n| j dkr| j |||||dS)NZinstance_data_sensitivezPIgnoring jinja template for %s. Could not find supported sub-handler for type %sr) rZCONTENT_SIGNALSrZ get_runpathrender_jinja_payload_from_fileZtype_from_starts_withrrLOGwarningZhandler_version handle_part) r datar!filenamepayloadZ frequencyZheadersZjinja_json_filerendered_payloadsubtypeZ sub_handlerrrrr&5s*       z$JinjaTemplatePartHandler.handle_partN)rrrprefixesrrr&rrrrr)srFcCst|ddkrtdi}d}tjj|s8td|ytt|}WnPtk r}z4d}t |t t fr~|j t kr~d|}t||WYdd}~XnXt||||}|sdS|S)aRRender a jinja template payload sourcing variables from jinja_vars_path. @param payload: String of jinja template content. Should begin with ## template: jinja . @param payload_fn: String representing the filename from which the payload was read used in error reporting. Generally in part-handling this is 'part-##'. @param instance_data_file: A path to a json file containing variables that will be used as jinja template variables. @return: A string of jinja-rendered content with the jinja header removed. Returns None on error. rZjinjazPayload is not a jinja templateNzFCannot render jinja template vars. Instance data not yet present at %sz"Loading Jinja instance data failedzGCannot render jinja template vars. No read permission on '%s'. Try sudo)r rospathexistsrrr Exception isinstanceIOErrorOSErrorerrnorrender_jinja_payload)r) payload_fnZinstance_data_filedebug instance_datar*emsgrrrr#Rs,   r#cCst||jdgdd}|r*tjdt|yt||}Wn6ttfk rn}ztjd|t |dSd}~XnXddt j dt |D}|rtjd |d j ||S) Nzbase64-encoded-keysT) decode_pathsinclude_key_aliaseszConverted jinja variables %sz"Ignoring jinja template for %s: %scSsg|]}d|jtdqS)z'%s')replacer ).0varrrr sz(render_jinja_payload..z%s[^\s]+z:Could not render jinja template variables in file '%s': %sz, )convert_jinja_instance_datarr$r7rr TypeErrorJUndefinedErrorr%strrefindallr join)r)r6r8r7Zinstance_jinja_varsr*r9warningsrrrr5s*   r5) orig_namereturncCstjtd|}||kr|SdS)aReturn a jinja variable alias, replacing any operators with underscores. Provide underscore-delimited key aliases to simplify dot-notation attribute references for keys which contain operators "." or "-". This provides for simpler short-hand jinja attribute notation allowing one to avoid quoting keys which contain operators. {{ ds.v1_0.config.user_network_config }} instead of {{ ds['v1.0'].config["user.network-config"] }}. :param orig_name: String representing a jinja variable name to scrub/alias. :return: A string with any jinja operators replaced if needed. Otherwise, none if no alias required. _N)rFsubr)rJ alias_namerrrget_jinja_variable_aliassrOr=/c Csi}dd|D}xt|jD]\}}|r:dj|||n|}||krNt|}t|trt|||||d||<tjd|rx0||jD]\} } t j | || <qWn|||<|r t |} | r t j |||| <q W|S)zProcess instance-data.json dict for use in jinja templates. Replace hyphens with underscores for jinja templates and decode any base64_encoded_keys. cSsg|]}|jddqS)-rL)r>)r?r.rrrrAsz/convert_jinja_instance_data..z {0}{1}{2})sepr;r<zv\d+$) sorteditemsformatrr1dictrBrFmatchcopydeepcopyrO) r'prefixrRr;r<resultkeyvalueZkey_pathZsubkeyZsubvaluerNrrrrBs,   rB)F)F)-rXZloggingr-rFr4rZtypingrrZ cloudinitrZcloudinit.atomic_helperrrZcloudinit.helpersrZcloudinit.settingsr Zcloudinit.templaterr r r Zcloudinit.utilr rr0rDZjinja2.exceptionsrZ jinja2.lexerr ImportErrorcompileZ getLoggerrr$rrrrr#r5rErOrBrrrrs6      * ,