
    f                         d Z ddlZddlZddlZddl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mZ d	Ze
d
ddddgZdgZddZddZd Zd Zedk(  r e        yy)zEDefine 'clean' utility and handler as part of cloud-init commandline.    N)settings)uses_systemd)CLOUDINIT_NETPLAN_FILE)Init)ProcessExecutionErrorrunpartssubp)del_dirdel_fileerror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           	         | 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
' for golden imagecreation. On next boot, systemd generates a new machine-id. Remove /etc/machine-id on non-systemd environments.)r   r   r   -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   nargsr   r   )argparseArgumentParseradd_argumentparsers    5/usr/lib/python3/dist-packages/cloudinit/cmd/clean.py
get_parserr)   *   s     (($
 &   C  	 J   D   

 2   M    c           
      h   t        g       }|j                          | r%t        |j                        D ]  }t	        |        |rLt        |      j                  ddg      r0t        D ]'  }t        j                  |      D ]  }t	        |        ) |r2t        |      j                  ddg      rt        D ]  }t	        |        t        j                  j                  |j                  j                        syt        j                  j                  |j                  j                  d      }t        j                  d|j                  j                  z        D ]L  }||k(  r|s	 t        j                  j                  |      rt!        |      st#        |       nt	        |       N 	 t-        t.        j0                         y# t$        $ r/}t'        dj)                  |t+        |                   Y d	}~ y
d	}~ww xY w# t2        $ r)}t'        dt.        j0                   d|        Y d	}~y
d	}~ww xY w)a  Helper 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.
    )ds_depsr   r   r   r   seedz%s/*zCould not remove {0}: {1}N   zFailure during run-parts of z: )r   read_cfgr   cfgr   setintersectionGEN_NET_CONFIG_FILESglobGEN_SSH_CONFIG_FILESospathisdirpaths	cloud_dirjoinr   r
   OSErrorr   formatstrr   r   CLEAN_RUNPARTS_DIR	Exception)	r   r   r    initlog_filer7   conf	seed_pathes	            r(   remove_artifactsrF   m   s    DMMO+DHH5 	HX	]+88%9KL( 	D		$ 	 ]+88	 ) 	DTN	 77==--.TZZ116:I		&4::#7#778 
9[	ww}}T"74=
,,-   	-44T3q6BC	
  *8+F+F*Gr!M	
 	s1   &AG*G? 	G<$G77G<?	H1H,,H1c           	         t        |j                  |j                  |j                        }|j                  r,t               rt        t        dd       nt        t               |dk(  r |j                  rg d}	 t        |d       |S |S # t        $ r1}t        dj                  |t        |                   d	}Y d
}~|S d
}~ww xY w)z3Handle calls to 'cloud-init clean' as a subcommand.zuninitialized
i$  )moder   )shutdownr   nowF)capturez-Could not reboot this system using "{0}": {1}r.   N)rF   r   r   r    
machine_idr   r   ETC_MACHINE_IDr   rebootr	   r   r   r=   r>   )nameargs	exit_codecmdrE   s        r(   handle_clean_argsrS      s     $**D,>,>I >~'8uE ^$A~$++'	e$ 9 % 	?FFQ
 I	s   :B 	C&C  Cc                  r    t               } t        j                  t        d| j	                                      y)z4Tool to collect and tar all cloud-init related logs.r   N)r)   sysexitrS   
parse_argsr&   s    r(   mainrX      s&    \FHHw(9(9(;<=r*   __main__)N)FN) __doc__r#   r4   r6   rU   	cloudinitr   cloudinit.distrosr   cloudinit.net.netplanr   cloudinit.stagesr   cloudinit.subpr   r   r	   cloudinit.utilr
   r   r   r   r   r   rM   r3   r5   r)   rF   rS   rX   __name__ r*   r(   <module>rc      s    L   	 
  * 8 ! @ @  #3EF21  0 
@F/d4> zF r*   