
    Vh                       d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZmZmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZmZ ddlmZ  G d dej:                        Z G d de      Z G d de      Z  G d de      Z!y)z7Connection abstraction for interacting with test hosts.    )annotationsN   )read_text_file)EnvironmentConfig)DisplayOutputStreamSubprocessErrorretry)run_command)DockerInspectdocker_execdocker_inspectdocker_network_disconnect)SshConnectionDetailssh_options_to_list)Becomec                      e Zd ZdZej
                  	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 	 	 ddZ	 d		 	 	 	 	 	 	 d
dZy)
Connectionz$Base class for connecting to a host.Nc                     y)0Run the specified command and return the result.N selfcommandcaptureinteractivedatastdinstdoutoutput_streams           R/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/connections.pyrunzConnection.run0   s        c                8     dddd|gt         fd       y)zAExtract the given archive file stream in the specified directory.taroxzf--Cc                 ,     j                  d      S )NT)r   r   r"   )r   srctar_cmds   r!   <lambda>z,Connection.extract_archive.<locals>.<lambda>E   s    dhhwc4h@ r#   N)r
   )r   chdirr+   r,   s   ` `@r!   extract_archivezConnection.extract_archive=   s     &#tU3@Ar#   c                     dddd|g}dg}|r|d|gz  }|j                  |       ||g}ddd	j                  d
 |D              gt         fd       y)zCreate the specified archive file stream from the specified directory, including the given name and optionally excluding the given name.r%   cfr'   r(   gzipz	--excludeshz-cz | c              3  F   K   | ]  }t        j                  |        y wN)shlexjoin).0r   s     r!   	<genexpr>z,Connection.create_archive.<locals>.<genexpr>Z   s     (UG)<(Us   !c                 ,    j                   d      S )NT)r   r   r*   )dstr   sh_cmds   r!   r-   z+Connection.create_archive.<locals>.<lambda>\   s    dhhvc4h@ r#   N)appendr7   r
   )	r   r.   namer;   excluder,   gzip_cmdcommandsr<   s	   `  `    @r!   create_archivezConnection.create_archiveG   sj     $T518W--Gt X&ejj(UH(UUV@Ar#   FNNNNr   z	list[str]r   boolr   rE   r   t.Optional[str]r   t.Optional[t.IO[bytes]]r   rG   r    zt.Optional[OutputStream]returnz'tuple[t.Optional[str], t.Optional[str]])r.   strr+   t.IO[bytes]r5   )r.   rI   r>   rI   r;   rJ   r?   rF   )	__name__
__module____qualname____doc__abcabstractmethodr"   r/   rB   r   r#   r!   r   r   -   s    .
 " $)-*.26
?
? 
? 	
?
 
? '
? (
? 0
? 
1
? 
?BB B $(BB B 	B
 !Br#   r   )	metaclassc                  J    e Zd ZdZddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)LocalConnectionzConnect to localhost.c                    || _         y r5   )args)r   rU   s     r!   __init__zLocalConnection.__init__b   s	    	r#   Nc           
     <    t        | j                  |||||||      S )r   )rU   cmdr   r   r   r   r   r    )r   rU   r   s           r!   r"   zLocalConnection.rune   s-     #'	
 		
r#   )rU   r   rH   NonerC   rD   )rK   rL   rM   rN   rV   r"   r   r#   r!   rS   rS   _   sn     " $)-*.26

 
 	

 
 '
 (
 0
 
1
r#   rS   c                  ^    e Zd ZdZdddZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZed
d       Zy)SshConnectionzConnect to a host using SSH.Nc                    || _         || _        || _        d|j                  g| _        t        ddddd      }|j                  |j                         | j                  j                  t        |             y )Nz-iyesnoz	/dev/null      )	BatchModeStrictHostKeyCheckingUserKnownHostsFileServerAliveIntervalServerAliveCountMax)	rU   settingsbecomeidentity_fileoptionsdictupdateextendr   )r   rU   rf   rg   ssh_optionss        r!   rV   zSshConnection.__init__   sp    	 h44548"&* " !5
 	8++,/<=r#   c                   
 t         j                        } j                  r j                  j                  |      }|j	                  d       |r|j	                  d       t        j                  dd      5 
