
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me                 @   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dddgZdg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-idz/etc/netplan/50-cloud-init.yamlz-/etc/NetworkManager/conf.d/99-cloud-init.confz?/etc/NetworkManager/conf.d/30-cloud-init-ip6-addr-gen-mode.confz@/etc/NetworkManager/system-connections/cloud-init-*.nmconnectionz,/etc/systemd/network/10-cloud-init-*.networkz+/etc/network/interfaces.d/50-cloud-init.cfgz)/etc/ssh/sshd_config.d/50-cloud-init.confc          	   C   s   | 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
 | jdddddgg 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.
    cleanzJRemove logs, configs 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   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.z-cz	--configsall
ssh_confignetwork+remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)choicesr   nargsr   r   )argparseArgumentParseradd_argument)parser r#   /usr/lib/python3.6/clean.py
get_parser)   sP    	r%   Fc       	      C   s  t g d}|j  | r2xt|jD ]}t| q"W |rrt|jddgrrx(tD ] }xtj|D ]}t| q^W qNW |rt|jddgrxt	D ]}t| qW t
jj|jjsdS t
jj|jjd}xtjd|jj D ]|}||kr| rqy0t
jj|rt| rt| nt| W q tk
rR } ztdj|t| d	S d
}~X qX qW yttj W n8 tk
r } ztdtj d|  d	S d
}~X nX dS )a  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.
    @param: remove_config: List of strings.
        Can be any of: all, network, ssh_config.
    @returns: 0 on success, 1 otherwise.
    )Zds_depsr   r   r   r   Zseedz%s/*zCould not remove {0}: {1}   NzFailure during run-parts of z: )r   Zread_cfgr   Zcfgr	   setintersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESospathisdirpathsZ	cloud_dirjoinr   r   OSErrorr
   formatstrr   r   ZCLEAN_RUNPARTS_DIR	Exception)	r   r   r   ZinitZlog_filer-   ZconfZ	seed_pather#   r#   r$   remove_artifactsl   sB    




r6   c             C   s   t |j|j|j}|jr6t r.ttddd ntt |dkr|j	rdddg}yt
|dd	 W n: tk
r } ztd
j|t| d}W Y dd}~X nX |S )z3Handle calls to 'cloud-init clean' as a subcommand.zuninitialized
i$  )moder   Zshutdownz-rZnowF)Zcapturez-Could not reboot this system using "{0}": {1}r&   N)r6   r   r   r   Z
machine_idr   r   ETC_MACHINE_IDr	   Zrebootr   r   r
   r2   r3   )nameargsZ	exit_codecmdr5   r#   r#   r$   handle_clean_args   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%   sysexitr<   
parse_args)r"   r#   r#   r$   main   s    r@   __main__)N)FN)__doc__r   r*   r,   r=   Z	cloudinitr   Zcloudinit.distrosr   Zcloudinit.stagesr   Zcloudinit.subpr   r   r   Zcloudinit.utilr   r	   r
   r   r   r   r8   r)   r+   r%   r6   r<   r@   __name__r#   r#   r#   r$   <module>   s.    	
C
2