Scan for Docker events and fire events

    Example Config

    .. code-block:: yaml

        engines:
          - docker_events:
              docker_url: unix://var/run/docker.sock
              filters:
                event:
                - start
                - stop
                - die
                - oom

    The config above sets up engines to listen
    for events from the Docker daemon and publish
    them to the Salt event bus.

    For filter reference, see https://docs.docker.com/engine/reference/commandline/events/

def start(docker_url='unix://var/run/docker.sock',
          timeout=CLIENT_TIMEOUT,
          tag='salt/engines/docker_events',
          filters=None):
    '''
    Scan for Docker events and fire events

    Example Config

    .. code-block:: yaml

        engines:
          - docker_events:
              docker_url: unix://var/run/docker.sock
              filters:
                event:
                - start
                - stop
                - die
                - oom

    The config above sets up engines to listen
    for events from the Docker daemon and publish
    them to the Salt event bus.

    For filter reference, see https://docs.docker.com/engine/reference/commandline/events/
    '''

    if __opts__.get('__role') == 'master':
        fire_master = salt.utils.event.get_master_event(
            __opts__,
            __opts__['sock_dir']).fire_event
    else:
        fire_master = None

    def fire(tag, msg):
        '''
        How to fire the event
        '''
        if fire_master:
            fire_master(msg, tag)
        else:
            __salt__['event.send'](tag, msg)

    try:
        # docker-py 2.0 renamed this client attribute
        client = docker.APIClient(base_url=docker_url, timeout=timeout)
    except AttributeError:
        client = docker.Client(base_url=docker_url, timeout=timeout)

    try:
        events = client.events(filters=filters)
        for event in events:
            data = salt.utils.json.loads(event.decode(__salt_system_encoding__, errors='replace'))
            # https://github.com/docker/cli/blob/master/cli/command/system/events.go#L109
            # https://github.com/docker/engine-api/blob/master/types/events/events.go
            # Each output includes the event type, actor id, name and action.
            # status field can be ommited
            if data['Action']:
                fire('{0}/{1}'.format(tag, data['Action']), data)
            else:
                fire('{0}/{1}'.format(tag, data['status']), data)
    except Exception:
        traceback.print_exc()