|
@@ -0,0 +1,70 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+from collections import defaultdict
|
|
4
|
+import docker
|
|
5
|
+import etcd
|
|
6
|
+
|
|
7
|
+docker_client = docker.Client(base_url='unix://var/run/docker.sock')
|
|
8
|
+etcd_client = etcd.Client(host='etcd', port=4001)
|
|
9
|
+prefix = '/docker'
|
|
10
|
+
|
|
11
|
+containers = {}
|
|
12
|
+label_index = defaultdict(dict)
|
|
13
|
+network_index = defaultdict(dict)
|
|
14
|
+
|
|
15
|
+for container in docker_client.containers():
|
|
16
|
+ name = container['Names'][0][1:]
|
|
17
|
+ image = container['Image']
|
|
18
|
+ labels = container['Labels']
|
|
19
|
+ addrs = {}
|
|
20
|
+ ports = defaultdict(dict)
|
|
21
|
+
|
|
22
|
+ for net_name, net_config in container['NetworkSettings']['Networks'].items():
|
|
23
|
+ addrs[net_name] = net_config['IPAddress']
|
|
24
|
+
|
|
25
|
+ for port in container['Ports']:
|
|
26
|
+ ports[port['Type']][port['PrivatePort']] = port['PublicPort'] if 'PublicPort' in port else 0
|
|
27
|
+
|
|
28
|
+ containers[name] = {
|
|
29
|
+ 'image': image,
|
|
30
|
+ 'labels': labels,
|
|
31
|
+ 'net': {
|
|
32
|
+ 'addr': addrs,
|
|
33
|
+ 'ports': ports
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ for k, v in labels.items():
|
|
38
|
+ label_index[k][name] = v
|
|
39
|
+ for k, v in addrs.items():
|
|
40
|
+ network_index[k][name] = v
|
|
41
|
+
|
|
42
|
+try:
|
|
43
|
+ etcd_client.delete(prefix, recursive=True)
|
|
44
|
+except etcd.EtcdKeyNotFound:
|
|
45
|
+ pass
|
|
46
|
+
|
|
47
|
+for name, details in containers.items():
|
|
48
|
+ cprefix = prefix + '/containers/' + name
|
|
49
|
+ etcd_client.write(cprefix + '/image', details['image'])
|
|
50
|
+ for k, v in details['labels'].items():
|
|
51
|
+ etcd_client.write(cprefix + '/labels/' + k, v)
|
|
52
|
+ for k, v in details['net']['addr'].items():
|
|
53
|
+ etcd_client.write(cprefix + '/net/addr/' + k, v)
|
|
54
|
+ for proto, ports in details['net']['ports'].items():
|
|
55
|
+ for k, v in ports.items():
|
|
56
|
+ etcd_client.write(cprefix + '/net/ports/' + proto + '/' + str(k), v)
|
|
57
|
+
|
|
58
|
+for name, values in label_index.items():
|
|
59
|
+ lprefix = prefix + '/labels/' + name + '/'
|
|
60
|
+ for cont, value in values.items():
|
|
61
|
+ etcd_client.write(lprefix + cont, value)
|
|
62
|
+
|
|
63
|
+for name, values in network_index.items():
|
|
64
|
+ nprefix = prefix + '/networks/' + name + '/'
|
|
65
|
+ for cont, value in values.items():
|
|
66
|
+ etcd_client.write(nprefix + cont, value)
|
|
67
|
+
|
|
68
|
+print(containers)
|
|
69
|
+print(label_index)
|
|
70
|
+print(network_index)
|