
    Vhu"                        d dl mZ d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 d d	lmZ d d
lmZmZ d dlmZ  e       Z G d de      Zy)    )annotationsa  
author: Maykel Moya (!UNKNOWN) <mmoya@speedyrails.com>
name: chroot
short_description: Interact with local chroot
description:
  - Run commands or put/fetch files to an existing chroot on the Ansible controller.
options:
  remote_addr:
    description:
      - The path of the chroot you want to access.
    type: string
    default: inventory_hostname
    vars:
      - name: inventory_hostname
      - name: ansible_host
  executable:
    description:
      - User specified executable shell.
    type: string
    ini:
      - section: defaults
        key: executable
    env:
      - name: ANSIBLE_EXECUTABLE
    vars:
      - name: ansible_executable
    default: /bin/sh
  chroot_exe:
    description:
      - User specified chroot binary.
    type: string
    ini:
      - section: chroot_connection
        key: exe
    env:
      - name: ANSIBLE_CHROOT_EXE
    vars:
      - name: ansible_chroot_exe
    default: chroot
  disable_root_check:
    description:
      - Do not check that the user is not root.
    ini:
      - section: chroot_connection
        key: disable_root_check
    env:
      - name: ANSIBLE_CHROOT_DISABLE_ROOT_CHECK
    vars:
      - name: ansible_chroot_disable_root_check
    default: false
    type: bool
    version_added: 7.3.0
z
- hosts: chroots
  connection: community.general.chroot
  tasks:
    - debug:
        msg: "This is coming from chroot environment"
N)AnsibleError)is_executable)get_bin_path)shlex_quote)to_bytes)ConnectionBaseBUFSIZE)Displayc                       e Zd ZdZdZdZdZdZ f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 chroot based connections zcommunity.general.chrootTFrootc                   t        t        | 
  ||g|i | | j                  j                  | _        t        j                  j                  | j
                        st        | j
                   d      t        j                  j                  | j
                  d      }t        |      sWt        j                  j                  |      rt        j                  j                  |      st        | j
                   d      y y )Nz is not a directoryzbin/shz6 does not look like a chrootable dir (/bin/sh missing))superr   __init___play_contextremote_addrchrootospathisdirr   joinr   lexistsislink)selfplay_context	new_stdinargskwargschrootsh	__class__s         o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/chroot.pyr   zConnection.__init__f   s    j$(yR4R6R((44 ww}}T[[)$++.ABCC77<<X6
 h'BGGOOH,E"''..YaJb$++.deff Kc'    c                   | j                  d      s"t        j                         dk7  rt        d      t        j                  j                  | j                  d            r| j                  d      | _        n 	 t        | j                  d            | _        t        t        | 3          | j                  s)t        j                  d| j                          d| _        yy# t        $ r}t        t        |            d}~ww xY w)	z connect to the chroot disable_root_checkr   zmchroot connection requires running as root. You can override this check with the `disable_root_check` option.
chroot_exeNzTHIS IS A LOCAL CHROOT DIRhostT)
get_optionr   geteuidr   r   isabs
chroot_cmdr   
ValueErrorstrr   r   _connect
_connecteddisplayvvvr   )r   er!   s     r"   r/   zConnection._connectw   s    349JTU U 77==67"ool;DO+".t|/L"M 	j$(*KK44;;KG"DO 	  +"3q6**+s   ;C" "	D+C??Dc                P   | j                  d      }| j                  | j                  |d|g}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 chroot.  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.
        
executablez-czEXEC r'   surrogate_or_stricterrorsF)shellstdinstdoutstderr)	r)   r,   r   r1   r2   r   
subprocessPopenPIPE)r   cmdr:   r5   	local_cmdips          r"   _buffered_exec_commandz!Connection._buffered_exec_command   s     __\2
__dkk:tSI	eI;'dkk:HQR1Xa(=>R	RYe5$.OOJOOM 	 Ss   B#c                    t         t        |   |||       | j                  |      }|j	                  |      \  }}|j
                  ||fS )z run a command on the chroot )in_datasudoable)r   r   exec_commandrD   communicate
returncode)r   r@   rF   rG   rC   r;   r<   r!   s          r"   rH   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   r   sepr   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| d|       	 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# t.        $ r t#        d|       w xY w)z& transfer a file from local to chroot zPUT  TO r'   r6   r7   rbz count=0 zdd of= bs=)r:   3chroot connection requires dd command in the chrootfailed to transfer file  to r   :

Nz"file or module does not exist at: )r   r   put_filer1   r2   r   r   rP   openr   r   fstatfilenost_sizerD   r
   OSErrorr   rI   	Exception	traceback	print_excrJ   IOError)	r   in_pathout_pathin_filecountrC   r;   r<   r!   s	           r"   r[   zConnection.put_file   s   j$((;d7)4z2Et66x@A	Ohw/DEtL pPWxx 0199&EE^33fXJd7)TYSZ4[cj3kA[%&]]_NFF <<1$&)A'$xjX[\b[cceflem'noo %p p  ^&'\]]^ ! ['')&)A'$xj'YZZ[p p   	O!CG9MNN	OsT   E /3E#DD&E=E DE/EEEE E E4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| d|       	 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 chroot to local zFETCH rR   r'   zdd if=rU   rV   r6   r7   zwb+rW   rX   r   rY   rZ   N)r   r   
fetch_filer1   r2   r   r   rP   rD   r
   r`   r   r\   r   r;   readwritera   rb   rc   rI   rJ   )	r   re   rf   rC   out_filechunkr;   r<   r!   s	           r"   rj   zConnection.fetch_file   s   j$*7H=fWIT(44;;Gd55g>?	V++fWIT',KLA (8,ABEJ 	lhWg.NN5)HHMM'2E  ]]_NFF||q "%=gYd8*TWX^W__abhai#jkk !	l 	l  	VTUU	V  W##%"%=gYd8*#UVVW	l 	ls1   D# 
E-AD; 9E-#D8;/E**E--E6c                8    t         t        |           d| _        y)z. terminate the connection; nothing to do here FN)r   r   closer0   )r   r!   s    r"   rp   zConnection.close   s    j$%'r#   )NF)__name__
__module____qualname____doc__	transporthas_pipelininghas_ttydefault_userr   r/   r=   r?   rD   rH   staticmethodrP   r[   rj   rp   __classcell__)r!   s   @r"   r   r   Z   sd    **IN GLg"#( 1; $, - -O4l0   r#   r   )
__future__r   DOCUMENTATIONEXAMPLESr   os.pathr=   rb   ansible.errorsr   ansible.module_utils.basicr   #ansible.module_utils.common.processr   ansible.module_utils.six.movesr   +ansible.module_utils.common.text.convertersr   ansible.plugins.connectionr	   r
   ansible.utils.displayr   r1   r    r#   r"   <module>r      sQ    #4l 
    ' 4 < 6 @ > )
)P  P r#   