forked from talpah/domg
/
hostmanager.py
79 lines (67 loc) · 2.09 KB
/
hostmanager.py
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
# coding=utf-8
"""
Updates hosts file based on docker containers
"""
__author__ = 'talpah@gmail.com'
import json
from os import getenv
import sys
import commands
from lib import Hosts
from docker import Client
HOSTS_PATH = '/host_hosts'
DOMAIN_SUFFIX = '.local.development'
def get_ip(container_id):
"""
:param container_id:
:return:
"""
info = docker.inspect_container(container_id)
return info['NetworkSettings']['IPAddress']
def get_hostname(container_id):
"""
:param container_id:
:return:
"""
info = docker.inspect_container(container_id)
dom_name = DOMAIN_SUFFIX
hostname = container_id
if info['Config']['Domainname']:
dom_name = info['Config']['Domainname']
if dom_name[0] != '.':
dom_name = '.%s' % dom_name
if info['Config']['Hostname']:
hostname = info['Config']['Hostname']
return "%s%s" % (hostname, dom_name)
else:
return None
if __name__ == '__main__':
docker = Client()
hostname = getenv('HOSTNAME', None)
if hostname:
if '.' not in hostname:
hostname = "%s%s" % (hostname, DOMAIN_SUFFIX)
print "Adding %s" % hostname
hosts = Hosts(HOSTS_PATH)
my_ip = commands.getoutput("ip -4 -f inet -o addr show eth0 | awk '{print $4}' | cut -d/ -f1")
hosts.set_one(hostname, my_ip)
hosts.write(HOSTS_PATH)
print "Go to http://%s/" % hostname
for event in docker.events():
event = json.loads(event)
if 'status' not in event:
continue
if event['status'] == 'start':
hostname = get_hostname(event['id'])
if hostname is None:
continue
print "Adding %s" % hostname
hosts = Hosts(HOSTS_PATH)
hosts.set_one(hostname, get_ip(event['id']))
hosts.write(HOSTS_PATH)
elif event['status'] == 'die':
hostname = get_hostname(event['id'])
print "Removing %s" % hostname
hosts = Hosts(HOSTS_PATH)
hosts.remove_one(hostname)
hosts.write(HOSTS_PATH)