܍~cdZddlZddlZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZmZmZmZmZdZddZdd Zd Zd Zed kr edSdS)zEDefine 'clean' utility and handler as part of cloud-init commandline.N)settings)Init)ProcessExecutionErrorrunpartssubp)del_dirdel_fileerrorget_config_logfilesis_linkz/etc/machine-idc|stjdd}|dddddd |d ddd |ddddd |dddddd |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. cleanzARemove logs and artifacts so cloud-init re-runs on a clean system)prog descriptionz-lz--logs store_trueF remove_logszRemove cloud-init logs.)actiondefaultdesthelpz --machine-idzWRemove /etc/machine-id for golden image creation. Next boot generates a new machine-id.)rrr-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.)argparseArgumentParser add_argumentparsers 5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.py get_parserrs  (!       &   5    J     D  MFc ttg}||r&t|jD]}t |t j|jj sdSt j |jj d}tj d|jj zD]}||kr|s t j|rt|st|nt |Z#t$r<}td|t#|Yd}~dSd}~wwxYw t%t&jn7#t*$r*}tdt&jd |Yd}~dSd}~wwxYwdS) a4Helper 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. @returns: 0 on success, 1 otherwise. )ds_depsrseedz%s/*zCould not remove {0}: {1}NzFailure during run-parts of z: )rread_cfgr cfgr ospathisdirpaths cloud_dirjoinglobr rOSErrorr formatstrrrCLEAN_RUNPARTS_DIR Exception)rrinitlog_file seed_pathr(es rremove_artifactsr7Ps    DMMOOO+DH55  H X     7==- . .q TZ16::I &4:#7788   9  [   w}}T"" 74==      -44T3q66BB C C C111111 ,----  M8+F M M! M M   qqqqq  1s1A D E#'0EE#'F F5 F00F5c Jt|j|j}|jrt t |dkrg|jr`gd} t|dnI#t$r<}td |t|d}Yd}~nd}~wwxYw|S)z3Handle calls to 'cloud-init clean' as a subcommand.r)shutdownrnowF)capturez-Could not reboot this system using "{0}": {1}r$N) r7rr machine_idr ETC_MACHINE_IDrebootrrr r/r0)nameargs exit_codecmdr6s rhandle_clean_argsrCws !143CDDI !   A~~$+~'''  e $ $ $ $ $$    ?FFQ    IIIIII   sA B $2BB ct}tjtd|dS)z4Tool to collect and tar all cloud-init related logs.rN)rsysexitrC parse_argsrs rmainrHs8 \\FH w(9(9(;(; < <=====r __main__)N)F)__doc__rr-r'rE cloudinitrcloudinit.stagesrcloudinit.subprrrcloudinit.utilrr r r r r=rr7rCrH__name__r rrQs-LK !!!!!!@@@@@@@@@@#1111h$ $ $ $ N&>>>  zDFFFFFr