
    ~ g8                     f    S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
Jr   " S S\5      rg)    N)RemoteTransport)ConnectionException#SaltStackMasterUnsupportedException)is_executablesos_get_command_outputc                      ^  \ rS rSrSrSrS r  SU 4S jjrS rS r	\
S 5       rSS	 jrSS
 jrS r\
S 5       rS rS rSrU =r$ )SaltStackMaster   z
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.
	saltstackc                 b    [        [        R                  " U5      R                  5       5      S   $ )Nr   )listjsonloadsvalues)selfjson_outputs     D/usr/lib/python3/dist-packages/sos/collector/transports/saltstack.py_convert_output_json$SaltStackMaster._convert_output_json!   s$    DJJ{+2245a88    c                    > [         TU ]  XX4U5      n[        R                  " [        5         U R                  US   5      US'   SSS5        U$ ! , (       d  f       U$ = f)zi
Run a command on the remote host using SaltStack Master.
If the output is json, convert it to a string.
outputN)superrun_command
contextlibsuppress	Exceptionr   )r   cmdtimeout	need_rootenv	use_shellret	__class__s          r   r   SaltStackMaster.run_command$   sY     g!))5  + 55c(mDCM ,
 ,+
s   A
Ac                 <    SU SU SU 3n[        USS9nUS   S:H  $ )z?
Execute cp.get_file on the remote host using SaltStack Master
zsalt-cp     r   statusr   )r   )r   nodefnamedestr   ress         r   _salt_copy_fileSaltStackMaster._salt_copy_file0   s7     awav.$S"58}!!r   c                     SU SU 3n[        U5      nUS   S:X  aU  SU R                   S3n[        R                  R	                  XbR                  S5      5      n[        R                  " Xs5        gg	)
z;
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shutilmove)r   r+   r,   r-   r   r.   cachedircachedir_files           r   _salt_retrieve_file#SaltStackMaster._salt_retrieve_file8   sk     dV9UG,$S)x=A8fMHGGLL<<3DEMKK,r   c                 2    U R                  SSS9nUS   S:H  $ )z>Check if the remote host is responding using SaltStack Master.echo Connected
   r)   r*   r   )r   )r   ups     r   	connectedSaltStackMaster.connectedE   s)     .;(|q  r   c                 J    Sn[        U5      nUS   S:X  a  US   S:H  $ [        e)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   )r   r   )r   passwordr   r.   s       r   _check_for_saltstack$SaltStackMaster._check_for_saltstackL   s4     '$S)x=Ax=A%%11r   c                 :   [        S5      (       d  U R                  S5        g U R                  5         U R	                  S5        SnU R                  USS9nUS	   S
:X  a  [        U R                  5      eUS	   S:H  $ ! [         a    U R                  S5        e f = f)a  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.
saltz salt command is not executable. Fz$Transport is not locally supported. z4Transport is locally supported and service running. r@      r)   r*      r   )r   	log_errorrG   r   log_infor   r4   )r   rF   r   results       r   _connectSaltStackMaster._connect`   s     V$$NN=>	%%' 	LM!!#s!3(q %dll33h1$$ # 	NNAB	s   A= =Bc                     g)NT )r   s    r   _disconnectSaltStackMaster._disconnecty   s    r   c                 ,    SnSU SU R                    S3$ )z7The remote execution command to use for this transport.z--out json --static --no-colorr2   r'   z cmd.exec_code sh )r4   )r   	salt_argss     r   remote_execSaltStackMaster.remote_exec|   s#     5	yk4<<.0BCCr   c                 `    U R                   (       a  U R                  U R                  X5      $ S$ )zCopy 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)rC   r/   r4   r   r,   r-   s      r   _copy_file_to_remote$SaltStackMaster._copy_file_to_remote   s0     ~~   u;	
 	
r   c                 `    U R                   (       a  U R                  U R                  X5      $ S$ )zRetrieve 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)rC   r=   r4   r[   s      r   _retrieve_fileSaltStackMaster._retrieve_file   s0     ~~ $$T\\5?	
 	
r   rS   )rK   FNF)N)__name__
__module____qualname____firstlineno____doc__namer   r   r/   r=   propertyrC   rG   rP   rT   rX   r\   r_   __static_attributes____classcell__)r$   s   @r   r	   r	      su     D9 BF#
" ! !2(%2 D D

 
 
r   r	   )r   r   r5   r9   sos.collector.transportsr   sos.collector.exceptionsr   r   sos.utilitiesr   r   r	   rS   r   r   <module>rm      s/      	  4K3J
o J
r   