Current File : //usr/lib/python3/dist-packages/cloudinit/cmd/devel/__pycache__/logs.cpython-312.pyc
�

Ӈg{7��J�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m	Z	m
Z
ddlmZm
Z
mZmZmZmZmZddlmZddlmZddlmZmZddlmZdd	lmZmZmZeej@ejBe"��Z#Gd
�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&	dRd)eejNd*ejNfd+�Z(d,ee)d-ejTd.e)d*ee)fd/�Z+d,ee)d-ejTd.e)d*dfd0�Z,d1ejTd2ejTd3e-d*dfd4�Z.d5ejTd3e-d*dfd6�Z/d5ejTd*dfd7�Z0d5ejTd3e-d*dfd8�Z1d9e
e)efd*eejTfd:�Z2ejTd;�fd<ejTd*eejTfd=�Z3d>ejTd*eejTfd?�Z4d@ejTd*eejTfdA�Z5d5ejTd9e
e)efd@ejTd>ejTd3e-d*dfdB�Z6ejTdC�ejTdD�dEfdFe)d9e
e)efd@ejTd>ejTd3e-d*dfdG�Z7dHe8d*dfdI�Z9			dSdFe)dHe8dJe-dKe-d*df
dL�Z:dMe)dNejvd*e8fdO�Z<e"dPk(r-ejze<dQe(�j}���yy)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�"�eZdZUeed<eed<y)�
ApportFile�path�labelN)�__name__�
__module__�__qualname__�str�__annotations__���:/usr/lib/python3/dist-packages/cloudinit/cmd/devel/logs.pyrrs��

