Browse Source

Refactor etcd writing

master
Chris Smith 8 years ago
parent
commit
6d9e474c9b
1 changed files with 14 additions and 20 deletions
  1. 14
    20
      report.py

+ 14
- 20
report.py View File

@@ -4,6 +4,17 @@ from collections import defaultdict
4 4
 import docker 
5 5
 import etcd
6 6
 
7
+
8
+def etcd_put(client, prefix, obj):
9
+  for key, value in obj.items():
10
+    new_prefix = "%s/%s" % (prefix, key)
11
+
12
+    if isinstance(value, dict):
13
+      etcd_put(client, new_prefix, value)
14
+    else:
15
+      client.write(new_prefix, str(value))
16
+
17
+
7 18
 docker_client = docker.Client(base_url='unix://var/run/docker.sock')
8 19
 etcd_client = etcd.Client(host='etcd', port=4001)
9 20
 prefix = '/docker'
@@ -44,26 +55,9 @@ try:
44 55
 except etcd.EtcdKeyNotFound:
45 56
   pass
46 57
 
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)
58
+etcd_put(etcd_client, prefix + '/containers', containers)
59
+etcd_put(etcd_client, prefix + '/labels', label_index)
60
+etcd_put(etcd_client, prefix + '/networks', network_index)
67 61
 
68 62
 print(containers)
69 63
 print(label_index)

Loading…
Cancel
Save