Adds fire_master to the Salt Event library, for use in 3rd-party code.

This commit is contained in:
Aurynn Shaw 2015-01-12 20:04:04 +13:00
parent 8d45f147bf
commit b7ae48642a
3 changed files with 50 additions and 2 deletions

View File

@ -151,11 +151,14 @@ dictionary should be sent instead.
# Import the proper library
import salt.utils.event
# bring in the system configuration
import salt.config
cfg = salt.config.minion_config("/etc/salt/minion")
# Fire deploy action
sock_dir = '/var/run/salt/minion'
payload = {'sample-msg': 'this is a test',
'example': 'this is the same test'}
event = salt.utils.event.SaltEvent('master', sock_dir)
event = salt.utils.event.SaltEvent('master', sock_dir, opts=cfg)
event.fire_event(payload, 'tag')
It should be noted that this code can be used in 3rd party applications as well.
@ -176,4 +179,20 @@ programmatically, without having to make other calls to Salt.
A 3rd party process can listen to the event bus on the master and another 3rd party
process can fire events to the process on the master, which Salt will happily
pass along.
pass along.
To fire an event to be sent to the master, from the minion, from code:
.. code-block:: python
import salt.utils.event
import salt.config
cfg = salt.config.minion_config("/etc/salt/minion")
sock_dir = '/var/run/salt/minion'
tag = "tag"
payload = {'sample-msg": "this is a test'}
# The message wrapper
# Create an event interface
event = salt.utils.event.SaltEvent("minion", sock_dir, opts=cfg)
# Fire the event across
event.fire_master(payload, tag)

View File

@ -495,6 +495,22 @@ class SaltEvent(object):
raise
return True
def fire_master(self, data, tag, timeout=1000):
''''
Send a single event to the master, with the payload "data" and the
event identifier "tag".
Default timeout is 1000ms
'''
msg = {
'tag': tag,
'data': data,
'events': None,
'pretag': None
}
return self.fire_event(msg, "fire_master", timeout)
def destroy(self, linger=5000):
if self.cpub is True and self.sub.closed is False:
# Wait at most 2.5 secs to send any remaining messages in the

View File

@ -294,6 +294,19 @@ class TestSaltEvent(TestCase):
evt = me.get_event(tag='testevents')
self.assertGotEvent(evt, {'data': '{0}'.format(i)}, 'Event {0}'.format(i))
# Test the fire_master function. As it wraps the underlying fire_event,
# we don't need to perform extensive testing.
def test_send_master_event(self):
""""Tests that sending an event through fire_master generates expected event"""
with eventpublisher_process():
me = event.SaltEvent("minion", SOCK_DIR)
data = {"data": "foo1"}
me.fire_master(data, "test_master")
evt = me.get_event(tag="fire_master")
self.assertGotEvent(evt, {"data": data, "tag": "test_master", "events": None, "pretag": None})
if __name__ == '__main__':
from integration import run_tests