Ë
    x[hN7  ã                   óJ  — 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ZddlZddl	m	Z	m
Z
 ddlmZmZmZmZmZmZ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  eej@                   ejB                  e"«      «      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&	 dRd)eejN                     d*ejN                  fd+„Z(d,ee)   d-ejT                  d.e)d*ee)   fd/„Z+d,ee)   d-ejT                  d.e)d*dfd0„Z,d1ejT                  d2ejT                  d3e-d*dfd4„Z.d5ejT                  d3e-d*dfd6„Z/d5ejT                  d*dfd7„Z0d5ejT                  d3e-d*dfd8„Z1d9ee)ef   d*eejT                     fd:„Z2 ejT                  d;«      fd<ejT                  d*eejT                     fd=„Z3d>ejT                  d*eejT                     fd?„Z4d@ejT                  d*eejT                     fdA„Z5d5ejT                  d9ee)ef   d@ejT                  d>ejT                  d3e-d*dfdB„Z6 ejT                  dC«       ejT                  dD«      dEfdFe)d9ee)ef   d@ejT                  d>ejT                  d3e-d*dfdG„Z7dHe8d*dfdI„Z9	 	 	 dSdFe)dHe8dJe-dKe-d*df
dL„Z:dMe)dNejv                  d*e8fdO„Z<e"dPk(  r- ejz                   e<dQ e(«       j}                  «       «      «       yy)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                   ó"   — 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ÚparserÚreturnc                 óê   — | st        j                  dd¬«      } | j                  dddddd	¬
«       | j                  dddd¬«       | j                  dddddd¬«       | j                  ddddd¬«       | 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.
    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)r6   r8   z--include-userdataz-uFÚ
store_trueÚuserdataz?DEPRECATED: This is default behavior and this flag does nothing)r6   r5   r7   r8   z--redact-sensitivez-rz€Redact potentially sensitive data from logs. Sensitive data may include passwords or keys in user data and root read-only files.)r6   r5   r8   )ÚargparseÚArgumentParserÚadd_argument)r.   s    r   Ú
get_parserr>   O   s»   € ñ Ü×(Ñ(ØØCô
ˆð ×ÑØØØØØØð ô ð ×ÑØØØ#ð*ð ô ð ×ÑØØØØØàMð ô 	ð ×ÑØØØØð$ð ô 
ð €Mr   ÚcmdÚ	file_pathÚmsgc                 óN  — |j                   j                  dd¬«       	 t        | «      j                  }t	        ||«       t
        j                  d||j                  «       |S # t        $ r8}t	        |t        |«      «       t
        j                  d|«       d}Y d}~|S d}~ww xY w)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   )r?   r@   rA   ÚoutputÚes        r   Ú_write_command_output_to_filerP   ˆ   s“   € ð ×Ñ×Ñ 4°$ÐÔ7ðDÜ�c“×!Ñ!ˆô 	�9˜fÔ%Ü�	‰	Ð-¨s°I·N±NÔCØ€Møô !ò Ü�9œc !›fÔ%Ü�	‰	Ð)¨3Ô/ØŒð €Mûðús   ŸA# Á#	B$Á,-BÂB$c                 ó€  — |j                   j                  dd¬«       	 |j                  d«      5 }t        j                  | ||¬«       ddd«       t
        j                  d||j                  «       y# 1 sw Y   Œ+xY w# t        $ r5}t        |t        |«      «       t
        j                  d|«       Y d}~yd}~ww xY w)zÐHelper 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.
    TrC   Úw)rJ   ÚstderrNrF   rG   )rH   rI   ÚopenÚ
