
"·T`  ã               @   s×   d  Z  d d l 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 m Z m Z d d „  Z d d d	 „ Z d
 d d „ Z d d „  Z d d „  Z e d k rÓ e ƒ  d S)zEDefine 'clean' utility and handler as part of cloud-init commandline.é    N)ÚInit)ÚProcessExecutionErrorÚsubp)Údel_dirÚdel_fileÚget_config_logfilesÚis_linkc             C   s   t  j j d |  d ƒ d  S)NzERROR: Ú
)ÚsysÚstderrÚwrite)Úmsg© r   ú5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.pyÚerror   s    r   c             C   s”   |  s t  j d d d d ƒ }  |  j d d d d d	 d
 d d d d ƒ|  j d d d d d	 d
 d d ƒ|  j d d d d d	 d
 d d d d ƒ|  S)a#  Build or extend an arg parser for clean utility.

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

    @returns: ArgumentParser with proper argument configuration.
    ÚprogÚcleanZdescriptionzARemove logs and artifacts so cloud-init re-runs on a clean systemz-lz--logsÚactionÚ
store_trueÚdefaultFÚdestÚremove_logsÚhelpzRemove cloud-init logs.z-rz--rebootz;Reboot system after logs are cleaned so cloud-init re-runs.z-sz--seedÚremove_seedz5Remove cloud-init seed directory /var/lib/cloud/seed.)ÚargparseÚArgumentParserÚadd_argument)Úparserr   r   r   Ú
get_parser   s    			r   Fc             C   s:  t  d g  ƒ } | j ƒ  |  rC x! t | j ƒ D] } t | ƒ q/ Wt j j | j j	 ƒ s_ d St j j
 | j j	 d ƒ } x¹ t j d | j j	 ƒ D]ž } | | k r° | r° q” y: t j j | ƒ rß t | ƒ rß t | ƒ n
 t | ƒ Wq” t k
 r1} z% t d j | t | ƒ ƒ ƒ d SWYd d } ~ Xq” Xq” Wd S)a4  Helper which removes artifacts dir and optionally log files.

    @param: remove_logs: Boolean. Set True to delete the cloud_dir path. False
        preserves them.
    @param: remove_seed: Boolean. Set True to also delete seed subdir in
        paths.cloud_dir.
    @returns: 0 on success, 1 otherwise.
    Zds_depsr   Zseedz%s/*zCould not remove {0}: {1}é   N)r   Zread_cfgr   Zcfgr   ÚosÚpathÚisdirÚpathsZ	cloud_dirÚjoinÚglobr   r   ÚOSErrorr   ÚformatÚstr)r   r   ZinitZlog_fileZ	seed_pathr!   Úer   r   r   Úremove_artifacts/   s&    	
 r*   c             C   s›   t  | j | j ƒ } | d k r— | j r— d d d g } y t | d d ƒWnG t k
 r– } z' t d j | t | ƒ ƒ ƒ d } WYd	 d	 } ~ Xn X| S)
z3Handle calls to 'cloud-init clean' as a subcommand.r   Zshutdownz-rZnowZcaptureFz-Could not reboot this system using "{0}": {1}r   N)	r*   r   r   Zrebootr   r   r   r'   r(   )ÚnameÚargsZ	exit_codeÚcmdr)   r   r   r   Úhandle_clean_argsO   s    r.   c              C   s)   t  ƒ  }  t j t d |  j ƒ  ƒ ƒ d S)z4Tool to collect and tar all cloud-init related logs.r   N)r   r
   Úexitr.   Ú
parse_args)r   r   r   r   Úmain^   s    	r1   Ú__main__)Ú__doc__r   r%   r    r
   Zcloudinit.stagesr   Zcloudinit.subpr   r   Zcloudinit.utilr   r   r   r   r   r   r*   r.   r1   Ú__name__r   r   r   r   Ú<module>   s   " 