def wait_for_event(
        name,
        id_list,
        event_id='id',
        timeout=300,
        node='master'):
    '''
    Watch Salt's event bus and block until a condition is met

    .. versionadded:: 2014.7.0

    name
        An event tag to watch for; supports Reactor-style globbing.
    id_list
        A list of event identifiers to watch for -- usually the minion ID. Each
        time an event tag is matched the event data is inspected for
        ``event_id``, if found it is removed from ``id_list``. When ``id_list``
        is empty this function returns success.
    event_id : id
        The name of a key in the event data. Default is ``id`` for the minion
        ID, another common value is ``name`` for use with orchestrating
        salt-cloud events.
    timeout : 300
        The maximum time in seconds to wait before failing.

    The following example blocks until all the listed minions complete a
    restart and reconnect to the Salt master:

    .. code-block:: yaml

        reboot_all_minions:
          salt.function:
            - name: system.reboot
            - tgt: '*'

        wait_for_reboots:
          salt.wait_for_event:
            - name: salt/minion/*/start
            - id_list:
              - jerry
              - stuart
              - dave
              - phil
              - kevin
              - mike
            - require:
              - salt: reboot_all_minions
    '''
    ret = {'name': name, 'changes': {}, 'comment': '', 'result': False}

    if __opts__.get('test'):
        ret['comment'] = \
            'Orchestration would wait for event \'{0}\''.format(name)
        ret['result'] = None
        return ret

    sevent = salt.utils.event.get_event(
            node,
            __opts__['sock_dir'],
            __opts__['transport'],
            opts=__opts__,
            listen=True)

    del_counter = 0
    starttime = time.time()
    timelimit = starttime + timeout
    while True:
        event = sevent.get_event(full=True)
        is_timedout = time.time() > timelimit

        if event is None and not is_timedout:
            log.trace("wait_for_event: No event data; waiting.")
            continue
        elif event is None and is_timedout:
            ret['comment'] = 'Timeout value reached.'
            return ret

        if fnmatch.fnmatch(event['tag'], name):
            val = event['data'].get(event_id)
            if val is None and 'data' in event['data']:
                val = event['data']['data'].get(event_id)

            if val is not None:
                try:
                    val_idx = id_list.index(val)
                except ValueError:
                    log.trace("wait_for_event: Event identifier '%s' not in "
                              "id_list; skipping.", event_id)
                else:
                    del id_list[val_idx]
                    del_counter += 1
                    minions_seen = ret['changes'].setdefault('minions_seen', [])
                    minions_seen.append(val)

                    log.debug("wait_for_event: Event identifier '%s' removed "
                              "from id_list; %s items remaining.",
                              val, len(id_list))
            else:
                log.trace("wait_for_event: Event identifier '%s' not in event "
                          "'%s'; skipping.", event_id, event['tag'])
        else:
            log.debug("wait_for_event: Skipping unmatched event '%s'",
                      event['tag'])

        if not id_list:
            ret['result'] = True
            ret['comment'] = 'All events seen in {0} seconds.'.format(
                    time.time() - starttime)
            return ret

        if is_timedout:
            ret['comment'] = 'Timeout value reached.'
            return ret
def wait_for_event ( name , id_list , event_id = 'id' , timeout = 300 , node = 'master' ) : ret = { 'name' : name , 'changes' : { } , 'comment' : '' , 'result' : False } if __opts__ . get ( 'test' ) : ret [ 'comment' ] = 'Orchestration would wait for event \'{0}\'' . format ( name ) ret [ 'result' ] = None return ret sevent = salt . utils . event . get_event ( node , __opts__ [ 'sock_dir' ] , __opts__ [ 'transport' ] , opts = __opts__ , listen = True ) del_counter = 0 starttime = time . time ( ) timelimit = starttime + timeout while True : event = sevent . get_event ( full = True ) is_timedout = time . time ( ) > timelimit if event is None and not is_timedout : log . trace ( "wait_for_event: No event data; waiting." ) continue elif event is None and is_timedout : ret [ 'comment' ] = 'Timeout value reached.' return ret if fnmatch . fnmatch ( event [ 'tag' ] , name ) : val = event [ 'data' ] . get ( event_id ) if val is None and 'data' in event [ 'data' ] : val = event [ 'data' ] [ 'data' ] . get ( event_id ) if val is not None : try : val_idx = id_list . index ( val ) except ValueError : log . trace ( "wait_for_event: Event identifier '%s' not in " "id_list; skipping." , event_id ) else : del id_list [ val_idx ] del_counter += 1 minions_seen = ret [ 'changes' ] . setdefault ( 'minions_seen' , [ ] ) minions_seen . append ( val ) log . debug ( "wait_for_event: Event identifier '%s' removed " "from id_list; %s items remaining." , val , len ( id_list ) ) else : log . trace ( "wait_for_event: Event identifier '%s' not in event " "'%s'; skipping." , event_id , event [ 'tag' ] ) else : log . debug ( "wait_for_event: Skipping unmatched event '%s'" , event [ 'tag' ] ) if not id_list : ret [ 'result' ] = True ret [ 'comment' ] = 'All events seen in {0} seconds.' . format ( time . time ( ) - starttime ) return ret if is_timedout : ret [ 'comment' ] = 'Timeout value reached.' return ret
Watch Salt's event bus and block until a condition is met

    .. versionadded:: 2014.7.0

    name
        An event tag to watch for; supports Reactor-style globbing.
    id_list
        A list of event identifiers to watch for -- usually the minion ID. Each
        time an event tag is matched the event data is inspected for
        ``event_id``, if found it is removed from ``id_list``. When ``id_list``
        is empty this function returns success.
    event_id : id
        The name of a key in the event data. Default is ``id`` for the minion
        ID, another common value is ``name`` for use with orchestrating
        salt-cloud events.
    timeout : 300
        The maximum time in seconds to wait before failing.

    The following example blocks until all the listed minions complete a
    restart and reconnect to the Salt master:

    .. code-block:: yaml

        reboot_all_minions:
          salt.function:
            - name: system.reboot
            - tgt: '*'

        wait_for_reboots:
          salt.wait_for_event:
            - name: salt/minion/*/start
            - id_list:
              - jerry
              - stuart
              - dave
              - phil
              - kevin
              - mike
            - require:
              - salt: reboot_all_minions