%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/sos/collector/transports/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/sos/collector/transports/__pycache__/__init__.cpython-312.pyc

�

i��d;��`�ddlZddlZddlZddlZddlmZddlmZmZddl	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�Z		dd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zy)�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_info1s9�������#�A�&��#�}�}�f�c�:��������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_error7s9�������#�A�&��#�}�}�f�c�:�������$�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=sI���$�$�S�)�������#�A�&��"�m�m�V�S�9�������#�rc�f�|jrd|jvr|jS|jS)N�	localhost)rr�rs rr*zRemoteTransport.hostnameDs(���>�>�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.connectedJs��
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_execQs��rc�X�|tur|j|�S|jd|jj	�jdd�z�|jr8|jtjur|j|j�y|jd�y)Nz%s Transport Detailed Help�_� z5Detailed 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����6��H�H�N�N�,�4�4�S�#�>�?�	@��;�;�3�;�;�o�.E�.E�E����S�[�[�)����G�
rc�`�|jd�|jdtd�z�|jdtd�z�|jd�ddlm}|D]G}td	|z�}d
|j�z}|jdj
d||�d
���Iy)NzSoS Remote Transport Helpz�
Transports define how SoS connects to nodes and executes commands on them for the purposes of an %s run. Generally, this means transports define how commands are wrapped locally so that they are executed on the remote node(s) instead.zsos collectz�Transports are generally selected by the cluster profile loaded for a given execution, however users may explicitly set one using '%s'. Note that not all transports will function for all cluster/node types.z--transport=$transport_namez�By default, OpenSSH Control Persist is attempted. Additional information for each supported transport is available in the following help sections:
r)�
TRANSPORTSzcollect.transports.%szThe '%s' transportz{:>8}{:<45}{:<30}r@F)�newline)rBrGr�sos.collector.sosnoderL�lower�format)rHrIrL�	transport�_sec�_descs      rrAz!RemoteTransport.display_self_helpjs������5�6����
G��=�!�	
"�	
�	���
"��0�1�	
2�	
�	���
)�	
�	5�#�	�I��/�)�;�<�D�(�9�?�?�+<�<�E����#�*�*�3��e�<��
�
�	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�2�td|jz��)z�Actually perform the connection requirements. Should be overridden
        by specific transports that subclass RemoteTransport
        z$Transport %s does not define connect��NotImplementedErrorrCrWs  rrUzRemoteTransport._connect�s ��"�"H�$(�I�I�#.�/�	/rc��d}d}|dkr3|jd|z�	|j|�ry	|dz
}|dkr�3|jd	�t	d
|z��#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�z"Attempting reconnect (#%s) to nodeTz	Attempt #z exception: Nz7Unable to reconnect to node after 5 attempts, aborting.z!last exception from transport: %s)r5rY�	Exceptionr2r)rrX�attempts�last_err�errs     r�	reconnectzRemoteTransport.reconnect�s���
������l��N�N�?�(�J�K�
��<�<��)��*�

��M�H���l�	
���#�	$�!�"E�$,�#-�.�	.��
�
����h��L�M����
�s�A�	B�%B�Bc��	|j�r|jd�y|jd�y#t$r}|jd|z�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: %sN)�_disconnectr5r2r`)rrcs  r�
disconnectzRemoteTransport.disconnect�sV��	=����!����D�E���� /�0���	=��N�N�5��;�<�<��	=�s�!6�6�	A�A�Ac�2�td|jz��)Nz'Transport %s does not define disconnectr[r8s rrfzRemoteTransport._disconnect�s��!�"K�$(�I�I�#.�/�	/rc�r�|jd|z�|rdt|�z}|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 get_pty:     Does ``cmd`` require a pty?
        :type get_pty:      ``bool``

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

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

        :param get_pty:     Does ``cmd`` require execution with a pty?
        :type get_pty:      ``bool``

        :returns:           Output of ``cmd`` and the exit code
        :rtype:             ``dict`` with keys ``output`` and ``status``
        zRunning command %sz/bin/bash -c %s)r5r�_run_command_with_pexpect)r�cmd�timeout�	need_root�env�get_ptys      r�run_commandzRemoteTransport.run_command�s@��8	
