aboutsummaryrefslogtreecommitdiff
path: root/hostsdns/event-watcher.py
blob: 34467772b7749c13675ef502d60e4bfc9fd8242a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python

import os, sys, json, re
import docker
import argparse

parser = argparse.ArgumentParser( description="Manage host entries for docker containers")
parser.add_argument('-n', '--native', help='run service on docker host', action='store_true' , dest='NATIVE_MODE')
parser.add_argument('-o','-1', '--once', help='run once and exit (no continuous polling)', action='store_true', dest='ONCE_MODE')
args = parser.parse_args()

if not args.NATIVE_MODE and os.path.exists("/.dockerenv"):
    print "Found /.dockerenv... reading from /tmp/hosts"
    HOSTS_FILE="/tmp/hosts"
else:
    print "Running in native mode... reading from /etc/hosts"
    HOSTS_FILE="/etc/hosts"

if os.environ.get("DOCKER_HOST"):
    client = docker.from_env()
else:
    try:
        client = docker.DockerClient(base_url='unix://var/run/docker.sock')
    except:
        print "Sorry, I can't find a dockerd to connect to."
        sys.exit(1)

def get_ip(c):
    try:
        networks = c.attrs.get("NetworkSettings").get("Networks")

        for n in networks:
            ip = networks[n]["IPAddress"]
            if ip is not None:
                return ip
    except:
        return None

def get_aliases(c):
    aliases = []
    try:
        envs = c.attrs.get("Config").get("Env")

        for e in envs:
            m = re.match('ALIAS=(?P<aliases>.*)', e)
            if m:
                for a in m.group('aliases').split(','):
                    aliases.append(a)
        return aliases
    except KeyError as e:
        return None

def event_remove(event):
    container = client.containers.get(event.get("id"))
    container_remove(container.name)

def event_add(event):
    container = client.containers.get(event.get("id"))
    container_add(container.name)

def container_add(name):
    try:
        container = client.containers.get(name)
    except:
        return

    container_remove(container.name)
    ip = get_ip(container)
    if ip:
        entry = "%s %s" % (ip, container.name)
        aliases = get_aliases(container)
        if aliases:
            entry += " %s" % ' '.join(aliases)

        open(HOSTS_FILE, 'a').write(entry+'\n')
        print "Added: %s" % entry

def container_remove(name):
    if name is None and len(name) > 0:
        return
    entry = "(.*)\s+%s(.*)" % (name)

    lines = open(HOSTS_FILE, 'r').readlines()
    outfile = open(HOSTS_FILE, 'w')

    for line in lines:
        if re.match(entry, line) is None:
            outfile.write(line)
    outfile.close()

    print "removed entry for pattern %s" % name


### Let's start doing something useful.
# first, we catch up with any existing containers that are already deployed.
for container in client.containers.list():
    container_add(container.name)

if args.ONCE_MODE:
    sys.exit(0)

# calling events will basically poll forever, so after this we're
#  essentially in not-quite-a-daemon mode
for event_json in client.events():
    print "SAW: %s" % str(event_json)
    event = json.loads(str(event_json))

    if event.get("status") == "start":
        event_add(event)
    elif event.get("status") == "kill":
        event_remove(event)