Current File : //usr/lib/python3/dist-packages/sos/collector/transports/__pycache__/__init__.cpython-312.pyc
�

-�_gA��`�ddlZddlZddlZddlmZddlZddlmZmZddl	m
Z
Gd�d�Zy)�N)�quote)�ConnectionException�CommandTimeoutException)�boldc��eZdZdZdZdZd�Zd�Zd�Zd�Z	d�Z
ed	��Zed
��Z
ed��Zed��Zed
��Zd�Zd�Zd�Zd�Zd�Zed��Z		dd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"y) �RemoteTransportaOThe base class used for defining supported remote transports to connect
    to remote nodes in conjunction with `sos collect`.

    This abstraction is used to manage the backend connections to nodes so that
    SoSNode() objects can be leveraged generically to connect to nodes, inspect
    those nodes, and run commands on them.
    �	undefinedNc���||_|d|_|d|_|d|_d|_tjd�|_tjd�|_y)N�cmdlineopts�tmpdir�	need_sudo�sos�sos_ui)	�address�optsrr
�	_hostname�logging�	getLogger�soslog�ui_log)�selfr�commonss   �C/usr/lib/python3/dist-packages/sos/collector/transports/__init__.py�__init__zRemoteTransport.__init__#sX������M�*��	��h�'��� ��-�������'�'��.����'�'��1���c�4�d}tj|d|�S)zUAttempts to obfuscate sensitive information in log messages such as
        passwordsz>(?P<var>(pass|key|secret|PASS|KEY|SECRET).*?=)(?P<value>.*?\s)z\g<var>****** )�re�sub)r�msg�regs   r�_sanitize_log_msgz!RemoteTransport._sanitize_log_msg,s��P���v�v�c�,�c�2�2rc��tj�dd}d|j�d|�d|��}|jj	|�y)z#Used to print and log info messages���[�:�] N)�inspect�stack�hostnamer�info�rr�caller�lmsgs    r�log_infozRemoteTransport.log_info2sE�������#�A�&���4�=�=�/��6�(�"�S�E�2��������rc��tj�dd}d|j�d|�d|��}|jj	|�y)z$Used to print and log error messagesr#r$r%r&r'N)r(r)r*r�errorr,s    r�	log_errorzRemoteTransport.log_error8sE�������#�A�&���4�=�=�/��6�(�"�S�E�2�������$�rc��|j|�}tj�dd}d|j�d|�d|��}|jj|�y)z$Used to print and log debug messagesr#r$r%r&r'N)r!r(r)r*r�debug)rrr-s   r�	log_debugzRemoteTransport.log_debug>sU���$�$�S�)�������#�A�&���$�-�-���&���C�5�1�������#�rc�f�|jrd|jvr|jS|jS)N�	localhost)rr�rs rr*zRemoteTransport.hostnameEs(���>�>�k����?��>�>�!��|�|�rc��y)z�Is the transport __currently__ connected to the node, or otherwise
        capable of seamlessly running a command or similar on the node?
        F�r8s r�	connectedzRemoteTransport.connectedKs��
rc��y)a�This is the command string needed to leverage the remote transport
        when executing commands. For example, for an SSH transport this would
        be the `ssh <options>` string prepended to any command so that the
        command is executed by the ssh binary.

        This is also referenced by the `remote_exec` parameter for policies
        when loading a policy for a remote node
        Nr:r8s r�remote_execzRemoteTransport.remote_execRs��rc�X�|tur|j|�S|j|jj	�jdd��d��|jr8|jtjur|j|j�y|jd�y)N�_� z Transport Detailed Helpz5Detailed information not available for this transport)r�display_self_help�	set_title�name�title�replace�__doc__�add_text)�cls�sections  r�display_helpzRemoteTransport.display_help^s����/�!��(�(��1�1����S�X�X�^�^�-�5�5�c�3�?�@�A4�4�	5��;�;�3�;�;�o�.E�.E�E����S�[�[�)�
�
���G�
�rc�V�|jd�|jdtd��d��|jdtd��d��|jd�d	d
lm}|D]@}td|���}d|j��d
�}|jdd�|d�|d��d���By)NzSoS Remote Transport Helpzb
Transports define how SoS connects to nodes and executes commands on them for the purposes of an zsos collectz� run. Generally, this means transports define how commands are wrapped locally so that they are executed on the remote node(s) instead.z�Transports are generally selected by the cluster profile loaded for a given execution, however users may explicitly set one using 'z--transport=$transport_namezI'. Note that not all transports will function for all cluster/node types.z�By default, OpenSSH Control Persist is attempted. Additional information for each supported transport is available in the following help sections:
r)�
TRANSPORTSzcollect.transports.zThe 'z' transportr@z>8z<45z<30F)�newline)rBrGr�sos.collector.sosnoderL�lower)rHrIrL�	transport�_sec�_descs      rrAz!RemoteTransport.display_self_helpls������5�6����
7�7;�M�7J�6K�L�
�	
�	���
��8�9�:�;D�
D�	
�	���
)�	
�	5�#�	�I��-�i�[�9�:�D��I�O�O�-�.�k�:�E�����r�(�4��*�U�3�K�0��
�
�	rc�`�|j|�r|js|j�yy)z�Perform the connection steps in order to ensure that we are able to
        connect to the node for all future operations. Note that this should
        not provide an interactive shell at this time.
        TF)�_connectr�
