U
    Ld                     @   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 ddl	m
Z
 ddlmZmZmZ ddlmZmZmZmZmZmZ dZdd	d
ZdddZdd Zdd Zedkre  dS )zEDefine 'clean' utility and handler as part of cloud-init commandline.    N)settings)uses_systemd)Init)ProcessExecutionErrorrunpartssubp)del_dirdel_fileerrorget_config_logfilesis_link
write_filez/etc/machine-idc                 C   sh   | st jddd} | jdddddd	d
 | j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helpz--machine-idzSet /etc/machine-id to 'uninitialized
' for golden imagecreation. On next boot, systemd generates a new machine-id. Remove /etc/machine-id on non-systemd environments.)r   r   r   -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parser r   5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.py
get_parser   sF    	
r   Fc                 C   s8  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|rt|st| nt| W qf tk
r } z td|t| W Y  dS d}~X Y qfX qfzttj W n@ tk
r2 } z tdtj d	|  W Y dS d}~X Y nX d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}   NzFailure during run-parts of z: )r   Zread_cfgr   Zcfgr	   ospathisdirpathsZ	cloud_dirjoinglobr   r   OSErrorr
   formatstrr   r   ZCLEAN_RUNPARTS_DIR	Exception)r   r   ZinitZlog_fileZ	seed_pathr"   er   r   r   remove_artifactsS   s4    	


r,   c              
   C   s   t |j|j}|jr2t r*ttddd ntt |dkr|jrdddg}zt	|dd	 W n: t
k
r } ztd
|t| d}W 5 d}~X Y nX |S )z3Handle calls to 'cloud-init clean' as a subcommand.zuninitialized
i$  )moder   Zshutdownr   ZnowF)Zcapturez-Could not reboot this system using "{0}": {1}r    N)r,   r   r   Z
machine_idr   r   ETC_MACHINE_IDr	   Zrebootr   r   r
   r(   r)   )nameargsZ	exit_codecmdr+   r   r   r   handle_clean_argsz   s$    
 r2   c                  C   s   t  } ttd|   dS )z4Tool to collect and tar all cloud-init related logs.r   N)r   sysexitr2   
parse_argsr   r   r   r   main   s    r6   __main__)N)F)__doc__r   r&   r!   r3   Z	cloudinitr   Zcloudinit.distrosr   Zcloudinit.stagesr   Zcloudinit.subpr   r   r   Zcloudinit.utilr   r	   r
   r   r   r   r.   r   r,   r2   r6   __name__r   r   r   r   <module>   s     	
5
'