subprocessÚcallrK   rL   rM   ÚOSErrorr   r   )r?   r@   rA   ÚfrO   s        r   Ú_stream_command_output_to_filerY   ›   sœ   € ð ×Ñ×Ñ 4°$ÐÔ7ðDØ�^‰^˜CÓ ð 	5 AÜ�O‰O˜C¨°!Õ4÷	5ô 	�	‰	Ð-¨s°I·N±NÕC÷	5ð 	5ûäò 0Ü�9œc !›fÔ%Ü�	‰	Ð)¨3×/Ñ/ûð0ús.   ŸA? °A3Á	A? Á3A<Á8A? Á?	B=Â+B8Â8B=r   Úout_dirÚinclude_sensitivec                 óD  — | j                  «       rz|s+| j                  «       j                  t        j                  z  r6|j	                  dd¬«       t        | |«       t        j                  d| «       yt        j                  d| «       yt        j                  d| «       y)z-Collect a file into what will be the tarball.TrC   zcollected file: %sz#sensitive file %s was not collectedzfile %s did not existN)	Úis_fileÚstatÚst_modeÚS_IROTHrI   r   rK   rL   Útrace)r   rZ   r[   s      r   Ú_collect_filerb   ®   sn   € ð ‡|�|„~Ù §	¡	£× 3Ñ 3´d·l±lÒ BØ�M‰M $°ˆMÔ6Ü��wÔÜ�I‰IÐ*¨DÕ1ä�I‰IÐ;¸TÕBä�	‰	Ð)¨4Õ0r   Úlog_dirc                 ó–  — t         D ]Z  }t        j                  | |j                  dd «      j                  }t        t        j                  |j                  «      |d¬«       Œ\ |rdt        D ]Z  }t        j                  | |j                  dd «      j                  }t        t        j                  |j                  «      |d¬«       Œ\ yy)z'Obtain subiquity logs and config files.é   NT)r[   )ÚINSTALLER_APPORT_FILESÚpathlibÚPathr   rH   rb   Ú INSTALLER_APPORT_SENSITIVE_FILES)rc   r[   Úsrc_fileÚdestination_dirs       r   Ú_collect_installer_logsrl   ½   sª   € ô +ò 
ˆÜ!Ÿ,™, w°·±¸a¸bÐ0AÓB×IÑIˆÜÜ�L‰L˜Ÿ™Ó'ØØ"ö	
ð
ñ Ü8ò 	ˆHÜ%Ÿl™l¨7°H·M±MÀ!À"Ð4EÓF×MÑMˆOÜÜ—‘˜XŸ]™]Ó+ØØ"&öñ	ð r   c                 ób   — t        ddg| dz  d¬«      }t        g d¢| dz  d¬«      }|s|xs d	}y
y
)z8Include cloud-init version and dpkg version in the logs.ú
cloud-initz	--versionÚversionzcloud-init --version©r?   r@   rA   )z
dpkg-queryz--showz-f=${Version}
rn   zdpkg-versionzdpkg versionznot-availableN)rP   )rc   ro   Údpkg_vers      r   Ú_collect_version_inforr   Ò   sM   € ä+Ø˜;Ð'Ø˜IÑ%Ø"ô€Gô
 -ÚEØ˜NÑ*Øô€Hñ
 ØÒ-˜o‰ð r   c                 óš   — |rt        dg| dz  d¬«       t        g d¢| dz  d¬«       t        g d¢t        j                  | d	«      d
