|
@@ -1,11 +1,21 @@
|
1
|
1
|
#!/usr/bin/env python3
|
|
2
|
+"""Library to connect to etcd and manage docker service information."""
|
2
|
3
|
|
3
|
4
|
import time
|
4
|
5
|
import etcd
|
5
|
6
|
|
6
|
7
|
|
7
|
8
|
class Connection:
|
|
9
|
+ """A high-level connection to etcd.
|
8
|
10
|
|
|
11
|
+ Manages a connection to etcd, and provides high-level methods for
|
|
12
|
+ interacting with service records and related meta-data.
|
|
13
|
+
|
|
14
|
+ Args:
|
|
15
|
+ host (str): Hostname to connect to etcd on.
|
|
16
|
+ port (int): Port to connect to etcd on.
|
|
17
|
+ prefix (str): Etcd node under which the service information is stored.
|
|
18
|
+ """
|
9
|
19
|
|
10
|
20
|
def __init__(self, host, port, prefix):
|
11
|
21
|
self._client = etcd.Client(host=host, port=port)
|
|
@@ -49,10 +59,12 @@ class Connection:
|
49
|
59
|
|
50
|
60
|
|
51
|
61
|
def wipe(self):
|
|
62
|
+ """Deletes all service entries and related structures in etcd."""
|
52
|
63
|
self._delete('')
|
53
|
64
|
|
54
|
65
|
|
55
|
66
|
def add_containers(self, new_containers):
|
|
67
|
+ """Writes the new containers' information to etcd."""
|
56
|
68
|
for container in new_containers:
|
57
|
69
|
name = container['name']
|
58
|
70
|
print('Adding container %s' % name)
|
|
@@ -66,6 +78,7 @@ class Connection:
|
66
|
78
|
|
67
|
79
|
|
68
|
80
|
def remove_containers(self, old_containers):
|
|
81
|
+ """Deletes the containers' entries from etcd."""
|
69
|
82
|
for container in old_containers:
|
70
|
83
|
name = container['name']
|
71
|
84
|
print('Removing container %s' % name)
|
|
@@ -79,6 +92,7 @@ class Connection:
|
79
|
92
|
|
80
|
93
|
|
81
|
94
|
def get_label(self, label):
|
|
95
|
+ """Gets a map of container names to values for the given label."""
|
82
|
96
|
node = self._read_recursive('/labels/%s' % label)
|
83
|
97
|
if node:
|
84
|
98
|
return {child.key.split('/')[-1]: child.value for child in node.children}
|
|
@@ -92,6 +106,12 @@ class Connection:
|
92
|
106
|
|
93
|
107
|
|
94
|
108
|
def wait_for_update(self):
|
|
109
|
+ """Waits for an update to occur.
|
|
110
|
+
|
|
111
|
+ When writing entries to etcd, a special _updated key is set to the
|
|
112
|
+ current unix timestamp. This method watches that key until it is
|
|
113
|
+ changed, blocking execution.
|
|
114
|
+ """
|
95
|
115
|
original_time = self._read('/_updated')
|
96
|
116
|
new_time = original_time
|
97
|
117
|
|