Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Saltstack "eventbus" returner
# -*- coding: utf-8 -*-
'''
Return data to a the Salt Eventbus.
Huh, that's default behavior isn't it? Technically yes.
But.. Not when you:
- use 'salt-call' on a minion
- use the Salt scheduler to execute periodically
Which means, the master doesn't get executed tasks in its job cache.
Very annoying if you want to create nice graphs of state executions.
Sure, you can use a returner to dump stuff in the DB but the downside of that
is that the minion has to (be able to) connect to that particular service.
Also, there is no way of verifying whether the return is legit.
The bug has been reported upstream (saltstack/salt/issues/12935) but still
needs to be resolved.
This is where this returner comes in. It will emulate the normal salt return
by raising the event and all data attached to it.
Please note: you still have to post-process the output as everything is nested
in the 'data' section unlike normal highstate returns.
To use this, append '--return eventbus' to the salt command. ex:
salt-call state.highstate --return eventbus
'''
# Import python libs
import logging
# Import third party libs
try:
import salt.client
HAS_SALTCLIENT = True
except ImportError:
HAS_SALTCLIENT = False
log = logging.getLogger(__name__)
# Define the module's virtual name
__virtualname__ = 'eventbus'
def __virtual__():
if not HAS_SALTCLIENT:
return False
return __virtualname__
def returner(ret):
'''
Return data to the eventbus
'''
caller = salt.client.Caller('/etc/salt/minion')
tag = 'salt/job/%s/ret/%s' % (ret['jid'], ret['id'], )
caller.sminion.functions['event.fire_master'](ret, tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment