
    VhO'                         d dl mZmZmZ e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mZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ  e       Z G d de      Z y)    )absolute_importdivisionprint_functiona&  
name: nsenter
short_description: execute on host running controller container
version_added: 1.9.0
description:
  - This connection plugin allows Ansible, running in a privileged container, to execute tasks on the container host instead
    of in the container itself.
  - This is useful for running Ansible in a pull model, while still keeping the Ansible control node containerized.
  - It relies on having privileged access to run C(nsenter) in the host's PID namespace, allowing it to enter the namespaces
    of the provided PID (default PID 1, or init/systemd).
author: Jeff Goldschrafe (@jgoldschrafe)
options:
  nsenter_pid:
    description:
      - PID to attach with using nsenter.
      - The default should be fine unless you are attaching as a non-root user.
    type: int
    default: 1
    vars:
      - name: ansible_nsenter_pid
    env:
      - name: ANSIBLE_NSENTER_PID
    ini:
      - section: nsenter_connection
        key: nsenter_pid
notes:
  - The remote user is ignored; this plugin always runs as root.
  - "This plugin requires the Ansible controller container to be launched in the following way: (1) The container image contains
    the C(nsenter) program; (2) The container is launched in privileged mode; (3) The container is launched in the host's
    PID namespace (C(--pid host))."
N)AnsibleError)binary_type	text_type)to_bytes	to_nativeto_text)ConnectionBase)Display)unfrackpath)	selectorsc                   V     e Zd ZdZdZdZ fdZd Zd
 fd	Z fdZ	 fdZ
d	 Z xZS )
Connectionz2Connections to a container host using nsenter
    zcommunity.docker.nsenterFc                 :    t        t        | 
  |i | d | _        y )N)superr   __init__cwd)selfargskwargs	__class__s      o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/docker/plugins/connection/nsenter.pyr   zConnection.__init__E   s    j$($9&9    c                    | j                  d      | _        d| j                  _        | j                  sUt
        j                  dj                  | j                  j                        | j                  j                         d| _        | S )Nnsenter_pidrootz*ESTABLISH NSENTER CONNECTION FOR USER: {0}hostT)	
get_option_nsenter_pid_play_contextremote_user
_connecteddisplayvvvformatremote_addrr   s    r   _connectzConnection._connectI   sw     OOM: *0&KK=DD&&22 ''33	   #DOr   c           
         t         t        |   |||       t        j	                  d       t
        j                  r!t
        j                  j                         d   nd }t        j                  j                  t        |d            st        d|z        ddd	d
ddddj                  | j                        dg	}t        |t         t"        f      r!||gz   }t        dj%                  |            }n||z   }|D cg c]  }t        |       }}t        j'                  dj                  t)        |            | j*                  j,                         t        j	                  d       d }t.        j0                  }	|r>| j2                  r2| j2                  j5                         r	 t7        j8                         \  }}	t/        j@                  |t        |t         t"        f      t        |t         t"        f      r|nd | jB                  |	t.        j0                  t.        j0                        }|t        jD                  |	       t        j	                  d       | j2                  rA| j2                  j5                         r&|r#tG        jF                  |jH                  tF        jJ                  tG        jF                  |jH                  tF        jL                        t        jN                  z         tG        jF                  |jP                  tF        jJ                  tG        jF                  |jP                  tF        jL                        t        jN                  z         tS        jT                         }|jW                  |jH                  tR        jX                         |jW                  |jP                  tR        jX                         d}	 | j2                  j[                  |      sH| j2                  j]                  |      s,|j_                  | j*                  j`                        }|s*|jc                         \  }}t        dt?        |      z         |D ]m  \  }}|jd                  |jH                  k(  r|jH                  jg                         }:|jd                  |jP                  k(  sT|jP                  jg                         }o s*|jc                         \  }}t        dt?        |      z         ||z  }| j2                  j[                  |      s| j2                  j]                  |      s,|jE                          | j2                  j[                  |      sw| j2                  ji                  d| j*                        }|*|jj                  jm                  t        |d      dz          n$t        jl                  |t        |d      dz          tG        jF                  |jH                  tF        jJ                  tG        jF                  |jH                  tF        jL                        t        jN                   z         tG        jF                  |jP                  tF        jJ                  tG        jF                  |jP                  tF        jL                        t        jN                   z         t        j	                  d       |jc                  |      \  }}t        j	                  d       |rt        jD                  |       t        j	                  d       |jn                  ||fS c c}w # t:        t<        f$ r,}
t        j	                  dt?        |
      z         Y d }
