File: //lib/python3.6/site-packages/sos/collector/transports/__pycache__/control_persist.cpython-36.pyc
3
\��h�! � @ sl d dl Z d dlZd dlZd dlmZ d dlmZmZmZm Z m
Z
mZmZm
Z
d dlmZ G dd� de�ZdS )� N)�RemoteTransport)�InvalidPasswordException�TimeoutPasswordAuthException�PasswordRequestException�AuthPermissionDeniedException�ConnectionException�ConnectionTimeoutException�ControlSocketMissingException�"ControlPersistUnsupportedException)�sos_get_command_outputc @ sV e Zd ZdZdZdd� Zddd�Zdd � Zed
d� �Z edd
� �Z
dd� Zdd� ZdS )�SSHControlPersista�
A transport for collect that leverages OpenSSH's ControlPersist
functionality which uses control sockets to transparently keep a connection
open to the remote host without needing to rebuild the SSH connection for
each and every command executed on the node.
This transport will by default assume the use of SSH keys, meaning keys
have already been distributed to target nodes. If this is not the case,
users will need to provide a password using the --password or
--password-per-node option, depending on if the password to connect to all
nodes is the same or not. Note that these options prevent the use of the
--batch option, as they require user input.
Zcontrol_persistc C sX dddg}t j|t jt jd��0}|j� \}}|jd�}d|ksFd|krJt�W dQ R X d S )
a Checks to see if the local system supported SSH ControlPersist.
ControlPersist allows OpenSSH to keep a single open connection to a
remote host rather than building a new session each time. This is the
same feature that Ansible uses in place of paramiko, which we have a
need to drop in sos collect.
This check relies on feedback from the ssh binary. The command being
run should always generate stderr output, but depending on what that
output reads we can determine if ControlPersist is supported or not.
For our purposes, a host that does not support ControlPersist is not
able to run sos collect.
Returns
True if ControlPersist is supported, else raise Exception.
Zsshz-oZControlPersist)�stdout�stderrzutf-8zBad configuration optionzUsage:NT)�
subprocess�Popen�PIPEZcommunicate�decoder
)�self�ssh_cmd�cmd�_�err� r �%/usr/lib/python3.6/control_persist.py�_check_for_control_persist- s
z,SSHControlPersist._check_for_control_persist� c C s� y| j � W n tk
r, | jd� � Y nX | jd� | j� d| j� �| _d| _d}d}d}| jj dkrzd| jj � d�}| jj
r�d | jj
� �}d
|� d|� d| j� d| jj� d| j� d
�}tj
|dd�}dddddtjg}|j|dd�}|dkr�d}n�|dk�rb|�r\ddtjg} |j|� |j| dd�}
|
dk�r<d}n|
dk�rLt�n|
dk�r`t�nt�nb|dk�rrt�nR|dk�r�t| j| jj ��n6|dk�r�t| j��n |dk�r�t�ntd|j� ���|�r�tjj| j��s�t�| jd | j� �� dS dS )!a.
Using ControlPersist, create the initial connection to the node.
This will generate an OpenSSH ControlPersist socket within the tmp
directory created or specified for sos collect to use.
At most, we will wait 30 seconds for a connection. This involves a 15
second wait for the initial connection attempt, and a subsequent 15
second wait for a response when we supply a password.
Since we connect to nodes in parallel (using the --threads value), this
means that the time between 'Connecting to nodes...' and 'Beginning
collection of sosreports' that users see can be up to an amount of time
equal to 30*(num_nodes/threads) seconds.
Returns
True if session is successfully opened, else raise Exception
zYOpenSSH ControlPersist is not locally supported. Please update your OpenSSH installation.z,Opening SSH session to create control socketz/.sos-collector-r F� z-p� z-izssh zT -oControlPersist=600 -oControlMaster=auto -oStrictHostKeyChecking=no -oControlPath=�@z "echo Connected"zutf-8)�encodingZ Connectedz password:z.*Permission denied.*z.* port .*: No route to hostz.*Could not resolve hostname.*� )�timeoutr T� z$Permission denied, please try again.� � � � zUnknown error, client returned z'Successfully created control socket at )r r
Z log_errorZlog_infoZtmpdir�address�control_pathr �opts�ssh_port�ssh_key�ssh_user�pexpectZspawnZTIMEOUT�expectZsendliner r r r r r � ExceptionZbefore�os�path�existsr � log_debug)r Zpassword� connectedr+ r* r �resZconnect_expects�indexZpass_expectsZ
pass_indexr r r �_connectH sn
,
zSSHControlPersist._connectc C s^ t jj| j�rPyt j| j� dS tk
rN } z| jd|� �� dS d }~X nX | jd� dS )NTzCould not disconnect properly: Fz?Control socket not present when attempting to terminate session)r0 r1 r2 r( �remover/ r3 )r r r r r �_disconnect� s
zSSHControlPersist._disconnectc C s t jj| j�S )a� Check if the SSH control socket exists
The control socket is automatically removed by the SSH daemon in the
event that the last connection to the node was greater than the timeout
set by the ControlPersist option. This can happen for us if we are
collecting from a large number of nodes, and the timeout expires before
we start collection.
)r0 r1 r2 r( )r r r r r4 � s
zSSHControlPersist.connectedc C s, | j s&d| j� d| jj� d| j� �| _ | j S )Nzssh -oControlPath=r r )r r( r) r, r'