Browse Source

Handle removing

master
Chris Smith 8 years ago
parent
commit
5c4d6b7c45
2 changed files with 20 additions and 2 deletions
  1. 3
    1
      README.md
  2. 17
    1
      report.py

+ 3
- 1
README.md View File

@@ -31,6 +31,9 @@ The following command line arguments are available:
31 31
   --name (default: unknown) name of the host running docker
32 32
 ```
33 33
 
34
+The script will update etcd as soon as it launches, and then
35
+monitor for docker events and apply the relevant updates.
36
+
34 37
 ## Schema
35 38
 
36 39
 The script stores values relating to containers, labels, hosts and
@@ -65,7 +68,6 @@ networks in etcd:
65 68
 
66 69
 * The docker node is deleted when the script starts, so you can't run multiple
67 70
   copies on multiple hosts
68
-* Containers that are stopped aren't removed
69 71
 * There's no way to get notified when the script has finished, rather than
70 72
   mid-update
71 73
 

+ 17
- 1
report.py View File

@@ -35,6 +35,22 @@ def add_containers(new_containers):
35 35
   etcd_put(etcd_client, prefix + '/hosts', host_index)
36 36
 
37 37
 
38
+def remove_containers(old_containers):
39
+  global containers, host_index, label_index, network_index
40
+  for container in old_containers:
41
+    name = container['name']
42
+    del containers[name]
43
+    etcd_client.delete(prefix + '/containers/' + name, recursive=True)
44
+
45
+    for k, v in container['labels'].items():
46
+      del label_index[k][name]
47
+      etcd_client.delete(prefix + '/labels/' + k + '/' + name)
48
+    for k, v in container['net']['addr'].items():
49
+      del network_index[k][name]
50
+      etcd_client.delete(prefix + '/networks/' + k + '/' + name)
51
+    etcd_client.delete(prefix + '/hosts/' + host + '/' + name)
52
+
53
+
38 54
 parser = argparse.ArgumentParser()
39 55
 parser.add_argument('--name', help='Name of this docker host', default='unknown')
40 56
 parser.add_argument('--etcd-port', type=int, help='Port to connect to etcd on', default=2379)
@@ -42,7 +58,7 @@ parser.add_argument('--etcd-host', help='Host to connect to etcd on', default='e
42 58
 parser.add_argument('--etcd-prefix', help='Prefix to use when adding keys to etcd', default='/docker')
43 59
 args = parser.parse_args()
44 60
 
45
-monitor = Monitor(args.name, add_containers, lambda x: None) 
61
+monitor = Monitor(args.name, add_containers, remove_containers)
46 62
 etcd_client = etcd.Client(host=args.etcd_host, port=args.etcd_port)
47 63
 prefix = args.etcd_prefix
48 64
 host = args.name

Loading…
Cancel
Save