_get_hostname�r�passwords  r�connectzRemoteTransport.connect�s)��
�=�=��"��>�>��"�"�$��rc�4�td|j�d���)z�Actually perform the connection requirements. Should be overridden
        by specific transports that subclass RemoteTransport
        �
Transport z does not define connect��NotImplementedErrorrCrVs  rrTzRemoteTransport._connect�s"��"������#;�<�>�	>rc��d}d}|dkr4|jd|�d��	|j|�ry	|dz
}|dkr�4|jd
�t	d|����#t$r#}|jd|�d|���|}Yd	}~�Qd	}~wwxYw)z�Attempts to reconnect to the node using the standard connect()
        but does not do so indefinitely. This imposes a strict number of retry
        attempts before failing out
        r#�unknown�zAttempting reconnect (#z	) to nodeTz	Attempt #z exception: Nz7Unable to reconnect to node after 5 attempts, aborting.zlast exception from transport: )r5rX�	Exceptionr2r)rrW�attempts�last_err�errs     r�	reconnectzRemoteTransport.reconnect�s���
������l��N�N�4�X�J�i�H�I�
��<�<��)��*�

��M�H���l�	
���#�	$�!�$C�H�:�"N�O�O��
�
�����8�*�L���F�G����
�s�A�	B	�&B�B	c��	|j�r|jd�y|jd�y#t$r}|jd|���Yd}~yd}~wwxYw)zfPerform whatever steps are necessary, if any, to terminate any
        connection to the node
        z#Successfully disconnected from nodez;Unable to successfully disconnect, see log for more detailszFailed to disconnect: N)�_disconnectr5r2r`)rrcs  r�
