
    Vh%                         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 d dlmZ  e       Z G d	 d
e      Zy)    )absolute_importdivisionprint_functiona  
    author: Tomas Tomecek (@TomasTomecek)
    name: podman
    short_description: Interact with an existing podman container
    description:
        - Run commands or put/fetch files to an existing container using podman tool.
    options:
      remote_addr:
        description:
          - The ID of the container you want to access.
        default: inventory_hostname
        vars:
          - name: ansible_host
          - name: inventory_hostname
          - name: ansible_podman_host
      remote_user:
        description:
            - User specified via name or UID which is used to execute commands inside the container. If you
              specify the user via UID, you must set C(ANSIBLE_REMOTE_TMP) to a path that exits
               inside the container and is writable by Ansible.
        ini:
          - section: defaults
            key: remote_user
        env:
          - name: ANSIBLE_REMOTE_USER
        vars:
          - name: ansible_user
      podman_extra_args:
        description:
          - Extra arguments to pass to the podman command line.
        default: ''
        ini:
          - section: defaults
            key: podman_extra_args
        vars:
          - name: ansible_podman_extra_args
        env:
          - name: ANSIBLE_PODMAN_EXTRA_ARGS
      podman_executable:
        description:
          - Executable for podman command.
        default: podman
        vars:
          - name: ansible_podman_executable
        env:
          - name: ANSIBLE_PODMAN_EXECUTABLE
N)get_bin_path)AnsibleError)to_bytes	to_native)ConnectionBaseensure_connect)Displayc                   p     e Zd ZdZdZdZ f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 )
Connectionzg
    This is a connection plugin for podman. It uses podman binary to interact with the containers
    zcontainers.podman.podmanFc                     t        t        | 
  ||g|i | | j                  j                  | _        d| _        d | _        | j                  j                  | _	        t        j                  d       y )NFz'Using podman 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        o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/containers/podman/plugins/connection/podman.pyr   zConnection.__init__W   sa    j$(yR4R6R!//;; &&22	>?    c                    | j                  d      }	 t        |      }|st        d|z        |g}| j                  d      r2|t	        j
                  t        | j                  d      d            z  }t        |t              r|j                  |       n|j                  |       |r|j                  | j                         |r||z  }|D cg c]  }t        |d       }}t        j                  d|| j                         t        j                   |dt        j"                  t        j"                  t        j"                  	      }	|	j%                  |
      \  }
}t        j'                  d|
z         t        j'                  d|z         t        j'                  d|	j(                  z         t        |
d      }
t        |d      }|	j(                  |
|fS # t        $ r t        d|z        w xY wc c}w )ar  
        run podman executable

        :param cmd: podman's command to execute (str or list)
        :param cmd_args: list of arguments to pass to the command (list of str/bytes)
        :param in_data: data passed to podman's stdin
        :param use_container_id: whether to append the container ID to the command
        :return: return code, stdout, stderr
        podman_executablez%s command not found in PATHpodman_extra_argssurrogate_or_stricterrorszRUN hostF)shellstdinstdoutstderr)inputz	STDOUT %sz	STDERR %sz
RC CODE %s)
get_optionr   
ValueErrorr   shlexsplitr	   
isinstancestrappendextendr   r   r   vvv
subprocessPopenPIPEcommunicatevvvvv
returncode)r   cmdcmd_argsin_datause_container_idpodman_exec
podman_cmd	local_cmdipr-   r.   s               r!   _podmanzConnection._podmana   s    oo&9:	M%k2J =KLLL	??./OO$78023 3I c3S!S!T//0!IHQR1Xa(=>R	R	+$2D2DEYe:??$.OOJOOM W5kF*+kF*+lQ\\12&)>?&)>?||VV++?  	M=KLL	M& Ss   G G8G5c                 R   t         t        |           | j                  d      \  }| _        }|dk7  r;t
        j                  d| j                  d|j                                d
| _        yt        j                  | j                  j                               s?t
        j                  d| j                  j                         z         d| _        d
| _        y| j                  j                         | _        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: z5Failed to mount container with CGroups2: empty dir %sNzMOUNTPOINT z RC  STDERR T)r   r   _connectrH   r   r   r   r   striposlistdirr=   r   )r   rcr.   r    s      r!   rL   zConnection._connect   s    
 	j$(*(,W(=%Dv7LLt?Q?QSYS_S_Sabc  D--3356LLPSWSdSdSjSjSllm $D  !% 1 1 7 7 9DMMT=N=NPRTZ[\r"   c                 6   t         t        |   |||       t        j                  t        |d            }dg}| j                  r|j                  d| j                  f       | j                  |||      \  }}}t        j                  d|d|       |||fS )z? run specified command in a running OCI container using podman )rA   sudoabler&   r'   execz--userzSTDOUT rK   )r   r   exec_commandr2   r3   r	   r   r7   rH   r   r=   )
r   r?   rA   rR   cmd_args_listexec_args_listrP   r-   r.   r    s
            r!   rT   zConnection.exec_command   s     	j$,S'H,U Ic:O$PQ 99!!8TYY"78!\\.-QFFvv>?66!!r"   c           
         t         t        |   ||       t        j	                  d|d|| j
                         | j                  r| j                  r| j                  d|| j
                  dz   |z   gd      \  }}}|dk7  rQ| j                  dd	|| j
                  dz   |z   gd      \  }}}|dk7  r!t        d
|d|d| j
                  d|      | j                  r#| j                  dd| j                  |g      \  }}}|dk7  r+t        d|d| j                  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)   cp:FrB   r   z--pause=falsezFailed to copy file from  to z in container 
rS   chownzFailed to chown file z
 for user r&   r'   N)r   r   put_filer   r8   r   r   r   rH   r   r   shutilcopyfile)r   in_pathout_pathrP   r-   r.   real_out_pathr    s          r!   r_   zConnection.put_file   s   j$((;gx8t?Q?QR  DII!%w 2 2S 88 CDW\ ". "B Qw%)\\?GT5G5G#5MPX5XYlq &2 &"FF 7&#Xt/A/A6K  yy%)\\Wdii:&<"FFQw" $))T-?-?I   !--J_0``MOO)>?/DEr"   c           
         t         t        |   ||       t        j	                  d|d|| j
                         | j                  sQ| j                  d| j
                  dz   |z   |gd      \  }}}|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 rX   r)   rY   rZ   Fr[   r   zFailed to fetch file from r\   z from container r]   r&   r'   N)r   r   
fetch_filer   r8   r   r   rH   r   r   r`   ra   )r   rb   rc   rP   r-   r.   real_in_pathr    s          r!   rf   zConnection.fetch_file   s    j$*7H=:ASAST  !%t))C/'98DW\ ". "^BQw"Xt'9'96$C D D   ,,xH]/^^LOO.CD*?@r"   c                 8    t         t        |           d| _        y)z  unmount container's filesystem FN)r   r   closer   )r   r    s    r!   ri   zConnection.close   s    j$%'  r"   )NNT)NF)__name__
__module____qualname____doc__	transporthas_pipeliningr   rH   rL   r   rT   r_   rf   ri   __classcell__)r    s   @r!   r   r   L   sP    
 +I N@,,\" " "@"   r"   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONrN   r2   r`   r9   #ansible.module_utils.common.processr   ansible.errorsr   ansible.module_utils._textr   r	   ansible.plugins.connectionr
   r   ansible.utils.displayr   r   r    r"   r!   <module>r{      sM    C B.` 
    < ' : E )
)[  [ r"   