
    Vh                        d dl mZ d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mZ d dlmZmZ d d	lmZ  e       Z G d
 de      Zy)    )annotationsa  
author: Ansible Core Team
name: jail
short_description: Run tasks in jails
description:
  - Run commands or put/fetch files to an existing jail.
options:
  remote_addr:
    description:
      - Path to the jail.
    type: string
    default: inventory_hostname
    vars:
      - name: inventory_hostname
      - name: ansible_host
      - name: ansible_jail_host
  remote_user:
    description:
      - User to execute as inside the jail.
    type: string
    vars:
      - name: ansible_user
      - name: ansible_jail_user
N)AnsibleError)shlex_quote)get_bin_path)to_bytes	to_nativeto_text)ConnectionBaseBUFSIZE)Displayc                       e Zd ZdZdZdZdZdZ fdZe	d        Z
d Z fd	Zej                  fd
Zd fd	Ze	d        Z fdZ fdZ fdZ xZS )
Connectionz" Local BSD Jail based connections conn_jail_namezcommunity.general.jailTFc                   t        t        | 
  ||g|i | | j                  j                  | _        | j                  |v r|| j                     | _        t        j                         dk7  rt        d      | j                  d      | _        | j                  d      | _        | j
                  | j                         vrt        d| j
                         y )Nr   z(jail connection requires running as rootjlsjexeczincorrect jail name )superr   __init___play_contextremote_addrjailmodified_jailname_keyosgeteuidr   _search_executablejls_cmd	jexec_cmd
list_jails)selfplay_context	new_stdinargskwargs	__class__s        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/jail.pyr   zConnection.__init__?   s    j$(yR4R6R&&22	%%/t99:DI::<1IJJ..u500999DOO--!5dii[ABB .    c                P    	 t        |       S # t        $ r t        |  d      w xY w)Nz command not found in PATH)r   
ValueErrorr   )
executables    r%   r   zConnection._search_executableO   s7    	J
++ 	J*-GHII	Js   
 %c                    t        j                  | j                  ddgt         j                  t         j                  t         j                        }|j	                         \  }}t        |d      j                         S )Nz-qname)stdinstdoutstderrsurrogate_or_stricterrors)
subprocessPopenr   PIPEcommunicater	   split)r   pr-   r.   s       r%   r   zConnection.list_jailsV   s\    dllD&9#-??$.OOJOOM v&;<BBDDr&   c                    t         t        |           | j                  s@t        j                  d| j                  j                   | j                         d| _        yy)z) connect to the jail; nothing to do here z$ESTABLISH JAIL CONNECTION FOR USER: hostTN)	r   r   _connect
_connecteddisplayvvvr   remote_userr   r   r$   s    r%   r;   zConnection._connect_   sO    j$(*KK>t?Q?Q?]?]>^_fjfofoKp"DO r&   c                    | j                   g}d}| j                  j                  5|d| j                  j                  gz  }d| j                  j                   d}|| j                  | j                  j                  d||z   gz  }t
        j                  d| | j                         |D cg c]  }t        |d	       }}t        j                  |d
|t        j                  t        j                        }|S c c}w )a-   run a command on the jail.  This is only needed for implementing
        put_file() get_file() so that we don't have to read the whole file
        into memory.

        compared to exec_command() it looses some niceties like being able to
        return the process's exit code immediately.
         z-UzHOME=~ z-czEXEC r9   r/   r0   F)shellr,   r-   r.   )r   r   r?   r   r)   r=   r>   r   r2   r3   r4   )r   cmdr,   	local_cmdset_envir7   s          r%   _buffered_exec_commandz!Connection._buffered_exec_commandf   s     ^^$	))5$ 2 2 > >??It11==>a@Gdii!3!3!>!>gPSmTT	eI;'dii8HQR1Xa(=>R	RYe5$.OOJOOM 	 Ss   -C;c                    t         t        |   |||       | j                  |      }|j	                  |      \  }}|j
                  ||fS )z run a command on the jail )in_datasudoable)r   r   exec_commandrI   r5   