disconnectzRemoteTransport.disconnect�sX��	;����!����D�E���� /�0���	;��N�N�3�C�5�9�:�:��	;�s�!6�6�	A�A�Ac�4�td|j�d���)NrZz does not define disconnectr[r8s rrfzRemoteTransport._disconnect�s"��!������#>�?�A�	Arc��y)z�
        Transports may override this to control when/if commands executed over
        the transport needs to utilize a shell on the remote host.
        Fr:r8s r�_need_shellzRemoteTransport._need_shell�s��rc���|jd|���|dus|dk(r
|jr$nn"dt|���}|jd|���|j||||�S)a�Run a command on the node, returning its output and exit code.
        This should return the exit code of the command being executed, not the
        exit code of whatever mechanism the transport uses to execute that
        command

        :param cmd:         The command to run
        :type cmd:          ``str``

        :param timeout:     The maximum time in seconds to allow the cmd to run
        :type timeout:      ``int```

        :param need_root:   Does ``cmd`` require root privileges?
        :type need_root:   ``bool``

        :param env:         Specify env vars to be passed to the ``cmd``
        :type env:          ``dict``

        :param use_shell:     Does ``cmd`` require execution within a shell?
        :type use_shell:      ``bool`` or ``auto`` for transport-determined

        :returns:           Output of ``cmd`` and the exit code
        :rtype:             ``dict`` with keys ``output`` and ``status``
        zRunning command T�autoz
/bin/bash -c z Shell requested, command is now )r5rjr�_run_command_with_pexpect)r�cmd�timeout�	need_root�env�	use_shells      r�run_commandzRemoteTransport.run_command�so��2	
���)�#��/�0����%.�&�%8��!�!�e�!�%��*��.�C��N�N�=�c�U�C�D��-�-�c�7�I�s�K�Krc�Z�|j�dt|���}|j�}|S)a�Format the command in the way needed for the remote transport to
        successfully execute it as one would when manually executing it

        :param cmd:     The command being executed, as formatted by SoSNode
        :type cmd:      ``str``


        :returns:       The command further formatted as needed by this
                        transport
        :rtype:         ``str``
        r@)r=r�lstrip)rrns  r�_format_cmd_for_execz$RemoteTransport._format_cmd_for_exec�s/���!�!�"�!�E�#�J�<�0���j�j�l���
rc��|j|�}|sd}	tj|d|��}tjtjg}|r,|jjdk7r|jdd	g�|j||�
�}|dvr%|j||�|j||�
�}|dk(r+|j}	|j�|j |	d�S|d
k(rt#|��|jd|�d|���ddd�S#tjj$r*}|j|j�ddd�cYd}~Sd}~wwxYw)a�Execute the command using pexpect, which allows us to more easily
        handle prompts and timeouts compared to directly leveraging the
        subprocess.Popen() method.

        :param cmd:     The command to execute. This will be automatically
                        formatted to use the transport.
        :type cmd:      ``str``

        :param timeout: The maximum time in seconds to run ``cmd``
        :type timeout:  ``int``

        :param need_root:   Does ``cmd`` need to run as root or with sudo?
        :type need_root:    ``bool``

        :param env:     Any env vars that ``cmd`` should be run with
        :type env:      ``dict``
        Nzutf-8)�encodingrq��)�status�output�rootz\[sudo\] password for .*:z	Password:�ro)�r$rr#zUnexpected index z from pexpect: i�)rv�pexpect�spawn�
exceptions�ExceptionPexpectr5�value�EOF�TIMEOUTr�ssh_user�extend�expect�_send_pexpect_password�before�close�
exitstatusr)
rrnrorprq�resultrc�_expects�index�outs
          rrmz)RemoteTransport._run_command_with_pexpectsM��$�'�'��,��
��C�	1��]�]�3��c�B�F�
�K�K����1������+�+�v�5��O�O�-���
�
�
�
�h��
�8���F�?��'�'��v�6��M�M�(�G�M�<�E��A�:��-�-�C��L�L�N�$�/�/�3�?�?��A�:�)�#�.�.�	
���*�5�'����I�J���,�,��7�!�!�2�2�	1��N�N�3�9�9�%�!�R�0�0��	1�s�D�E�2E�E�Ec��|dk(rp|jjs4|jjsd}|j|�t	|��|j|jj�y|dk(rZ|jjsd}|j|�t	|��|j|jj�yy)a`Handle password prompts for sudo and su usage for non-root SSH users

        :param index:       The index pexpect.spawn returned to match against
                            either a sudo or su prompt
        :type index:        ``int``

        :param result:      The spawn running the command
        :type result:       ``pexpect.spawn``
        rz>Unable to run command: sudo password required but not providedr$z5Unable to run command as root: no root password givenN)r�sudo_pw�
nopasswd_sudor2r`�sendline�
root_password)rr�r�rs    rr�z&RemoteTransport._send_pexpect_password;s����A�:��9�9�$�$�T�Y�Y�-D�-D�3�����s�#���n�$��O�O�D�I�I�-�-�.�
�a�Z��9�9�*�*�M�����s�#���n�$��O�O�D�I�I�3�3�4�rc���|jd�}|ddk(r|dj�|_|js|j|_|j	d|j���|jS)z�Determine the hostname of the node and set that for future reference
        and logging

        :returns:   The hostname of the system, per the `hostname` command
        :rtype:     ``str``
        r*r{rr|zHostname set to )rs�striprrr/)r�_outs  rrUzRemoteTransport._get_hostnameSsi�����
�+����>�Q��!�(�^�1�1�3�D�N��~�~�!�\�\�D�N��
�
�(����(8�9�:��~�~�rc	��d}	|dkr5|dz
}|j||�}|ry|jd|�d��|dkr�5|jd�y#t$r!}|jd	|�d
|�d|���|�d}~wwxYw)
aZCopy a local file, fname, to dest on the remote node

        :param fname:   The name of the file to copy
        :type fname:    ``str``

        :param dest:    Where to save the file to remotely
        :type dest:     ``str``

        :returns:   True if file was successfully copied to remote, or False
        :rtype:     ``bool``
        rr$r#TzFile copy attempt � failedz!File copy failed after 3 attemptsFz1Exception encountered during config copy attempt � for �: N)�_copy_file_to_remoter/r`r2�r�fname�destra�retrcs      r�copy_file_to_remotez#RemoteTransport.copy_file_to_remotecs�����	��Q�,��A�
���/�/��t�<�����
�
� 2�8�*�G�D�E��Q�,�
�M�M�=�>����	��N�N�N�&�Z�u�U�G�2�c�U�<�
=��I��	��!�A�A�A�	A:�A5�5A:c�4�td|j�d����NrZz does not support file copyingr[�rr�r�s   rr�z$RemoteTransport._copy_file_to_remote~�"��!������#A�B�D�	Drc	��d}	|dkr5|dz
}|j||�}|ry|jd|�d��|dkr�5|jd�y#t$r!}|jd	|�d
|�d|���|�d}~wwxYw)
a_Copy a remote file, fname, to dest on the local node

        :param fname:   The name of the file to retrieve
        :type fname:    ``str``

        :param dest:    Where to save the file to locally
        :type dest:     ``str``

        :returns:   True if file was successfully copied from remote, or False
        :rtype:     ``bool``
        rr_r#TzFile retrieval attempt r�z&File retrieval failed after 5 attemptsFz/Exception encountered during retrieval attempt r�r�N)�_retrieve_filer/r`r2r�s      r�
