o
    i                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlmZ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 dd	lmZmZmZ dd
lmZmZmZmZmZ eeZ dZ!edddddgZ"dgZ#de$de%fddZ&d ddZ'd!ddZ(dd Z)dd Z*edkre*  dS dS )"zFDefine 'clean' utility and handler as part of cloud-init command line.    N)settingssources)	cc_mounts)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.conflog_filereturnc                 C   sD   zt | }t|jst|jrW dS W dS  ty!   Y dS w )zCheck if a log file should be removed.

    Avoid tracebacks from attempting to remove device files.

    @param log_file: Path to the log file to check.
    @returns: True if the file should be removed, False otherwise.
    FT)osstatS_ISBLKst_modeS_ISCHROSError)r   	file_stat r   5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.pyshould_remove_log_file.   s   
r   c              	   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-idzSet /etc/machine-id to 'uninitialized\n' for golden image creation. 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
datasourcefstab+remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)choicesr#   nargsr$   r%   )argparseArgumentParseradd_argumentparserr   r   r   
get_parser?   sX   	
r5   Fc           
      C   s  |    |rt| jD ]
}t|rt| q|r2t|ddgr2tD ]}t|D ]}t| q*q#|rFt|ddgrFt	D ]}t| q?|rUt|ddgrUt
  |o_t|ddg}tj| jjsstjdttjd dS |rz|    W n tjy   tjd	ttjd Y nw tj| jjd
}td| jj D ]=}||kr|sqztj|rt|st| 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, datasource, fstab.
    @returns: 0 on success, 1 otherwise.
    r'   r)   r(   r+   r*   zArtifacts already cleaned.)log	log_levelr   z%No datasource found, nothing cleaned.seedz%s/*zCould not remove {0}: {1}N   zFailure during run-parts of z: )%read_cfgr   cfgr   r   setintersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESr   cleanup_fstabr   pathisdirpaths	cloud_dirr   	multi_logLOGloggingINFOfetchr   r   DataSourceNotFoundExceptionjoinr   r   r   errorformatstrr
   r   CLEAN_RUNPARTS_DIR	Exception)
initr!   r&   r-   r   rB   confclean_datasource	seed_pather   r   r   remove_artifacts   s|   



rW   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)rY   delaymessageF)capturez-Could not reboot this system using "{0}": {1}r9   )r   rW   r!   r&   r-   
machine_idr   r   ETC_MACHINE_IDr   rZ   distroshutdown_commandr   r	   r   rM   rN   rO   )nameargsrR   	exit_codecmdrV   r   r   r   handle_clean_args   s4   
rg   c                  C   s   t  } ttd|   dS )z4Tool to collect and tar all cloud-init related logs.r   N)r5   sysexitrg   
parse_argsr3   r   r   r   main   s   rk   __main__)N)FN)+__doc__r0   r?   rH   r   r   rh   	cloudinitr   r   cloudinit.configr   cloudinit.distrosr   cloudinit.logr   cloudinit.net.netplanr   cloudinit.stagesr   cloudinit.subpr	   r
   r   cloudinit.utilr   r   r   r   r   	getLogger__name__rG   r`   r>   r@   rO   boolr   r5   rW   rg   rk   r   r   r   r   <module>   sD   
	

EI
