
    VhA                    t    d dl mZ dZd dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZ  e       Z G d d	e      Zy)
    )annotationsaD  
name: qubes
short_description: Interact with an existing QubesOS AppVM

description:
  - Run commands or put/fetch files to an existing Qubes AppVM using qubes tools.
author: Kushal Das (@kushaldas)


options:
  remote_addr:
    description:
      - VM name.
    type: string
    default: inventory_hostname
    vars:
      - name: ansible_host
  remote_user:
    description:
      - The user to execute as inside the VM.
    type: string
    default: The I(user) account as default in Qubes OS.
    vars:
      - name: ansible_user
#        keyword:
#            - name: hosts
N)to_bytes)ConnectionBaseensure_connect)AnsibleConnectionFailure)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 )
ConnectionzcThis is a connection plugin for qubes: it uses qubes-run-vm binary to interact with the containers.zcommunity.general.qubesTc                    t        t        | 
  ||g|i | | j                  j                  | _        d| _        d| _        | j                  j                  r| j                  j                  | _        y y )NFuser)	superr
   __init___play_contextremote_addr_remote_vmname
_connectedr   remote_user)selfplay_context	new_stdinargskwargs	__class__s        n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/qubes.pyr   zConnection.__init__<   sg    j$(yR4R6R"00<<	))**66DI *    c                   t         j                  d|       |j                  d      s| d}g }|j                  g d       | j                  dk7  r|j                  d| j                  g       |j                  | j                         |j                  |       |D cg c]  }t        |d       }}t         j                  d|       t         j                  d	| | j                  
       t        j                  |dt        j                  t        j                  t        j                        }|j                  j                  t        |d             |j                  |      \  }}|j                  ||fS c c}w )zrun qvm-run executable

        :param cmd: cmd string for remote system
        :param in_data: data passed to qvm-run-vm's stdin
        :return: return code, stdout, stderr
        zCMD: 
)qvm-run	--pass-ioz	--servicer   z-usurrogate_or_strict)errorszLocal cmd: zRUN hostF)shellstdinstdoutstderr)input)displayvvvvendswithextendr   appendr   r   vvv
subprocessPopenPIPEr%   writecommunicate
returncode)	r   cmdin_datar$   	local_cmdipr&   r'   s	            r   _qubeszConnection._qubesF   s7    	Wc"||D!E*C	 	>?99dDII./,,-HQR1Xa(=>R	R]I.d9+&T-@-@AYe:??$.OOJOOM 	
hs+@ABW5||VV++ Ss   E<c                8    t         t        |           d| _        y)z-No persistent connection is being maintained.TN)r   r
   _connectr   r   r   s    r   r<   zConnection._connecti   s    j$(*r   c                    t         t        |   |||       t        j	                  d|        | j                  |      \  }}}t        j                  d|d|       |||fS )z+Run specified command in a running QubesVM )r6   sudoablezCMD IS: zSTDOUT z STDERR )r   r
   exec_commandr)   r*   r:   vvvvv)r   r5   r6   r?   rcr&   r'   r   s          r   r@   zConnection.exec_commandn   si     	j$,S'H,Uxu%&![[-FFz&<=66!!r   c                x   t         t        |   ||       t        j	                  d| d| | j
                         t        |d      5 }|j                         }ddd       | j                  d| dd      \  }}}|d	k(  r| j                  d| d|      \  }}}|d
k7  rt        d|       y# 1 sw Y   WxY w)zA Place a local file located in 'in_path' inside VM at 'out_path' zPUT  TO r"   rbNzcat > "z"
zqubes.VMRootShell   r   zFailed to put_file to )
r   r
   put_filer)   r.   r   openreadr:   r   )r   in_pathout_pathfobjsource_dataretcodedummyr   s          r   rG   zConnection.put_filez   s    j$((;d7)4z29L9LM'4  	&D))+K	& !%ghZt,DkSf g c>$(KK'(40H+$V!GUEa<*-CH:+NOO 	& 	&s   	B00B9c                p   t         t        |   ||       t        j	                  d| d| | j
                         dd| j
                  d| g}t        |d      5 }t        j                  |d|	      }|j                          |j                  d
k7  rt        d|       	 ddd       y# 1 sw Y   yxY w)zRObtain file specified via 'in_path' from the container and place it at 'out_path' zFETCH rD   r"   r   r   zcat wbF)r$   r&   r   zFailed to fetch file to N)r   r
   
fetch_filer)   r.   r   rH   r/   r0   r3   r4   r   )r   rJ   rK   cmd_args_listrL   r9   r   s         r   rR   zConnection.fetch_file   s    j$*7H=fWIT(44;N;NO #K1D1DWIFVW(D! 	VT  eDIAMMO||q .1I(/TUU !	V 	V 	Vs   AB,,B5c                8    t         t        |           d| _        y)z Closing the connection FN)r   r
   closer   r=   s    r   rU   zConnection.close   s    j$%'r   )NNzqubes.VMShell)NF)__name__
__module____qualname____doc__	transporthas_pipeliningr   r:   r<   r   r@   rG   rR   rU   __classcell__)r   s   @r   r
   r
   5   sL    m *IN7!,F
 	" 	"P"V   r   r
   )
__future__r   DOCUMENTATIONr/   +ansible.module_utils.common.text.convertersr   ansible.plugins.connectionr   r   ansible.errorsr   ansible.utils.displayr   r)   r
    r   r   <module>rd      s7    #8  @ E 3 )
)f  f r   