
    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 d dlmZmZ d d	lmZ  e       Z G d
 de      Zy)    )annotationsaT  
author: Ansible Core Team
name: zone
short_description: Run tasks in a zone instance
description:
  - Run commands or put/fetch files to an existing zone.
options:
  remote_addr:
    description:
      - Zone identifier.
    type: string
    default: inventory_hostname
    vars:
      - name: ansible_host
      - name: ansible_zone_host
N)AnsibleError)shlex_quote)get_bin_path)to_bytes)ConnectionBaseBUFSIZE)Displayc                       e Zd ZdZdZdZdZ fdZed        Z	d Z
d Z fd	Zej                  fd
Zd fd	Zd Z fdZ fdZ fdZ xZS )
Connectionz Local zone based connections zcommunity.general.zoneTFc                   t        t        | 
  ||g|i | | j                  j                  | _        t        j                         dk7  rt        d      t        | j                  d            | _        t        | j                  d            | _        | j
                  | j                         vrt        d| j
                         y )Nr   z(zone connection requires running as rootzoneadmzloginzincorrect zone name )superr   __init___play_contextremote_addrzoneosgeteuidr   r   _search_executablezoneadm_cmd
zlogin_cmd
list_zones)selfplay_context	new_stdinargskwargs	__class__s        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/zone.pyr   zConnection.__init__4   s    j$(yR4R6R&&22	::<1IJJ#D$;$;I$FG"4#:#:8#DE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_executableB   s7    	J
++ 	J*-GHII	Js   
 %c                D   t        j                  | j                  ddgt         j                  t         j                  t         j                        }g }|j                  j                         D ]0  }|j                  d      }|d   dk7  s|j                  |d          2 |S )Nlistz-ipstdinstdoutstderr:   global)
subprocessPopenr   PIPEr*   	readlinessplitappend)r   processzonesliness        r!   r   zConnection.list_zonesI   s    ""D$4$4fe#D)3*4//*//S NN,,. 	#D

3AtxQqT"		# r"   c                4   t        j                  | j                  dt        | j                        ddgt         j
                  t         j
                  t         j
                        }|j                  j                         d   j                  d      d   }| dS )	Nz-zr'   z-pr(   r   r,      z/root)	r/   r0   r   r   r   r1   r*   r2   r3   )r   r5   paths      r!   get_zone_pathzConnection.get_zone_pathW   s|     ""D$4$4dHTYY<OQWY]#^)3*4//*//S
 ~~'')!,2237:u~r"   c                    t         t        |           | j                  s)t        j                  d| j                         d| _        yy)z) connect to the zone; nothing to do here zTHIS IS A LOCAL ZONE DIRhostTN)r   r   _connect
_connecteddisplayvvvr   r   r    s    r!   r@   zConnection._connectb   s8    j$(*KK2KC"DO r"   c                   | j                   | j                  |g}t        t        |      }t        j                  d| | j                         t        j                  |d|t        j                  t        j                        }|S )a-   run a command on the zone.  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EXEC r>   F)shellr)   r*   r+   )	r   r   mapr   rB   rC   r/   r0   r1   )r   cmdr)   	local_cmdps        r!   _buffered_exec_commandz!Connection._buffered_exec_commandi   sj     __dii5	),	eI;'dii8Ye5$.OOJOOM r"   c                    t         t        |   |||       | j                  |      }|j	                  |      \  }}|j
                  ||fS )z run a command on the zone )in_datasudoable)r   r   exec_commandrK   communicate
returncode)r   rH   rM   rN   rJ   r*   r+   r    s          r!   rO   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;   sepjoinnormpath)r   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        |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 zone zPUT  TO r>   rbz count=0 zdd of= bs=)r)   z/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_filerB   rC   r   r   rX   openr   fstatfilenost_sizerK   r	   OSErrorr   rP   	Exception	traceback	print_excrQ   IOError)	r   in_pathout_pathin_filecountrJ   r*   r+   r    s	           r!   rb   zConnection.put_file   s   j$((;d7)4z2Ct66x@A	Ogt$ pxx 0199&EEZ33fXJd7)TYSZ4[cj3kA[%&]]_NFF <<1$&)A'$xjX[\b[cceflem'noo %p p  Z&'XYYZ ! ['')&)A'$xj'YZZ[p p   	O!CG9MNN	OsT   E $3EC;8D&E2E ;DE/EEE
E E E)c                   t         t        |   ||       t        j	                  d| d| | j
                         t        | j                  |            }	 | j                  d| dt               }t        |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 zone to local zFETCH rZ   r>   zdd if=r]   z/zone connection requires dd command in the zonezwb+r^   r_   r   r`   ra   N)r   r   
fetch_filerB   rC   r   r   rX   rK   r	   rg   r   rc   r*   readwriterh   ri   rj   rP   rQ   )	r   rl   rm   rJ   out_filechunkr*   r+   r    s	           r!   rq   zConnection.fetch_file   s{   j$*7H=fWIT(4499Ed55g>?	R++fWIT',KLA (E" 	lhWg.NN5)HHMM'2E  ]]_NFF||q "%=gYd8*TWX^W__abhai#jkk !	l 	l  	RPQQ	R  W##%"%=gYd8*#UVVW	l 	ls1   D ?E"AD09E"D-0/EE""E+c                8    t         t        |           d| _        y)z. terminate the connection; nothing to do here FN)r   r   closerA   rD   s    r!   rw   zConnection.close   s    j$%'r"   )NF)__name__
__module____qualname____doc__	transporthas_pipelininghas_ttyr   staticmethodr   r   r<   r@   r/   r1   rK   rO   rX   rb   rq   rw   __classcell__)r    s   @r!   r   r   -   sn    ((INGC J J	# 1; (,-O4l0   r"   r   )
__future__r   DOCUMENTATIONr   os.pathr/   ri   ansible.errorsr   ansible.module_utils.six.movesr   #ansible.module_utils.common.processr   +ansible.module_utils.common.text.convertersr   ansible.plugins.connectionr   r	   ansible.utils.displayr
   rB   r    r"   r!   <module>r      sF    #" 
    ' 6 < @ > )
)\  \ r"   