forked from garbados/jepsen-couchdb
/
sync.py
45 lines (41 loc) · 1.06 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Sets up syncing between each node.
"""
import requests
import json
from config import nodes, make_url
sync_doc = """{
"source": "%s/test",
"target": "test",
"continuous": true
}"""
def sync(nodes=nodes):
for i, node in enumerate(nodes):
# create test db in source
r = requests.put('/'.join([make_url(node), 'test']))
if r.status_code not in [201, 412]:
print r.status_code
raise Exception(r.json())
# get list of nodes to create replications with
other_nodes = nodes[::]
other_nodes.pop(i)
# push replication docs
for other_node in other_nodes:
doc = sync_doc % make_url(other_node)
# print doc
headers = {
'Content-Type': 'application/json'
}
r = requests.post('/'.join([make_url(node), '_replicator']),
data=doc,
headers=headers)
if 'error' in r.json():
raise Exception(r.json())
print "Synced."
def part_network():
group1 = nodes[:2]
group2 = nodes[2:]
sync(group1)
sync(group2)
if __name__ == '__main__':
sync()