
    x[hN7                     t   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	J	r	J
r
  SSKJrJrJrJrJrJrJr  SSKJr  SSKJr  SSKJrJr  SSKJr  SS	KJrJrJr  \" \R@                  \RB                  " \"5      5      r# " S
 S\5      r$\$" SS5      \$" SS5      \$" SS5      /r%\$" SS5      \$" SS5      \$" SS5      \$" SS5      \$" SS5      \$" SS5      \$" SS5      \$" S S!5      \$" S"S#5      \$" S$S%5      \$" S&S%5      \$" S'S(5      /r& SRS)\\RN                     S*\RN                  4S+ jjr(S,\\)   S-\RT                  S.\)S*\\)   4S/ jr+S,\\)   S-\RT                  S.\)S*S4S0 jr,S1\RT                  S2\RT                  S3\-S*S4S4 jr.S5\RT                  S3\-S*S4S6 jr/S5\RT                  S*S4S7 jr0S5\RT                  S3\-S*S4S8 jr1S9\\)\4   S*\\RT                     4S: jr2\RT                  " S;5      4S<\RT                  S*\\RT                     4S= jjr3S>\RT                  S*\\RT                     4S? jr4S@\RT                  S*\\RT                     4SA jr5S5\RT                  S9\\)\4   S@\RT                  S>\RT                  S3\-S*S4SB jr6\RT                  " SC5      \RT                  " SD5      SE4SF\)S9\\)\4   S@\RT                  S>\RT                  S3\-S*S4SG jjr7SH\8S*S4SI jr9   SSSF\)SH\8SJ\-SK\-S*S4
SL jjr:SM\)SN\Rv                  S*\84SO jr<\"SP:X  a-  \Rz                  " \<" SQ\(" 5       R}                  5       5      5        gg)TzGDefine 'collect-logs' utility and handler to include in cloud-init cmd.    N)datetimetimezone)AnyDictIteratorList
NamedTupleOptionalcast)loggers)Init)ProcessExecutionErrorsubp)tempdir)copyget_config_logfiles
write_filec                   *    \ rS rSr% \\S'   \\S'   Srg)
ApportFile   pathlabel N)__name__
__module____qualname____firstlineno__str__annotations____static_attributes__r       :/usr/lib/python3/dist-packages/cloudinit/cmd/devel/logs.pyr   r      s    
IJr!   r   z(/var/log/installer/autoinstall-user-dataAutoInstallUserDataz/autoinstall.yamlAutoInstallYAMLz'/etc/cloud/cloud.cfg.d/99-installer.cfgInstallerCloudCfgz//var/log/installer/ubuntu_desktop_installer.logUdiLogz-/var/log/installer/subiquity-server-debug.logSubiquityServerDebugz-/var/log/installer/subiquity-client-debug.logSubiquityClientDebugz%/var/log/installer/curtin-install.log	CurtinLogz0/var/log/installer/subiquity-curtin-install.confCurtinInstallConfigz8/var/log/installer/curtin-install/subiquity-initial.confCurtinConfigInitialz:/var/log/installer/curtin-install/subiquity-curthooks.confCurtinConfigCurtHooksz8/var/log/installer/curtin-install/subiquity-extract.confCurtinConfigExtractz=/var/log/installer/curtin-install/subiquity-partitioning.confCurtinConfigPartitioningz(/var/log/installer/curtin-error-logs.tarCurtinErrorz$/var/log/installer/curtin-errors.tarz(/var/log/installer/block/probe-data.json	ProbeDataparserreturnc           	          U (       d  [         R                  " SSS9n U R                  SSSSSS	S
9  U R                  SSSSS9  U R                  SSSSSSS9  U R                  SSSSSS9  U $ )a  Build or extend and arg parser for collect-logs utility.

@param parser: Optional existing ArgumentParser instance representing the
    collect-logs subcommand which will be extended to support the args of
    this utility.

