
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!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rÈeƒ  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-idz§Set /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 |ržt|ƒjddgƒržxt	D ]}t|ƒ qŽW t
jj|jjƒs²dS t
jj|jjdƒ}x’tjd|jj ƒD ]|}||krì| rìqØy0t
jj|ƒ�rt|ƒ �rt|ƒ nt|ƒ W qØ tk
�rR } ztdj|t|ƒƒƒ d	S d
}~X qØX qØW 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	ršd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