ó
    x[hk,  ã                   ó  • S r SSKrSSKrSSKrSSKrSSKJr  SSKJrJ	r	  SSK
Jr  SSKJrJrJr  SSKJr  SSKJr  S	r\R*                  " \5      rSS
 jrS rS\4S jrS\S\S\S\4S jrS rS r \S:X  a  \ " 5         gg)aT  Query standardized instance metadata provided to machine, returning a JSON
structure.

Some instance-data values may be binary on some platforms, such as userdata and
vendordata. Attempt to decompress and decode UTF-8 any binary values.

Any binary values in the instance metadata will be base64-encoded and prefixed
with "ci-b64:" in the output. userdata and, where applicable, vendordata may
be provided to the machine gzip-compressed (and therefore as binary data).
query will attempt to decompress these to a string before emitting the JSON
output; if this fails, they are treated as binary.
é    N)ÚEACCES)Úatomic_helperÚutil)Úread_cfg_paths)Úconvert_jinja_instance_dataÚget_jinja_variable_aliasÚrender_jinja_payload)ÚREDACT_SENSITIVE_VALUE)ÚJinjaSyntaxParsingExceptionÚqueryc           	      óÔ  • U (       d  [         R                  " [        [        S9n U R	                  SSSSSS9  U R	                  SS	[
        S
[        5       R                  S5       3S9  U R	                  SSSSSS9  U R	                  SS[
        SS9  U R	                  SS[
        SS9  U R	                  S[
        SSS9  U R	                  SSSSSSS9  U R	                  SS [
        S!S"S#9  U $ )$a  Build or extend an arg parser for query utility.

@param parser: Optional existing ArgumentParser instance representing the
    query subcommand which will be extended to support the args of
    this utility.

@returns: ArgumentParser with proper argument configuration.
)ÚprogÚdescriptionz-dz--debugÚ
store_trueFz+Add verbose messages during template render)ÚactionÚdefaultÚhelpz-iz--instance-dataz,Path to instance-data.json file. Default is Úinstance_data)Útyper   z-lz--list-keyszBList query keys available at the provided instance-data <varname>.z-uz--user-datazHPath to user-data file. Default is /var/lib/cloud/instance/user-data.txtz-vz--vendor-datazLPath to vendor-data file. Default is /var/lib/cloud/instance/vendor-data.txtÚvarnameÚ?zÇA dot-delimited specific variable to query from instance-data. For example: v1.local_hostname. If the value is not JSON serializable, it will be base64-encoded and will contain the prefix "ci-b64:". )r   Únargsr   z-az--allÚdump_allz Dump all available instance-data)r   r   Údestr   z-fz--formatÚformatzŸOptionally specify a custom output format string. Any instance-data variable can be specified between double-curly braces. For example -f "{{ v2.cloud_name }}")r   r   r   )ÚargparseÚArgumentParserÚNAMEÚ__doc__Úadd_argumentÚstrr   Úget_runpath©Úparsers    Ú5/usr/lib/python3/dist-packages/cloudinit/cmd/query.pyÚ
get_parserr&   &   sX  € ö Ü×(Ò(¬dÄÑHˆØ
×ÑØØØØØ:ð ñ ð ×ÑØØÜà:ÜÓ×+Ñ+¨OÓ<Ð=ð?ð ñ ð ×ÑØØØØðð ñ 	ð ×ÑØØÜð5ð ñ ð ×ÑØØÜð7ð ñ ð ×ÑØÜØð3ð ñ 
ð ×ÑØØØØØØ/ð ñ ð ×ÑØØÜØð<ð ñ 
ð €Mó    c                 óœ   • [         R                  " U SS9n UR                  S5      $ ! [         a    [         R                  " USSS9s $ f = f)zìAttempt to return a string of user-data from ud_file_path

Attempt to decode or decompress if needed.
If unable to decode the content, raw bytes will be returned.

@returns: String of uncompressed userdata if possible, otherwise bytes.
T)Úquietzutf-8F)r)   Údecode)r   Úload_binary_filer*   ÚUnicodeDecodeErrorÚdecomp_gzip)Úud_file_pathÚbdatas     r%   Úload_userdatar0   ~   sQ   € ô ×!Ò! ,°dÑ;€EðAØ�|‰|˜GÓ$Ð$øÜó AÜ×Ò ¨U¸4Ñ@Ò@ðAús   —( ¨ AÁ
AÚreturnc                 ój  • [         R                  " 5       n[        5       nU (       a  U nOkUR                  S5      nUS:X  aR  UR                  S5      n[         R                  R                  U5      (       a  UnO[        R                  SUU5        UnOUnU(       a  UnO*[         R                  R                  UR                  S5      nU(       a  Un	O*[         R                  R                  UR                  S5      n	UR                  S5      n
 [        R                  " U5      n[        R"                  " U5      n  [        R"                  " [        R                  " U
5      5      nUS:w  a5  S[$        < SU< 3U S'   S[$        < SU	< 3U S'   S[$        < SU
< 3U S'   U $ ['        U5      U S'   ['        U	5      U S'   XÐS'   U $ ! [        [        4 aG  nUR                  [        :X  a  [        R!                  SU5        e [        R!                  S	U5        e S
nAff = f! [        [        4 a    S
n NËf = f)a~  Return a dict of merged instance-data, vendordata and userdata.

The dict will contain supplemental userdata and vendordata keys sourced
from default user-data and vendor-data files.

Non-root users will have redacted INSTANCE_JSON_FILE content and redacted
vendordata and userdata values.

:raise: IOError/OSError on absence of instance-data.json file or invalid
    access perms.
r   r   Úinstance_data_sensitivez4Missing root-readable %s. Using redacted %s instead.zuser-data.txtzvendor-data.txtÚcombined_cloud_configz$No read permission on '%s'. Try sudozMissing instance-data file: %sNÚ<z> file:ÚuserdataÚ
vendordata)ÚosÚgetuidr   r"   ÚpathÚexistsÚLOGÚwarningÚjoinÚinstance_linkr   Úload_text_fileÚIOErrorÚOSErrorÚerrnor   ÚerrorÚ	load_jsonr
   r0   )r   Ú	user_dataÚvendor_dataÚuidÚpathsÚinstance_data_fnÚredacted_data_fnÚsensitive_data_fnÚuser_data_fnÚvendor_data_fnÚcombined_cloud_config_fnÚinstance_jsonÚer4   s                 r%   Ú_read_instance_datarR   �   s  € ô �)Š)‹+€CÜÓ€EÞØ(Ñà ×,Ñ,¨_Ó=ÐØ�!‹8Ø %× 1Ñ 1Ð2KÓ LÐÜ�w‰w�~‰~Ð/×0Ñ0Ø#4Ñ ä—‘ØJØ%Ø$ôð
 $4Ñ à/ÐÞØ ‰ä—w‘w—|‘| E×$7Ñ$7¸ÓIˆÞØ$‰äŸ™Ÿ™ e×&9Ñ&9Ð;LÓMˆØ$×0Ñ0Ð1HÓIÐðÜ×+Ò+Ð,<Ó=ˆô —N’N =Ó1€Mð%Ü $§¢Ü×ÒÐ 8Ó9ó!
Ðð ˆa„xç"Úð%
ˆ�jÒ!÷
 #Úð'
ˆ�lÒ#÷
 #Ú$ð2
ˆÐ-Ñ.ð Ðô %2°,Ó$?ˆ�jÑ!Ü&3°NÓ&Cˆ�lÑ#Ø1FÐ-Ñ.ØÐøôE ”WÐó Ø�7‰7”fÓÜ�I‰IÐ<Ð>NÔOð 	ô �I‰IÐ6Ð8HÔIØûðûô ”WÐó %ð !%Òð%ús+   ÄG Ä<*H ÇHÇAHÈHÈH2È1H2Újinja_vars_without_aliasesÚjinja_vars_with_aliasesr   Ú	list_keysc                 óD  • SnU nUR                  S5       H@  n X   nXe;   a  XV   nOU H  n	[	        U	5      U:X  d  M  XY   n  O   U(       a  US-  nXF-  nMB     U$ ! [         a9  nU(       a  SR                  XdS9nOSR                  U5      n[        U5      UeSnAff = f)a›  Return the value of the dot-delimited varname path in instance-data

Split a dot-delimited jinja variable name path into components, walk the
path components into the instance_data and look up a matching jinja
variable name or cloud-init's underscore-delimited key aliases.

:raises: ValueError when varname represents an invalid key name or path or
    if list-keys is provided by varname isn't a dict object.
Ú Ú.z*instance-data '{key_path}' has no '{leaf}')ÚleafÚkey_pathz Undefined instance-data key '{}'N)ÚsplitÚKeyErrorr   Ú
ValueErrorr   )
rS   rT   r   rU   Úwalked_key_pathÚresponseÚkey_path_partrQ   ÚmsgÚkeys
             r%   Ú(_find_instance_data_leaf_by_varname_pathrc   Ý   sÌ   € ð €OØ)€HØ Ÿ™ sÖ+ˆð	)ð '>Ñ&LÐ#ð Ó$ØÑ.‰Hã�Ü+¨CÓ0°MÕAØ'™}�HÙñ  ö Ø˜sÑ"ˆOØÑ(Šñ- ,ð. €Oøô% ó 	)ÞØB×IÑIØ&ð Jð ‘ð 9×?Ñ?ÀÓH�Ü˜S“/ qÐ(ûð	)ús   šAÁ
BÁ&4BÂBc                 óÀ  • [        UR                  UR                  UR                  UR                  /5      (       d.  [
        R                  S5        [        5       R                  5         g [        UR                  UR                  UR                  5      nUR                  (       aM  SR                  UR                  S9n [        USUUR                   (       a  SOSS9nU(       a  ['        U5        gg[)        U5      nUR                  (       a+  [)        USS9n [+        UUUR                  UR                  S9nUR                  (       a^  [1        U[2        5      (       d!  [
        R                  SUR                  5        gSR5                  [7        UR9                  5       5      5      n[1        U[$        5      (       d  [:        R<                  " U5      n['        U5        g! [        [        4 a     gf = f! ["         a)  n[
        R                  S	[%        U5      5         S
nAgS
nAff = f! [,        [.        4 a  n[
        R                  U5         S
nAgS
nAff = f)z3Handle calls to 'cloud-init query' as a subcommand.zDExpected one of the options: --all, --format, --list-keys or varnameé   z## template: jinja
{fmt})Úfmtzquery command lineTF)ÚpayloadÚ
payload_fnr   Údebugz#Failed to render templated data. %sNr   )Úinclude_key_aliases)rS   rT   r   rU   z+--list-keys provided but '%s' is not a dictÚ
)ÚanyrU   r   r   r   r<   rD   r&   Ú
print_helprR   r   rF   rG   rA   rB   r	   ri   r   r!   Úprintr   rc   r\   r]   Ú
isinstanceÚdictr>   ÚsortedÚkeysr   Ú
json_dumps)ÚnameÚargsr   rg   Úrendered_payloadrQ   r_   rT   s           r%   Úhandle_argsrw     sè  € ä�—‘ §¡¨d¯k©k¸4¿=¹=ÐI×JÑJÜ�	‰	ð&ô	
ô 	‹×ÑÔ!ØðÜ+Ø×Ñ §¡°×0@Ñ0@ó
ˆð
 ‡{‡{Ø-×4Ñ4¸¿¹Ð4ÐEˆð	Ü3ØØ/Ø+Ø"ŸjŸj‘d¨eñ	 Ðö ÜÐ"Ô#ØØô +¨=Ó9€HØ‡|‡|Ü"=Ø¨tñ#
Ðð		Ü?Ø+3Ø(?ØŸ™ØŸ.™.ñ	ˆHð ‡~‡~Ü˜(¤D×)Ñ)Ü�I‰IØ=¸t¿|¹|ôð Ø—9‘9œV H§M¡M£OÓ4Ó5ˆÜ�h¤×$Ñ$Ü ×+Ò+¨HÓ5ˆÜ	ˆ(„OØøôg ”WÐó Ùðûô +ó 	Ü�I‰IØ5Ü�A“ôô ûð	ûô6 œ*Ð%ó 	Ü�I‰I�aŒLÜûð	úsB   Á,+G" ÃG8 Ä H. Ç"G5Ç4G5Ç8
H+ÈH&È&H+È.IÈ>IÉIc                  ó|   • [        5       n [        R                  " [        [        U R                  5       5      5        g)z,Tool to query specific instance-data values.N)r&   ÚsysÚexitrw   r   Ú
parse_argsr#   s    r%   Úmainr|   K  s%   € ä‹\€FÜ‡H‚HŒ[œ˜v×0Ñ0Ó2Ó3Õ4r'   Ú__main__)N)!r   r   Úloggingr8   ry   rC   r   Ú	cloudinitr   r   Úcloudinit.cmd.develr   Ú!cloudinit.handlers.jinja_templater   r   r	   Úcloudinit.sourcesr
   Úcloudinit.templaterr   r   Ú	getLoggerÚ__name__r<   r&   r0   rp   rR   r!   Úboolrc   rw   r|   © r'   r%   Ú<module>rˆ      s®   ðñ
ó Û Û 	Û 
Ý ç )Ý .÷ñ õ
 5Ý ;à€Ø×Ò˜Ó!€ôUòpAðMÀ$ô Mð`(Ø $ð(à!ð(ð ð(ð ô	(òV@òF5ð ˆzÓÙ…Fð r'   