idfddlZddlZddlZddlZddlmZddlmZmZddl m Z m Z GddeZ y)N)RemoteTransport)ConnectionException#SaltStackMasterUnsupportedException) is_executablesos_get_command_outputcreZdZdZdZdZ d fd ZdZedZ d dZ d dZ d Z ed Z d ZxZS)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. saltstackc`ttj|jdS)Nr)listjsonloadsvalues)self json_outputs D/usr/lib/python3/dist-packages/sos/collector/transports/saltstack.py_convert_output_jsonz$SaltStackMaster._convert_output_json!s$DJJ{+2245a88ctt| |||||}tjt 5|j |d|d<ddd|S#1swY|SxYw)z Run a command on the remote host using SaltStack Master. If the output is json, convert it to a string. outputN)superr run_command contextlibsuppress Exceptionr)rcmdtimeout need_rootenvget_ptyret __class__s rrzSaltStackMaster.run_command$sd OT6 )S'3   + E 55c(mDCM E  E s AAcd|d|}t|}|ddk(rVd|jd}tjj ||j d}t j||yy ) zK Execute cp.push on the remote host using SaltStack Master salt z cp.push statusrz/var/cache/salt/master/minions/z/files/TF)raddressospathjoinlstripshutilmove)rnodefnamedestrrescachedir cachedir_files r_salt_retrieve_filez#SaltStackMaster._salt_retrieve_file0sodV9UG,$S) x=A 8fMHGGLL5<<3DEM KK t ,rc8|jdd}|ddk(S)z>Check if the remote host is responding using SaltStack Master.echo Connected rr%r)r)rups r connectedzSaltStackMaster.connected=s)  .  ;(|q  rcHd}t|}|ddk(r|ddk(St)aChecks 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-collector. Returns True if SaltStack Master is supported, else raise Exception zsalt-run manage.statusr%r)rr)rpasswordrr1s r_check_for_saltstackz$SaltStackMaster._check_for_saltstackCs4'$S) x=A x=A% %5 5rctds|jdy |j|j dd}|j |d}|d d k(S#t$r|jdwxYw) aDConnect 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. r6r8r%r)r log_errorr=rlog_infor)rr<rresults r_connectzSaltStackMaster._connectXsV$ NN= >   % % ' LM!!#s!3h1$$ #  NNA B  s AA9cy)NT)rs r _disconnectzSaltStackMaster._disconnectosrc,d}d|d|jdS)z7The remote execution command to use for this transport.z--out json --static --no-colorr$ z cmd.shell )r')r salt_argss r remote_execzSaltStackMaster.remote_execrs"5 yk4<<. <rXs/ 4K3p op r