Skip to content

Commit 5f19920

Browse files
author
Robert Gartman
committed
Automate _cluster_setup
1 parent 017fe92 commit 5f19920

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

mem3_helper.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ def connect_the_dots(names):
5353
resp = requests.put(uri, data=json.dumps(doc))
5454
print('Adding CouchDB cluster node', name, "to this pod's CouchDB", flush=True)
5555

56+
# Run action:enable_cluster on every CouchDB cluster node
57+
def enable_cluster(nr_of_peers):
58+
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
59+
if creds[0] and creds[1]:
60+
headers = {'Content-type': 'application/json'}
61+
print ("== Enabling cluster mode ===")
62+
# http://docs.couchdb.org/en/stable/cluster/setup.html
63+
payload = {}
64+
payload['action'] = 'enable_cluster'
65+
payload['bind_address'] = '0.0.0.0'
66+
payload['username'] = creds[0]
67+
payload['password'] = creds[1]
68+
payload['node_count'] = nr_of_peers
69+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
70+
payload['password'] = "**masked**"
71+
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
72+
print ("\tResponse:", setup_resp)
73+
5674
# Compare (json) objects - order does not matter. Credits to:
5775
# https://stackoverflow.com/a/25851972
5876
def ordered(obj):
@@ -85,7 +103,7 @@ def finish_cluster(names):
85103
local_resp = requests.get(local_membership_uri, auth=creds)
86104
else:
87105
local_resp = requests.get(local_membership_uri)
88-
# Step through every peer pod and grab the _membership.
106+
# Step through every peer. Ensure they are "ready" before progressing.
89107
for name in names:
90108
print("Probing {0} for cluster membership".format(name))
91109
remote_membership_uri = "http://{0}:5984/_membership".format(name)
@@ -103,8 +121,10 @@ def finish_cluster(names):
103121
time.sleep(5)
104122
if creds[0] and creds[1]:
105123
remote_resp = requests.get(remote_membership_uri, auth=creds)
124+
print("Node {0} has all node members in place!".format(name))
106125
# The node in <name> is primed
107126
# http://docs.couchdb.org/en/stable/cluster/setup.html
127+
print ("== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
108128

109129
headers = {'Content-type': 'application/json'}
110130

@@ -122,7 +142,8 @@ def finish_cluster(names):
122142
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
123143
payload['password'] = "**masked**"
124144
payload['remote_current_password'] = "**masked**"
125-
print ("POST to http://127.0.0.1:5984/_cluster_setup returned",setup_resp.status_code,"payload=",json.dumps(payload))
145+
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
146+
print ("\tResponse:", setup_resp)
126147

127148
# action: add_node
128149
payload = {}
@@ -133,7 +154,8 @@ def finish_cluster(names):
133154
payload['host'] = name
134155
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
135156
payload['password'] = "**masked**"
136-
print ("POST to http://127.0.0.1:5984/_cluster_setup returned",setup_resp.status_code,"payload=",json.dumps(payload))
157+
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
158+
print ("\tResponse:", setup_resp)
137159

138160
print('CouchDB cluster peer {} added to "setup coordination node"'.format(name))
139161
# At this point ALL peers have _nodes populated. Finish the cluster setup!
@@ -146,23 +168,6 @@ def finish_cluster(names):
146168
else:
147169
print("This pod is intentionally skipping the call to http://127.0.0.1:5984/_cluster_setup")
148170

149-
# Run action:enable_cluster on every CouchDB cluster node
150-
def enable_cluster(nr_of_peers):
151-
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
152-
if creds[0] and creds[1]:
153-
headers = {'Content-type': 'application/json'}
154-
155-
# http://docs.couchdb.org/en/stable/cluster/setup.html
156-
payload = {}
157-
payload['action'] = 'enable_cluster'
158-
payload['bind_address'] = '0.0.0.0'
159-
payload['username'] = creds[0]
160-
payload['password'] = creds[1]
161-
payload['node_count'] = '{0}'.format(nr_of_peers)
162-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
163-
payload['password'] = "**masked**"
164-
print ("POST to http://127.0.0.1:5984/_cluster_setup returned",setup_resp.status_code,"payload=",json.dumps(payload))
165-
166171
def sleep_forever():
167172
while True:
168173
time.sleep(5)

0 commit comments

Comments
 (0)