3 \me@sdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z mZddlmZmZmZmZmZmZdZd d d d d dgZdgZdddZdddZddZddZedkredS)zEDefine 'clean' utility and handler as part of cloud-init commandline.N)settings) uses_systemd)Init)ProcessExecutionErrorrunpartssubp)del_dirdel_fileerrorget_config_logfilesis_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 Cs|stjddd}|jdddddd d |jd ddd d |jdddddd |jddddddd |jdddddggdddd|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 --configsall ssh_confignetwork+ remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)choicesrnargsrr)argparseArgumentParser add_argument)parserr#/usr/lib/python3.6/clean.py get_parser)sP r%Fc Cstgd}|j|r2xt|jD] }t|q"W|rrt|jddgrrx(tD] }xtj|D] }t|q^WqNW|rt|jddgrxt D] }t|qWt j j |j jsdSt j j|j jd}xtjd|j jD]|}||kr| rqy0t j j |rt| rt|nt|Wqtk rR}ztdj|t|d Sd }~XqXqWyttjWn8tk r}ztd tjd |d Sd }~XnXdS) aHelper 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. )Zds_depsrrrrZseedz%s/*zCould not remove {0}: {1}NzFailure during run-parts of z: )rZread_cfgr Zcfgr set intersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESospathisdirpathsZ cloud_dirjoinr rOSErrorr formatstrrrZCLEAN_RUNPARTS_DIR Exception) rrrZinitZlog_filer-ZconfZ seed_pather#r#r$remove_artifactslsB         r6cCst|j|j|j}|jr6tr.ttdddntt|dkr|j rdddg}yt |dd Wn:t k r}zt d j |t|d }WYd d }~XnX|S) z3Handle calls to 'cloud-init clean' as a subcommand.zuninitialized i$)moderZshutdownz-rZnowF)Zcapturez-Could not reboot this system using "{0}": {1}r&N)r6rrrZ machine_idrr ETC_MACHINE_IDr Zrebootrrr r2r3)nameargsZ exit_codecmdr5r#r#r$handle_clean_argss  r<cCst}tjtd|jdS)z4Tool to collect and tar all cloud-init related logs.rN)r%sysexitr< parse_args)r"r#r#r$mainsr@__main__)N)FN)__doc__rr*r,r=Z cloudinitrZcloudinit.distrosrZcloudinit.stagesrZcloudinit.subprrrZcloudinit.utilrr r r r r r8r)r+r%r6r<r@__name__r#r#r#r$s.     C 2