o x[h@sdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZdd lmZmZmZmZmZd Ze d d d ddgZdgZdddZdddZddZddZe dkrpedSdS)zFDefine 'clean' utility and handler as part of cloud-init command line.N)settings) uses_systemd)log_util)CLOUDINIT_NETPLAN_FILE)Init)ProcessExecutionErrorrunpartssubp)del_dirdel_fileget_config_logfilesis_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 Cs|s tjddd}|jdddddd d |jd ddd d |jdddddd |jddddddd |jddgdgdddd|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_logszRemove cloud-init logs.)actiondefaultdesthelpz --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.)rrrz-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_confignetwork+ remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)choicesrnargsrr)argparseArgumentParser add_argumentparserr%5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.py get_parser*sX  r'Fc Cs||rt|jD]}t|q |r.t|ddgr.tD]}t|D]}t|q&q|rBt|ddgrBtD]}t|q;t j |j j sLdSt j |j j d}td|j j D]=}||krg|sgq^zt j |rwt|swt|nt|Wq^ty}ztd|t|WYd}~d Sd}~wwzttjWdSty}ztd tjd |WYd}~d Sd}~ww) aHelper 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. rrrrseedz%s/*zCould not remove {0}: {1}NzFailure during run-parts of z: )read_cfgr cfgr set intersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESospathisdirpaths cloud_dirjoinr r OSErrorrerrorformatstrrrCLEAN_RUNPARTS_DIR Exception) initrrrlog_filer2conf seed_pather%r%r&remove_artifactsmsP        rBc Cstgd}t||j|j|j}|jr!trttdddnt t|dkr[|j r[|j j dddd }z t |d d W|StyZ}ztd |t|d }WYd}~|Sd}~ww|S)z3Handle calls to 'cloud-init clean' as a subcommand.)ds_depszuninitialized i$)moderrebootnowN)rDdelaymessageF)capturez-Could not reboot this system using "{0}": {1}r))rrBrrr machine_idrrETC_MACHINE_IDr rEdistroshutdown_commandr rrr8r9r:)nameargsr= exit_codecmdrAr%r%r&handle_clean_argss4 rRcCst}ttd|dS)z4Tool to collect and tar all cloud-init related logs.rN)r'sysexitrR parse_argsr#r%r%r&mainsrV__main__)N)FN)!__doc__r r/r1rS cloudinitrcloudinit.distrosr cloudinit.logrcloudinit.net.netplanrcloudinit.stagesrcloudinit.subprrr cloudinit.utilr r r r rrKr.r0r'rBrRrV__name__r%r%r%r&s:        C2