
    i                     8   d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZmZmZmZmZ  ej<                  e      Z dZ!edddddgZ"dgZ#de$de%fdZ&ddZ'ddZ(d Z)d Z*edk(  r e*        yy)zFDefine 'clean' utility and handler as part of cloud-init command line.    N)settingssources)	cc_mounts)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.conflog_filereturnc                     	 t        j                  |       }t        j                  |j                        st        j                  |j                        ry	 y# t
        $ r Y yw xY w)zCheck if a log file should be removed.

    Avoid tracebacks from attempting to remove device files.

    @param log_file: Path to the log file to check.
    @returns: True if the file should be removed, False otherwise.
    FT)osstatS_ISBLKst_modeS_ISCHROSError)r   	file_stats     5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.pyshould_remove_log_filer   .   sZ    GGH%	<<	))*dll9;L;L.M /N   s   AA 	A$#A$c           	         | st        j                  dd      } | j                  dddddd	
       | j                  dddd       | j                  ddddd       | j                  dddddd
       | j                  ddg dg ddd       | 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_logszRemove cloud-init logs.)actiondefaultdesthelpz--machine-idzSet /etc/machine-id to 'uninitialized\n' for golden image creation. On next boot, systemd generates a new machine-id. Remove /etc/machine-id on non-systemd environments.)r$   r%   r'   z-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
datasourcefstab+remove_configzbRemove cloud-init generated config files of a certain type. Config types: all, ssh_config, network)choicesr%   nargsr&   r'   )argparseArgumentParseradd_argumentparsers    r   
get_parserr7   ?   s     (($
 &   C  	 J   D   
 2    M    c           
      R   | j                          |r1t        | j                        D ]  }t        |      st	        |        |rLt        |      j                  ddg      r0t        D ]'  }t        j                  |      D ]  }t	        |        ) |r2t        |      j                  ddg      rt        D ]  }t	        |        |r0t        |      j                  ddg      rt        j                          |xr t        |      j                  ddg      }t        j                  j                  | j                  j                         s+t#        j$                  dt&        t(        j*                         y|r	 | j-                         j/                          t        j                  j5                  | j                  j                   d
      }t        j                  d| j                  j                   z        D ]L  }||k(  r|s	 t        j                  j                  |      rt7        |      st9        |       nt	        |       N 	 tC        tD        jF                         y# t0        j2                  $ r. t#        j$                  d	t&        t(        j*                         Y 
w xY w# t:        $ r9}	t#        j<                  dj?                  |tA        |	                   Y d}	~	 yd}	~	ww xY w# tH        $ r3}	t#        j<                  dtD        jF                   d|	        Y d}	~	yd}	~	ww xY w)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, datasource, fstab.
    @returns: 0 on success, 1 otherwise.
    r)   r+   r*   r-   r,   zArtifacts already cleaned.)log	log_levelr   z%No datasource found, nothing cleaned.seedz%s/*zCould not remove {0}: {1}N   zFailure during run-parts of z: )%read_cfgr   cfgr   r   setintersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESr   cleanup_fstabr   pathisdirpaths	cloud_dirr   	multi_logLOGloggingINFOfetchr   r   DataSourceNotFoundExceptionjoinr   r   r   errorformatstrr   r   CLEAN_RUNPARTS_DIR	Exception)
initr#   r(   r/   r   rF   confclean_datasource	seed_pathes
             r   remove_artifactsr[      s    	MMO+DHH5 	#H%h/"	# ]+88%9KL( 	D		$ 	 ]+88	 ) 	DTN	]+88%9IJ!$ ]);)H)H	* 77==--.(ll	

 	JJL  TZZ116:I		&4::#7#778 
9[	ww}}T"74=
,,- 5 22 	7!,, 	   	NN6==dCFKL	
  *8+F+F*Gr!M	
 	sC   9I! AJ%K* !=J"!J"%	K'..K""K'*	L&3)L!!L&c           	         t        g       }t        ||j                  |j                  |j                        }|j
                  r,t               rt        t        dd       nt        t               |dk(  r:|j                  r.|j                  j                  ddd	      }	 t        |d
       |S |S # t        $ r;}t        j                   dj#                  |t%        |                   d}Y d}~|S d}~ww xY w)z3Handle calls to 'cloud-init clean' as a subcommand.)ds_depszuninitialized
i$  )moder   rebootnowN)r^   delaymessageF)capturez-Could not reboot this system using "{0}": {1}r=   )r	   r[   r#   r(   r/   
machine_idr   r   ETC_MACHINE_IDr   r_   distroshutdown_commandr   r
   r   rQ   rR   rS   )nameargsrV   	exit_codecmdrZ   s         r   handle_clean_argsrl      s    D d 0 0$2D2DI >~'8uE ^$A~$++kk** + 
	e$ 9 % 	NN?FFQ
 I	s   !B2 2	C6;0C11C6c                  r    t               } t        j                  t        d| j	                                      y)z4Tool to collect and tar all cloud-init related logs.r   N)r7   sysexitrl   
parse_argsr5   s    r   mainrq      s&    \FHHw(9(9(;<=r8   __main__)N)FN)+__doc__r2   rC   rL   r   r   rn   	cloudinitr   r   cloudinit.configr   cloudinit.distrosr   cloudinit.logr   cloudinit.net.netplanr   cloudinit.stagesr	   cloudinit.subpr
   r   r   cloudinit.utilr   r   r   r   r   	getLogger__name__rK   re   rB   rD   rS   boolr   r7   r[   rl   rq    r8   r   <module>r      s    M    	  
 ' & * " 8 ! @ @  g!"3EF21  0 
S T "BJFR:> zF r8   