|j                  dd
j                  g        j                  j                  r0|j                  dt         j                  j                        g       |j	                   j                  j                   d	 j                  j                          |j	                  t        j                  |             d 
fd
}	t!         j"                  dg|z   |||||||		      cddd       S # 1 sw Y   yxY w)r   z-qz-ttzansible-test-ssh-debug-z.log)prefixsuffixz-vvvz-Ez-p@c                >    j                  j                  |        y)zError handler.N)capture_log_detailsr>   )exr   ssh_logfiles    r!   error_callbackz)SshConnection.run.<locals>.error_callback   s    (()9)92>r#   ssh)	rU   rX   r   r   r   r   r   r    rv   N)rt   r	   rH   rY   )listri   rg   prepare_commandr=   tempfileNamedTemporaryFilerl   r>   rf   portrI   userhostr6   r7   r   rU   )r   r   r   r   r   r   r   r    ri   rv   ru   s   `         @r!   r"   zSshConnection.run   s0    t||$;;kk11':GtNN5!((0IRXY 	]hNNFD+*:*:;<}}!!c$--*<*<&=>?NNdmm0014==3E3E2FGHNN5::g./? YYGg%'+-
	 	 	s   :C/E33E<c                   |j                   dk7  ryg d}t        |       }g }t        |j                               D ])  |j	                         t        fd|D              s) n dj                  t        |            }|xj                  dz  c_        |xj                  |j                         t        j                  dz  c_        y)zTRead the specified SSH debug log and add relevant details to the provided exception.   N)zdebug1: Connection Establishedz!debug1: Authentication successfulz$debug1: Entering interactive sessionzdebug1: Sending commandz'debug2: PTY allocation request acceptedzdebug2: exec request acceptedc              3  @   K   | ]  }j                  |        y wr5   )
startswith)r8   markerlines     r!   r9   z4SshConnection.capture_log_details.<locals>.<genexpr>   s     Av4??6*As   
z>>> SSH Debug Output
)statusr   reversed
splitlinesr=   anyr7   messagestripr   clear)pathrt   markersfile_contentsmessagesr   r   s         @r!   rs   z!SshConnection.capture_log_details   s     99
 't,]5578 	DOOD!AAA		 ))HX./


..



'--/7==AA
r#   r5   )rU   r   rf   r   rg   zt.Optional[Become]rH   rY   rC   rD   )r   rI   rt   r	   rH   rY   )rK   rL   rM   rN   rV   r"   staticmethodrs   r   r#   r!   r[   r[   |   s    &>. " $)-*.26,, , 	,
 , ', (, 0, 
1,\ B Br#   r[   c                  \    e Zd ZdZdddZ	 	 	 	 	 d		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZddZddZy)DockerConnectionzConnect to a host using Docker.Nc                .    || _         || _        || _        y r5   )rU   container_idr}   )r   rU   r   r}   s       r!   rV   zDockerConnection.__init__   s    	(%)	r#   c                    g }| j                   r|j                  d| j                   g       |r|j                  d       t        | j                  | j
                  ||||||||
      S )r   z--userz-it)
rU   r   rX   ri   r   r   r   r   r   r    )r}   rl   r=   r   rU   r   )	r   r   r   r   r   r   r   r    ri   s	            r!   r"   zDockerConnection.run   si     99NNHdii01NN5!**#'
 	
r#   c                B    t        | j                  | j                        S )zKInspect the container and return a DockerInspect instance with the results.)r   rU   r   )r   s    r!   inspectzDockerConnection.inspect  s    dii):):;;r#   c                F    t        | j                  | j                  |       y)z4Disconnect the container from the specified network.N)r   rU   r   )r   networks     r!   disconnect_networkz#DockerConnection.disconnect_network
  s    !$))T->->Hr#   r5   )rU   r   r   rI   r}   rF   rH   rY   rC   rD   )rH   r   )r   rI   rH   rY   )rK   rL   rM   rN   rV   r"   r   r   r   r#   r!   r   r      sz    )* " $)-*.26

 
 	

 
 '
 (
 0
 
1
@<Ir#   r   )"rN   
__future__r   rO   r6   rz   typingtior   configr   utilr   r   r	   r
   util_commonr   docker_utilr   r   r   r   rw   r   r   rg   r   ABCMetar   rS   r[   r   r   r#   r!   <module>r      s    = " 
     

/B3;; /Bd
j 
:_BJ _BD.Iz .Ir#   