���+�c�1�2��#�e�C�j�0�C��-�-�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)rrks  r�_format_cmd_for_execz$RemoteTransport._format_cmd_for_exec�s(���)�)�5��:�6���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#|��y#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)�encodingrn��)�status�output�rootz\[sudo\] password for .*:z	Password:�rl)�r$rr#)rs�pexpect�spawn�
exceptions�ExceptionPexpectr5�value�EOF�TIMEOUTr�ssh_user�extend�expect�_send_pexpect_password�before�close�
exitstatusr)
rrkrlrmrn�resultrc�_expects�index�outs
          rrjz)RemoteTransport._run_command_with_pexpect�s+��$�'�'��,��
��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�Z�)�#�.�.���+�!�!�2�2�	1��N�N�3�9�9�%�!�R�0�0��	1�s�C:�:E�D<�6E�<Ec��|dk(rp|jjs4|jjsd}|j	|�t|��|j
|jj�y|dk(rZ|jjsd}|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``
        r|z>Unable to run command: sudo password required but not providedr$z5Unable to run command as root: no root password givenN)r�sudo_pw�opt�
nopasswd_sudor2r`�sendline�
root_password)rr�r�rs    rr�z&RemoteTransport._send_pexpect_password/s����A�:��9�9�$�$�T�X�X�-C�-C�3�����s�#���n�$��O�O�D�I�I�-�-�.�
�a�Z��9�9�*�*�N�����s�#���n�$��O�O�D�I�I�3�3�4�rc���|jd�}|ddk(r|dj�|_|js|j|_|j	d|jz�|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*rxrryzHostname set to %s)rp�striprrr/)r�_outs  rrVzRemoteTransport._get_hostnameGsf�����
�+����>�Q��!�(�^�1�1�3�D�N��~�~�!�\�\�D�N��
�
�*�T�^�^�;�<��~�~�rc	��d}	|dkr4|dz
}|j||�}|ry|jd|z�|dkr�4|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#Tz File retrieval attempt %s failedz&File retrieval failed after 5 attemptsFz/Exception encountered during retrieval attempt z for �: N)�_retrieve_filer/r`r2)r�fname�destra�retrcs      r�
retrieve_filezRemoteTransport.retrieve_fileWs�����	��Q�,��A�
���)�)�%��6�����
�
�@�8�K�L��Q�,�
�M�M�B�C����	��N�N�+3�U�C�A�
B��I��	�s!�A�A�A�	A9�A4�4A9c�2�td|jz��)Nz*Transport %s does not support file copyingr[)rr�r�s   rr�zRemoteTransport._retrieve_filers��!�"N�$(�I�I�#.�/�	/rc�L�|jd|z�|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``
        zReading file %s)r5�
_read_file)rr�s  r�	read_filezRemoteTransport.read_filevs%��	
���(�5�0�1����u�%�%rc���|jd|zd��}|ddk(r|dSd|dvr|jd|z�y
|jd	|�d
|djd�dd���y
)Nzcat %s�
r{rxrryzNo such filezFile %s does not exist on nodezError reading r�r&r#rw)rpr5r2�split)rr��ress   rr�zRemoteTransport._read_file�s������x�%�/���<���x�=�A���x�=� ���X��.����?�!&� '�(�
���� %�s�8�}�':�':�3�'?���'C� E�F�r)�FNF) �__name__�
__module__�__qualname__rFrC�default_userrr!r/r2r5�propertyr*r;r=�classmethodrJrArYrUrdrgrfrprsrjr�rVr�r�r�r�r:rrrrs�����D��L�2�3�� ������
�����	��	��
��
�����B	�/�.�*=�/�BF�!�%L�N� 2/�h5�0� �6/�
&�rr)r(rr}r�pipesr�sos.collector.exceptionsrr�
sos.utilitiesrrr:rr�<module>r�s*�����	��?��w�wr

Zerion Mini Shell 1.0