Is it possible to call salt-call state.highstate
(command) inside a python script and know when it’s done? (Masterless)
Asked
Active
Viewed 4,326 times
3

relopezz
- 59
- 1
- 5
-
does the command return immediatly if you call it from the shell? or does it return when its done? – Joran Beasley Apr 30 '15 at 21:29
-
http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – bosnjak Apr 30 '15 at 21:32
-
yes, the salt-call command is used to run module functions locally on a minion and display the return of data. – relopezz Apr 30 '15 at 21:36
2 Answers
4
You can use the Salt Client API to achieve this.
When running masterless, you must use the Caller
class, which provides the same interface as the salt-call
command-line tool on the Minion.
import salt
caller = salt.client.Caller()
output = caller.function('state.highstate')
The output here is the full highstate result - there is no current way to run this asynchronously.
In order to to run highstate on a minion, use the LocalClient
interface on the salt-master:
import salt
client = salt.client.LocalClient()
jid = client.cmd_async('minion-name', 'state.highstate')
The jid
variable here is the Salt "job ID" for the highstate job. You can then query Salt for running jobs with:
client.cmd('minion-name', 'saltutil.running')
Which when run in a loop will enable you to check if the highstate has completed.

mafrosis
- 2,720
- 1
- 25
- 34
-
salt.client.LocalClient() seems it doesn't work with standalone minion (masterless) – relopezz May 01 '15 at 15:00
0
it looks like you might need to write a returner, that stores the result in a file or something
and call the command with
subprocess.check_output("salt-call state.highstate --returner=myReturner",shell=True)
#if the command blocks and does not return immediatly at this point it is finished
#if the command does not block you will have to check for the file that your returner creates until it exists
while not os.path.exists("my_returner_output.txt"):
time.sleep(1)
print "OK COMMAND COMPLETE"

Joran Beasley
- 110,522
- 12
- 160
- 179