
                         \   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKJr  SSKJr  S SKrS SKr\R"                  R$                  r " S S\	R                  5      r " S S\R(                  R*                  5      r " S	 S
\R.                  R0                  5      r " S S\5      rg)    N)BaseHTTPAdapter   )	constantsc                   R   ^  \ rS rSrU 4S jrS rS rS rS rS r	S r
S	 rS
rU =r$ )	SSHSocket   c                 t  > [         TU ]  [        R                  [        R                  5        Xl        S U l        S U l        SU R
                  ;   a(  U R
                  R                  S5      u  U l        U l        SU R
                  ;   a(  U R
                  R                  S5      u  U l        U l        S U l	        g )N:@)
super__init__socketAF_INETSOCK_STREAMhostportusersplitproc)selfr   	__class__s     +lib/third_party/docker/transport/sshconn.pyr   SSHSocket.__init__   s    NNF..	0			$))#'99??3#7 DIty$))#'99??3#7 DIty	    c                    S/nU R                   (       a  USU R                   /-   nU R                  (       a  USU R                  /-   nUSU R                  S/-   nS n[        R                  (       d  S nUn[        [        R                  5      nUR                  SS 5        UR                  SS 5        [        R                  " UU[        R                  [        R                  US	9U l        g )
Nsshz-lz-pz--docker system dial-stdioc                  j    [         R                   " [         R                  [         R                  5        g N)signalSIGINTSIG_IGN r   r   fSSHSocket.connect.<locals>.f.   s    fmmV^^<r   LD_LIBRARY_PATHSSL_CERT_FILE)envstdoutstdin
preexec_fn)r   r   r   r   IS_WINDOWS_PLATFORMdictosenvironpop
subprocessPopenPIPEr   )r   kwargsargspreexec_funcr$   r(   s         r   connectSSHSocket.connect"   s    w994++D994++DtTYY(BCC,,=L2:: 	!4(&$$??//#%	r   c                    U R                   (       a%  U R                   R                  R                  (       a  [        S5      eU R                   R                  R	                  U5      nU R                   R                  R                  5         U$ Nz<SSH subprocess not initiated.connect() must be called first.)r   r*   closed	Exceptionwriteflush)r   datawrittens      r   _writeSSHSocket._write?   s\    yyDIIOO22 > ? ?))//''-		r   c                 &    U R                  U5        g r   rA   r   r?   s     r   sendallSSHSocket.sendallG   s    Dr   c                 $    U R                  U5      $ r   rD   rE   s     r   sendSSHSocket.sendJ   s    {{4  r   c                     U R                   (       d  [        S5      eU R                   R                  R                  U5      $ r:   )r   r<   r)   read)r   ns     r   recvSSHSocket.recvM   s6    yy > ? ?yy$$Q''r   c                     U R                   (       d  U R                  5         X R                   R                  l        U R                   R                  $ r   )r   r7   r)   channel)r   modes     r   makefileSSHSocket.makefileS   s2    yyLLN#'		 yyr   c                 8   U R                   (       a%  U R                   R                  R                  (       a  g U R                   R                  R                  S5        U R                   R                  R	                  5         U R                   R                  5         g )Ns   

)r   r*   r;   r=   r>   	terminater   s    r   closeSSHSocket.closeZ   sV    yyDIIOO22		g&				r   )r   r   r   r   )__name__
__module____qualname____firstlineno__r   r7   rA   rF   rI   rN   rS   rX   __static_attributes____classcell__r   s   @r   r   r      s0    %:!(  r   r   c                   2   ^  \ rS rSrSU 4S jjrS rSrU =r$ )SSHConnectionb   c                 F   > [         TU ]  SUS9  Xl        X l        X0l        g )N	localhost)timeout)r   r   ssh_transportrf   ssh_host)r   rg   rf   r   r   s       r   r   SSHConnection.__init__c   s,     	 	
 +r   c                 @   U R                   (       aG  U R                   R                  5       nUR                  U R                  5        UR	                  S5        O@[        U R                  5      nUR                  U R                  5        UR                  5         Xl        g )Nr   )	rg   open_session
