
    f'                     Z   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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 dd
lmZ ddlmZmZmZmZmZ dZ G d de
      Z edd       edd       edd      gZ edd       edd       edd       edd       edd       edd        ed!d"       ed#d$       ed%d&       ed'd(       ed)d(       ed*d+      gZd,e fd-Z!d,e fd.Z"d;d/Z#d0 Z$d1 Z%d2 Z&d3 Z'd4 Z(d5 Z)d<d6e*fd7Z+d8 Z,d9 Z-e.d:k(  r ej^                   e-              yy)=zGDefine 'collect-logs' utility and handler to include in cloud-init cmd.    N)datetime)Path)
NamedTuple)read_cfg_paths)Paths)Init)ProcessExecutionErrorsubp)tempdir)chdircopy
ensure_dirget_config_logfiles
write_filez/run/cloud-initc                   "    e Zd ZU eed<   eed<   y)
ApportFilepathlabelN)__name__
__module____qualname__str__annotations__     :/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returnc                  8    t               } | j                  d      S )Nuserdata_raw)r   get_ipath_curpathss    r   _get_user_data_filer1   T   s    E~..r   c                  8    t               } | j                  d      S )Ndata)r   	get_cpathr/   s    r   _get_cloud_data_pathr5   Y   s    E??6""r   c           
          | st        j                  dd      } | j                  dddddd	
       | j                  dddd       t               }| j                  ddddddj	                  |             | S )a2  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.
    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)r=   r?   z--include-userdataz-uF
store_trueuserdatazPOptionally include user-data from {0} which could contain sensitive information.)r=   r<   r>   r?   )argparseArgumentParseradd_argumentr1   format)parseruser_data_files     r   
get_parserrH   ^   s     ((C
    #*   )*N
&&,f^&<  
 Mr   c                     dg}t        j                         dk7  r'|j                  t        i       j                  d          |S )z>Return a list of files to ignore for /run/cloud-init directoryzhook-hotplug-cmdr   instance_data_sensitive)osgetuidappendr   lookups)curdirfilesignored_filess      r   _copytree_rundir_ignore_filesrR      sB     	M 
yy{aU2Y../HIJr   c                 0   t        t        j                  j                  |             	 t	        |       j
                  }t        ||       t        d|z  d|       |S # t        $ r/}t        |t        |             t        d|z  d|       Y d}~yd}~ww xY w)CHelper which runs a command and writes output or error to filename.collected %s
   collecting %s failed.
N)
r   rK   r   dirnamer
   stdoutr   _debugr	   r   )cmdfilenamemsgr;   outputes         r   _write_command_output_to_filer`      s    rwwx()c!!
 	8V$#%q)4 ! >8SV$(3.9==>s   A 	B&%BBc                 ^   t        t        j                  j                  |             	 t	        |d      5 }t        j                  | ||       ddd       t        d|z  d|       y# 1 sw Y   xY w# t        $ r/}t        |t        |             t        d|z  d|       Y d}~yd}~ww xY w)rT   w)rY   stderrNrU   rV   rW   )r   rK   r   rX   open
subprocesscallrZ   OSErrorr   r   )r[   r\   r]   r;   fr_   s         r   _stream_command_output_to_fileri      s    rwwx()5(C  	5AOOC!4	5 	#%q)4	5 	5 >8SV$(3.9==>s.   A4 A(A4 (A1-A4 4	B,=%B''B,c                 N    ||k  r t         j                  j                  |        y y N)sysrc   write)r]   levelr;   s      r   rZ   rZ      s!    	

 r   c                     t         j                  j                  |       rt        | |       t	        d| z  d|       y t	        d| z  d|       y )Nzcollected file: %s
rV   zfile %s did not exist
   )rK   r   isfiler   rZ   )r   out_dirr;   s      r   _collect_filers      s@    	ww~~dT7%,a;(4/I>r   c                    t         D ]h  }t        | |j                  z         j                  }|j	                         st        t        |             t        |j                  t        |      |       j |rrt        D ]h  }t        | |j                  z         j                  }|j	                         st        t        |             t        |j                  t        |      |       j yy)z'Obtain subiquity logs and config files.N)	INSTALLER_APPORT_FILESr   r   parentexistsr   r   rs    INSTALLER_APPORT_SENSITIVE_FILES)log_dirinclude_userdatar;   src_filedestination_dirs        r   collect_installer_logsr}      s    * Fw67>>%%'s?+,hmmS%99E	F
 8 	JH"7X]]#:;BBO"))+3/0(--_)=yI		J r   rz   c                 b   |r7t        j                         dk7  r t        j                  j	                  d       yt        g       }t         j                  j                  |       } t        j                         j                         j                  d      }t        d      5 }t         j                  j                  ||      }t        dd	gt         j                  j                  |d
      d|      }t        g dt         j                  j                  |d      d|      }|s|r|nd}t        d|       t!        d|z  d|       t#        dgt         j                  j                  |d      d|       t#        g dt         j                  j                  |d      d|       |j%                          t'        |j(                        D ]  }t+        |||        |rt-               }	t+        |	||       t/        |||       t         j                  j                  |d      }
t1        |
       t         j                  j3                  t4              rT	 t7        j8                  t4        t         j                  j                  |
d      t:               t!        dt4        z  d|       nt!        dt4        z  d|       t         j                  j3                  t         j                  j                  t4        d             rWtA        tC                     }|j3                         r4t7        j8                  t?        |      tA        |t?        |      z                tE        |      5  tG        d!d"| |jI                  |d#z   d$      g       ddd       ddd       t        j                  j	                  d%| z         y# t6        j<                  $ rU}t        j                  j	                  d       t        j                  j	                  t?        |      dz          Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w)&zCollect all cloud-init logs and tar them up into the provided tarfile.

    @param tarfile: The path of the tar-gzipped file to create.
    @param include_userdata: Boolean, true means include user-data.
    r   zMTo include userdata, root user is required. Try sudo cloud-init collect-logs
rV   )ds_depszcloud-init-logs-%Y-%m-%dz/tmp)dir
cloud-initz	--versionversionzcloud-init --version)r[   r\   r]   r;   )z
dpkg-queryz--showz-f=${Version}
r   zdpkg-versionzdpkg versionznot-availablez	version: z!collected cloud-init version: %s
dmesgz	dmesg.txtzdmesg output)
journalctlz--boot=0z-ozshort-precisezjournal.txtzsystemd journal of current bootrun)ignorez(Failed collecting file(s) due to error:

Nzcollected dir %s
zdirectory '%s' did not exist
disabledtarczvf/ z	Wrote %s
)%rK   rL   rl   rc   rm   r   r   abspathr   utcnowdatestrftimer   joinr`   printrZ   ri   read_cfgr   cfgrs   r1   r}   r   rw   CLOUDINIT_RUN_DIRshutilcopytreerR   Errorr   r   r5   r   r
   replace)tarfilerz   r;   initry   tmp_dirr   dpkg_verlogrG   run_dirr_   cloud_data_dirs                r   collect_logsr      sK    BIIK1,

