U
    b  ã                   @   s¾   d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZ dZejG dd	„ d	ejƒƒZdd
d„Zdd„ Zdd„ Zddd„Zdd„ Zedkrºeƒ  dS )zFDefine 'status' utility and handler as part of cloud-init commandline.é    N)ÚgmtimeÚsleepÚstrftime)Úuses_systemd)ÚInit)Úget_cmdlineÚ	load_fileÚ	load_jsonz/etc/cloud/cloud-init.disabledc                   @   s$   e Zd ZdZdZdZdZdZdZdS )ÚUXAppStatusz=Enum representing user-visible cloud-init application status.znot runZrunningZdoneÚerrorÚdisabledN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNOT_RUNÚRUNNINGÚDONEÚERRORÚDISABLED© r   r   ú6/usr/lib/python3/dist-packages/cloudinit/cmd/status.pyr
      s   r
   c                 C   s>   | st jddd�} | jdddddd	� | jd
ddddd	� | S )a%  Build or extend an arg parser for status utility.

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

    @returns: ArgumentParser with proper argument configuration.
    ÚstatuszReport run status of cloud init)ÚprogZdescriptionz-lz--longÚ
store_trueFzJReport long format of statuses including run stage name and error messages)ÚactionÚdefaultÚhelpz-wz--waitz'Block waiting on cloud-init to complete)ÚargparseÚArgumentParserÚadd_argument©Úparserr   r   r   Ú
get_parser    s(    	 ÿú
ûr#   c                 C   s¼   t g d�}| ¡  t|jƒ\}}}|jrt|tjtjfkrhtj	 
d¡ tj	 ¡  t|jƒ\}}}tdƒ q(tj	 
d¡ td |j¡ƒ |jrª|rœtd |¡ƒ td |¡ƒ |tjkr¸dS d	S )
z4Handle calls to 'cloud-init status' as a subcommand.©Zds_depsÚ.g      Ð?Ú
zstatus: {0}z	time: {0}zdetail:
{0}é   r   )r   Úread_cfgÚget_status_detailsÚpathsÚwaitr
   r   r   ÚsysÚstdoutÚwriteÚflushr   ÚprintÚformatÚvalueZlongr   )ÚnameÚargsÚinitr   Ústatus_detailÚtimer   r   r   Úhandle_status_argsA   s     


r8   c                 C   s¢   d}t ƒ  ¡ }tƒ sd}n€d|kr(d}nrtj | ¡rDd}d | ¡}nVd|krVd}d}nDtj tj |jd	¡¡rxd}d
}n"tj tj |jd¡¡r–d}nd}||fS )a  Report whether cloud-init is disabled.

    @param disable_file: The path to the cloud-init disable file.
    @param paths: An initialized cloudinit.helpers.Paths object.
    @returns: A tuple containing (bool, reason) about cloud-init's status and
    why.
    FzCloud-init enabled on sysvinitzcloud-init=enabledz<Cloud-init enabled by kernel command line cloud-init=enabledTzCloud-init disabled by {0}zcloud-init=disabledz;Cloud-init disabled by kernel parameter cloud-init=disabledr   z+Cloud-init disabled by cloud-init-generatorZenabledz2Cloud-init enabled by systemd cloud-init-generatorz'Systemd generator may not have run yet.)	r   Úsplitr   ÚosÚpathÚexistsr1   ÚjoinÚrun_dir)Zdisable_filer*   Úis_disabledZcmdline_partsÚreasonr   r   r   Ú_is_cloudinit_disabledV   s&    
rA   c                 C   s¦  | st g d�}| ¡  |j} tj}d}i }tj | jd¡}tj | jd¡}t	t
| ƒ\}}|rftj}|}tj |¡r˜tj |¡s„tj}tt|ƒƒ di ¡}g }	d}
t| ¡ ƒD ] \}}|dkrÒ|rÐtj}d |¡}q¬|d	krà|}q¬t|tƒr¬|	 | d
g ¡¡ | d¡�p
d}| d¡�pd}|dk�r6|dk�r6tj}t||ƒ}||
kr¬|}
q¬|	�rftj}d |	¡}n|tjk�r‚|
dk�r‚tj}|
�r˜tdt|
ƒƒ}nd}|||fS )zÊReturn a 3-tuple of status, status_details and time of last event.

    @param paths: An initialized cloudinit.helpers.paths object.

    Values are obtained from parsing paths.run_dir/status.json.
    r$   Ú zstatus.jsonzresult.jsonZv1r   ZstagezRunning in stage: {0}Z
datasourceÚerrorsÚstartÚfinishedr&   z%a, %d %b %Y %H:%M:%S %z)r   r(   r*   r
   r   r:   r;   r=   r>   rA   ÚCLOUDINIT_DISABLED_FILEr   r<   r   r	   r   ÚgetÚsortedÚitemsr1   Ú
isinstanceÚdictÚextendÚmaxr   r   r   r   )r*   r5   r   r6   Z	status_v1Zstatus_fileZresult_filer?   r@   rC   Zlatest_eventÚkeyr2   rD   rE   Z
event_timer7   r   r   r   r)   t   s^    
 ÿ

r)   c                  C   s   t ƒ } t td|  ¡ ƒ¡ dS )z$Tool to report status of cloud-init.r   N)r#   r,   Úexitr8   Ú
parse_argsr!   r   r   r   Úmain¯   s    rQ   Ú__main__)N)N)r   r   Úenumr:   r,   r7   r   r   r   Zcloudinit.distrosr   Zcloudinit.stagesr   Zcloudinit.utilr   r   r	   rF   ÚuniqueÚEnumr
   r#   r8   rA   r)   rQ   r   r   r   r   r   Ú<module>   s$   

!
;