Skip to content

Commit 0785207

Browse files
author
Robert Gartman
committed
Automate _cluster_setup
1 parent efba00b commit 0785207

File tree

1 file changed

+10
-61
lines changed

1 file changed

+10
-61
lines changed

mem3_helper.py

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
This script grabs the list of pod names behind the headless service
44
associated with our StatefulSet and feeds those as `couchdb@FQDN`
55
nodes to mem3.
6+
7+
As a final step - on just one of the nodes - the cluster finalize is triggered.
8+
For that part to happen we need username and password.
69
"""
710

811
import json
@@ -54,24 +57,6 @@ def connect_the_dots(names):
5457
if resp.status_code == 201:
5558
print('Adding CouchDB cluster node', name, "to this pod's CouchDB. Response code:", resp.status_code ,flush=True)
5659

57-
# Run action:enable_cluster on every CouchDB cluster node
58-
def enable_cluster(nr_of_peers):
59-
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
60-
if creds[0] and creds[1]:
61-
headers = {'Content-type': 'application/json'}
62-
print ("=== Enabling cluster mode ===")
63-
# http://docs.couchdb.org/en/stable/cluster/setup.html
64-
payload = {}
65-
payload['action'] = 'enable_cluster'
66-
payload['bind_address'] = '0.0.0.0'
67-
payload['username'] = creds[0]
68-
payload['password'] = creds[1]
69-
payload['node_count'] = nr_of_peers
70-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
71-
payload['password'] = "**masked**"
72-
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
73-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
74-
7560
# Compare (json) objects - order does not matter. Credits to:
7661
# https://stackoverflow.com/a/25851972
7762
def ordered(obj):
@@ -82,7 +67,7 @@ def ordered(obj):
8267
else:
8368
return obj
8469

85-
# Run action:finish_cluster on one and only one CouchDB cluster node
70+
# Run action:finish_cluster on one (and only one) CouchDB cluster node
8671
def finish_cluster(names):
8772
# The HTTP POST to /_cluster_setup should be done to
8873
# one (and only one) of the CouchDB cluster nodes.
@@ -94,52 +79,15 @@ def finish_cluster(names):
9479
# on the "first" pod only with this hack:
9580
if (os.getenv("HOSTNAME").endswith("-0")):
9681
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
97-
98-
headers = {'Content-type': 'application/json'}
99-
print ("=== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
100-
for name in names:
101-
# Exclude "this" pod
102-
if (name.split(".",1)[0] != os.getenv("HOSTNAME")):
103-
# action: enable_cluster
104-
payload = {}
105-
payload['action'] = 'enable_cluster'
106-
payload['bind_address'] = '0.0.0.0'
107-
payload['username'] = creds[0]
108-
payload['password'] = creds[1]
109-
payload['port'] = 5984
110-
payload['node_count'] = len(names)
111-
payload['remote_node'] = name
112-
payload['remote_current_user'] = creds[0]
113-
payload['remote_current_password'] = creds[1]
114-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
115-
payload['password'] = "**masked**"
116-
payload['remote_current_password'] = "**masked**"
117-
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
118-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
119-
120-
# action: add_node
121-
payload = {}
122-
payload['action'] = 'add_node'
123-
payload['username'] = creds[0]
124-
payload['password'] = creds[1]
125-
payload['port'] = 5984
126-
payload['host'] = name
127-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
128-
payload['password'] = "**masked**"
129-
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
130-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
131-
132-
133-
82+
print("== Get the cluster up and running ===")
13483
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
84+
print ('\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}')
85+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
13586
if (setup_resp.status_code == 201):
136-
print("== CouchDB cluster setup done! ===")
137-
print ('\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}')
138-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
87+
print ("\tSweet! Just a final check for the logs...")
13988
setup_resp=requests.get("http://127.0.0.1:5984/_cluster_setup", auth=creds)
14089
print ('\tRequest: GET http://127.0.0.1:5984/_cluster_setup')
14190
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
142-
14391
print("Time to relax!")
14492
else:
14593
print('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}'.format(setup_resp.status_code))
@@ -157,7 +105,7 @@ def diff(first, second):
157105
max_tries=10
158106
)
159107
# Check if the _membership API on all (known) CouchDB nodes have the same values.
160-
# Returns true if sam. False in any other situation.
108+
# Returns true if same. False in any other situation.
161109
def are_nodes_in_sync(names):
162110
# Make sure that ALL (known) CouchDB cluster peers have been
163111
# have the same _membership data.Use "this" nodes memebership as
@@ -207,6 +155,7 @@ def sleep_forever():
207155
peer_names = discover_peers(construct_service_record())
208156
connect_the_dots(peer_names)
209157
print("Got the following peers' fqdm from DNS lookup:",peer_names,flush=True)
158+
210159
# loop until all CouchDB nodes discovered
211160
while not are_nodes_in_sync(peer_names):
212161
time.sleep(5)

0 commit comments

Comments
 (0)