U
    Ld                     @   s   d 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
mZ ddlmZ dZddd	Zd
d Zdd Zedkre  dS )zCCommandline utility to list the canonical cloud-id for an instance.    N)read_cfg_paths)UXAppStatusget_status_details)METADATA_UNKNOWNcanonical_cloud_id)errorzcloud-idc                 C   sd   t  d}| stjtdd} | jdddddd	 | jd
ddddd	 | jddt|d| 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.
    instance_dataz/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 )typer   r   )r   Zget_runpathargparseArgumentParserNAMEadd_argumentstr)parserZdefault_instance_json r   8/usr/lib/python3/dist-packages/cloudinit/cmd/cloud_id.py
get_parser   s8    	
r   c           	   
   C   sf  t  }|jtjkr,tjd|jj dS |jtj	krRtjd|jj dS z$t
|j}t|}W 5 Q R X 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r0tjd ||d< tj|dddd}n"|jrNd||d	tf }n|}tjd|  dS )zHandle 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platformz%DEPRECATED: Use: cloud-init query v1
cloud_id   T),z: )indentZ	sort_keysZ
separatorsz%s	%sz%s
r   )r   Zstatusr   ZDISABLEDsysstdoutwriteformatvalueZNOT_RUNopenr   jsonloadIOErrorr   
ValueErrorgetr   r   stderrdumpsZlong)	nameargsZstatus_detailsfiler   er   r   Zresponser   r   r   handle_args=   sP    


   r1   c                  C   s   t  } ttt|   dS )z,Tool to query specific instance-data values.N)r   r    exitr1   r   
parse_args)r   r   r   r   mainl   s    r4   __main__)N)__doc__r   r&   r    Zcloudinit.cmd.develr   Zcloudinit.cmd.statusr   r   Zcloudinit.sourcesr   r   Zcloudinit.utilr   r   r   r1   r4   __name__r   r   r   r   <module>   s   
*/