�I��Jrrz(/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���|stjdd��}|jdddddd	�
�|jddd
d��|jdddddd��|jddddd��|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)r6r8z--include-userdataz-uF�
store_true�userdataz?DEPRECATED: This is default behavior and this flag does nothing)r6r5r7r8z--redact-sensitivez-rz�Redact potentially sensitive data from logs. Sensitive data may include passwords or keys in user data and root read-only files.)r6r5r8)�argparse�ArgumentParser�add_argument)r.s r�
get_parserr>Os�����(�(��C�
���������
�
�
�������#�
*����������
�M��	��������
$�
�
��Mr�cmd�	file_path�msgc�N�|jjdd��	t|�j}t	||�t
j
d||j�|S#t$r8}t	|t|��t
j
d|�d}Yd}~|Sd}~wwxYw)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�stemrr)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��|jjdd��	|jd�5}tj|||��ddd�t
j
d||j�y#1swY�+xYw#t$r5}t|t|��t
j
d|�Yd}~yd}~wwxYw)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�stderrNrFrG)rHrI�open�
subprocess�callrKrLrM�OSErrorrr)r?r@rA�frOs     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?�3A<�8A?�?	B=�+B8�8B=r�out_dir�include_sensitivec�D�|j�rz|s+|j�jtjzr6|j	dd��t||�tjd|�ytjd|�ytjd|�y)z-Collect a file into what will be the tarball.TrCzcollected file: %sz#sensitive file %s was not collectedzfile %s did not existN)	�is_file�stat�st_mode�S_IROTHrIrrKrL�trace)rrZr[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��tD]Z}tj||jdd�j}ttj|j�|d���\|rdtD]Z}tj||jdd�j}ttj|j�|d���\yy)z'Obtain subiquity logs and config files.�NT)r[)�INSTALLER_APPORT_FILES�pathlib�PathrrHrb� INSTALLER_APPORT_SENSITIVE_FILES)rcr[�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�]�]�+��"&�
�	�rc�b�tddg|dzd��}tgd�|dzd��}|s|xsd	}y
y
)z8Include cloud-init version and dpkg version in the logs.�
cloud-initz	--version�versionzcloud-init --version�r?r@rA)z
dpkg-queryz--showz-f=${Version}
rnzdpkg-versionzdpkg versionz
not-availableN)rP)rcro�dpkg_vers   r�_collect_version_inforr�sM��+�
�;�'��I�%�"��G�
-�E��N�*���H�
��-�o��rc��|rtdg|dzd��tgd�|dzd��tgd�tj|d	�d
��y)z0Include dmesg and journalctl output in the logs.�dmesgz	dmesg.txtzdmesg outputrp)�
journalctlz--boot=0�-o�
short-precisezjournal.txtzsystemd journal of current boot)ruz	--boot=-1rvrwzjournal-previous.txtz systemd journal of previous bootN)rYrgrh)rcr[s  r�_collect_system_logsrx�sV���&��	���+��	
�
#�=��M�)�-��
#�>��,�,�w�(>�?�.�r�log_cfgc#�ZK�t|�D]}tj|����y�w)z7Get paths for cloud-init.log and cloud-init-output.log.N)rrgrh)ryrs  r�_get_cloudinit_logsr{�s+����$�G�,�!���l�l�4� � �!�s�)+z
/etc/cloud�
etc_cloud_dirc#�l�K�|dz|dzdg��fd�|jd�D�Ed{���y7��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�	templatesz99-installer.cfgc3�Z�K�|]"}|j�vr|j�vr|���$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	���tj|jd�|jd�|jd�d�|jd�D�|jd��S)zkGet paths for files in /var/lib/cloud.

    Skip user-provided scripts, semaphores, and old instances.
    zdata/*z
handlers/*zseed/*c3�BK�|]}|j�s�|���y�wr�)r]�r��ps  rr�z%_get_var_lib_cloud.<locals>.<genexpr>s����@�q�A�I�I�K��@�s��z
instance/*zinstance/handlers)�	itertools�chainr��r�s r�_get_var_lib_cloudr�sW��
�?�?����x� ����|�$����x� �@�I�N�N�<�0�@����*�+��r�run_dirc�2�d�|jd�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.
    c3�@K�|]}|jdk7s�|���y�w)zhook-hotplug-cmdN)r�r�s  rr�z_get_run_dir.<locals>.<genexpr>*s����I�!�A�F�F�6H�,H�A�I�s���*r��r�s r�_get_run_dirr�$s��J�w�|�|�C�(�I�Irc���t|�t||�t||�t|�D]>}t	||tj|�jjd�zd��@tjt�t|��t|���D]>}t	||tj|�jjd�z|��@y)z8Collect all cloud-init logs into the provided directory.�/Tr�r�N)rrrxrlr{rbrgrhrH�relative_tor�r�r�r�r�)rcryr�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��	
�	
rz/run/cloud-initz/var/lib/cloudT�tarfilec���tjj|�}tjt
j�j�jd�}t|��5}tj||�}t|||||��tdd|d|t|�j|�d�d�g�d	d	d	�t j#d
|�y	#1swY� xYw)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)rcryr�r�r[�tar�czfz-Cr��NzWrote %s)�osr�abspathr�nowr�utc�date�strftimerrgrhr�rr�replacerK�info)r�ryr�r�r[�dir_name�tmp_dirrcs        r�collect_logsr�Ns����g�g�o�o�g�&�G����X�\�\�"�'�'�)�2�2�3M�N�
�
�W�	�
���,�,�w��1��"�����/�	
�	
�������G��$�$��y��]�B�7�

�		
�
�&�H�H�Z��!�'
�
�s
�,AC�C(r4c�h�tj�|dk(rtj}n&|dk(rtj}ntj
}tj|�tj�}|jtjd��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.
    rrez%(message)sN)r�
reset_logging�logging�INFO�DEBUG�TRACErK�setLevel�
StreamHandler�setFormatter�	Formatter�
addHandler)r4�level�handlers   r�
_setup_loggerr�wsx�������A�~�����	�a���
�
���
�
���L�L����#�#�%�G�����*�*�=�9�:��N�N�7�r�redact_sensitive�include_userdatac���t|�tj�dk7rtd��|rtjd�t
g��}|j�t||jtj|jj�tj|jj�|��|stjd�yy)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.)�ds_deps)r�ryr�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��cfgrgrh�pathsr�r�)r�r4r�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|tj��Yd}~yd}~wwxYw)z}Handle the CLI interface to the module.

    Parse CLI args, redirect all exceptions to stderr, and return an exit code.
    )r4r�r�r�r)�fileNre)r>�
parse_argsr�r4r�r�r:�	Exception�print�sysrS)r�r�rOs   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�)rTF)?�__doc__r;r�r�r�rgr^rUr�rr�typingrrrrr	r
r�
cloudinit.logr�cloudinit.stagesr
�cloudinit.subprr�cloudinit.temp_utilsr�cloudinit.utilrrr�CustomLoggerType�	getLoggerrrKrrirfr<r>rrhrPrY�boolrbrlrrrxr{r�r�r�r�r��intr�r��	Namespacer��exitr�rrr�<module>r�sk��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"��L15�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��J�'�,�,�J�8�G�L�L�+A�J�
�
�\�\�
�
�#�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