3

Is it possible to call salt-call state.highstate (command) inside a python script and know when it’s done? (Masterless)

relopezz
  • 59
  • 1
  • 5

2 Answers2

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
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