2	
 Dggoog&Goo$$&//0JKG	V	 CO'',,w0/{+WW\\'95&	
 1IWW\\'>:	
 "*hGk7#3g=q)L&	WW\\';7		
 	'AWW\\'=91		
 	&txx0 	3C#w	2	302N.'9=w(8)D'',,w.777>>+,0%GGLL,78 '*;;Q	J03DD
 77>>"'',,'8*EF!"6"89N$$&'3~#667 7^ 	O%'//'C-*LMN	OECOH JJ\G+,+ << 0

  !LM

  Q$//0$	O 	OECO COsQ   "F+P%>N.CP%#P;P%.PA
PP%PP%P"	P%%P.c                 X    t        |j                  |j                  |j                        S )z:Handle calls to 'cloud-init collect-logs' as a subcommand.)r   r   rA   r;   )nameargss     r   handle_collect_logs_argsr   &  s    dmmT^^DDr   c                  J    t               } t        d| j                               S )z4Tool to collect and tar all cloud-init related logs.r7   )rH   r   
parse_args)rF   s    r   mainr   +  s    \F#NF4E4E4GHHr   __main__rk   )r   )0__doc__rB   rK   r   re   rl   r   pathlibr   typingr   cloudinit.cmd.develr   cloudinit.helpersr   cloudinit.stagesr   cloudinit.subpr	   r
   cloudinit.temp_utilsr   cloudinit.utilr   r   r   r   r   r   r   rx   ru   r   r1   r5   rH   rR   r`   ri   rZ   rs   r}   boolr   r   r   r   exitr   r   r   <module>r      s   N  	   
    . # ! 6 (  &   24I "$568:MN$   @(K79O 79O 6D: B D B G"
 9=I5}E9;GC" J/S /
#c #
+\
5
?JUD UpE
I zCHHTV r   