
"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 d l m	 Z	 d d l
 m Z d d l m Z m Z m Z d Z d Z d	 Z d
 Z d Z d Z d d d  Z d d   Z d d   Z 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strftimesleep)uses_systemd)Init)get_cmdline	load_file	load_jsonz/etc/cloud/cloud-init.disabledznot runrunningdoneerrorZdisabledc          	   C   sf   |  s t  j d d d d  }  |  j d d d d d	 d
 d d |  j d d 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.
    progstatusZdescriptionzReport run status of cloud initz-lz--longaction
store_truedefaultFhelpzJReport long format of statuses including run stage name and error messagesz-wz--waitz'Block waiting on cloud-init to complete)argparseArgumentParseradd_argument)parser r   6/usr/lib/python3/dist-packages/cloudinit/cmd/status.py
get_parser   s    			r   c             C   s  t  d g   } | j   t | j  \ } } } | j r xU | t t f k r t j j	 d  t j j
   t | j  \ } } } t d  q= Wt j j	 d  | j r t d j |   | r t d j |   t d j |   n t d j |   | t k rd Sd	 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   Zread_cfg_get_status_detailspathswaitSTATUS_ENABLED_NOT_RUNSTATUS_RUNNINGsysstdoutwriteflushr   ZlongprintformatSTATUS_ERROR)nameargsZinitr   status_detailtimer   r   r   handle_status_args1   s"    
		r-   c             C   s   d } t    j   } t   s' d } n d | k r< d } n~ t j j |   rf d } d j |   } nT d | k r d } d } n9 t j j t j j | j d	   s d } 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=disabledenabledz+Cloud-init disabled by cloud-init-generatorz2Cloud-init enabled by systemd cloud-init-generator)	r   splitr   ospathexistsr'   joinrun_dir)Zdisable_filer   is_disabledZcmdline_partsreasonr   r   r   _is_cloudinit_disabledI   s"    				$	r7   c             C   s  t  } d } i  } t j j |  j d  } t j j |  j d  } t t |   \ } } | ri t } | } t j j |  r t j j |  s t	 } t
 t |   j d i   } g  } d }	 x t | j    D] \ }
 } |
 d k r| rt	 } d j |  } q |
 d k r| } q t | t  r | j | j d	 g    | j d
  pUd } | j d  pjd } | d k r| d k rt	 } t | |  } | |	 k r | }	 q W| rt } d j |  } n | t  k r|	 d k rt } |	 r
t d t |	   } n d } | | | f S)zReturn 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.
     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    r0   r1   r3   r4   r7   CLOUDINIT_DISABLED_FILESTATUS_DISABLEDr2   r!   r	   r   getsorteditemsr'   
isinstancedictextendmaxr(   STATUS_DONEr   r   )r   r   r+   Z	status_v1Zstatus_fileZresult_filer5   r6   r9   Zlatest_eventkeyvaluer:   r;   Z
event_timer,   r   r   r   r   e   sR    	
r   c              C   s)   t    }  t j t d |  j     d S)z$Tool to report status of cloud-init.r   N)r   r"   exitr-   
parse_args)r   r   r   r   main   s    	rJ   __main__)__doc__r   r0   r"   r,   r   r   r   Zcloudinit.distrosr   Zcloudinit.stagesr   Zcloudinit.utilr   r   r	   r<   r    r!   rE   r(   r=   r   r-   r7   r   rJ   __name__r   r   r   r   <module>   s(   5