retrieve_filezRemoteTransport.retrieve_file�s�����	��Q�,��A�
���)�)�%��6�����
�
� 7��z��I�J��Q�,�
�M�M�B�C����	��N�N�L�&�Z�u�U�G�2�c�U�<�
=��I��	�r�c�4�td|j�d���r�r[r�s   rr�zRemoteTransport._retrieve_file�r�rc�L�|jd|���|j|�S)z�Read the given file fname and return its contents

        :param fname:   The name of the file to read
        :type fname:    ``str``

        :returns:   The content of the file
        :rtype:     ``str``
        z
Reading file )r5�
_read_file)rr�s  r�	read_filezRemoteTransport.read_file�s&��	
����u�g�.�/����u�%�%rc���|jd|��d��}|ddk(r|dSd|dvr|jd|�d	��y|jd
|�d|djd�d
d���y)Nzcat �
r~r{rr|zNo such filezFile z does not exist on nodezError reading r�r&r#rz)rsr5r2�split)rr��ress   rr�zRemoteTransport._read_file�s�������e�W�~�r��:���x�=�A���x�=� ��S��]�*��N�N�U�5�'�)@�A�B��
�N�N�^�E�7�"�!�(�m�1�1�#�6�q�r�:�;�=�
>�r)�FNrl)#�__name__�
__module__�__qualname__rFrC�default_userrr!r/r2r5�propertyr*r;r=�classmethodrJrArXrTrdrgrfrjrsrvrmr�rUr�r�r�r�r�r�r:rrrrs����D��L�2�3�� ������
�����	��	���������@	�>�P�(;�A�����BF�$�$L�L� 7-�r5�0� �6D��6D�
&�	rr)r(rr�shlexrr��sos.collector.exceptionsrr�
sos.utilitiesrrr:rr�<module>r�s*����	���?��_�_r