
    VhO3                    p   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl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 d dlmZ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! d d
l"m#Z# d dl$m%Z%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-  e+       Z.d Z/ed        Z0 G d de1      Z2ddZ3e4dk(  r e3        yy)    )annotationsN)contextmanager)	constants)option_helpers)to_bytesto_text)
ConnectionConnectionError	send_data	recv_data)fork_process)AnsibleJSONEncoderAnsibleJSONDecoder)PlayContext)connection_loaderinit_plugin_loader)unfrackpathmakedirs_safe)Display)JsonRpcServerc                    t        | j                         j                               }| j                  |      }t	        |      |k  rt        d      |S )Nz"EOF found before data was complete)intreadlinestripreadlen	Exception)byte_streamsizedatas      _/home/dcms/DCMS/lib/python3.12/site-packages/ansible/cli/scripts/ansible_connection_cli_stub.pyread_streamr"   "   sK    {##%++-.DD!D
4y4<==K    c              #  :  K   t        j                  | t         j                  t         j                  z  d      }t	        j
                  |t        j                         d t	        j
                  |t        j                         t        j                  |       yw)z
    Uses contextmanager to create and release a file lock based on the
    given path. This allows us to create locks using `with file_lock()`
    to prevent deadlocks related to failure to unlock properly.
    i  N)	osopenO_RDWRO_CREATfcntllockfLOCK_EXLOCK_UNclose)	lock_pathlock_fds     r!   	file_lockr0   ,   sZ      ggiRZZ!7?G	KK'		KK'HHWs   BBc                  <    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	 Z
y)ConnectionProcessz
    The connection process wraps around a Connection object that manages
    the connection to a remote device that persists over the playbook
    Nc                    || _         || _        || _        || _        || _        d | _        t               | _        d | _        d | _	        || _
        y N)play_contextsocket_pathoriginal_path
_task_uuidfd	exceptionr   srvsock
connection_ansible_playbook_pid)selfr9   r5   r6   r7   	task_uuidansible_playbook_pids          r!   __init__zConnectionProcess.__init__@   sN    (&*# ?	%9"r#   c                   t               }i }	 |j                  dd| j                  z  f       | j                  j                  rh| j                  j                  d   dvrMt
        j                  j                  | j                  | j                  j                        | j                  _        t        j                  | j                  j                  | j                  d| j                  | j                        | _        	 | j                  j                  |       | j                  | j                  _        | j$                  j'                  | j                         |j)                  t*        j,                  j/                         j1                         D cg c]  }d|f c}       t3        j2                  t2        j4                  t2        j6                        | _        | j8                  j;                  | j                         | j8                  j=                  d
       |j                  d       ||d<   | jF                  jI                  tK        jL                  |tN                     | jF                  jQ                          y # t        $ r,}|j                  dt!        |      f       t        d	      d }~ww xY wc c}w # t>        $ rX}|j)                  | j                  jA                                t!        |      |d<   tC        jD                         |d<   Y d }~d }~ww xY w# ||d<   | jF                  jI                  tK        jL                  |tN                     | jF                  jQ                          w xY w)Nvvvvzcontrol socket path is %sr   z~/z	/dev/null)r@   rA   directdebugXUnable to decode JSON from response set_options. See the debug log for more information.   )rD   z2local domain socket listeners started successfullyerrorr:   messagescls))listappendr6   r5   private_key_filer%   pathjoinr7   r   getr=   r8   r>   set_optionsr
   r   _socket_pathr;   registerextendsysstdoutgetvalue
splitlinessocketAF_UNIXSOCK_STREAMr<   bindlistenr   pop_messages	traceback
format_excr9   writejsondumpsr   r-   )r?   optionsrK   resultexcmsgs         r!   startzConnectionProcess.startO   s   6	OOV%@4CSCS%STU
   11d6G6G6X6XYZ6[cg6g57WW\\$BTBTVZVgVgVxVx5y!!2/33D4E4E4P4PRVRcRcep>Boodhd~d~@DOB++7+;
 ,0+;+;DOO(HHdoo.OOcjj6I6I6K6V6V6XYsfc]YZfnnf6H6HIDIIINN4++,IIQOOZ[ "*F:GGMM$**V1CDEGGMMO' # B'#, 78%  'A  B  BB Z  	9OODOO88:;%clF7O"+"6"6"8F;	9
 "*F:GGMM$**V1CDEGGMMOs^   C/J> >J A9J> J9BJ> 	J6
'J11J66J> >	LALL" LL" "AM7c                `   	 | j                   j                  d      }| j                   j                  s:t        j                  t        j                  | j
                         t        j                  t        j                  | j                         t        j                  | j                   j                  d             d | _	        | j                  j                         \  }}t        j                  d       t        j                  t        j                  | j                         	 t        |      }|sn|rt        j                  d|z  d       t        j                   t#        |d            }|j%                  d	      d
