Skip to content

Commit f478e18

Browse files
author
Robert Gartman
committed
Automate _cluster_setup
1 parent fccddce commit f478e18

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

mem3_helper.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,14 @@ def discover_peers(service_record):
3232
# name to form the hostname used for the Erlang node. This feels hacky
3333
# but not sure of a more official answer
3434
result = [rdata.target.to_text()[:-1] for rdata in answers]
35-
print("\tGot the following peers' fqdm from DNS lookup:",result,flush=True)
35+
print("\t| Got the following peers' fqdm from DNS lookup:",result,flush=True)
3636
return result
3737

3838
def backoff_hdlr(details):
3939
print ("Backing off {wait:0.1f} seconds afters {tries} tries "
4040
"calling function {target} with args {args} and kwargs "
4141
"{kwargs}".format(**details))
4242

43-
def pod_not_ready_yet(details):
44-
print ('\tConnection failure. CouchDB is not responding. Will retry.')
45-
46-
@backoff.on_exception(
47-
backoff.expo,
48-
requests.exceptions.ConnectionError,
49-
max_tries=3,
50-
on_giveup=pod_not_ready_yet
51-
)
5243
def connect_the_dots(names):
5344

5445
# Ordinal Index: For a StatefulSet with N replicas, each Pod in the StatefulSet
@@ -84,12 +75,15 @@ def connect_the_dots(names):
8475
uri = "http://127.0.0.1:5986/_nodes/couchdb@{0}".format(name)
8576
doc = {}
8677
print('Adding CouchDB cluster node', name, "to this pod's CouchDB.")
87-
print ('\tRequest: PUT',uri)
88-
if creds[0] and creds[1]:
89-
resp = requests.put(uri, data=json.dumps(doc), auth=creds)
90-
else:
91-
resp = requests.put(uri, data=json.dumps(doc))
92-
print ("\tResponse:", setup_resp.status_code, setup_resp.json(),flush=True)
78+
print ('\t| Request: PUT',uri)
79+
try:
80+
if creds[0] and creds[1]:
81+
resp = requests.put(uri, data=json.dumps(doc), auth=creds)
82+
else:
83+
resp = requests.put(uri, data=json.dumps(doc))
84+
print ("\t| Response:", setup_resp.status_code, setup_resp.json(),flush=True)
85+
except requests.exceptions.ConnectionError:
86+
print ('\t| Connection failure. CouchDB not responding. Will retry.')
9387

9488
# Compare (json) objects - order does not matter. Credits to:
9589
# https://stackoverflow.com/a/25851972
@@ -115,13 +109,13 @@ def finish_cluster(names):
115109
creds = (os.getenv("COUCHDB_USER"), os.getenv("COUCHDB_PASSWORD"))
116110
print("== Get the cluster up and running ===")
117111
setup_resp=requests.post("http://127.0.0.1:5984/_cluster_setup", json={"action": "finish_cluster"}, auth=creds)
118-
print ('\tRequest: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}')
119-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
112+
print ('\t| Request: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}')
113+
print ("\t|\tResponse:", setup_resp.status_code, setup_resp.json())
120114
if (setup_resp.status_code == 201):
121-
print ("\tSweet! Just a final check for the logs...")
115+
print ("\t| Sweet! Just a final check for the logs...")
122116
setup_resp=requests.get("http://127.0.0.1:5984/_cluster_setup", auth=creds)
123-
print ('\tRequest: GET http://127.0.0.1:5984/_cluster_setup')
124-
print ("\t\tResponse:", setup_resp.status_code, setup_resp.json())
117+
print ('\t| Request: GET http://127.0.0.1:5984/_cluster_setup')
118+
print ("\t|\tResponse:", setup_resp.status_code, setup_resp.json())
125119
print("Time to relax!")
126120
else:
127121
print('Ouch! Failed the final step finalizing the cluster.')
@@ -162,30 +156,30 @@ def are_nodes_in_sync(names):
162156
if len(names) < 2:
163157
# Minimum 2 nodes to form cluster!
164158
not_ready = True
165-
print("\tNeed at least 2 DNS records to start with. Got ",len(names))
159+
print("\t| Need at least 2 DNS records to start with. Got ",len(names))
166160

167161
# Compare local and remote _mebership data. Make sure the set
168162
# of nodes match. This will ensure that the remote nodes
169163
# are fully primed with nodes data before progressing with
170164
# _cluster_setup
171165
if (remote_resp.status_code == 200) and (local_resp.status_code == 200):
172166
if ordered(local_resp.json()) == ordered(remote_resp.json()):
173-
print ("\tIn sync!")
167+
print ("\t| In sync!")
174168
else:
175169
not_ready = True
176170
# Rest is for logging only...
177171
records_in_local_but_not_in_remote = set(local_resp.json()['cluster_nodes']) - set(remote_resp.json()['cluster_nodes'])
178172
if records_in_local_but_not_in_remote:
179-
print ("\tCluster members in {0} not yet present in {1}: {2}".format(os.getenv("HOSTNAME"), name.split(".",1)[0], records_in_local_but_not_in_remote))
173+
print ("\t| Cluster members in {0} not yet present in {1}: {2}".format(os.getenv("HOSTNAME"), name.split(".",1)[0], records_in_local_but_not_in_remote))
180174
records_in_remote_but_not_in_local = set(remote_resp.json()['cluster_nodes']) - set(local_resp.json()['cluster_nodes'])
181175
if records_in_remote_but_not_in_local:
182-
print ("\tCluster members in {0} not yet present in {1}: {2}".format(name.split(".",1)[0], os.getenv("HOSTNAME"), records_in_remote_but_not_in_local))
176+
print ("\t| Cluster members in {0} not yet present in {1}: {2}".format(name.split(".",1)[0], os.getenv("HOSTNAME"), records_in_remote_but_not_in_local))
183177

184178
# Cover the case where local pod has 1 record only
185179
if len(local_resp.json()['cluster_nodes']) < 2:
186180
# Minimum 2 nodes to form cluster!
187181
not_ready = True
188-
print("\tNeed at least 2 cluster nodes in the _membership of pod",os.getenv("HOSTNAME"))
182+
print("\t| Need at least 2 cluster nodes in the _membership of pod",os.getenv("HOSTNAME"))
189183
else:
190184
not_ready = True
191185
return not not_ready

0 commit comments

Comments
 (0)