o id@s`ddlZddlZddlZddlZddlmZddlmZmZddl m Z m Z GdddeZ dS)N)RemoteTransport)ConnectionException#SaltStackMasterUnsupportedException) is_executablesos_get_command_outputcsteZdZdZdZddZ dfdd Zd d Zed d Z dddZ dddZ ddZ eddZ ddZZS)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. saltstackcCstt|dS)Nr)listjsonloadsvalues)self json_outputrD/usr/lib/python3/dist-packages/sos/collector/transports/saltstack.py_convert_output_json!sz$SaltStackMaster._convert_output_jsonFNcsZtt||||||}tt||d|d<Wd|S1s&wY|S)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)r cmdtimeout need_rootenvget_ptyret __class__rrr$s    zSaltStackMaster.run_commandcCsZd|d|}t|}|ddkr+d|jd}tj||d}t||dSd S) 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)r nodefnamedestrrescachedir cachedir_filerrr_salt_retrieve_file0s  z#SaltStackMaster._salt_retrieve_filecCs|jddd}|ddkS)z>Check if the remote host is responding using SaltStack Master.echo Connected rr"r)r)r uprrr connected=s zSaltStackMaster.connectedcCs(d}t|}|ddkr|ddkSt)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)r passwordrr.rrr_check_for_saltstackCs   z$SaltStackMaster._check_for_saltstackcCsftds |ddSz|Wn ty|dw|dd}|j|dd}|d d kS) 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. r2rr4r"r)r log_errorr8rlog_infor)r r7rresultrrr_connectXs      zSaltStackMaster._connectcCsdS)NTr)r rrr _disconnectoszSaltStackMaster._disconnectcCsd}d|d|jdS)z7The remote execution command to use for this transport.z--out json --static --no-colorr! z cmd.shell )r$)r salt_argsrrr remote_execrszSaltStackMaster.remote_execcCs|jr ||j||SdS)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)r6r1r$)r r,r-rrr_retrieve_filexs  zSaltStackMaster._retrieve_file)rFNF)N)__name__ __module__ __qualname____doc__namerrr1propertyr6r8r=r>rArB __classcell__rrrrrs     r) rr r%r)sos.collector.transportsrsos.collector.exceptionsrr sos.utilitiesrrrrrrrs