Browse Source

Add host index

master
Chris Smith 8 years ago
parent
commit
31faa3c4fe
2 changed files with 35 additions and 18 deletions
  1. 20
    14
      README.md
  2. 15
    4
      report.py

+ 20
- 14
README.md View File

@@ -28,29 +28,35 @@ The following command line arguments are available:
28 28
   --etcd-host (default: etcd) hostname where ectd is running
29 29
   --etcd-port (default: 2379) port to connect to ectd on
30 30
   --etcd-prefix (default: /docker) prefix to write keys to
31
-  --name (default: docker) name of the host running docker
31
+  --name (default: unknown) name of the host running docker
32 32
 ```
33 33
 
34 34
 ## Schema
35 35
 
36
-The script stores values relating to containers, labels, and
36
+The script stores values relating to containers, labels, hosts and
37 37
 networks in etcd:
38 38
 
39 39
 ```
40
-/docker/containers/{name}/image = "ubuntu:xenial"
41
-                         /labels/service = "foo"
42
-                         /labels/org.example.some-label = "bar"
43
-                         /net/addr/{network1} = "172.1.2.3"
44
-                                  /{network2} = "172.0.2.3"
45
-                             /ports/tcp/{container_port1} = {host_port} 
46
-                                       /{container_port2} = 0 # Not exposed
47
-                                   /udp/...
48
-
49
-/docker/labels/{label_1}/{container_name_1} = "foo"
40
+/docker/containers/{name1}/host = "server1.example.com"
41
+                          /image = "ubuntu:xenial"
42
+                          /labels/service = "foo"
43
+                          /labels/org.example.some-label = "bar"
44
+                          /net/addr/{network1} = "172.1.2.3"
45
+                                   /{network2} = "172.0.2.3"
46
+                              /ports/tcp/{container_port1} = {host_port}
47
+                                        /{container_port2} = 0 # Not exposed
48
+                                    /udp/...
49
+                  /{name2}/...
50
+
51
+       /hosts/{host_1}/{container_name_1} = {container_name_1}
52
+                      /...
53
+             /{host_2}/...
54
+
55
+       /labels/{label_1}/{container_name_1} = "foo"
50 56
                         /{container_name_2} = "bar"
51 57
               /{label_2}/...
52 58
 
53
-/docker/networks/{network1}/{container_name_1} = "172.1.2.3"
59
+       /networks/{network1}/{container_name_1} = "172.1.2.3"
54 60
                            /{container_name_2} = "172.0.2.3"
55 61
                 /{network2}/...
56 62
 ```
@@ -59,7 +65,7 @@ networks in etcd:
59 65
 
60 66
 * The docker node is deleted when the script starts, so you can't run multiple
61 67
   copies on multiple hosts
62
-* The script updates once and exits, instead of listening for events
68
+* Containers that are stopped aren't removed 
63 69
 * There's no way to get notified when the script has finished, rather than
64 70
   mid-update
65 71
 

+ 15
- 4
report.py View File

@@ -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:

Loading…
Cancel
Save