|
@@ -29,9 +29,10 @@ def get_ports(container):
|
29
|
29
|
|
30
|
30
|
|
31
|
31
|
def add_containers(infos):
|
32
|
|
- global containers, label_index, network_index
|
|
32
|
+ global containers, host_index, label_index, network_index
|
33
|
33
|
for info in infos:
|
34
|
34
|
container = {
|
|
35
|
+ 'host': host,
|
35
|
36
|
'image': info['Image'],
|
36
|
37
|
'labels': info['Labels'],
|
37
|
38
|
'net': {
|
|
@@ -48,6 +49,15 @@ def add_containers(infos):
|
48
|
49
|
for k, v in container['net']['addr'].items():
|
49
|
50
|
network_index[k][name] = v
|
50
|
51
|
|
|
52
|
+ host_index[host][name] = name
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+def write_all():
|
|
56
|
+ etcd_put(etcd_client, prefix + '/containers', containers)
|
|
57
|
+ etcd_put(etcd_client, prefix + '/labels', label_index)
|
|
58
|
+ etcd_put(etcd_client, prefix + '/networks', network_index)
|
|
59
|
+ etcd_put(etcd_client, prefix + '/hosts', host_index)
|
|
60
|
+
|
51
|
61
|
|
52
|
62
|
parser = argparse.ArgumentParser()
|
53
|
63
|
parser.add_argument('--name', help='Name of this docker host', default='unknown')
|
|
@@ -59,12 +69,14 @@ args = parser.parse_args()
|
59
|
69
|
docker_client = docker.Client(base_url='unix://var/run/docker.sock')
|
60
|
70
|
etcd_client = etcd.Client(host=args.etcd_host, port=args.etcd_port)
|
61
|
71
|
prefix = args.etcd_prefix
|
|
72
|
+host = args.name
|
62
|
73
|
|
63
|
74
|
event_gen = docker_client.events(decode=True, filters={'type': 'container', 'event': ['die', 'start']})
|
64
|
75
|
|
65
|
76
|
containers = {}
|
66
|
77
|
label_index = defaultdict(dict)
|
67
|
78
|
network_index = defaultdict(dict)
|
|
79
|
+host_index = defaultdict(dict)
|
68
|
80
|
|
69
|
81
|
add_containers(docker_client.containers())
|
70
|
82
|
|
|
@@ -73,14 +85,13 @@ try:
|
73
|
85
|
except etcd.EtcdKeyNotFound:
|
74
|
86
|
pass
|
75
|
87
|
|
76
|
|
-etcd_put(etcd_client, prefix + '/containers', containers)
|
77
|
|
-etcd_put(etcd_client, prefix + '/labels', label_index)
|
78
|
|
-etcd_put(etcd_client, prefix + '/networks', network_index)
|
|
88
|
+write_all()
|
79
|
89
|
|
80
|
90
|
for event in event_gen:
|
81
|
91
|
if event['Action'] == 'start':
|
82
|
92
|
print('New container %s' % event['id'])
|
83
|
93
|
add_containers(docker_client.containers(filters={'id': event['id']}))
|
|
94
|
+ write_all()
|
84
|
95
|
elif event['Action'] == 'die':
|
85
|
96
|
print('Dead container %s' % event['id'])
|
86
|
97
|
else:
|