o
    x[hˆ  ã                   @   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 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 d
Ze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rpeƒ  dS dS )zFDefine 'clean' utility and handler as part of cloud-init command line.é    N)Úsettings)Úuses_systemd)Úlog_util)ÚCLOUDINIT_NETPLAN_FILE)ÚInit)ÚProcessExecutionErrorÚrunpartsÚsubp)Údel_dirÚdel_fileÚget_config_logfilesÚis_linkÚ
write_filez/etc/machine-idz-/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g d¢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%   ú5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.pyÚ
get_parser*   sX   	ýúû
ûúõr'   Fc           	      C   sˆ  |   ¡  |rt| jƒD ]}t|ƒ q|r.t|ƒ ddg¡r.tD ]}t |¡D ]}t|ƒ q&q|rBt|ƒ ddg¡rBtD ]}t|ƒ q;t	j
 | jj¡sLdS t	j
 | jjd¡}t d| jj ¡D ]=}||krg|sgq^zt	j
 |¡rwt|ƒswt|ƒ nt|ƒ W q^ ty› } 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 )a¶  Helper which removes artifacts dir and optionally log files.

    @param: init: Init object to use
    @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.
    r   r   r   r   Úseedz%s/*zCould not remove {0}: {1}Né   zFailure during run-parts of z: )Úread_cfgr   Úcfgr   ÚsetÚintersectionÚGEN_NET_CONFIG_FILESÚglobÚGEN_SSH_CONFIG_FILESÚosÚpathÚisdirÚpathsÚ	cloud_dirÚjoinr   r
   ÚOSErrorr   ÚerrorÚformatÚstrr   r   ÚCLEAN_RUNPARTS_DIRÚ	Exception)	Úinitr   r   r   Úlog_filer2   ÚconfÚ	seed_pathÚer%   r%   r&   Úremove_artifactsm   sP   

ÿÿ

€€þûÿ€ürB   c              
   C   sº   t g d�}t||j|j|jƒ}|jr!tƒ rttddd� nt	tƒ |dkr[|j
r[|jjdddd	�}z	t|d
d� W |S  tyZ } zt d |t|ƒ¡¡ d}W Y d}~|S d}~ww |S )z3Handle calls to 'cloud-init clean' as a subcommand.)Úds_depszuninitialized
i$  )Úmoder   ÚrebootÚnowN)rD   ÚdelayÚmessageF)Úcapturez-Could not reboot this system using "{0}": {1}r)   )r   rB   r   r   r   Ú
machine_idr   r   ÚETC_MACHINE_IDr   rE   ÚdistroÚshutdown_commandr	   r   r   r8   r9   r:   )ÚnameÚargsr=   Ú	exit_codeÚcmdrA   r%   r%   r&   Úhandle_clean_argsŸ   s4   
ÿÿùÿÿ€ùrR   c                  C   s   t ƒ } t td|  ¡ ƒ¡ dS )z4Tool to collect and tar all cloud-init related logs.r   N)r'   ÚsysÚexitrR   Ú
parse_argsr#   r%   r%   r&   Úmain¼   s   rV   Ú__main__)N)FN)!Ú__doc__r    r/   r1   rS   Ú	cloudinitr   Úcloudinit.distrosr   Úcloudinit.logr   Úcloudinit.net.netplanr   Úcloudinit.stagesr   Úcloudinit.subpr   r   r	   Úcloudinit.utilr
   r   r   r   r   rK   r.   r0   r'   rB   rR   rV   Ú__name__r%   r%   r%   r&   Ú<module>   s:   ú	ÿ

C2
ÿ