settimeoutrf   exec_commandr   rh   r7   sock)r   rn   s     r   r7   SSHConnection.connectk   sj    %%224DOODLL)89T]]+DOODLL)LLN	r   )rn   rh   rg   rf   )N<   N)rZ   r[   r\   r]   r   r7   r^   r_   r`   s   @r   rb   rb   b   s    
 
r   rb   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )SSHConnectionPoolx   r   c                    > [         TU ]  SX#S9  S U l        X l        U(       a  UR	                  5       U l        X@l        g )Nre   )rf   maxsize)r   r   rg   rf   get_transportrh   )r   
ssh_clientrf   ru   r   r   s        r   r   SSHConnectionPool.__init__{   sB     	 	
 "!+!9!9!;Dr   c                 X    [        U R                  U R                  U R                  5      $ r   )rb   rg   rf   rh   rW   s    r   	_new_connSSHConnectionPool._new_conn   s    T//t}}MMr   c                 j   S n U R                   R                  U R                  US9nU=(       d    U R                  5       $ ! [         a!    [        R
                  R                  U S5      e[        R                   a4    U R                  (       a   [        R
                  R                  U S5      e Nf = f)N)blockrf   zPool is closed.z>Pool reached maximum size and no more connections are allowed.)poolgetr}   AttributeErrorurllib3
exceptionsClosedPoolErrorqueueEmptyEmptyPoolErrorrz   )r   rf   conns      r   	_get_connSSHConnectionPool._get_conn   s    	99==tzz7=CD 't~~''  	N$$44T;LMM{{ 	zz((77/  	s   $A A.B21B2)rh   rg   rf   )Nrp   
   N)
rZ   r[   r\   r]   schemer   rz   r   r^   r_   r`   s   @r   rr   rr   x   s    FN( (r   rr   c                      ^  \ rS rSr\R
                  R                  R                  / SQ-   rS\R                  \R                  S4U 4S jjrS rS rSS jrU 4S	 jrS
rU =r$ )SSHHTTPAdapter   )poolsrf   rw   
ssh_paramsmax_pool_sizerp   Fc                   > S U l         U(       d!  U R                  U5        U R                  5         Xl        UR	                  S5      (       a  U[        S5      S  U l        X l        X@l        [        US S9U l	        [        TU ]-  5         g )Nzssh://c                 "    U R                  5       $ r   )rX   )ps    r   <lambda>)SSHHTTPAdapter.__init__.<locals>.<lambda>   s
    QWWYr   )dispose_func)rw   _create_paramiko_client_connectrh   
startswithlenrf   r   RecentlyUsedContainerr   r   r   )r   base_urlrf   pool_connectionsr   	shell_outr   s         r   r   SSHHTTPAdapter.__init__   sx     ((2MMO x(($S]^4DM**+>

 	r   c                 ^   [         R                  " S5      R                  [         R                  5        [        R
                  " 5       U l        [        R                  R                  U5      nUR                  UR                  UR                  S.U l        [        R                  R!                  S5      n[        R                  R#                  U5      (       a  [        R$                  " 5       n['        U5       nUR                  U5        S S S 5        UR)                  UR                  5      nSU;   a&  [        R*                  " US   5      U R                  S'   SU;   a  US   U R                  S'   UR                  c  SU;   a  US   U R                  S'   UR                  c  SU;   a  US   U R                  S	'   S
U;   a  US
   U R                  S'   U R                  R-                  5         U R                  R/                  [        R0                  " 5       5        g ! , (       d  f       GN= f)Nparamiko)hostnamer   usernamez~/.ssh/configproxycommandrn   r   r   r   r   identityfilekey_filename)logging	getLoggersetLevelWARNINGr   	SSHClientrw   urllibparseurlparser   r   r   r   r.   path
expanduserexists	SSHConfigopenlookupProxyCommandload_system_host_keysset_missing_host_key_policyRejectPolicy)r   r   ssh_config_fileconfr$   host_configs         r   r   &SSHHTTPAdapter._create_paramiko_client   s   *%..w?",,.<<((2 ))MM ))
 '',,_=77>>/**%%'Do&!

1 '++h&7&78K,*2*?*?/+' [(.9*.E
+}}$;)>*5f*='  (V{-B.9&.A
+,2=n2M/--/33H4I4I4KL# '&s   9H
H,c                 t    U R                   (       a'  U R                   R                  " S0 U R                  D6  g g )Nr#   )rw   r7   r   rW   s    r   r   SSHHTTPAdapter._connect   s'    ??OO##6doo6 r   c                 P   U R                   (       d4  [        U R                   U R                  U R                  U R                  S9$ U R
                  R                     U R
                  R                  U5      nU(       a  UsS S S 5        $ U R                   (       a/  U R                   R                  5       (       d  U R                  5         [        U R                   U R                  U R                  U R                  S9nX0R
                  U'   S S S 5        U$ ! , (       d  f       W$ = f)N)rw   rf   ru   r   )
rw   rr   rf   r   rh   r   lockr   rv   r   )r   urlproxiesr~   s       r   get_connectionSSHHTTPAdapter.get_connection   s    $??**]]	  ZZ__::>>#&D _ t'D'D'F'F$??**]]	D #JJsO " # _" s   $D
BD
D%c                 z   > [         TU ]  5         U R                  (       a  U R                  R                  5         g g r   )r   rX   rw   )r   r   s    r   rX   SSHHTTPAdapter.close   s(    ??OO!!# r   )r   r   rw   rh   r   rf   r   )rZ   r[   r\   r]   requestsadaptersHTTPAdapter	__attrs__r   DEFAULT_NUM_POOLSDEFAULT_MAX_POOL_SIZEr   r   r   r   rX   r^   r_   r`   s   @r   r   r      s]    !!--77 ; I *,"+"="=(>> (M>76$ $r   r   )r   r   urllib.parser   requests.adaptersr   r   r.   r    r   r1    docker.transport.basehttpadapterr    r   r   urllib3.connection_collectionsr   r   
connectionHTTPConnectionrb   connectionpoolHTTPConnectionPoolrr   r   r#   r   r   <module>r      s         	    <   ,,BB K K\G&&55 ,$(..AA $(N[$_ [$r   