diff options
author | Kelley Spoon <kelley.spoon@linaro.org> | 2018-03-07 09:10:05 -0600 |
---|---|---|
committer | Kelley Spoon <kelley.spoon@linaro.org> | 2018-03-07 09:10:16 -0600 |
commit | a1090e83a51303fe966042962db52d33daca6f57 (patch) | |
tree | 223201252686c590da1d7c45f7ab3745ebc78af2 | |
parent | a64d5bbebe2eac462b325946348d5ca4b721a8e3 (diff) | |
parent | 07b8429f06f87b27771d9375764a293c319be761 (diff) |
Merge branch 'master' of https://git.linaro.org/infrastructure/docker-ansible-baseimage into compose-democompose-demo
Change-Id: Ia9556357bdda9d044b7071722a93f648bf5bfabb
-rw-r--r-- | hostsdns/Dockerfile | 2 | ||||
-rw-r--r-- | hostsdns/event-watcher.py | 57 |
2 files changed, 32 insertions, 27 deletions
diff --git a/hostsdns/Dockerfile b/hostsdns/Dockerfile index 1ea0f6e..6f5a399 100644 --- a/hostsdns/Dockerfile +++ b/hostsdns/Dockerfile @@ -1,4 +1,4 @@ -FROM python:2.7-alpine +FROM python:2.7.14-alpine3.7 RUN apk add --no-cache bash wget diff --git a/hostsdns/event-watcher.py b/hostsdns/event-watcher.py index b86d27f..b51ec76 100644 --- a/hostsdns/event-watcher.py +++ b/hostsdns/event-watcher.py @@ -6,18 +6,23 @@ import docker import json import re -HOSTS_FILE="/tmp/hosts" +if os.path.exists("/.dockerenv"): + print "Found /.dockerenv... reading from /tmp/hosts" + HOSTS_FILE="/tmp/hosts" +else: + print "No /.dockerenv found... 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') + 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 ): +def get_ip(c): try: networks = c.attrs.get("NetworkSettings").get("Networks") @@ -34,65 +39,65 @@ def get_aliases(c): envs = c.attrs.get("Config").get("Env") for e in envs: - m = re.match( 'ALIAS=(?P<aliases>.*)', e ) + m = re.match('ALIAS=(?P<aliases>.*)', e) if m: for a in m.group('aliases').split(','): - aliases.append( a ) + 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_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 event_add(event): + container = client.containers.get(event.get("id")) + container_add(container.name) -def container_add( name ): +def container_add(name): try: - container = client.containers.get( name ) + container = client.containers.get(name) except: return - container_remove( container.name ) + container_remove(container.name) ip = get_ip(container) if ip: - entry = "%s %s" % ( ip, container.name ) - aliases = get_aliases( container ) + entry = "%s %s" % (ip, container.name) + aliases = get_aliases(container) if aliases: entry += " %s" % ' '.join(aliases) - open( HOSTS_FILE, 'a').write(entry+'\n') + open(HOSTS_FILE, 'a').write(entry+'\n') print "Added: %s" % entry -def container_remove( name ): +def container_remove(name): if name is None and len(name) > 0: return - entry = "(.*)%s(.*)" % ( name ) + entry = "(.*)\s+%s(.*)" % (name) - lines = open( HOSTS_FILE, 'r').readlines() - outfile = open( HOSTS_FILE, 'w' ) + 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 ) + if re.match(entry, line) is None: + outfile.write(line) outfile.close() - print "removed entry for pattern %s" % entry + 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 ) + container_add(container.name) # 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) ) + event = json.loads(str(event_json)) if event.get("status") == "start": event_add(event) |