returncode)r   rE   rK   rL   r7   r-   r.   r$   s          r%   rM   zConnection.exec_command   sL    j$,S'H,U'',w/||VV++r&   c                   | j                  t        j                  j                        s8t        j                  j	                  t        j                  j                  |       } t        j                  j                  |       S )a   Make sure that we put files into a standard path

            If a path is relative, then we need to choose where to put it.
            ssh chooses $HOME but we aren't guaranteed that a home dir will
            exist in any given chroot.  So for now we're choosing "/" instead.
            This also happens to be the former default.

            Can revisit using $HOME instead if it is a problem
        )
startswithr   pathsepjoinnormpath)remote_paths    r%   _prefix_login_pathzConnection._prefix_login_path   sK     %%bggkk2'',,rww{{K@Kww,,r&   c                   t         t        |   ||       t        j	                  d| d| | j
                         t        | j                  |            }	 t        t        |d      d      5 }t        j                  |j                               j                  sd}nd}	 | j                  d	| d
t         | |      }	 |j%                         \  }}|j,                  dk7  r)t#        d| d| dt/        |       dt/        |             	 ddd       y# t         $ r t#        d      w xY w# t&        $ r& t)        j*                          t#        d| d|       w xY w# 1 sw Y   yxY w# t0        $ r t#        d|       w xY w)z$ transfer a file from local to jail zPUT  TO r9   r/   r0   rbz count=0rB   zdd of= bs=)r,   /jail connection requires dd command in the jailfailed to transfer file  to r   :

Nz"file or module does not exist at: )r   r   put_filer=   r>   r   r   rV   openr   r   fstatfilenost_sizerI   r   OSErrorr   r5   	Exception	traceback	print_excrN   r   IOError)	r   in_pathout_pathin_filecountr7   r-   r.   r$   s	           r%   r`   zConnection.put_file   s   j$((;d7)4z2Ct66x@A	Ohw/DEtL FPWxx 0199&EEZ33fXJd7)TYSZ4[cj3kA[%&]]_NFF <<1$&)A'$xjX[\efl\m[nnpqz  |B  rC  qD  (E  F  F %F F  Z&'XYYZ ! ['')&)A'$xj'YZZ[F F   	O!CG9MNN	OsT   E. /3E"#DD08E"E. D--E"0/EE""E+'E. +E. .Fc                   t         t        |   ||       t        j	                  d| d| | j
                         t        | j                  |            }	 | j                  d| dt               }t        t        |d      d	      5 }	 |j                  j                  t              }|r3|j!                  |       |j                  j                  t              }|r3|j)                         \  }}|j*                  dk7  r)t        d
| d| dt-        |       dt-        |             	 ddd       y# t        $ r t        d      w xY w# t"        $ r& t%        j&                          t        d
| d|       w xY w# 1 sw Y   yxY w)z! fetch a file from jail to local zFETCH rX   r9   zdd if=rZ   r[   r/   r0   zwb+r\   r]   r   r^   r_   N)r   r   
fetch_filer=   r>   r   r   rV   rI   r   re   r   ra   r   r-   readwriterf   rg   rh   r5   rN   r   )	r   rj   rk   r7   out_filechunkr-   r.   r$   s	           r%   ro   zConnection.fetch_file   s   j$*7H=fWIT(4499Ed55g>?	R++fWIT',KLA (8,ABEJ 	BhWg.NN5)HHMM'2E  ]]_NFF||q "%=gYd8*TWXabhXiWjjlmvw}m~l  $A  B  B !	B 	B  	RPQQ	R  W##%"%=gYd8*#UVVW	B 	Bs2   D5 
E?AE AE?5E
/E<<E??Fc                8    t         t        |           d| _        y)z. terminate the connection; nothing to do here FN)r   r   closer<   r@   s    r%   ru   zConnection.close   s    j$%'r&   )NF)__name__
__module____qualname____doc__r   	transporthas_pipelininghas_ttyr   staticmethodr   r   r;   r2   r4   rI   rM   rV   r`   ro   ru   __classcell__)r$   s   @r%   r   r   4   s    ,,(I NGC  J JE# 1; 4, - -O4B0   r&   r   )
__future__r   DOCUMENTATIONr   os.pathr2   rg   ansible.errorsr   ansible.module_utils.six.movesr   #ansible.module_utils.common.processr   +ansible.module_utils.common.text.convertersr   r   r	   ansible.plugins.connectionr
   r   ansible.utils.displayr   r=   r    r&   r%   <module>r      sI    #2 
    ' 6 < T T > )
)Y  Y r&   