U
    -_g1                     @   s`   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 d dl	m
Z
mZ G dd deZdS )    N)RemoteTransport)ConnectionException#SaltStackMasterUnsupportedException)is_executablesos_get_command_outputc                       s   e Zd ZdZdZdd Zd fdd		Zd
d Zdd Ze	dd Z
dddZdddZdd Ze	dd Zdd Zdd Z  ZS )SaltStackMasterz
    A transport for collect that leverages SaltStack's Master Pub/Sub
    functionality to send commands to minions.

    This transport will by default assume the use cmd.shell module to
    execute commands on the minions.
    Z	saltstackc                 C   s   t t| d S )Nr   )listjsonloadsvalues)selfZjson_output r   D/usr/lib/python3/dist-packages/sos/collector/transports/saltstack.py_convert_output_json!   s    z$SaltStackMaster._convert_output_json   FNc              	      s@   t  |||||}tt | |d |d< W 5 Q R X |S )z
        Run a command on the remote host using SaltStack Master.
        If the output is json, convert it to a string.
        output)superrun_command
contextlibsuppress	Exceptionr   )r   cmdtimeoutZ	need_rootenvZ	use_shellZret	__class__r   r   r   $   s        zSaltStackMaster.run_commandc                 C   s.   d| d| d| }t |dd}|d dkS )zO
        Execute cp.get_file on the remote host using SaltStack Master
        zsalt-cp     r   statusr   )r   )r   nodefnamedestr   resr   r   r   _salt_copy_file0   s    zSaltStackMaster._salt_copy_filec                 C   sZ   d| d| }t |}|d dkrVd| j d}tj||d}t|| dS d	S )
zK
        Execute cp.push on the remote host using SaltStack Master
        salt z	 cp.push r   r   z/var/cache/salt/master/minions/z/files/TF)r   addressospathjoinlstripshutilZmove)r   r    r!   r"   r   r#   ZcachedirZcachedir_filer   r   r   _salt_retrieve_file8   s    z#SaltStackMaster._salt_retrieve_filec                 C   s   | j ddd}|d dkS )z>Check if the remote host is responding using SaltStack Master.echo Connected
   r   r   r   )r   )r   Zupr   r   r   	connectedE   s    zSaltStackMaster.connectedc                 C   s,   d}t |}|d dkr$|d dkS tdS )a  Checks to see if the local system supported SaltStack Master.

        This check relies on feedback from the salt binary. The command being
        run should always generate stderr output, but depending on what that
        output reads we can determine if SaltStack Master is supported or not.

        For our purposes, a host that does not support SaltStack Master is not
        able to run sos collect.

        Returns
            True if SaltStack Master is supported, else raise Exception
        zsalt-run manage.statusr   r   N)r   r   )r   passwordr   r#   r   r   r   _check_for_saltstackL   s
    z$SaltStackMaster._check_for_saltstackc                 C   s   t ds| d dS z|   W n  tk
rB   | d  Y nX | d d}| j|dd}|d	 d
krvt| j|d	 dkS )aD  Connect to the remote host using SaltStack Master.

        This method will attempt to connect to the remote host using SaltStack
        Master. If the connection fails, an exception will be raised.

        If the connection is successful, the connection will be stored in the
        self._connection attribute.
        Zsaltz salt command is not executable. Fz$Transport is not locally supported. z4Transport is locally supported and service running. r.   r   r   r      r   )r   Z	log_errorr2   r   Zlog_infor   r'   )r   r1   r   resultr   r   r   _connect`   s    	



zSaltStackMaster._connectc                 C   s   dS )NTr   )r   r   r   r   _disconnecty   s    zSaltStackMaster._disconnectc                 C   s   d}d| d| j  dS )z7The remote execution command to use for this transport.z--out json --static --no-colorr%   r   z cmd.shell )r'   )r   Z	salt_argsr   r   r   remote_exec|   s    zSaltStackMaster.remote_execc                 C   s   | j r| | j||S dS )a  Copy a file to the remote host using SaltStack Master

        Parameters
            fname   The path to the file on the master
            dest    The path to the destination directory on the remote host

        Returns
            True if the file was copied, else False
        F)r0   r$   r'   r   r!   r"   r   r   r   _copy_file_to_remote   s    z$SaltStackMaster._copy_file_to_remotec                 C   s   | j r| | j||S dS )a  Retrieve a file from the remote host using saltstack

        Parameters
            fname   The path to the file on the remote host
            dest    The path to the destination directory on the master

        Returns
            True if the file was retrieved, else False
        F)r0   r-   r'   r8   r   r   r   _retrieve_file   s    zSaltStackMaster._retrieve_file)r   FNF)N)N)__name__
__module____qualname____doc__namer   r   r$   r-   propertyr0   r2   r5   r6   r7   r9   r:   __classcell__r   r   r   r   r      s"     



r   )r   r	   r(   r,   Zsos.collector.transportsr   Zsos.collector.exceptionsr   r   Zsos.utilitiesr   r   r   r   r   r   r   <module>   s   