Browse Source

Monitor for deletions

master
Chris Smith 8 years ago
parent
commit
fddd8ebdf2
1 changed files with 11 additions and 6 deletions
  1. 11
    6
      monitor.py

+ 11
- 6
monitor.py View File

@@ -22,35 +22,40 @@ class Monitor:
22 22
 
23 23
     for event in self._events:
24 24
       if event['Action'] == 'start':
25
-        print('New container %s' % event['id'])
26 25
         self._add(self._client.containers(filters={'id': event['id']}))
27 26
       elif event['Action'] == 'die':
28
-        print('Dead container %s' % event['id'])
27
+        self._remove(event['id'])
29 28
       else:
30
-        print('Unexpected event %s' % event['Action'])
29
+        print('Monitor.monitor(): unexpected event %s' % event['Action'])
31 30
 
32 31
 
33 32
   def _add(self, infos):
34 33
     res = []
35 34
     for info in infos:
36
-      name = info['Names'][0][1:]
37 35
       container = {
38 36
         'host': self._host,
39 37
         'image': info['Image'],
40 38
         'labels': info['Labels'],
41
-        'name': name,
39
+        'name': info['Names'][0][1:],
42 40
         'net': {
43 41
           'addr': self._get_addresses(info),
44 42
           'ports': self._get_ports(info)
45 43
         }
46 44
       }
47 45
 
48
-      self._containers[name] = container
46
+      self._containers[info['Id']] = container
49 47
       res.append(container)
50 48
 
51 49
     self._on_added(res)
52 50
 
53 51
 
52
+  def _remove(self, container_id):
53
+    if container_id in self._containers:
54
+      container = self._containers[container_id]
55
+      del self._containers[container_id]
56
+      self._on_removed([container])
57
+
58
+
54 59
   def _get_addresses(self, container):
55 60
     return {k: v['IPAddress'] for k, v in container['NetworkSettings']['Networks'].items()}
56 61
 

Loading…
Cancel
Save