Skip to content

Commit c7b8f53

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

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

mem3_helper.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def enable_cluster(nr_of_peers):
5858
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
5959
if creds[0] and creds[1]:
6060
headers = {'Content-type': 'application/json'}
61-
print ("== Enabling cluster mode ===")
61+
print ("=== Enabling cluster mode ===")
6262
# http://docs.couchdb.org/en/stable/cluster/setup.html
6363
payload = {}
6464
payload['action'] = 'enable_cluster'
@@ -68,8 +68,8 @@ def enable_cluster(nr_of_peers):
6868
payload['node_count'] = nr_of_peers
6969
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
7070
payload['password'] = "**masked**"
71-
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
72-
print ("\tResponse:", setup_resp)
71+
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
72+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
7373

7474
# Compare (json) objects - order does not matter. Credits to:
7575
# https://stackoverflow.com/a/25851972
@@ -91,7 +91,6 @@ def finish_cluster(names):
9191
# https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity
9292
# we can make sure that this code runs
9393
# on the "first" pod only with this hack:
94-
print('HOSTNAME={0}'.format(os.getenv("HOSTNAME")))
9594
if (os.getenv("HOSTNAME").endswith("-0")):
9695
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
9796
# Make sure that ALL CouchDB cluster peers have been
@@ -124,7 +123,7 @@ def finish_cluster(names):
124123
print("Node {0} has all node members in place!".format(name))
125124
# The node in <name> is primed
126125
# http://docs.couchdb.org/en/stable/cluster/setup.html
127-
print ("== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
126+
print ("=== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
128127

129128
headers = {'Content-type': 'application/json'}
130129

@@ -142,8 +141,8 @@ def finish_cluster(names):
142141
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
143142
payload['password'] = "**masked**"
144143
payload['remote_current_password'] = "**masked**"
145-
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
146-
print ("\tResponse:", setup_resp)
144+
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
145+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
147146

148147
# action: add_node
149148
payload = {}
@@ -154,15 +153,33 @@ def finish_cluster(names):
154153
payload['host'] = name
155154
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
156155
payload['password'] = "**masked**"
157-
print ("\tRequest: http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
158-
print ("\tResponse:", setup_resp)
156+
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
157+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
159158

160159
print('CouchDB cluster peer {} added to "setup coordination node"'.format(name))
161160
# At this point ALL peers have _nodes populated. Finish the cluster setup!
162-
if creds[0] and creds[1]:
163-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
161+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
164162
if (setup_resp.status_code == 201):
163+
print("== CouchDB cluster setup done! ===")
164+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup")
165165
print("CouchDB cluster setup done. Time to relax!")
166+
print ("\tRequest: GET http://127.0.0.1:5984/_cluster_setup")
167+
168+
print("== Creating default databases ===")
169+
170+
setup_resp=requests.put("http://127.0.0.1:5984/_users", auth=creds, headers=headers)
171+
print ("\tRequest: PUT http://127.0.0.1:5984/_users")
172+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
173+
174+
setup_resp=requests.put("http://127.0.0.1:5984/_replicator", auth=creds, headers=headers)
175+
print ("\tRequest: PUT http://127.0.0.1:5984/_replicator")
176+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
177+
178+
setup_resp=requests.put("http://127.0.0.1:5984/_global_changes", auth=creds, headers=headers)
179+
print ("\tRequest: PUT http://127.0.0.1:5984/_global_changes")
180+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
181+
182+
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
166183
else:
167184
print('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}'.format(setup_resp.status_code))
168185
else:

0 commit comments

Comments
 (0)