a '¡Dgˆã@sàdZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddlmZmZmZdd lmZmZmZmZmZd Ze d d d ddgZdgZddd„Zddd„Zdd„Zdd„Ze dkrÜeƒdS)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 Cs„|stjddd�}|jdddddd d �|jd ddd d �|jdddddd �|jddddddd �|jddgd¢gdddd�|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.)Ú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.)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_configÚnetworkú+Ú remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)ÚchoicesrÚnargsrr)ÚargparseÚArgumentParserÚ add_argument©Úparser©r%ú7/usr/lib/python3.9/site-packages/cloudinit/cmd/clean.pyÚ get_parser*sX ýúû ûúõr'Fc Cs�| ¡|r$t|jƒD] }t|ƒq|r\t|ƒ ddg¡r\tD]}t |¡D] }t|ƒqLq>|r„t|ƒ ddg¡r„tD] }t|ƒqvt j   |j j ¡s˜dSt j  |j j d¡}t d|j j ¡D]~}||krÎ|sÎq¼z*t j   |¡rît|ƒsît|ƒnt|ƒWq¼t�y8}z&t d |t|ƒ¡¡WYd}~d Sd}~00q¼zttjƒWn@t�yŠ}z&t d tj›d |›�¡WYd}~d Sd}~00dS) 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. rrrrZseedz%s/*zCould not remove {0}: {1}NézFailure during run-parts of z: )Zread_cfgr Zcfgr ÚsetÚ intersectionÚGEN_NET_CONFIG_FILESÚglobÚGEN_SSH_CONFIG_FILESÚosÚpathÚisdirÚpathsZ cloud_dirÚjoinr r ÚOSErrorrÚerrorÚformatÚstrrrZCLEAN_RUNPARTS_DIRÚ Exception) ÚinitrrrZlog_filer/ZconfZ seed_pathÚer%r%r&Úremove_artifactsmsD    ÿ    ÿr:c Cs¶tgd�}t||j|j|jƒ}|jrBtƒr:ttddd�nt tƒ|dkr²|j r²|j j dddd �}zt |d d �Wn>ty°}z&t d  |t|ƒ¡¡d }WYd}~n d}~00|S)z3Handle calls to 'cloud-init clean' as a subcommand.)Zds_depszuninitialized i$)ÚmoderÚrebootZnowN)r;ZdelayÚmessageF)Zcapturez-Could not reboot this system using "{0}": {1}r()rr:rrrZ machine_idrrÚETC_MACHINE_IDr r<ZdistroZshutdown_commandr rrr4r5r6)ÚnameÚargsr8Z exit_codeÚcmdr9r%r%r&Úhandle_clean_argsŸs, ÿÿÿÿrBcCstƒ}t td| ¡ƒ¡dS)z4Tool to collect and tar all cloud-init related logs.rN)r'ÚsysÚexitrBÚ parse_argsr#r%r%r&Úmain¼srFÚ__main__)N)FN)!Ú__doc__r r,r.rCZ cloudinitrZcloudinit.distrosrZ cloudinit.logrZcloudinit.net.netplanrZcloudinit.stagesrZcloudinit.subprrr Zcloudinit.utilr r r r rr>r+r-r'r:rBrFÚ__name__r%r%r%r&Ús6     ú ÿ C 2