
    Vh`                    ~    d dl mZ dZd dlZd dlZd dlmZ d dlmZ dZ		 d dl
mZ dZ	 G d d	e      Zy# e$ r Y w xY w)
    )annotationsz
author: Michael Scherer (@mscherer) <misc@zarb.org>
name: saltstack
short_description: Allow ansible to piggyback on salt minions
description:
  - This allows you to use existing Saltstack infrastructure to connect to targets.
N)errors)ConnectionBaseFTc                  f     e Zd ZdZdZdZ fdZd Zd fd	Ze	d        Z
 fdZ fd	Zd
 Z xZS )
Connectionz Salt-based connections Fzcommunity.general.saltstackc                l    t        t        | 
  ||g|i | | j                  j                  | _        y )N)superr   __init___play_contextremote_addrhost)selfplay_context	new_stdinargskwargs	__class__s        r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/connection/saltstack.pyr
   zConnection.__init__*   s1    j$(yR4R6R&&22	    c                |    t         st        j                  d      t        j                         | _        d| _        | S )Nzsaltstack is not installedT)HAVE_SALTSTACKr   AnsibleErrorscLocalClientclient
_connectedr   s    r   _connectzConnection._connect.   s1    %%&BCCnn&r   c                   t         t        |   |||       |rt        j                  d      | j
                  j                  d| | j                         | j                  j                  | j                  ddd| g      }| j                  |vr#t        j                  d| j                   d	      || j                     }|d
   |d   |d   fS )z$ run a command on the remote minion )in_datasudoablezHInternal Error: this module does not support optimized module pipeliningzEXEC r   zcmd.exec_code_allbashztrue;zMinion zG didn't answer, check if salt-minion is running and the name is correctretcodestdoutstderr)
r	   r   exec_commandr   r   _displayvvvr   r   cmd)r   r*   r    r!   respr   s         r   r'   zConnection.exec_command6   s    j$,S'H,U%%&pqqE#-dii8kkoodii)<vse}>UV99C%%		{  ;B  'C  D  D		N|Qx[!H+55r   c                H   | j                  t        j                  j                        s8t        j                  j	                  t        j                  j                  |       } t        j                  j                  |       }t        j                  j	                  ||dd        S )N   )
startswithospathsepjoinnormpath)r1   prefixr4   s      r   _normalize_pathzConnection._normalize_pathF   sa    rww{{+77<<T2D77##D)ww||FHQRL11r   c                   t         t        |   ||       | j                  |d      }| j                  j                  d| d| | j                         t        |d      5 }|j                         }ddd       | j                  j                  | j                  dt        j                        |g       y# 1 sw Y   FxY w)z& transfer a file from local to remote /zPUT  TO r"   rbNzhashutil.base64_decodefile)r	   r   put_filer6   r(   r)   r   openreadr   r*   base64	b64encode)r   in_pathout_pathin_fhcontentr   s        r   r;   zConnection.put_fileM   s     	j$((;''#6D	hZ8tyyI'4  	#EjjlG	#		#?&BRBRSZB[]eAfg	# 	#s   !B77C c                L   t         t        |   ||       | j                  |d      }| j                  j                  d| d| | j                         | j                  j                  | j                  d|g      | j                     }t        |d      j                  |       y)z# fetch a file from remote to local r8   zFETCH r9   r"   zcp.get_file_strwbN)r	   r   
fetch_filer6   r(   r)   r   r   r*   r<   write)r   r@   rA   rC   r   s       r   rF   zConnection.fetch_fileY   s     	j$*7H=&&w4F7)4z:K++//$))->	J499UXt""7+r   c                     y)z. terminate the connection; nothing to do here N r   s    r   closezConnection.closec   s    r   )NF)__name__
__module____qualname____doc__has_pipelining	transportr
   r   r'   staticmethodr6   r;   rF   rJ   __classcell__)r   s   @r   r   r   "   sE    "N .I36  2 2	h,r   r   )
__future__r   DOCUMENTATIONr0   r>   ansibler   ansible.plugins.connectionr   r   salt.clientr   r   ImportErrorr   rI   r   r   <module>rY      sS    # 
   5	N
C C	  		s   4 <<