add fire_event option to salt state compiler

Adding  `- fire_event: True` to a state will cause an event to be
sent with the event `name` in the tag and the state return as the
event data.

Adding  `- fire_event: my/custom/event/tag` to a state will cause an
event to be sent with 'my/custom/event/tag` in the tag and the state
return as the event data.
This commit is contained in:
David Boucha 2015-05-05 10:44:43 -06:00
parent c5b46d05c3
commit 4a7b686557

View File

@ -1775,16 +1775,36 @@ class State(object):
return status, reqs return status, reqs
def event(self, chunk_ret, length): def event(self, chunk_ret, length, fire_event_flag=False):
''' '''
Fire an event on the master bus Fire an event on the master bus
If `fire_event_flag` is set to True an event will be sent with the
chunk name in the tag and the chunk result in the event data.
If `fire_event_flag` is set to a string such as `mystate/is/finished`,
an event will be sent with the string added to the tag and the chunk
result in the event data.
If the `state_events` is set to True in the config, then after the
chunk is evaluated an event will be set up to the master with the
results.
''' '''
if not self.opts.get('local') and self.opts.get('state_events', True) and self.opts.get('master_uri'): if not self.opts.get('local') and (self.opts.get('state_events', True) or fire_event_flag) and self.opts.get('master_uri'):
ret = {'ret': chunk_ret, ret = {'ret': chunk_ret}
'len': length} if fire_event_flag is True:
tag = salt.utils.event.tagify( tag = salt.utils.event.tagify(
[self.jid, 'prog', self.opts['id'], str(chunk_ret['__run_num__'])], 'job' [self.jid, self.opts['id'], str(chunk_ret['name'])], 'state_result'
) )
elif isinstance(fire_event_flag, six.string_types):
tag = salt.utils.event.tagify(
[self.jid, self.opts['id'], str(fire_event_flag)], 'state_result'
)
else:
tag = salt.utils.event.tagify(
[self.jid, 'prog', self.opts['id'], str(chunk_ret['__run_num__'])], 'job'
)
ret['len'] = length
preload = {'jid': self.jid} preload = {'jid': self.jid}
self.functions['event.fire_master'](ret, tag, preload=preload) self.functions['event.fire_master'](ret, tag, preload=preload)
@ -1856,7 +1876,7 @@ class State(object):
'__run_num__': self.__run_num, '__run_num__': self.__run_num,
'__sls__': low['__sls__']} '__sls__': low['__sls__']}
self.__run_num += 1 self.__run_num += 1
self.event(running[tag], len(chunks)) self.event(running[tag], len(chunks), fire_event_flag=low.get('fire_event'))
return running return running
for chunk in reqs: for chunk in reqs:
# Check to see if the chunk has been run, only run it if # Check to see if the chunk has been run, only run it if
@ -1881,7 +1901,7 @@ class State(object):
'__run_num__': self.__run_num, '__run_num__': self.__run_num,
'__sls__': low['__sls__']} '__sls__': low['__sls__']}
self.__run_num += 1 self.__run_num += 1
self.event(running[tag], len(chunks)) self.event(running[tag], len(chunks), fire_event_flag=low.get('fire_event'))
return running return running
running = self.call_chunk(chunk, running, chunks) running = self.call_chunk(chunk, running, chunks)
if self.check_failhard(chunk, running): if self.check_failhard(chunk, running):
@ -1980,7 +2000,7 @@ class State(object):
else: else:
running[tag] = self.call(low, chunks, running) running[tag] = self.call(low, chunks, running)
if tag in running: if tag in running:
self.event(running[tag], len(chunks)) self.event(running[tag], len(chunks), fire_event_flag=low.get('fire_event'))
return running return running
def call_listen(self, chunks, running): def call_listen(self, chunks, running):