¬«       y)z0Include dmesg and journalctl output in the logs.Údmesgz	dmesg.txtzdmesg outputrp   )Ú
journalctlz--boot=0ú-oúshort-precisezjournal.txtzsystemd journal of current boot)ru   z	--boot=-1rv   rw   zjournal-previous.txtz systemd journal of previous bootN)rY   rg   rh   )rc   r[   s     r   Ú_collect_system_logsrx   â   sV   € ñ Ü&Ø�	Ø Ñ+Øõ	
ô
 #Ú=Ø˜MÑ)Ø-õô
 #Ú>Ü—,‘,˜wÐ(>Ó?Ø.ör   Úlog_cfgc              #   óZ   K  — t        | «      D ]  }t        j                  |«      –— Œ y­w)z7Get paths for cloud-init.log and cloud-init-output.log.N)r   rg   rh   )ry   r   s     r   Ú_get_cloudinit_logsr{   ø   s+   è ø€ ô $ GÓ,ò !ˆÜ�l‰l˜4Ó Ó ñ!ùs   ‚)+z
/etc/cloudÚetc_cloud_dirc              #   ól   ‡K  — | dz  | dz  dgŠˆfd„| j                  d«      D «       E d{  –—†  y7 Œ­w)zÂGet 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   óZ   •K  — | ]"  }|j                   ‰vr|j                  ‰vr|–— Œ$ y ­w©N)ÚnamerH   )Ú.0r   Úignores     €r   ú	<genexpr>z!_get_etc_cloud.<locals>.<genexpr>  s0   øè ø€ ò àØ�9‰9˜FÑ" t§{¡{¸&Ñ'@ô 	ñùs   ƒ(+z**/*N©Úglob)r|   r„   s    @r   Ú_get_etc_cloudrˆ      sE   øè ø€ ð 	˜ÑØ˜Ñ#àð	€Fóà!×&Ñ& vÓ.ô÷ ò ús   ƒ)4¬2­4Ú	cloud_dirc           	      óØ   — t        j                  | j                  d«      | j                  d«      | j                  d«      d„ | j                  d«      D «       | j                  d«      «      S )zkGet paths for files in /var/lib/cloud.

    Skip user-provided scripts, semaphores, and old instances.
    zdata/*z
handlers/*zseed/*c              3   óB   K  — | ]  }|j                  «       sŒ|–— Œ y ­wr�   )r]   )rƒ   Úps     r   r…   z%_get_var_lib_cloud.<locals>.<genexpr>  s   è ø€ Ò@ˆq°A·I±IµKŒÑ@ùs   ‚˜z
instance/*zinstance/handlers)Ú	itertoolsÚchainr‡   ©r‰   s    r   Ú_get_var_lib_cloudr�     sW   € ô
 �?‰?Ø�‰�xÓ Ø�‰�|Ó$Ø�‰�xÓ Ù@�I—N‘N <Ó0Ô@Ø�‰Ð*Ó+óð r   Úrun_dirc                 ó$   — | j                  d«      S )zÀGet 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                 óÊ  — t        | «       t        | |«       t        | |«       t        |«      D ]>  }t	        || t        j                  |«      j                  j                  d«      z  d«       Œ@ t        j                  t        «       t        |¬«      t        |¬«      «      D ]>  }t	        || t        j                  |«      j                  j                  d«      z  |«       Œ@ y)z8Collect all cloud-init logs into the provided directory.ú/Tr�   r”   N)rr   rx   rl   r{   rb   rg   rh   rH   Úrelative_tor�   rŽ   rˆ   r�   r•   )rc   ry   r‘   r‰   r[   Úlogfiles         r   Ú_collect_logs_into_tmp_dirrš   -  sÊ   € ô ˜'Ô"Ü˜Ð"3Ô4Ü˜GÐ%6Ô7ä& wÓ/ò 
ˆô 	ØØ”g—l‘l 7Ó+×2Ñ2×>Ñ>¸sÓCÑCØõ	
ð	
ô —?‘?ÜÓÜ YÔ/Ü˜WÔ%óò 	
ˆô
 	ØØ”g—l‘l 7Ó+×2Ñ2×>Ñ>¸sÓCÑCØõ	
ñ	
r   z/run/cloud-initz/var/lib/cloudTÚtarfilec                 óÖ  — t         j                  j                  | «      } t        j                  t
        j                  «      j                  «       j                  d«      }t        |¬«      5 }t        j                  ||«      }t        |||||¬«       t        dd| d|t        |«      j                  |› d�d«      g«       d	d	d	«       t         j#                  d
| «       y	# 1 sw Y   Œ xY w)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)rc   ry   r‘   r‰   r[   ÚtarÚczfz-Cr—   Ú NzWrote %s)Úosr   Úabspathr   Únowr   ÚutcÚdateÚstrftimer   rg   rh   rš   r   r   ÚreplacerK   Úinfo)r›   ry   r‘   r‰   r[   Údir_nameÚtmp_dirrc   s           r   Úcollect_logsr«   N  sÐ   € ô �g‰g�o‰o˜gÓ&€Gä�‰”X—\‘\Ó"×'Ñ'Ó)×2Ñ2Ð3MÓNð ô 
�WÔ	ð 
 Ü—,‘,˜w¨Ó1ˆÜ"ØØØØØ/õ	
ô 	àØØØØÜ�G“×$Ñ$¨ y° ]°BÓ7ðô		
÷
ô& ‡H�HˆZ˜Õ!÷'
ð 
ús   Á,ACÃC(r4   c                 óh  — t        j                  «        | dk(  rt        j                  }n&| dk(  rt        j                  }nt         j
                  }t        j                  |«       t        j                  «       }|j                  t        j                  d«      «       t        j                  |«       y)z�Set up the logger for CLI use.

    The verbosity controls which level gets printed to stderr. By default,
    DEBUG and TRACE are hidden.
    r   re   z%(message)sN)r   Úreset_loggingÚloggingÚINFOÚDEBUGÚTRACErK   ÚsetLevelÚStreamHandlerÚsetFormatterÚ	FormatterÚ
addHandler)r4   ÚlevelÚhandlers      r   Ú_setup_loggerr¹   w  sx   € ô ×ÑÔØ�A‚~Ü—‘‰Ø	�aŠÜ—‘‰ä—‘ˆÜ‡L�L�ÔÜ×#Ñ#Ó%€GØ×Ñœ×*Ñ*¨=Ó9Ô:Ü‡N�N�7Õr   Úredact_sensitiveÚinclude_userdatac                 óÆ  — t        |«       t        j                  «       dk7  rt        d«      ‚|rt        j                  d«       t        g ¬«      }|j                  «        t        | |j                  t        j                  |j                  j                  «      t        j                  |j                  j                  «      | ¬«       |st        j                  d«       yy)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›   ry   r‘   r‰   r[   z¹WARNING:
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ÚRuntimeErrorrK   Úwarningr   Úread_cfgr«   Úcfgrg   rh   Úpathsr‘   r‰   )r›   r4   rº   r»   Úinits        r   Úcollect_logs_clirÅ   Š  s­   € ô �)ÔÜ	‡y�yƒ{�aÒÜÐ>Ó?Ð?ÙÜ�‰ØIô	
ô ˜Ô€DØ‡M�M„OäØØ—‘Ü—‘˜TŸZ™Z×/Ñ/Ó0Ü—,‘,˜tŸz™z×3Ñ3Ó4Ø.Ð.õñ Ü�‰ðFõ	
ð r   Ú_nameÚargsc                 ó  — t        «       j                  «       }	 t        |j                  |j                  |j
                  |j                  ¬«       y# t        $ r%}t        |t        j                  ¬«       Y d}~yd}~ww xY w)z}Handle the CLI interface to the module.

    Parse CLI args, redirect all exceptions to stderr, and return an exit code.
    )r4   r›   rº   r»   r   )ÚfileNre   )r>   Ú
parse_argsrÅ   r4   r›   rº   r:   Ú	ExceptionÚprintÚsysrS   )rÆ   rÇ   rO   s      r   Úhandle_collect_logs_argsrÎ   «  se   € ô
 ‹<×"Ñ"Ó$€Dð
ÜØ—n‘nØ—L‘LØ!×2Ñ2Ø!Ÿ]™]õ		
ð øÜò Üˆa”c—j‘jÕ!Üûðús   š7A Á	B ÁA;Á;B Ú__main__r    r�   )r   TF)?Ú__doc__r;   r�   r®   r¡   rg   r^   rU   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   rK   r   ri   rf   r<   r>   r   rh   rP   rY   Úboolrb   rl   rr   rx   r{   rˆ   r�   r•   rš   r«   Úintr¹   rÅ   Ú	NamespacerÎ   ÚexitrÊ   r   r   r   ú<module>rÝ      sh  ðñ Nã Û Û Û 	Û Û Û Û 
ß 'ß H× HÑ Hå !Ý !ß 6Ý (ß @Ñ @á
ˆ7×#Ñ#Ð%6 W×%6Ñ%6°xÓ%@ÓA€ô�ô ñ Ø2Ð4Ióñ Ð"Ð$5Ó6ÙÐ8Ð:MÓNð$Ð  ñ Ð@À(ÓKÙØ7Ð9Oóñ Ø7Ð9Oóñ Ð6¸ÓDáØ:Øóñ ØBØóñ ØDØóñ ØBØóñ ØGØ"óñ
 Ð9¸=ÓIÙÐ5°}ÓEÙÐ9¸;ÓGðC"Ð ðL 15ñ6Ø�X×,Ñ,Ñ-ð6à×Ñó6ðrØ	ˆc‰ðà�|‰|ðð 
ðð ˆc�]ó	ð&DØ	ˆc‰ðDØ&Ÿ|™|ðDØ25ðDà	óDð&1Ø
�,‰,ð1Ø!(§¡ð1ØBFð1à	ó1ðØ�\‰\ðØ.2ðà	óð*. 7§<¡<ð .°Dó .ð Ø�\‰\ðØ.2ðà	óð,!Ø�#�s�(‰^ð!àˆg�l‰lÑó!ð #/ '§,¡,¨|Ó"<ñØ—<‘<ðàˆg�l‰lÑóð, '§,¡,ð °8¸G¿L¹LÑ3Ió ð˜'Ÿ,™,ð ¨8°G·L±LÑ+Aó ð
Ø�\‰\ð
à�#�s�(‰^ð
ð �\‰\ð
ð �|‰|ð	
ð
 ð
ð 
ó
ðH )˜GŸL™LÐ):Ó;Ø*˜gŸl™lÐ+;Ó<Ø"ñ&"Øð&"à�#�s�(‰^ð&"ð �\‰\ð&"ð �|‰|ð	&"ð
 ð&"ð 
ó&"ðR˜Sð  Tó ð* Ø!Ø"ñ	
Øð
àð
ð ð
ð ð	
ð
 
ó
ðB Cð ¨x×/AÑ/Að Àcó ð& ˆzÒØ€C‡H�HÑ% b©*«,×*AÑ*AÓ*CÓDÕEð r   