k(  r0| j                   j&                  s| j                   j)                          t        j                  | j                   j                  d             | j*                  j-                  |      }t        j                  d       |rt        j                  d|z  d       t/        |t1        |              |j3                          | j                   j                  s:tA        jB                  d       | jE                          y # t4        $ rf}t7        |d      r7|j8                  t8        j:                  k7  r3t=        j>                         | _	        nt=        j>                         | _	        Y d }~d }~ww xY w# tA        jB                  d       | jE                          w xY w)Npersistent_log_messagespersistent_connect_timeoutr   Tzjsonrpc request: %slog_onlysurrogate_or_strict)errorsmethodexec_commandpersistent_command_timeoutzjsonrpc response: %serrnog?)#r=   
get_option_conn_closedsignalSIGALRMconnect_timeoutSIGTERMhandleralarmr:   r<   acceptcommand_timeoutr   displayre   loadsr   rS   	connected_connectr;   handle_requestr   r   r-   r   hasattrrv   EINTRrb   rc   timesleepshutdown)r?   log_messagessaddrr    requestrespes           r!   runzConnectionProcess.runt   s"   0	??556OPLoo22fnnd.B.BCfnndll;T__778TUV!% II,,.	DQfnnd.B.BC$Q<D#(=(DtT"jj>S)TUG{{8,>tG`G`002LL!;!;<X!YZ882248DLLO#(>(EPTUa$0) , 	? oo22V JJsO MMO  	8q'"77ekk)%.%9%9%;DN!*!5!5!7	8 JJsO MMOs+   I*J 	LAK>9L >LL 'L-c                    d| j                   j                  d      z  }t        j                  |d       t        |      )Nzpersistent connection idle timeout triggered, timeout value is %s secs.
See the timeout setting options in the Network Debug and Troubleshooting Guide.rn   Tro   r=   rw   r   r   r?   signumframerj   s       r!   r{   z!ConnectionProcess.connect_timeout   s<    ')-)C)CD`)abd+nr#   c                    d| j                   j                  d      z  }t        j                  |d       t        |      )Nzcommand timeout triggered, timeout value is %s secs.
See the timeout setting options in the Network Debug and Troubleshooting Guide.ru   Tro   r   r   s       r!   r   z!ConnectionProcess.command_timeout   s=     V**+GHId+nr#   c                P    d|z  }t         j                  |d       t        |      )Nz%signal handler called with signal %s.Tro   )r   r   r   s       r!   r}   zConnectionProcess.handler   s&    5>d+nr#   c                   t        dt        j                  j                  | j                        z        }t        j                  j                  | j                        r	 | j                  r| j                  j                          | j                  rn| j                  j                          | j                  j                  d      r9| j                  j                         D ]  \  }}t        j                  |d        t        j                  j                  | j                        rNt        j                  | j                         t        | j                  dd       t        | j                  dd       	 t        j                  j                  |      rt        j                  |       t        j                  d	d       y# t        $ r Y w xY w# t        j                  j                  | j                        rNt        j                  | j                         t        | j                  dd       t        | j                  dd       w w xY w)
z, Shuts down the local domain socket
        %s/.ansible_pc_lock_%srm   Tro   rU   N
_connectedFzshutdown complete)r   r%   rQ   splitr6   existsr<   r-   r=   rw   ra   r   r   removesetattr)r?   r.   _levelmessages       r!   r   zConnectionProcess.shutdown   s      8277==IYIY;Z Z[	77>>$**+B99IIOO%??OO))+112KL/3/K/K/M DOFG#OOGdOCD
 77>>$"2"23IId../DOO^TBDOO\5A77>>)$IIi +d;   77>>$"2"23IId../DOO^TBDOO\5A 4s&   !B G 	GG GG A9I)NN)__name__