@returns: ArgumentParser with proper argument configuration.
zcollect-logsz)Collect and tar all cloud-init debug info)progdescriptionz	--verbosez-vcountr   	verbosityzBe more verbose.)actiondefaultdesthelpz	--tarfilez-tzcloud-init.tar.gzzOThe tarfile to create containing all collected logs. Default: cloud-init.tar.gz)r9   r;   z--include-userdataz-uF
store_trueuserdataz?DEPRECATED: This is default behavior and this flag does nothing)r9   r8   r:   r;   z--redact-sensitivez-rzRedact potentially sensitive data from logs. Sensitive data may include passwords or keys in user data and root read-only files.)r9   r8   r;   )argparseArgumentParseradd_argument)r1   s    r"   
get_parserrA   O   s     ((C
    #*   M  	 $  
 Mr!   cmd	file_pathmsgc                 F   UR                   R                  SSS9   [        U 5      R                  n[	        X5        [
        R                  SX!R                  5        U$ ! [         a8  n[	        U[        U5      5        [
        R                  SU5        Sn SnAU$ SnAff = f)zCHelper which runs a command and writes output or error to filename.Tparentsexist_okcollected %s to file '%s'collecting %s failed.N)
parentmkdirr   stdoutr   LOGdebugstemr   r   )rB   rC   rD   outputes        r"   _write_command_output_to_filerS      s     4$7Dc!! 	9%		-sNNCM ! 9c!f%		)3/ Ms   A 
B (-BB c                 ~   UR                   R                  SSS9   UR                  S5       n[        R                  " XUS9  SSS5        [
        R                  SX!R                  5        g! , (       d  f       N/= f! [         a5  n[        U[        U5      5        [
        R                  SU5         SnAgSnAff = f)zHelper which runs a command and writes output or error to filename.

`subprocess.call` is invoked directly here to stream output to the file.
Otherwise memory usage can be high for large outputs.
TrF   w)rM   stderrNrI   rJ   )rK   rL   open
subprocesscallrN   rO   rP   OSErrorr   r   )rB   rC   rD   frR   s        r"   _stream_command_output_to_filer\      s     4$7D^^C AOOC!4 ! 			-sNNC !  09c!f%		)3//0s.   A= A,A= ,
A:6A= =
B<+B77B<r   out_dirinclude_sensitivec                 Z   U R                  5       (       a  U(       d0  U R                  5       R                  [        R                  -  (       a2  UR	                  SSS9  [        X5        [        R                  SU 5        g[        R                  SU 5        g[        R                  SU 5        g)z-Collect a file into what will be the tarball.TrF   zcollected file: %sz#sensitive file %s was not collectedzfile %s did not existN)	is_filestatst_modeS_IROTHrL   r   rN   rO   trace)r   r]   r^   s      r"   _collect_filere      sn     ||~~		 3 3dll BMM$M6II*D1II;TB		)40r!   log_dirc                    [          HY  n[        R                  " XR                  SS 5      R                  n[        [        R                  " UR                  5      USS9  M[     U(       ad  [         HY  n[        R                  " XR                  SS 5      R                  n[        [        R                  " UR                  5      USS9  M[     gg)z'Obtain subiquity logs and config files.   NT)r^   )INSTALLER_APPORT_FILESpathlibPathr   rK   re    INSTALLER_APPORT_SENSITIVE_FILES)rf   r^   src_filedestination_dirs       r"   _collect_installer_logsro      s     +!,,wab0ABIILL'"	
 + 8H%ll7MM!"4EFMMOX]]+"& 9 r!   c                 j    [        SS/U S-  SS9n[        / SQU S-  SS9nU(       d  U=(       d    S	ng
g
)z8Include cloud-init version and dpkg version in the logs.
cloud-initz	--versionversionzcloud-init --versionrB   rC   rD   )z
dpkg-queryz--showz-f=${Version}
rq   zdpkg-versionzdpkg versionznot-availableN)rS   )rf   rr   dpkg_vers      r"   _collect_version_inforu      sM    +;'I%"G
 -EN*H
 -o r!   c                     U(       a  [        S/U S-  SS9  [        / SQU S-  SS9  [        / SQ[        R                  " U S	5      S
S9  g)z0Include dmesg and journalctl output in the logs.dmesgz	dmesg.txtzdmesg outputrs   )
journalctlz--boot=0-oshort-precisezjournal.txtzsystemd journal of current boot)rx   z	--boot=-1ry   rz   zjournal-previous.txtz systemd journal of previous bootN)r\   rj   rk   )rf   r^   s     r"   _collect_system_logsr{      sV     &	+	

 #=M)-
 #>,,w(>?.r!   log_cfgc              #   `   #    [        U 5       H  n[        R                  " U5      v   M     g7f)z7Get paths for cloud-init.log and cloud-init-output.log.N)r   rj   rk   )r|   r   s     r"   _get_cloudinit_logsr~      s%      $G,ll4   -s   ,.z
/etc/cloudetc_cloud_dirc              #   n   ^#    U S-  U S-  S/mU4S jU R                  S5       5        Sh  vN   g N7f)zGet paths for all files in /etc/cloud.

Excludes:
  /etc/cloud/keys because it may contain non-useful sensitive data.
  /etc/cloud/templates because we already know its contents
keys	templatesz99-installer.cfgc              3   p   >#    U  H+  nUR                   T;  d  M  UR                  T;  d  M'  Uv   M-     g 7fN)namerK   ).0r   ignores     r"   	<genexpr>!_get_etc_cloud.<locals>.<genexpr>  s7      .D99F" 	'+{{&'@ 	.s   66	6z**/*Nglob)r   r   s    @r"   _get_etc_cloudr      sD      	#	F!&&v.  s   *535	cloud_dirc           	          [         R                  " U R                  S5      U R                  S5      U R                  S5      S U R                  S5       5       U R                  S5      5      $ )zcGet paths for files in /var/lib/cloud.

Skip user-provided scripts, semaphores, and old instances.
zdata/*z
handlers/*zseed/*c              3   R   #    U  H  oR                  5       (       d  M  Uv   M     g 7fr   )r`   )r   ps     r"   r   %_get_var_lib_cloud.<locals>.<genexpr>  s     @0qIIK0s   '	'z
instance/*zinstance/handlers)	itertoolschainr   r   s    r"   _get_var_lib_cloudr     sW    
 ??x |$x @INN<0@*+ r!   run_dirc                 $    U R                  S5      $ )zGet all paths under /run/cloud-init except for hook-hotplug-cmd.

Note that this only globs the top-level directory as there are currently
no relevant files within subdirectories.
*r   r   s    r"   _get_run_dirr   $  s     <<r!   c                    [        U 5        [        X5        [        X5        [        U5       H@  n[	        UU [
        R                  " U5      R                  R                  S5      -  S5        MB     [        R                  " [        5       [        US9[        US95       H@  n[	        UU [
        R                  " U5      R                  R                  S5      -  U5        MB     g)z8Collect all cloud-init logs into the provided directory./Tr   r   N)ru   r{   ro   r~   re   rj   rk   rK   relative_tor   r   r   r   r   )rf   r|   r   r   r^   logfiles         r"   _collect_logs_into_tmp_dirr   -  s     '"4G7&w/ 	gll7+22>>sCC	
	 0 ??Y/W%
 	gll7+22>>sCC	
r!   z/run/cloud-initz/var/lib/cloudTtarfilec                    [         R                  R                  U 5      n [        R                  " [
        R                  5      R                  5       R                  S5      n[        US9 n[        R                  " Xe5      n[        UUUUUS9  [        SSU SU[        U5      R                  U S3S5      /5        S	S	S	5        [         R#                  S
U 5        g	! , (       d  f       N%= f)a  Collect all cloud-init logs and tar them up into the provided tarfile.

:param tarfile: The path of the tar-gzipped file to create.
:param log_cfg: The cloud-init base configuration containing logging cfg.
:param run_dir: The path to the cloud-init run directory.
:param cloud_dir: The path to the cloud-init cloud directory.
:param include_sensitive: Boolean, true means include sensitive data.
zcloud-init-logs-%Y-%m-%d)dir)rf   r|   r   r   r^   tarczfz-Cr    NzWrote %s)osr   abspathr   nowr   utcdatestrftimer   rj   rk   r   r   r   replacerN   info)r   r|   r   r   r^   dir_nametmp_dirrf   s           r"   collect_logsr   N  s     ggoog&GX\\"'')223MN  
W	,,w1"/	
 	G$$y]B7		
 
& HHZ!' 
	s   *AC
C(r7   c                 r   [         R                  " 5         U S:X  a  [        R                  nO'U S:X  a  [        R                  nO[         R
                  n[        R                  U5        [        R                  " 5       nUR                  [        R                  " S5      5        [        R                  U5        g)zSet up the logger for CLI use.

The verbosity controls which level gets printed to stderr. By default,
DEBUG and TRACE are hidden.
r   rh   z%(message)sN)r   reset_loggingloggingINFODEBUGTRACErN   setLevelStreamHandlersetFormatter	Formatter
addHandler)r7   levelhandlers      r"   _setup_loggerr   w  sx     A~	aLL##%G**=9:NN7r!   redact_sensitiveinclude_userdatac                    [        U5        [        R                  " 5       S:w  a  [        S5      eU(       a  [        R                  S5        [        / S9nUR                  5         [        U UR                  [        R                  " UR                  R                  5      [        R                  " UR                  R                  5      U(       + S9  U(       d  [        R                  S5        gg)z:Handle calls to 'cloud-init collect-logs' as a subcommand.r   z!This command must be run as root.z;The --include-userdata flag is deprecated and does nothing.)ds_deps)r   r|   r   r   r^   zWARNING:
Sensitive data may have been included in the collected logs.
Please review the contents of the tarball before sharing or
rerun with --redact-sensitive to redact sensitive data.N)r   r   getuidRuntimeErrorrN   warningr   read_cfgr   cfgrj   rk   pathsr   r   )r   r7   r   r   inits        r"   collect_logs_clir     s     )	yy{a>??I	
 DMMOTZZ//0,,tzz334.. F	
 r!   _nameargsc                     [        5       R                  5       n [        UR                  UR                  UR
                  UR                  S9  g! [         a"  n[        U[        R                  S9   SnAgSnAff = f)zuHandle the CLI interface to the module.

Parse CLI args, redirect all exceptions to stderr, and return an exit code.
)r7   r   r   r   r   )fileNrh   )rA   
parse_argsr   r7   r   r   r=   	ExceptionprintsysrV   )r   r   rR   s      r"   handle_collect_logs_argsr     se    
 <""$D
nnLL!22!]]		
  acjj!s   4A 
A;A66A;__main__r   r   )r   TF)?__doc__r>   r   r   r   rj   ra   rX   r   r   r   typingr   r   r   r   r	   r
   r   cloudinit.logr   cloudinit.stagesr   cloudinit.subpr   r   cloudinit.temp_utilsr   cloudinit.utilr   r   r   CustomLoggerType	getLoggerr   rN   r   rl   ri   r?   rA   r   rk   rS   r\   boolre   ro   ru   r{   r~   r   r   r   r   r   intr   r   	Namespacer   exitr   r   r!   r"   <module>r      s]   N    	    
 ' H H H ! ! 6 ( @ @
7##W%6%6x%@A  24I "$568:MN$   @(K79O 79O 6D: B D B G"
 9=I5}E9;GC" L 156X,,-66r	c|| 
 c]	&D	cD&||D25D	D&1
,,1!(1BF1	1\\.2	*.7<< .D . \\.2	,!#s(^!gll! #*,,|"<<<gll,',, 8GLL3I ',, 8GLL+A 
\\
#s(^
 \\
 ||	

 
 

H $LL):;%ll+;<"&"&"#s(^&" \\&" ||	&"
 &" 
&"RS T * !"	


 
 	

 

BC x/A/A c & zHH%b*,*A*A*CDE r!   