Making a highstate run wait for an event

669 views
Skip to first unread message

Juho Mäkinen

unread,
Dec 21, 2015, 1:42:07 AM12/21/15
to Salt-users
I have problem figuring out how I can make a custom module inside a highstate run wait until a special event is published from a runner running in the  salt-master to the event bus.

Here's what I want to accomplish:
1) A highstate run is triggered.
2) During the highstate run a custom state module (of mine) is executed.
3) The state module sends an event (via events.send) to the event bus
4) This event triggers a reactor in the salt-master
5) Reactor starts a runner.

So far everything works beautifully. What I need to do after is that the runner must be able to send some data back to the minion while its still doing its highstate run.

I guess that I could modify my custom state module (see the 2nd step) to wait (or poll every few seconds) for a specially named event from the event bus which the reactor will be sending, but I just don't know how. The goal is that the highstate run will be blocked until the runner sends this event, because all other states following this cannot be executed unless the runner has done its job.

I'm an experienced Python programmer, so I know that I must be able to do this somehow, but I'm not familiar with Salt internals. Naturally the first choice would be to use the existing event bus, but I haven't found suitable API from Salt minion side which I could easily use from a custom python state module. All ideas and pointers are appreciated.

Thanks,

 - Garo

Steve Hajducko

unread,
Dec 21, 2015, 1:52:17 AM12/21/15
to Salt-users
Garo,

Have you checked out salt.states.saltmod.wait_for_event?  It does what you're talking about ( polls the event bus for a certain event ).


May be what you're looking for.

--
sh

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juho Mäkinen

unread,
Dec 21, 2015, 1:56:03 AM12/21/15
to salt-...@googlegroups.com
Thanks Steve.

Unfortunately documentation says that "This state is intended for use from the Salt Master." If I can run this from a custom python state module in a minion then this would most likely fit my need exactly.

 - Garo

On Mon, Dec 21, 2015 at 8:52 AM, Steve Hajducko <hajd...@gmail.com> wrote:
Have you checked out salt.states.saltmod.wait_for_event?  It does what you're talking about ( polls the event bus for a certain event ). https://docs.saltstack.com/en/latest/ref/states/all/salt.states.saltmod.html

On Sun, Dec 20, 2015 at 10:42 PM Juho Mäkinen <juho.m...@gmail.com> wrote:

Steve Hajducko

unread,
Dec 21, 2015, 2:16:26 AM12/21/15
to salt-...@googlegroups.com
Hey Garo,

Yeah, although the code that module uses may be helpful at figuring out the 'listen for an event' on the minion side.

In any case, you'll want to take at utils/event.py, which contains alot of the code for managing events.

Seth House

unread,
Dec 21, 2015, 11:40:34 AM12/21/15
to salt users list
There is a recent minion-side addition for watching the event bus. It
is a `state.event` execution module (that purposefully mirrors the
master-side runner of the same name).

https://github.com/saltstack/salt/blob/develop/salt/modules/state.py#L1462

It's only in the 'develop' branch but it can be ported to existing
Salt versions as a custom `/srv/salt/_modules/state_backports.py` type
module. I haven't tested it from Jinja but it's a blocking call for
use with shell scripts so I expect it should work with Jinja
out-of-box.

Juho Mäkinen

unread,
Jan 4, 2016, 5:50:57 AM1/4/16
to salt-...@googlegroups.com
Thanks Seth.

I got a prototype working on a minion when I execute "salt minion_id event.fire ..." from master.

What is the correct way to fire an event into a specific minion's event bus from a runner running in master?

 - Garo

You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/oynM8nCq0b0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.

Seth House

unread,
Jan 4, 2016, 12:16:14 PM1/4/16
to salt users list
On Mon, Jan 4, 2016 at 3:50 AM, Juho Mäkinen <juho.m...@gmail.com> wrote:
> What is the correct way to fire an event into a specific minion's event bus
> from a runner running in master?

The correct way is to call `event.fire` like in your prototype. To
call that from a custom Runner module, you need to tap into Salt's
Python API. Instantiate LocalClient and call `event.fire` from there:

https://docs.saltstack.com/en/latest/ref/clients/
Reply all
Reply to author
Forward
0 new messages