
"·T`  ã               @   s    d  Z  d d l Z d d l Z d d l Z d d l m Z m Z m Z d e Z d Z	 d d d „ Z
 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)ÚINSTANCE_JSON_FILEÚMETADATA_UNKNOWNÚcanonical_cloud_idz/run/cloud-init/%szcloud-idc          
   C   sŒ   |  s t  j d t d d ƒ }  |  j d d d d d d	 d
 d ƒ|  j d d d d d d	 d
 d ƒ|  j d d d t d t d
 d t ƒ|  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.
    ÚprogÚdescriptionz/Report the canonical cloud-id for this instancez-jz--jsonÚactionÚ
store_trueÚdefaultFÚhelpz5Report all standardized cloud-id information as json.z-lz--longz=Report extended cloud-id information as tab-delimited string.z-iz--instance-dataÚtypez.Path to instance-data.json file. Default is %s)Ú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   s   t  j j d |  ƒ d S)Nz
ERROR: %s
é   )ÚsysÚstderrÚwrite)Úmsgr   r   r   Úerror+   s    r   c             C   sB  y t  j t | j ƒ ƒ } Wn^ t k
 rA t d | j ƒ SYn< t k
 r| } z t d | j | f ƒ SWYd d } ~ Xn X| j d i  ƒ } t | j d t	 ƒ | j d t	 ƒ | j d t	 ƒ ƒ } | j  rü | | d <t  j
 | d	 d
 d d d d ƒ} n. | j r$d | | j d t	 ƒ f } n | } t j 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 otherwise.
    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Úindentr   Ú	sort_keysTZ
separatorsú,ú: z%s	%sz%s
r   )r!   r"   )ÚjsonÚloadÚopenÚinstance_dataÚIOErrorr   Ú
ValueErrorÚgetr   r   ÚdumpsZlongr   Ústdoutr   )ÚnameÚargsr&   Úer   r   Úresponser   r   r   Úhandle_args0   s.    '	
	r0   c              C   s)   t  ƒ  }  t j t t |  j ƒ  ƒ ƒ d S)z,Tool to query specific instance-data values.N)r   r   Úexitr0   r   Ú
parse_args)r   r   r   r   ÚmainQ   s    	r3   Ú__main__)Ú__doc__r   r#   r   Zcloudinit.sourcesr   r   r   r   r   r   r   r0   r3   Ú__name__r   r   r   r   Ú<module>   s   
!