Merge pull request #27015 from whiteinge/state_event-minion-side

Add state.event execution module; refactor runner to reuse it
This commit is contained in:
Mike Place 2015-09-14 12:07:06 -06:00
commit 2e402a3577
2 changed files with 83 additions and 34 deletions

View File

@ -14,10 +14,12 @@ states themselves.
# Import python libs
from __future__ import absolute_import
import copy
import fnmatch
import json
import logging
import os
import shutil
import sys
import tarfile
import tempfile
import time
@ -1361,3 +1363,67 @@ def _disabled(funs):
ret.append(err)
continue
return ret
def event(tagmatch='*',
count=-1,
quiet=False,
sock_dir=None,
pretty=False,
node='minion'):
r'''
Watch Salt's event bus and block until the given tag is matched
.. versionadded:: Boron
This is useful for utilizing Salt's event bus from shell scripts or for
taking simple actions directly from the CLI.
Enable debug logging to see ignored events.
:param tagmatch: the event is written to stdout for each tag that matches
this pattern; uses the same matching semantics as Salt's Reactor.
:param count: this number is decremented for each event that matches the
``tagmatch`` parameter; pass ``-1`` to listen forever.
:param quiet: do not print to stdout; just block
:param sock_dir: path to the Salt master's event socket file.
:param pretty: Output the JSON all on a single line if ``False`` (useful
for shell tools); pretty-print the JSON output if ``True``.
:param node: Watch the minion-side or master-side event bus.
CLI Example:
.. code-block:: bash
salt-call --local state.event pretty=True
'''
sevent = salt.utils.event.get_event(
node,
sock_dir or __opts__['sock_dir'],
__opts__['transport'],
opts=__opts__,
listen=True)
while True:
ret = sevent.get_event(full=True)
if ret is None:
continue
if fnmatch.fnmatch(ret['tag'], tagmatch):
if not quiet:
print('{0}\t{1}'.format(
ret['tag'],
json.dumps(
ret['data'],
sort_keys=pretty,
indent=None if not pretty else 4)))
sys.stdout.flush()
count -= 1
log.debug('Remaining event matches: %s', count)
if count == 0:
break
else:
log.debug('Skipping event tag: %s', ret['tag'])
continue

View File

@ -4,13 +4,10 @@ Execute orchestration functions
'''
# Import pytohn libs
from __future__ import absolute_import, print_function
import fnmatch
import json
import logging
import sys
# Import salt libs
import salt.syspaths
import salt.loader
import salt.utils.event
from salt.exceptions import SaltInvocationError
@ -130,7 +127,12 @@ def orchestrate_high(data, test=None, queue=False, pillar=None, **kwargs):
return ret
def event(tagmatch='*', count=-1, quiet=False, sock_dir=None, pretty=False):
def event(tagmatch='*',
count=-1,
quiet=False,
sock_dir=None,
pretty=False,
node='master'):
r'''
Watch Salt's event bus and block until the given tag is matched
@ -149,6 +151,8 @@ def event(tagmatch='*', count=-1, quiet=False, sock_dir=None, pretty=False):
:param sock_dir: path to the Salt master's event socket file.
:param pretty: Output the JSON all on a single line if ``False`` (useful
for shell tools); pretty-print the JSON output if ``True``.
:param node: Watch the minion-side or master-side event bus.
.. versionadded:: Boron
CLI Examples:
@ -175,33 +179,12 @@ def event(tagmatch='*', count=-1, quiet=False, sock_dir=None, pretty=False):
See :glob:`tests/eventlisten.sh` for an example of usage within a shell
script.
'''
sevent = salt.utils.event.get_event(
'master',
sock_dir or __opts__['sock_dir'],
__opts__['transport'],
opts=__opts__,
listen=True)
statemod = salt.loader.raw_mod(__opts__, 'state', None)
while True:
ret = sevent.get_event(full=True)
if ret is None:
continue
if fnmatch.fnmatch(ret['tag'], tagmatch):
if not quiet:
print('{0}\t{1}'.format(
ret['tag'],
json.dumps(
ret['data'],
sort_keys=pretty,
indent=None if not pretty else 4)))
sys.stdout.flush()
count -= 1
LOGGER.debug('Remaining event matches: {0}'.format(count))
if count == 0:
break
else:
LOGGER.debug('Skipping event tag: {0}'.format(ret['tag']))
continue
return statemod['state.event'](
tagmatch=tagmatch,
count=count,
quiet=quiet,
sock_dir=sock_dir,
pretty=pretty,
node=node)