
    VhZ                         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mZmZ d dlmZmZ d dlmZ  e       Z G d d	e      Zy)
    )absolute_importdivisionprint_functionae  
    short_description: Interact with an existing buildah container
    description:
        - Run commands or put/fetch files to an existing container using buildah tool.
    author: Tomas Tomecek (@TomasTomecek)
    name: buildah
    options:
      remote_addr:
        description:
            - The ID of the container you want to access.
        default: inventory_hostname
        vars:
            - name: ansible_host
            - name: inventory_hostname
#        keyword:
#            - name: hosts
      remote_user:
        description:
            - User specified via name or ID which is used to execute commands inside the container.
        ini:
          - section: defaults
            key: remote_user
        env:
          - name: ANSIBLE_REMOTE_USER
        vars:
          - name: ansible_user
#        keyword:
#            - name: remote_user
N)AnsibleError)to_bytes	to_nativeto_text)ConnectionBaseensure_connect)Displayc                   v     e Zd ZdZdZdZ fdZd ZddZ fdZ	e
d fd	       Z fd	Z fd
Z fdZ xZS )
Connectionzi
    This is a connection plugin for buildah: it uses buildah binary to interact with the containers
    zcontainers.podman.buildahTc                     t        t        | 
  ||g|i | | j                  j                  | _        d| _        d | _        | j                  j                  | _	        t        j                  d       y )NFz(Using buildah connection from collection)superr   __init___play_contextremote_addr_container_id
_connected_mount_pointremote_useruserdisplayvvvv)selfplay_context	new_stdinargskwargs	__class__s        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/containers/podman/plugins/connection/buildah.pyr   zConnection.__init__C   sc    j$(yR4R6R!//;;  &&22	?@    c                 Z    | j                  ddt        | j                  d      z   g       y )Ns   configs   --user=surrogate_or_stricterrors)_buildahr   r   )r   s    r!   	_set_userzConnection._set_userO   s$    i*x		J_/`"`!abr"   c                 F   d}|g}t        |t              r|j                  |       n|j                  |       | j                  rT| j                  dk7  rE|dk(  r|j                  d| j                  f       n"|dk(  r|j                  d| j                  f       |j                  | j
                         |r3t        |t              r|j                  |       n|j                  |       |D cg c]  }t        |d       }}t        j                  d	|| j
                  
       |rt        |d      }nt        j                  }t        j                  |dt        j                  |t        j                        }	|	j                  |      \  }
}t        j                  dt        |
      z         t        j                  dt        |      z         t        j                  d|	j                   z         t        |
d      }
t        |d      }|	j                   |
|fS c c}w )aT  
        run buildah executable

        :param cmd: buildah's command to execute (str)
        :param cmd_args: list of arguments to pass to the command (list of str/bytes)
        :param in_data: data passed to buildah's stdin
        :param outfile_stdout: file for writing STDOUT to
        :return: return code, stdout, stderr
        buildahrootrunz--usercopyz--chownr$   r%   zRUN hostwbF)shellstdinstdoutstderr)inputz	STDOUT %sz	STDERR %sz
RC CODE %s)
isinstancestrappendextendr   r   r   r   vvvopen
subprocessPIPEPopencommunicater   r	   
returncode)r   cmdcmd_argsin_dataoutfile_stdoutbuildah_exec	local_cmdi	stdout_fdpr3   r4   s               r!   r'   zConnection._buildahR   s    !!N	c3S!S!99f,e|  (DII!67  )TYY!78++,(C(  *  * () a(=> )	 ) 		+$2D2DE^T2I"IYe:??$-jooG W5[76?23[76?23\ALL01&)>?&)>?||VV++#)s   -Hc                    t         t        |           | j                  d      \  }| _        }|dk7  r;t
        j                  d| j                  d|j                                d
| _        y| j                  j                         t        t        j                  j                  d      z   | _        t
        j                  d| j                  d|d	|       d
| _        y)z
        no persistent connection is being maintained, mount container's filesystem
        so we can easily access it
        mountr   zFailed to mount container z: r$   r%   zMOUNTPOINT z RC  STDERR TN)r   r   _connectr'   r   r   vr   stripr   ospathsepr   r   )r   rcr4   r    s      r!   rM   zConnection._connect   s    
 	j$(*(,g(>%Dv7IID<N<NPVP\P\P^_`  !% 1 1 7 7 9HRWW[[Yn<o oDLLD<M<MrSYZ[r"   c                     t         t        |   |||       t        j                  t        |d            }| j                  d||      \  }}}t        j                  d|d|       |||fS )z@ run specified command in a running OCI container using buildah )rC   sudoabler$   r%   r,   zSTDOUT z
STDERR )	r   r   exec_commandshlexsplitr   r'   r   r   )	r   rA   rC   rU   cmd_args_listrS   r3   r4   r    s	           r!   rV   zConnection.exec_command   sj     	j$,S'H,U Ic:O$PQ!]]5-IFFvv>?66!!r"   c           
         t         t        |   ||       t        j	                  d|d|| j
                         | j                  r| j                  r?| j                  d||g      \  }}}|dk7  r!t        d|d|d| j
                  d	|      y| j                  t        |d
      z   }t        j                  t        |d
      t        |d
             y)zH Place a local file located in 'in_path' inside container at 'out_path' zPUT  TO r.   r-   r   zFailed to copy file from  to z in container 
r$   r%   N)r   r   put_filer   r:   r   r   r   r'   r   r   shutilcopyfile)r   in_pathout_pathrS   r3   r4   real_out_pathr    s          r!   r^   zConnection.put_file   s    j$((;gx8t?Q?QR  DII!%(+"-BQw"4+=+=vG   !--J_0``MOO)>?/DEr"   c           
         t         t        |   ||       t        j	                  d|d|| j
                         | j                  sL| j                  ddt        |d      g|      \  }}}|d	k7  r!t        d
|d|d| j
                  d|      y| j                  t        |d      z   }t        j                  t        |d      t        |d             y)zS obtain file specified via 'in_path' from the container and place it at 'out_path' zFETCH r[   r.   r,   catr$   r%   )rD   r   zFailed to fetch file from r\   z from container r]   N)r   r   
fetch_filer   r:   r   r   r'   r   r   r_   r`   )r   ra   rb   rS   r3   r4   real_in_pathr    s          r!   rf   zConnection.fetch_file   s    j$*7H=h(.2.@.@ 	 	B  !%1FGH' "/ ")B Qw"Xt'9'96$C D D   ,,)>?@LOO.CD*?@r"   c                     t         t        |           | j                  d      \  }}}t        j                  d|d|d|       d| _        y)z  unmount container's filesystem umountzRC z STDOUT rL   FN)r   r   closer'   r   r   r   )r   rS   r3   r4   r    s       r!   rj   zConnection.close   s?    j$%'!]]84FFBGHr"   )NNN)NF)__name__
__module____qualname____doc__	transporthas_pipeliningr   r(   r'   rM   r   rV   r^   rf   rj   __classcell__)r    s   @r!   r   r   :   sS    
 ,IN
Ac/,b 
" 
"&*   r"   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONrP   rW   r_   r<   ansible.errorsr   ansible.module_utils._textr   r   r	   ansible.plugins.connectionr
   r   ansible.utils.displayr   r   r    r"   r!   <module>r{      sL    C B< 
    ' C C E )
)Q  Q r"   