name : logs.cpython-39.opt-1.pyc
a

'�Dg{7�@s4dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m	Z	m
Z
ddlmZm
Z
mZmZmZmZmZddlmZddlmZddlmZmZddlmZdd	lmZmZmZeej e�!e"��Z#Gd
d�de�Z$e$dd
�e$dd�e$dd�gZ%e$dd�e$dd�e$dd�e$dd�e$dd�e$dd�e$dd�e$d d!�e$d"d#�e$d$d%�e$d&d%�e$d'd(�gZ&d^eej'ej'd)�d*d+�Z(ee)ej*e)ee)d,�d-d.�Z+ee)ej*e)dd,�d/d0�Z,ej*ej*e-dd1�d2d3�Z.ej*e-dd4�d5d6�Z/ej*dd7�d8d9�Z0ej*e-dd4�d:d;�Z1e
e)efeej*d<�d=d>�Z2e�*d?�fej*eej*d@�dAdB�Z3ej*eej*dC�dDdE�Z4ej*eej*dF�dGdH�Z5ej*e
e)efej*ej*e-ddI�dJdK�Z6e�*dL�e�*dM�dNfe)e
e)efej*ej*e-ddO�dPdQ�Z7e8ddR�dSdT�Z9d_e)e8e-e-ddV�dWdX�Z:e)ej;e8dY�dZd[�Z<e"d\k�r0e�=e<d]e(��>���dS)`zGDefine '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@seZdZUeed<eed<dS)�
ApportFile�pathZlabelN)�__name__�
__module__�__qualname__�str�__annotations__�rr�</usr/lib/python3.9/site-packages/cloudinit/cmd/devel/logs.pyrs
rz(/var/log/installer/autoinstall-user-dataZAutoInstallUserDataz/autoinstall.yamlZAutoInstallYAMLz'/etc/cloud/cloud.cfg.d/99-installer.cfgZInstallerCloudCfgz//var/log/installer/ubuntu_desktop_installer.logZUdiLogz-/var/log/installer/subiquity-server-debug.logZSubiquityServerDebugz-/var/log/installer/subiquity-client-debug.logZSubiquityClientDebugz%/var/log/installer/curtin-install.logZ	CurtinLogz0/var/log/installer/subiquity-curtin-install.confZCurtinInstallConfigz8/var/log/installer/curtin-install/subiquity-initial.confZCurtinConfigInitialz:/var/log/installer/curtin-install/subiquity-curthooks.confZCurtinConfigCurtHooksz8/var/log/installer/curtin-install/subiquity-extract.confZCurtinConfigExtractz=/var/log/installer/curtin-install/subiquity-partitioning.confZCurtinConfigPartitioningz(/var/log/installer/curtin-error-logs.tarZCurtinErrorz$/var/log/installer/curtin-errors.tarz(/var/log/installer/block/probe-data.jsonZ	ProbeData)�parser�returncCsh|stjddd�}|jdddddd	d
�|jddd
dd�|jddddddd�|jdddddd�|S)a2Build 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�	verbosityzBe more verbose.)�action�default�dest�helpz	--tarfilez-tzcloud-init.tar.gzzOThe tarfile to create containing all collected logs. Default: cloud-init.tar.gz)r#r%z--include-userdataz-uF�
store_true�userdataz?DEPRECATED: This is default behavior and this flag does nothing)r#r"r$r%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.)r#r"r%)�argparse�ArgumentParser�add_argument)rrrr�
get_parserOsF���	�
�r+)�cmd�	file_path�msgrc
Cs�|jjddd�zt|�j}WnBty`}z*t|t|��t�d|�d}WYd}~n$d}~00t||�t�d||j	�|S)zCHelper which runs a command and writes output or error to filename.T��parents�exist_ok�collecting %s failed.N�collected %s to file '%s')
�parent�mkdirr�stdoutr
rr�LOG�debug�stem)r,r-r.�output�errr�_write_command_output_to_file�s
r<c
Cs�|jjddd�z>|�d�� }tj|||d�Wd�n1sB0YWn>ty�}z&t|t|��t�	d|�WYd}~nd}~00t�	d||j
�dS)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.
    Tr/�w)r6�stderrNr2r3)r4r5�open�
subprocess�call�OSErrorrrr7r8r9)r,r-r.�fr;rrr�_stream_command_output_to_file�s2"rD)r�out_dir�include_sensitivercCs`|��rP|s|��jtj@rB|jddd�t||�t�d|�q\t�d|�nt�d|�dS)z-Collect a file into what will be the tarball.Tr/zcollected file: %sz#sensitive file %s was not collectedzfile %s did not existN)	�is_file�stat�st_mode�S_IROTHr5rr7r8Ztrace)rrErFrrr�
_collect_file�s
rK)�log_dirrFrcCsxtD]2}t�||jdd��j}tt�|j�|dd�q|rttD]2}t�||jdd��j}tt�|j�|dd�q@dS)z'Obtain subiquity logs and config files.�NT)rF)�INSTALLER_APPORT_FILES�pathlib�Pathrr4rK� INSTALLER_APPORT_SENSITIVE_FILES)rLrFZsrc_fileZdestination_dirrrr�_collect_installer_logs�s
�
�rR)rLrcCs<tddg|ddd�}tgd�|ddd�}|s8|p6d	}d
S)z8Include cloud-init version and dpkg version in the logs.�
cloud-initz	--version�versionzcloud-init --version�r,r-r.)z
dpkg-queryz--showz-f=${Version}
rSzdpkg-versionzdpkg versionz
not-availableN)r<)rLrTZdpkg_verrrr�_collect_version_info�s��rVcCsL|rtdg|ddd�tgd�|ddd�tgd�t�|d	�d
d�dS)z0Include dmesg and journalctl output in the logs.Zdmesgz	dmesg.txtzdmesg outputrU)�
journalctlz--boot=0�-o�
short-precisezjournal.txtzsystemd journal of current boot)rWz	--boot=-1rXrYzjournal-previous.txtz systemd journal of previous bootN)rDrOrP)rLrFrrr�_collect_system_logs�s ��
�rZ)�log_cfgrccst|�D]}t�|�VqdS)z7Get paths for cloud-init.log and cloud-init-output.log.N)rrOrP)r[rrrr�_get_cloudinit_logs�sr\z
/etc/cloud)�
etc_cloud_dirrc#s4|d|ddg��fdd�|�d�D�EdHdS)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
    �keysZ	templatesz99-installer.cfgc3s&|]}|j�vr|j�vr|VqdS�N)�namer4)�.0r��ignorerr�	<genexpr>s�z!_get_etc_cloud.<locals>.<genexpr>z**/*N��glob)r]rrbr�_get_etc_clouds
�
�rg)�	cloud_dirrc	Cs:t�|�d�|�d�|�d�dd�|�d�D�|�d��S)zkGet paths for files in /var/lib/cloud.

    Skip user-provided scripts, semaphores, and old instances.
    zdata/*z
handlers/*zseed/*css|]}|��r|VqdSr_)rG�ra�prrrrd�z%_get_var_lib_cloud.<locals>.<genexpr>z
instance/*zinstance/handlers)�	itertools�chainrf�rhrrr�_get_var_lib_clouds�ro)�run_dirrcCsdd�|�d�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.
    css|]}|jdkr|VqdS)zhook-hotplug-cmdN)r`rirrrrd*rkz_get_run_dir.<locals>.<genexpr>�*re�rprrr�_get_run_dir$srs)rLr[rprhrFrcCs�t|�t||�t||�t|�D]"}t||t�|�j�d�d�q$t	�
t�t|d�t
|d��D]"}t||t�|�j�d�|�qddS)z8Collect all cloud-init logs into the provided directory.�/TrnrrN)rVrZrRr\rKrOrPr4�relative_torlrmrgrors)rLr[rprhrFZlogfilerrr�_collect_logs_into_tmp_dir-s&

���rvz/run/cloud-initz/var/lib/cloudT)�tarfiler[rprhrFrcCs�tj�|�}t�tj����d�}t	|d��T}t
�||�}t|||||d�t
dd|d|t|��|�d�d�g�Wd	�n1s�0Yt�d
|�d	S)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.
    zcloud-init-logs-%Y-%m-%d)�dir)rLr[rprhrF�tarZczfz-Crt�NzWrote %s)�osr�abspathrZnowrZutc�date�strftimerrOrPrvrr�replacer7�info)rwr[rprhrFZdir_nameZtmp_dirrLrrr�collect_logsNs,����"
r�)r!rcCs^t��|dkrtj}n|dkr(tj}ntj}t�|�t��}|�	t�
d��t�|�dS)z�Set up the logger for CLI use.

    The verbosity controls which level gets printed to stderr. By default,
    DEBUG and TRACE are hidden.
    rrMz%(message)sN)rZ
reset_logging�logging�INFO�DEBUGZTRACEr7ZsetLevelZ
StreamHandlerZsetFormatterZ	FormatterZ
addHandler)r!�levelZhandlerrrr�
_setup_loggerws
r�F)rwr!�redact_sensitive�include_userdatarcCsxt|�t��dkrtd��|r*t�d�tgd�}|��t||j	t
�|jj
�t
�|jj�|d�|stt�d�dS)z:Handle calls to 'cloud-init collect-logs' as a subcommand.rz!This command must be run as root.z;The --include-userdata flag is deprecated and does nothing.)Zds_deps)rwr[rprhrFz�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�RuntimeErrorr7ZwarningrZread_cfgr�ZcfgrOrP�pathsrprh)rwr!r�r��initrrr�collect_logs_cli�s(�
��r�)�_name�argsrc
Csbt���}zt|j|j|j|jd�WdSty\}zt|t	j
d�WYd}~dSd}~00dS)z}Handle the CLI interface to the module.

    Parse CLI args, redirect all exceptions to stderr, and return an exit code.
    )r!rwr�r�r)�fileNrM)r+�
parse_argsr�r!rwr�r'�	Exception�print�sysr>)r�r�r;rrr�handle_collect_logs_args�s
�r��__main__rz)N)rTF)?�__doc__r(rlr�r{rOrHr@r�rr�typingrrrrrr	r
Z
cloudinit.logrZcloudinit.stagesrZcloudinit.subpr
rZcloudinit.temp_utilsrZcloudinit.utilrrrZCustomLoggerTypeZ	getLoggerrr7rrQrNr)r+rrPr<rD�boolrKrRrVrZr\rgrorsrvr��intr�r��	Namespacer��exitr�rrrr�<module>s�$��	��������&��:��
���
�	��

�$�
�)��!


© 2025 Cubjrnet7