U
    bå  ã                   @   s„   d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
 ddlmZ de ZdZddd	„Zd
d„ Zdd„ Zedkr€eƒ  dS )zCCommandline utility to list the canonical cloud-id for an instance.é    N)ÚUXAppStatusÚget_status_details)ÚINSTANCE_JSON_FILEÚMETADATA_UNKNOWNÚcanonical_cloud_id)Úerrorz/run/cloud-init/%szcloud-idc                 C   sV   | st jtdd�} | jdddddd� | jd	d
dddd� | jddttdt d� | S )a*  Build or extend an arg parser for the cloud-id 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.
    z/Report the canonical cloud-id for this instance)ÚprogZdescriptionz-jz--jsonÚ
store_trueFz5Report all standardized cloud-id information as json.)ÚactionÚdefaultÚhelpz-lz--longz=Report extended cloud-id information as tab-delimited string.z-iz--instance-dataz.Path to instance-data.json file. Default is %s)Útyper   r   )ÚargparseÚArgumentParserÚNAMEÚadd_argumentÚstrÚDEFAULT_INSTANCE_JSON©Úparser© r   ú8/usr/lib/python3/dist-packages/cloudinit/cmd/cloud_id.pyÚ
get_parser   s:    	þûûÿûr   c           
   
   C   sH  t ƒ \}}}|tjkr.tj d |j¡¡ dS |tjkrPtj d |j¡¡ dS zt	 
t|jƒ¡}W nX tk
r†   td|j ƒ Y S  tk
r¼ } ztd|j|f ƒ W Y ¢S d}~X Y nX | di ¡}t| dt¡| d	t¡| d
t¡ƒ}|j	�r||d< t	j|dddd�}	n"|j�r0d|| d	t¡f }	n|}	tj d|	 ¡ dS )z¸Handle calls to 'cloud-id' cli.

    Print the canonical cloud-id on which the instance is running.

    @return: 0 on success, 1 on error, 2 on disabled, 3 on cloud-init not run.
    z{0}
é   é   zTFile not found '%s'. Provide a path to instance data json file using --instance-datazFile '%s' is not valid json. %sNÚv1Z
cloud_nameZregionÚplatformÚcloud_idé   T)ú,z: )ÚindentZ	sort_keysZ
separatorsz%s	%sz%s
r   )r   r   ZDISABLEDÚsysÚstdoutÚwriteÚformatÚvalueZNOT_RUNÚjsonÚloadÚopenÚinstance_dataÚIOErrorr   Ú
ValueErrorÚgetr   r   ÚdumpsZlong)
ÚnameÚargsZstatusZ_status_detailsZ_timer)   Úer   r   Zresponser   r   r   Úhandle_args=   sL    

ÿÿÿ


ý   ÿr1   c                  C   s   t ƒ } t tt|  ¡ ƒ¡ dS )z,Tool to query specific instance-data values.N)r   r!   Úexitr1   r   Ú
parse_argsr   r   r   r   Úmainj   s    r4   Ú__main__)N)Ú__doc__r   r&   r!   Zcloudinit.cmd.statusr   r   Zcloudinit.sourcesr   r   r   Zcloudinit.utilr   r   r   r   r1   r4   Ú__name__r   r   r   r   Ú<module>   s   
'-