__module____qualname____doc__rB   rk   r   r{   r   r}   r    r#   r!   r2   r2   ;   s,    :#J1f
<r#   r2   c           	        t        j                  d      }t        j                  |       |j                  d       |j                  d       |j	                  | | dd n|       } t                | j                  t        _        d}i }t               }d}t        j                  j                  }t        j                  }t        j                         t        _        	 t        |      }t        |      }	t!        j"                  |	d      }
t!        j"                  |d      }t%               }|j'                  |
       |dk(  rt3        j4                  d
d      }| j6                  }| j8                  }|j;                  j<                  |j>                  |j@                  |jB                  |      }tE        tF        jH                        }tK        |       tE        |tM        |      z        }tE        dtN        jP                  jS                  |      z        }tU        |      5  tN        jP                  jW                  |      sZ|jY                  d       tO        jZ                         }tO        j\                         \  }}t_               }|dk(  r	 tO        j`                  |       tO        jb                  |d      }te        ||||||      }|jg                         |dk(  rji                          njk                          t        jl                  |       ntO        j`                  |       tO        jb                  |d      }to        j"                  |jq                         tr              }|ju                  |jw                  d             |j+                  |       n]|jY                  d       ty        |      }	 |j{                         t-        	      }
	 |j                  |
       |j                  |       ddd       tN        jP                  jW                  |      r(|ju                  ty        |      j                                |jY                  dt        j                  j                         f       |j+                  ||d       |t        _        d|v r;d}t        j                  j                  to        j                  |t                     n:d}t        j                  j                  to        j                  |t                     t        jl                  |       y# t(        $ r<}d}|j+                  t-        |      t/        j0                         d	       Y d}~(d}~ww xY w# t(        $ r+ |jY                  dt/        j0                         f       d}Y w xY w# t|        $ r,}|jY                  dt-        |      f       t}        d      d}~ww xY w# t(        $ r[}t        |t|              rt        |dd      dk(  rn/|j+                  t-        |      t/        j0                         d	       Y d}~Bd}~ww xY w# 1 sw Y   LxY w)z9 Called to initiate the connect to the remote device
    N)progplaybook_pidr@   rI   r   bytes)encoding)rJ   r:   sshT)
class_only)	directoryr   )rD   z/local domain socket does not exist, starting itwrJ   rrL   rK   )rD   z-found existing local domain socket, using it!rE   rG   rH   codeirD   )rK   r6   r:   )Iopt_helpcreate_base_parseradd_verbosity_optionsadd_argument
parse_argsr   	verbosityr   rN   rX   stdinbufferrY   ioStringIOr"   pickler   r   deserializer   updater   rb   rc   r   rS   r   r@   _create_control_pathremote_addrportremote_userr=   r   CPERSISTENT_CONTROL_PATH_DIRr   dictr%   rQ   r   r0   r   rO   getcwdpiper   r-   fdopenr2   rk   r   r   exitre   r   r   rW   popr	   rT   r
   update_play_contextset_check_prompt
isinstancegetattrra   rZ   stderrrd   rf   r   )argsparserrcrh   rK   r6   r   saved_stdout	opts_data	init_datapc_datarg   r5   r   r   rA   r@   cptmp_pathr.   r7   r   r   pidwfdprocessrfdr    connri   s                                 r!   mainr      s    ((d3F""6*
'
$)9T!"XtDD G	
BFvHK IIE ::LCJ&	&	,,y7;,,y7;"}  ) 
Qw##Ed;#00NN	%%l&>&>@Q@QS_SkSkmy  nE  nE  G[  \ q<<=h!"th'?"?@ 8277==;U UV	y! 4	77>>+. [\ "		wwy1"n!8 ii3/"3C{Tacl  oC  #Dg.
 Qw((*HHRL HHQK))As+C::chhj6HIDOODHHZ$89MM$'  YZ!+.F$$G$4 "),,,W5)))4U4	l 
ww~~k"
;/<<>?OOVSZZ00234
MM" 
 CJf

F0BCD

F0BCDHHRLu  QZ"--/
 	 	B %  )2F2F2H(IJ. ' FOOWgcl$;<)  +E  F  FF ! 	 "#7GCQU<VZ`<`%,S\)2)=)=)?' 	W4	 4	s   AT$  A,YAU,CY8V#
Y"W$	U)-1U$$U),0V YV  Y#	W,'WWY	X?$AX:4Y:X??YY__main__r4   )5
__future__r   r)   r   r%   r   ry   r\   rX   r   rb   rv   re   
contextlibr   ansibler   r   ansible.cli.argumentsr   r   +ansible.module_utils.common.text.convertersr   r   ansible.module_utils.connectionr	   r
   r   r   ansible.module_utils.servicer   ansible.parsing.ajsonr   r   ansible.playbook.play_contextr   ansible.plugins.loaderr   r   ansible.utils.pathr   r   ansible.utils.displayr   ansible.utils.jsonrpcr   r   r"   r0   objectr2   r   r   r   r#   r!   <module>r      s    #  	 	    
     % " < I ] ] 5 H 5 H 9 ) /
)  U< U<pD zF r#   