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 ddlmZm	Z	 ddl
mZmZmZmZm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ÚerrorÚget_config_logfilesÚis_linkc                 C   sV   | st jddd�} | jdddddd	d
� | jdddddd� | j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.
    ÚcleanzARemove logs and artifacts so cloud-init re-runs on a clean system)ÚprogZdescriptionz-lz--logsÚ
store_trueFÚremove_logszRemove cloud-init logs.)ÚactionÚdefaultÚdestÚhelpú-rz--rebootz;Reboot system after logs are cleaned so cloud-init re-runs.)r   r   r   z-sz--seedÚremove_seedz5Remove cloud-init seed directory /var/lib/cloud/seed.)ÚargparseÚArgumentParserÚadd_argument©Úparser© r   ú5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.pyÚ
get_parser   s:    	ýúûúr   Fc                 C   sè   t g d�}| ¡  | r.t|jƒD ]}t|ƒ q tj |jj	¡sBdS tj 
|jj	d¡}t d|jj	 ¡D ]|}||krx|sxqfz*tj |¡r˜t|ƒs˜t|ƒ nt|ƒ W qf tk
rà } z td |t|ƒ¡ƒ W Y ¢ dS d}~X Y qfX qfd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_artifactsB   s&    	


r'   c              
   C   sv   t |j|jƒ}|dkrr|jrrdddg}zt|dd� W n: tk
rp } ztd |t|ƒ¡ƒ d}W 5 d	}~X Y nX |S )
z3Handle calls to 'cloud-init clean' as a subcommand.r   Zshutdownr   ZnowF)Zcapturez-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_argsb   s    
 ÿÿr+   c                  C   s   t ƒ } t td|  ¡ ƒ¡ dS )z4Tool to collect and tar all cloud-init related logs.r
   N)r   ÚsysÚexitr+   Ú
parse_argsr   r   r   r   Úmains   s    r/   Ú__main__)N)F)Ú__doc__r   r"   r   r,   Zcloudinit.stagesr   Zcloudinit.subpr   r   Zcloudinit.utilr   r   r   r   r	   r   r'   r+   r/   Ú__name__r   r   r   r   Ú<module>   s   	
+
 