Skip to content

Commit fd53bf1

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

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

mem3_helper.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def enable_cluster(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**"
7171
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())
72+
print ("\t\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
@@ -93,6 +93,41 @@ def finish_cluster(names):
9393
# on the "first" pod only with this hack:
9494
if (os.getenv("HOSTNAME").endswith("-0")):
9595
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
96+
97+
headers = {'Content-type': 'application/json'}
98+
print ("=== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
99+
for name in names:
100+
# Exclude "this" pod
101+
if (name.split(".", 1) != os.getenv("HOSTNAME")):
102+
# action: enable_cluster
103+
payload = {}
104+
payload['action'] = 'enable_cluster'
105+
payload['bind_address'] = '0.0.0.0'
106+
payload['username'] = creds[0]
107+
payload['password'] = creds[1]
108+
payload['port'] = 5984
109+
payload['node_count'] = len(names)
110+
payload['remote_node'] = name
111+
payload['remote_current_user'] = creds[0]
112+
payload['remote_current_password'] = creds[1]
113+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
114+
payload['password'] = "**masked**"
115+
payload['remote_current_password'] = "**masked**"
116+
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
117+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
118+
119+
# action: add_node
120+
payload = {}
121+
payload['action'] = 'add_node'
122+
payload['username'] = creds[0]
123+
payload['password'] = creds[1]
124+
payload['port'] = 5984
125+
payload['host'] = name
126+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
127+
payload['password'] = "**masked**"
128+
print ("\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload:",json.dumps(payload))
129+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
130+
96131
# Make sure that ALL CouchDB cluster peers have been
97132
# primed with _nodes data before /_cluster_setup
98133
# Use the _membership of "this" pod's CouchDB as reference
@@ -121,65 +156,32 @@ def finish_cluster(names):
121156
if creds[0] and creds[1]:
122157
remote_resp = requests.get(remote_membership_uri, auth=creds)
123158
print("Node {0} has all node members in place!".format(name))
124-
# The node in <name> is primed
125-
# http://docs.couchdb.org/en/stable/cluster/setup.html
126-
print ("=== Adding nodes to CouchDB cluster via the “setup coordination node” ===")
127-
128-
headers = {'Content-type': 'application/json'}
129-
130-
# action: enable_cluster
131-
payload = {}
132-
payload['action'] = 'enable_cluster'
133-
payload['bind_address'] = '0.0.0.0'
134-
payload['username'] = creds[0]
135-
payload['password'] = creds[1]
136-
payload['port'] = 5984
137-
payload['node_count'] = len(names)
138-
payload['remote_node'] = name
139-
payload['remote_current_user'] = creds[0]
140-
payload['remote_current_password'] = creds[1]
141-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
142-
payload['password'] = "**masked**"
143-
payload['remote_current_password'] = "**masked**"
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())
146-
147-
# action: add_node
148-
payload = {}
149-
payload['action'] = 'add_node'
150-
payload['username'] = creds[0]
151-
payload['password'] = creds[1]
152-
payload['port'] = 5984
153-
payload['host'] = name
154-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json.dumps(payload), auth=creds, headers=headers)
155-
payload['password'] = "**masked**"
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())
159+
158160

159161
print('CouchDB cluster peer {} added to "setup coordination node"'.format(name))
160162
# At this point ALL peers have _nodes populated. Finish the cluster setup!
161163
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
162164
if (setup_resp.status_code == 201):
163165
print("== CouchDB cluster setup done! ===")
164166
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup")
165-
print("CouchDB cluster setup done. Time to relax!")
167+
print("CouchDB cluster setup done.")
166168
print ("\tRequest: GET http://127.0.0.1:5984/_cluster_setup")
167-
169+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
168170
print("== Creating default databases ===")
169171

170172
setup_resp=requests.put("http://127.0.0.1:5984/_users", auth=creds, headers=headers)
171173
print ("\tRequest: PUT http://127.0.0.1:5984/_users")
172-
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
174+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
173175

174176
setup_resp=requests.put("http://127.0.0.1:5984/_replicator", auth=creds, headers=headers)
175177
print ("\tRequest: PUT http://127.0.0.1:5984/_replicator")
176-
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
178+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
177179

178180
setup_resp=requests.put("http://127.0.0.1:5984/_global_changes", auth=creds, headers=headers)
179181
print ("\tRequest: PUT http://127.0.0.1:5984/_global_changes")
180-
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
182+
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
181183

182-
print ("\tResponse:", setup_resp.status_code, setup_resp.json())
184+
print("Time to relax!")
183185
else:
184186
print('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}'.format(setup_resp.status_code))
185187
else:

0 commit comments

Comments
 (0)