o
    w7e                     @   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r[e  dS 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)prog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   s.  t g d}|  | rt|jD ]}t| qtj|jj	s!dS tj
|jj	d}td|jj	 D ]<}||kr<|s<q3ztj|rLt|sLt| nt| W q3 tyo } ztd|t| W Y d}~ dS d}~ww zttj W dS  ty } ztdtj d	|  W Y d}~dS d}~ww )
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.
    )ds_depsr   seedz%s/*zCould not remove {0}: {1}N   zFailure during run-parts of z: )r   read_cfgr   cfgr	   ospathisdirpaths	cloud_dirjoinglobr   r   OSErrorr
   formatstrr   r   CLEAN_RUNPARTS_DIR	Exception)r   r   initlog_file	seed_pathr'   er   r   r   remove_artifactsS   s>   
	

r6   c              
   C   s   t |j|j}|jrt rttddd ntt |dkrM|jrMg d}z	t	|dd W |S  t
yL } ztd|t| d	}W Y d
}~|S d
}~ww |S )z3Handle calls to 'cloud-init clean' as a subcommand.zuninitialized
i$  )moder   )shutdownr   nowF)capturez-Could not reboot this system using "{0}": {1}r#   N)r6   r   r   
machine_idr   r   ETC_MACHINE_IDr	   rebootr   r   r
   r.   r/   )nameargs	exit_codecmdr5   r   r   r   handle_clean_argsz   s*   rB   c                  C   s   t  } ttd|   dS )z4Tool to collect and tar all cloud-init related logs.r   N)r    sysexitrB   
parse_argsr   r   r   r   main   s   rF   __main__)N)F)__doc__r   r,   r&   rC   	cloudinitr   cloudinit.distrosr   cloudinit.stagesr   cloudinit.subpr   r   r   cloudinit.utilr   r	   r
   r   r   r   r<   r    r6   rB   rF   __name__r   r   r   r   <module>   s$    	

5'
