Browse Source

Simplfy dictionary building

master
Chris Smith 8 years ago
parent
commit
e83a8cdf82
1 changed files with 19 additions and 19 deletions
  1. 19
    19
      report.py

+ 19
- 19
report.py View File

@@ -15,6 +15,17 @@ def etcd_put(client, prefix, obj):
15 15
       client.write(new_prefix, str(value))
16 16
 
17 17
 
18
+def get_addresses(container):
19
+  return {k: v['IPAddress'] for k, v in container['NetworkSettings']['Networks'].items()}
20
+
21
+
22
+def get_ports(container):
23
+  ports = defaultdict(dict)
24
+  for port in container['Ports']:
25
+    ports[port['Type']][port['PrivatePort']] = port['PublicPort'] if 'PublicPort' in port else 0
26
+  return ports
27
+
28
+
18 29
 docker_client = docker.Client(base_url='unix://var/run/docker.sock')
19 30
 etcd_client = etcd.Client(host='etcd', port=4001)
20 31
 prefix = '/docker'
@@ -24,30 +35,19 @@ label_index = defaultdict(dict)
24 35
 network_index = defaultdict(dict)
25 36
 
26 37
 for container in docker_client.containers():
27
-  name = container['Names'][0][1:]
28
-  image = container['Image']
29
-  labels = container['Labels']
30
-  addrs = {}
31
-  ports = defaultdict(dict)
32
-
33
-  for net_name, net_config in container['NetworkSettings']['Networks'].items():
34
-    addrs[net_name] = net_config['IPAddress']
35
-
36
-  for port in container['Ports']:
37
-    ports[port['Type']][port['PrivatePort']] = port['PublicPort'] if 'PublicPort' in port else 0
38
-
39
-  containers[name] = {
40
-    'image': image,
41
-    'labels': labels,
38
+  containers[container['Names'][0][1:]] = {
39
+    'image': container['Image'],
40
+    'labels': container['Labels'],
42 41
     'net': {
43
-      'addr': addrs,
44
-      'ports': ports
42
+      'addr': get_addresses(container),
43
+      'ports': get_ports(container)
45 44
     }
46 45
   }
47 46
 
48
-  for k, v in labels.items():
47
+for name, details in containers.items():
48
+  for k, v in details['labels'].items():
49 49
     label_index[k][name] = v
50
-  for k, v in addrs.items():
50
+  for k, v in details['net']['addr'].items():
51 51
     network_index[k][name] = v
52 52
 
53 53
 try:

Loading…
Cancel
Save