
    VhP                         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Zd dlZdZ		 d dl
ZdZ	d dlmZ d dlmZmZ d dlmZ  G d	 d
e      Zy# e$ r Y 'w xY w)    )annotationsa  
author: Joerg Thalheim (!UNKNOWN) <joerg@higgsboson.tk>
name: lxc
short_description: Run tasks in LXC containers using lxc python library
description:
  - Run commands or put/fetch files to an existing LXC container using lxc python library.
options:
  remote_addr:
    description:
      - Container identifier.
    type: string
    default: inventory_hostname
    vars:
      - name: inventory_hostname
      - name: ansible_host
      - name: ansible_lxc_host
  executable:
    default: /bin/sh
    description:
      - Shell executable.
    type: string
    vars:
      - name: ansible_executable
      - name: ansible_lxc_executable
NFTerrors)to_bytes	to_native)ConnectionBasec                  x     e Zd ZdZdZdZdZ fdZ fdZe	d        Z
d Zd fd		Z fd
Z fdZ fdZ xZS )
Connectionz Local lxc based connections zcommunity.general.lxcTrootc                R    t        t        | 
  ||g|i | d | _        d | _        y N)superr
   __init__container_name	container)selfplay_context	new_stdinargskwargs	__class__s        l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/lxc.pyr   zConnection.__init__=   s-    j$(yR4R6R"    c                   t         t        |           t        sd}t	        j
                  |      | j                  d      }| j                  r| j                  |k(  ry|| _        | j                  j                  d| j                         t        j                  | j                        | _        | j                  j                  dk(  r"t	        j
                  | j                   d      y)z( connect to the lxc; nothing to do here z%lxc python bindings are not installedremote_addrNzTHIS IS A LOCAL LXC DIRhostSTOPPEDz is not running)r   r
   _connect
HAS_LIBLXCr   AnsibleError
get_optionr   r   _displayvvv_lxc	Containerstate)r   msgr   r   s      r   r   zConnection._connectC   s    j$(*9C%%c**7>>d11^C,3$:M:MN(;(;<>>9,%%)<)<(=_&MNN -r   c                   |g |g i}||g}|r|g}ng }t        |      dkD  st        |      dkD  r	 t        j                  ||g       \  }}	}
|	D ];  }|t        j                  ||      d  }t        |      dk(  s+|j                  |       = |D ]?  }t        j                  |d      }|s|j                  |       ||   j                  |       A t        |      dkD  rt        |      dkD  rt        j                  | d      \  } }|dj                  ||         dj                  ||         fS # t        j                  $ r,}|j                  d   t        j
                  k(  rY d }~U d }~ww xY w)Nr   i   r   )lenselecterrorr   errnoEINTRoswriteremovereadappendwaitpidjoin)pidin_datastdinstdoutstderrbufread_fds	write_fdsready_readsready_writesdummyefddata
returncodes                  r   _communicatezConnection._communicateV   si   r62&F#II(ma3y>A#539==9VX3Y0\5
 # )!"((2w"7"89w<1$$$R() " %wwr5)OOB'Bt$	% (ma3y>A#5" JJsA.j388CK0#((3v;2GGG! << 66!9+s   D/ /E. E)(E))E.c                    t        j                   |t         j                        t        j                  z  }t        j                   |t         j                  |       |S r   )fcntlF_GETFLr/   
O_NONBLOCKF_SETFL)r   rB   flagss      r   _set_nonblockingzConnection._set_nonblockings   s8    B.>Bu-	r   c                   t         t        |   |||       t        | j	                  d      d      }|dt        |d      g}d\  }}d\  }}	d\  }
}	 t        j                         \  }}t        j                         \  }}	| j                  |      | j                  |	      t        j                  d}|r+t        j                         \  }
}| j                  |
      |d<   | j                  j                  d	| | j                  
        | j                  j                  t        j                  |fi |}|dk(  r$d| j                   }t!        j"                  |      t        j$                  |      }t        j$                  |	      }	|
rt        j$                  |
      }
