Skip to content

Commit 9367e6b

Browse files
author
Robert Gartman
committed
Automate _cluster_setup
1 parent 970510b commit 9367e6b

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

mem3_helper.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ def ordered(obj):
6363
else:
6464
return obj
6565

66+
# Run action:finish_cluster on one and only one CouchDB cluster node
6667
def finish_cluster(names):
6768
# The HTTP POST to /_cluster_setup should be done to
6869
# one (and only one) of the CouchDB cluster nodes.
70+
# Search for "setup coordination node" in
71+
# http://docs.couchdb.org/en/stable/cluster/setup.html
6972
# Given that K8S has a standardized naming for the pods:
7073
# https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity
71-
# we can make sure that this code is only bing run
72-
# on the "first" pod using this hack:
74+
# we can make sure that this code runs
75+
# on the "first" pod only with this hack:
7376
print('HOSTNAME={0}'.format(os.getenv("HOSTNAME")))
7477
if (os.getenv("HOSTNAME").endswith("-0")):
7578
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
@@ -88,8 +91,6 @@ def finish_cluster(names):
8891
remote_membership_uri = "http://{0}:5984/_membership".format(name)
8992
if creds[0] and creds[1]:
9093
remote_resp = requests.get(remote_membership_uri, auth=creds)
91-
else:
92-
remote_resp = requests.get(remote_membership_uri)
9394
# Compare local and remote _mebership data. Make sure the set
9495
# of nodes match. This will ensure that the remote nodes
9596
# are fully primed with nodes data before progressing with
@@ -102,21 +103,37 @@ def finish_cluster(names):
102103
time.sleep(5)
103104
if creds[0] and creds[1]:
104105
remote_resp = requests.get(remote_membership_uri, auth=creds)
105-
else:
106-
remote_resp = requests.get(remote_membership_uri)
107-
print("CouchDB cluster peer {} ready to form a cluster".format(name))
106+
# The node in <name> is primed
107+
# http://docs.couchdb.org/en/stable/cluster/setup.html
108+
109+
# action: enable_cluster
110+
payload = '"action": "enable_cluster", "bind_address":"0.0.0.0", "username":"{0}", "password":"{1}", "port": 5984, "node_count":"{2}" "remote_node": "{3}", "remote_current_user": "{0}", "remote_current_password": "{1}"'.format(creds[0],creds[1],nr_of_peers,name)
111+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={payload}, auth=creds)
112+
print ("POST to http://127.0.0.1:5984/_cluster_setup returned",setup_resp.status_code,"payload=",payload)
113+
114+
# action: add_node
115+
payload = '"action": "add_node", "host":"{0}", "port": 5984, "username": "{1}", "password":"{2}"'.format(name, creds[0],creds[1])
116+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={payload}, auth=creds)
117+
print ("POST to http://127.0.0.1:5984/_cluster_setup returned",setup_resp.status_code,"payload=",payload)
118+
119+
print('CouchDB cluster peer {} added to "setup coordination node"'.format(name))
108120
# At this point ALL peers have _nodes populated. Finish the cluster setup!
109121
if creds[0] and creds[1]:
110122
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
111-
else:
112-
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"})
113123
if (setup_resp.status_code == 201):
114124
print("CouchDB cluster setup done. Time to relax!")
115125
else:
116126
print('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}'.format(setup_resp.status_code))
117127
else:
118128
print("This pod is intentionally skipping the call to http://127.0.0.1:5984/_cluster_setup")
119129

130+
# Run action:enable_cluster on every CouchDB cluster node
131+
def enable_cluster(nr_of_peers):
132+
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
133+
if creds[0] and creds[1]:
134+
# http://docs.couchdb.org/en/stable/cluster/setup.html
135+
payload = '"action": "enable_cluster", "bind_address":"0.0.0.0", "{0}}": "admin", "{1}}":"password", "node_count":"{2}"'.format(creds[0],creds[1],nr_of_peers)
136+
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={payload}, auth=creds)
120137

121138
def sleep_forever():
122139
while True:
@@ -127,5 +144,9 @@ def sleep_forever():
127144
print("Got the following peers' fqdm from DNS lookup:",peer_names,flush=True)
128145
connect_the_dots(peer_names)
129146
print('Cluster membership populated!')
130-
finish_cluster(peer_names)
147+
if (os.getenv("COUCHDB_USER") and os.getenv("COUCHDB_PASSWORD")):
148+
enable_cluster(len(peer_names))
149+
finish_cluster(peer_names)
150+
else:
151+
print ('Skipping cluster setup. Username and/or password not provided')
131152
sleep_forever()

0 commit comments

Comments
 (0)