a >h8@s`ddlZddlZddlZddlZddlmZddlmZmZddl m Z m Z GdddeZ dS)N)RemoteTransport)ConnectionException#SaltStackMasterUnsupportedException) is_executablesos_get_command_outputcseZdZdZdZddZdfdd Zd d Zd d Ze ddZ dddZ dddZ ddZ e ddZddZddZZS)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 saltstackcCstt|dS)Nr)listjsonloadsvalues)selfZ json_outputr F/usr/lib/python3.9/site-packages/sos/collector/transports/saltstack.py_convert_output_json!sz$SaltStackMaster._convert_output_jsonFNcsTt|||||}tt"||d|d<Wdn1sF0Y|S)z Run a command on the remote host using SaltStack Master. If the output is json, convert it to a string. outputN)super run_command contextlibsuppress Exceptionr)r cmdtimeoutZ need_rootenvZ use_shellret __class__r rr$s   0zSaltStackMaster.run_commandcCs.d|d|d|}t|dd}|ddkS)zO Execute cp.get_file on the remote host using SaltStack Master zsalt-cp  rstatusr)r)r nodefnamedestrresr r r_salt_copy_file0s zSaltStackMaster._salt_copy_filecCsZd|d|}t|}|ddkrVd|jd}tj||d}t||dSd S) zK Execute cp.push on the remote host using SaltStack Master salt z cp.push r rz/var/cache/salt/master/minions/z/files/TF)raddressospathjoinlstripshutilmove)r r!r"r#rr$ZcachedirZ cachedir_filer r r_salt_retrieve_file8s  z#SaltStackMaster._salt_retrieve_filecCs|jddd}|ddkS)z>Check if the remote host is responding using SaltStack Master.echo Connected rr r)r)r Zupr r r connectedEszSaltStackMaster.connectedcCs,d}t|}|ddkr$|ddkStdS)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 collect. Returns True if SaltStack Master is supported, else raise Exception zsalt-run manage.statusr rN)rr)r passwordrr$r r r_check_for_saltstackLs   z$SaltStackMaster._check_for_saltstackcCstds|ddSz |Wnty@|dYn0|dd}|j|dd}|d d krtt|j|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. Zsaltz salt command is not executable. Fz$Transport is not locally supported. z4Transport is locally supported and service running. r0rrr r)rZ log_errorr4rZlog_inforr()r r3rresultr r r_connect`s        zSaltStackMaster._connectcCsdS)NTr )r r r r _disconnectyszSaltStackMaster._disconnectcCsd}d|d|jdS)z7The remote execution command to use for this transport.z--out json --static --no-colorr&rz cmd.exec_code sh )r()r Z salt_argsr r r remote_exec|szSaltStackMaster.remote_execcCs|jr||j||SdS)aCopy 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)r2r%r(r r"r#r r r_copy_file_to_remotes z$SaltStackMaster._copy_file_to_remotecCs|jr||j||SdS)aRetrieve 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)r2r/r(r:r r r_retrieve_files zSaltStackMaster._retrieve_file)rFNF)N)N)__name__ __module__ __qualname____doc__namerrr%r/propertyr2r4r7r8r9r;r< __classcell__r r rrrs"      r) rr r)r-Zsos.collector.transportsrZsos.collector.exceptionsrrZ sos.utilitiesrrrr r r r s