Ë
    x[hk,  ã                   óü   — d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZ d	Z ej*                  e«      Zdd
„Zd„ Zdefd„Zdedededefd„Zd„ Zd„ Z edk(  r e «        yy)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           	      óþ  — | st        j                  t        t        ¬«      } | j	                  ddddd¬«       | j	                  dd	t
        d
t        «       j                  d«      › �¬«       | j	                  ddddd¬«       | j	                  ddt
        d¬«       | j	                  ddt
        d¬«       | j	                  dt
        dd¬«       | j	                  dddddd¬«       | j	                  dd t
        d!d"¬#«       | S )$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                 ó¢   — t        j                  | d¬«      }	 |j                  d«      S # t        $ r t        j                  |dd¬«      cY S w xY w)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                 ó$  — t        j                  «       }t        «       }| r| }ne|j                  d«      }|dk(  rM|j                  d«      }t         j                  j                  |«      r|}nt        j                  d||«       |}n|}|r|}n*t         j                  j                  |j                  d«      }|r|}	n*t         j                  j                  |j                  d«      }	|j                  d«      }
	 t        j                  |«      }t        j"                  |«      } 	 t        j"                  t        j                  |
«      «      }|dk7  r/dt$        ›d|›�| d<   dt$        ›d|	›�| d<   dt$        ›d|
›�| d<   | S t'        |«      | d<   t'        |	«      | d<   || d<   | S # t        t        f$ rF}|j                  t        k(  rt        j!                  d|«       ‚ t        j!                  d	|«       ‚ d
}~ww xY w# t        t        f$ r d
}Y ŒÂw xY w)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Ø#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+   Ã9F# Ä$(G; Æ#G8Æ2AG3Ç3G8Ç;HÈHÚjinja_vars_without_aliasesÚjinja_vars_with_aliasesr   Ú	list_keysc                 ó2  — d}| }|j                  d«      D ]:  }	 ||   }||v r||   }n|D ]  }	t	        |	«      |k(  sŒ||	   } n |r|dz  }||z  }Œ< |S # t        $ r8}|rdj                  ||¬«      }ndj                  |«      }t        |«      |‚d}~ww xY w)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Ó+ò )ˆð	)ð '>¸mÑ&LÐ#ð ˜HÑ$Ø Ñ.‰Hàò �Ü+¨CÓ0°MÓAØ'¨™}�HÙðñ Ø˜sÑ"ˆOØ˜=Ñ(‰ð-)ð. €Oøô% ò 	)ÙØB×IÑIØ&°ð Jó ‘ð 9×?Ñ?ÀÓH�Ü˜S“/ qÐ(ûð	)ús   šAÁ	BÁ3BÂBc                 ó€  — t        |j                  |j                  |j                  |j                  g«      s.t
        j                  d«       t        «       j                  «        y	 t        |j                  |j                  |j                  «      }|j                  rIdj                  |j                  ¬«      }	 t        |d||j                   rdnd¬«      }|rt'        |«       yyt)        |«      }|j                  r1t)        |d¬«      }	 t+        |||j                  |j                  ¬«      }|j                  rYt1        |t2        «      s!t
        j                  d|j                  «       ydj5                  t7        |j9                  «       «      «      }t1        |t$        «      st;        j<                  |«      }t'        |«       y# t        t        f$ r Y yw xY w# t"        $ r)}t
        j                  d	t%        |«      «       Y d
}~yd
}~ww xY w# t,        t.        f$ r}t
        j                  |«       Y d
}~yd
}~ww xY w)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Ü�	‰	ð&ô	
ô 	‹×ÑÔ!ØðÜ+Ø×Ñ §¡°×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 Â;G Ä#H ÇGÇGÇ	HÇ#HÈHÈH=ÈH8È8H=c                  óz   — t        «       } t        j                  t        t        | j                  «       «      «       y)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Ý ;à€Ø€g×Ñ˜Ó!€óUòpAðMÀ$ó Mð`(Ø $ð(à!ð(ð ð(ð ó	(òV@òF5ð ˆzÒÙ…Fð r'   