Skip to main content

A Python client for docker events API.

Project description

objective

  • have a CLI to start a process which runs abitrary code on docker events

  • be able to provide a way to change behavior of and provide data to event subscribers

  • load subscribers by module or file

  • events may be extended

install

# pip install docker-events

usage

 # docker-events --help
 Usage: docker-events [OPTIONS]

 Options:
   -s, --sock TEXT        the docker socket
   -c, --config FILENAME  a config yaml
   -m, --module TEXT      a python module to load
   -f, --file PATH        a python file to load
   -l, --log PATH         logging config
   --debug                enable debug log level
   --help                 Show this message and exit.

 # create a config for skydns
 # cat > skydns-config.yaml << EOF
 ---

 skydns:
   domain: foo

 skydns.containers:
   redis:
     domain: myredis.{domain}
 EOF

# run skydns subscriber on start events
# docker-events -c skydns-config.yaml -m docker_events.tools.skydns

API

There are some predefined events: start, stop, create, die, destroy, pull

You may write your own events like this:

@event
def pull(client, event_data):
    return event_data.get('status') == 'pull'

Events may be extended by other events:

@docker_events.pull.extend
def pull_something(client, event_data):
    return event_data['id'].startswith("foobar")

For example if you want to use skydns, you may want to put the following code into a file or module and load it via -m or -f option:

"""
setup skydns records for containers
"""

import docker_events
import etcd
import simplejson as json


etcd_client = etcd.Client()


@docker_events.start.subscribe
def set_skydns_record(client, docker_event, config):
    # get ip of container
    container = client.inspect_container(docker_event['id'])

    container_name = container['Name'].strip("/")
    container_ip = container['NetworkSettings']['IPAddress']

    skydns_config = config.get('skydns', {})
    skydns_containers = config.get('skydns.containers', {
        'domain': 'docker.local'
    })

    # find domain name for this container
    if skydns_config and container_name in skydns_containers:
        # use template
        domain = skydns_containers[container_name].get('domain').format(**skydns_config)

    else:
        # join container_name with domain
        domain = '.'.join((container_name, skydns_config.get('domain')))

    domain_path = '/'.join(reversed(domain.split('.')))


    etcd_client.write('/skydns/local/skydns/{}'.format(domain_path), json.dumps({
        'host': container_ip
    }))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docker-events-0.0.9.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

docker_events-0.0.9-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file docker-events-0.0.9.tar.gz.

File metadata

File hashes

Hashes for docker-events-0.0.9.tar.gz
Algorithm Hash digest
SHA256 f1a3460774cf01544810d675f521a99d051d56034be3db506635c8b716246aba
MD5 0681e56a0fba7d1d5bc44030137e9b72
BLAKE2b-256 0832b17b3c1a9a4cba74cabb2ba5657a36c68beb0c6cd7e70815fef8ef757f92

See more details on using hashes here.

Provenance

File details

Details for the file docker_events-0.0.9-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for docker_events-0.0.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ee44cf8257b1147507d7d6eaf9e47b391d24db18ada88139636ebc29b524876
MD5 584b15b33f1716d59bc1459777a4ae40
BLAKE2b-256 96352fd19555a8c0bb0e640af03e3847a5228f54e7ba57d1abdb1f87c72676c2

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page