x[hdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZdd lmZmZmZmZmZd Ze d d d ddgZdgZddZddZdZdZe dk(reyy)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 |stjdd}|jdddddd |jd ddd |jddddd |jdddddd |jddgdgddd|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_argumentparsers 5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.py get_parserr(*s (($     &   C     J     D     2  Mc x|j|r%t|jD] }t||rLt |j ddgr0t D]'}tj|D] }t|)|r2t |j ddgrtD] }t|tjj|jjsytjj|jjd}tjd|jjzD]L}||k(r|s tjj|rt|s t!|n t|N t-t.j0y#t"$r9}t%j&dj)|t+|Yd}~y d}~wwxYw#t2$r3}t%j&d t.j0d |Yd}~y d}~wwxYw) 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_dirjoinrr OSErrorrerrorformatstrr rCLEAN_RUNPARTS_DIR Exception) initrrrlog_filer5conf seed_pathes r'remove_artifactsrEms MMO+DHH5 H X  ]+88%9KL( D $   ]+88  ) D TN  77==-- . TZZ116:I &4::#7#778  9 [  ww}}T"74=  ,,-   NN6==dCFK L  *8+F+F*Gr! M  s1AF8G=8 G:.G55G:= H9)H44H9c tg}t||j|j|j}|j r,t rttddntt|dk(r:|jr.|jjddd } t|d |S|S#t$r;}tj d j#|t%|d }Yd}~|Sd}~wwxYw)z3Handle calls to 'cloud-init clean' as a subcommand.)ds_depszuninitialized i$)moderrebootnowN)rHdelaymessageF)capturez-Could not reboot this system using "{0}": {1}r,)rrErrr machine_idrrETC_MACHINE_IDr rIdistroshutdown_commandr rrr;r<r=)nameargsr@ exit_codecmdrDs r'handle_clean_argsrVs  D  d 0 0$2D2DI  > ~'8u E ^ $A~$++kk**+   e $ 9%  NN?FFQ  I  s! B22 C6;0C11C6crt}tjtd|j y)z4Tool to collect and tar all cloud-init related logs.rN)r(sysexitrV parse_argsr%s r'mainr[s& \FHH w(9(9(; <=r)__main__)N)FN)!__doc__r"r2r4rX cloudinitrcloudinit.distrosr cloudinit.logrcloudinit.net.netplanrcloudinit.stagesrcloudinit.subprr r cloudinit.utilr r r rrrOr1r3r(rErVr[__name__r)r'rgsM *"8!@@#3EF21 0 @F/ d:>  zFr)