| j'                  |||||      ||||	|
|g}|D ]  }|st        j$                  |        S # ||||	|
|g}|D ]  }|st        j$                  |        w xY w)z run a command on the chroot )r7   sudoable
executablesurrogate_or_strictr   z-c)NN)r9   r:   
env_policyr8   zEXEC r   zfailed to attach to container )r   r
   exec_commandr   r"   r/   piperL   r%   LXC_ATTACH_CLEAR_ENVr#   r$   r   r   attachattach_run_commandr   r!   closerE   )r   cmdr7   rN   rO   	local_cmdread_stdoutwrite_stdoutread_stderrwrite_stderr
read_stdinwrite_stdinr   r6   r(   fdsrB   r   s                    r   rS   zConnection.exec_commandx   s   j$,S'H,U t|<EZ[
y=R'ST	$.!\$.!\",
K'	!(*	%K(*	%K //=//="77F *,'')'
K"&"7"7
"CwMMi[18K8KL'$..''(?(?UfUCby6t7J7J6KL))#..88L1L88L1LXXj1
$$S%,%0%0%0	2  C  !HHRL!  C  !HHRL!s   E!G H.Hc                   t         t        |   |       | j                  j	                  d| d | j
                         t        |d      }t        d      t        j                  j                  |      sd| }t        j                  |      	 t        |d      	 fd	}	 | j"                  j%                  |d
       	 j'                          y
# t        $ r- t        j                          t        j                   d|       w xY w# t        $ r/ t        j                          d }t        j                   |      w xY w# j'                          w xY w)z# transfer a file from local to lxc zPUT  TO r   rP   r   zfile or module does not exist: rbzfailed to open input file to c                t    t        d      5 }t        j                  |       d d d        y # 1 sw Y   y xY w)Nzwb+)openshutilcopyfileobj)r   dst_fileout_pathsrc_files     r   
write_filez'Connection.put_file.<locals>.write_file   s4    (E* ;h&&x:; ; ;s   .7Nzfailed to transfer file to )r   r
   put_filer#   r$   r   r   r/   pathexistsr   AnsibleFileNotFoundrf   IOError	traceback	print_excr!   r   attach_waitrX   )r   in_pathrj   r(   rl   rk   r   s     `  @r   rm   zConnection.put_file   sD   j$((;D	hZ8t?R?RS7+@AH-BCww~~g&3G9=C,,S11	QGT*H	;/**:t< NN  	Q!%%(EgY&OPP	Q  /##%3H:>))#../
 NNs*   C &E -D 6D8EE E!c                `   t         t        |   |       | j                  j	                  d d| | j
                         t        d      t        |d      }	 t        |d      	 fd}	 | j                  j                  |d	       	 j                          y	# t        $ r/ t        j                          d| }t        j                  |      w xY w# t        $ r2 t        j                          d
 d| }t        j                  |      w xY w# j                          w xY w)z  fetch a file from lxc to local zFETCH rc   r   rP   r   wbzfailed to open output file c                    	 t        d      5 }t        j                  |       d d d        j                          y # 1 sw Y   xY w# j                          w xY w)Nrd   )rf   rg   rh   rX   )r   rk   ri   ru   s     r   rl   z)Connection.fetch_file.<locals>.write_file   sR    %gt, ?**8X>?
 NN$? ?
 NN$s   A ?A AA ANzfailed to transfer file from z to )r   r
   
fetch_filer#   r$   r   r   rf   rq   rr   rs   r   r!   r   rt   rX   )r   ru   rj   r(   rl   ri   r   s    `   @r   ry   zConnection.fetch_file   s   j$*7H=F7)4z:ATATU7+@AH-BC	+Hd+H
	%/**:t< NN)  	+!/z:C%%c**	+  /##%5gYd8*M))#../
 NNs*    B" -D 4C "8C;DD D-c                8    t         t        |           d| _        y)z. terminate the connection; nothing to do here FN)r   r
   rX   
_connected)r   r   s    r   rX   zConnection.close   s    j$%'r   )NF)__name__
__module____qualname____doc__	transporthas_pipeliningdefault_userr   r   staticmethodrE   rL   rS   rm   ry   rX   __classcell__)r   s   @r   r
   r
   6   sU    ''INLO& H H8
3!j8>   r   r
   )
__future__r   DOCUMENTATIONr/   rg   rr   r+   rG   r-   r    lxcr%   ImportErroransibler   +ansible.module_utils.common.text.convertersr   r   ansible.plugins.connectionr   r
    r   r   <module>r      sd    #4 
     
	J  K 5u  u   		s   A
 
AA