~
d }
~
ww xY w# |jE                          w xY w) N)in_datasudoablezin nsenter.exec_command()r   surrogate_or_stricterrorsz^failed to find the executable specified %s. Please verify if the executable exists and re-try.nsenterz--ipcz--mountz--netz--pidz--utsz--preserve-credentialsz--target={0}z-- zEXEC {0}r   zopening command with Popen()zUnable to open pty: %s)shell
executabler   stdinstdoutstderrz!done running command with Popen()r   z:timeout waiting for privilege escalation password prompt:
z;privilege output closed while waiting for password prompt:
become_pass)playcontext   
z!getting output with communicate()zdone communicatingz done with nsenter.exec_command())8r   r   exec_commandr&   debugCDEFAULT_EXECUTABLEsplitospathexistsr	   r   r(   r"   
isinstancer   r   joinr'   r   r#   r)   
subprocessPIPEbecomeexpect_promptptyopenptyIOErrorOSErrorr
   Popenr   closefcntlr7   F_SETFLF_GETFL
O_NONBLOCKr8   r   DefaultSelectorregister
EVENT_READcheck_successcheck_password_promptselecttimeoutcommunicatefileobjreadr!   r6   write
returncode)r   cmdr-   r.   r5   nsenter_cmd_parts	cmd_partsargmasterr6   epselectorbecome_outputeventsr7   r8   keyeventchunkr9   r   s                        r   r<   zConnection.exec_commandZ   s   j$,S'H,U12898L8LQ))//1!4RV
ww~~hz:OPQ  UWa b c c
 $!!$"3"34

 cI{34)SE1I388I./C)C/I,56S8C=6C6K&&ws|44;M;M;Y;YZ45 (A(A(C
G # S9k":;%/i5M%NzTX????
 HHUO9:;;4;;4468KK%--QXXu}}1UXZXeXe1efKK%--QXXu}}1UXZXeXe1ef 002Hahh	(<(<=ahh	(<(<=M!++33MB4;;KlKlmzK{%__T-?-?-G-GHF!)**+hkt  vC  lD  ,D  E  E&, 4
U;;!((2$%HHMMOE [[AHH4$%HHMMOE	4 !)**+ilu  wD  mE  ,E  F  F!U*M ++33MB4;;KlKlmzK{"  ;;,,];"kk44]PTPbPb4c>GGMM(;?T"UX]"]^HHVXkBW%X[`%`aKK%--QXXu}}1UY[YfYfXf1fgKK%--QXXu}}1UY[YfYfXf1fg9:w/*+ HHV89ff--g 7" W% G61EFFGZ  s2   <\#\  C] !B]  ]/!]]]0c           	      V   t         t        |   ||       t        || j                        }t        || j                        }t
        j                  dj                  ||      | j                  j                         	 t        t        |d      d      5 }|j                         }d d d        | j                  d|g      \  }}}|d	k7  rt        d
j                  ||            y # 1 sw Y   CxY w# t        $ r)}t        d
j                  |t!        |                  d }~ww xY w)NbasedirzPUT {0} to {1}r   r/   r0   rbtee)r`   r-   r   #failed to transfer file to {0}: {1})r   r   put_filer   r   r&   r'   r(   r#   r)   openr	   r]   r<   r   rL   r
   )
r   in_pathout_pathin_filer-   rcouterrre   r   s
            r   rs   zConnection.put_file   s   j$((;gtxx8x:%,,Wh?dFXFXFdFde	ehw/DEtL )PW!,,.),,%1BG,TLBSQw"#H#O#OPXZ]#^__ ) )
  	eDKKHV_`aVbcdd	es1    C6 C*(AC6 *C3/C6 6	D(?$D##D(c           	      h   t         t        |   ||       t        || j                        }t        || j                        }	 | j                  d|g      \  }}}t        j                  dj                  ||      | j                  j                         |dk7  rt        dj                  ||            t        t        |d	      d
      5 }|j                  |       d d d        y # 1 sw Y   y xY w# t        $ r2}t        dj                  t!        |      t!        |                  d }~ww xY w)Nrn   cat)r`   zFETCH {0} TO {1}r   r   rr   r/   r0   wb)r   r   
fetch_filer   r   r<   r&   r'   r(   r#   r)   r   rt   r	   r^   rL   r
   )	r   ru   rv   rx   ry   rz   out_filere   r   s	           r   r~   zConnection.fetch_file   s   j$*7H=gtxx8x:	p,,%1A,BLBSKK+227HEDL^L^LjLjKkQw"#H#O#OPWY\#]^^hx0EFM $QYs#$ $ $ 	pDKKIV^L_ajklamnoo	ps7   B
C6 C*!C6 *C3/C6 3C6 6	D1?-D,,D1c                     d| _         y)z. terminate the connection; nothing to do here FN)r%   r*   s    r   rO   zConnection.close   s	    r   )NT)__name__
__module____qualname____doc__	transporthas_pipeliningr   r+   r<   rs   r~   rO   __classcell__)r   s   @r   r   r   >   s6     +IN"p.de p  r   r   )!
__future__r   r   r   type__metaclass__DOCUMENTATIONrA   rJ   rF   rP   ansible.constants	constantsr>   ansible.errorsr   ansible.module_utils.sixr   r   +ansible.module_utils.common.text.convertersr	   r
   r   ansible.plugins.connectionr   ansible.utils.displayr   ansible.utils.pathr   Cansible_collections.community.docker.plugins.module_utils.selectorsr   r&   r    r   r   <module>r      s[    C B@ 
 
    ' ; T T 5 ) * Y )p  p r   