3
3
This script grabs the list of pod names behind the headless service
4
4
associated with our StatefulSet and feeds those as `couchdb@FQDN`
5
5
nodes to mem3.
6
+
7
+ As a final step - on just one of the nodes - the cluster finalize is triggered.
8
+ For that part to happen we need username and password.
6
9
"""
7
10
8
11
import json
@@ -54,24 +57,6 @@ def connect_the_dots(names):
54
57
if resp .status_code == 201 :
55
58
print ('Adding CouchDB cluster node' , name , "to this pod's CouchDB. Response code:" , resp .status_code ,flush = True )
56
59
57
- # Run action:enable_cluster on every CouchDB cluster node
58
- def enable_cluster (nr_of_peers ):
59
- creds = (os .getenv ("COUCHDB_USER" ), os .getenv ("COUCHDB_PASSWORD" ))
60
- if creds [0 ] and creds [1 ]:
61
- headers = {'Content-type' : 'application/json' }
62
- print ("=== Enabling cluster mode ===" )
63
- # http://docs.couchdb.org/en/stable/cluster/setup.html
64
- payload = {}
65
- payload ['action' ] = 'enable_cluster'
66
- payload ['bind_address' ] = '0.0.0.0'
67
- payload ['username' ] = creds [0 ]
68
- payload ['password' ] = creds [1 ]
69
- payload ['node_count' ] = nr_of_peers
70
- setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json .dumps (payload ), auth = creds , headers = headers )
71
- payload ['password' ] = "**masked**"
72
- print ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
73
- print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
74
-
75
60
# Compare (json) objects - order does not matter. Credits to:
76
61
# https://stackoverflow.com/a/25851972
77
62
def ordered (obj ):
@@ -82,7 +67,7 @@ def ordered(obj):
82
67
else :
83
68
return obj
84
69
85
- # Run action:finish_cluster on one and only one CouchDB cluster node
70
+ # Run action:finish_cluster on one ( and only one) CouchDB cluster node
86
71
def finish_cluster (names ):
87
72
# The HTTP POST to /_cluster_setup should be done to
88
73
# one (and only one) of the CouchDB cluster nodes.
@@ -94,52 +79,15 @@ def finish_cluster(names):
94
79
# on the "first" pod only with this hack:
95
80
if (os .getenv ("HOSTNAME" ).endswith ("-0" )):
96
81
creds = (os .getenv ("COUCHDB_USER" ), os .getenv ("COUCHDB_PASSWORD" ))
97
-
98
- headers = {'Content-type' : 'application/json' }
99
- print ("=== Adding nodes to CouchDB cluster via the “setup coordination node” ===" )
100
- for name in names :
101
- # Exclude "this" pod
102
- if (name .split ("." ,1 )[0 ] != os .getenv ("HOSTNAME" )):
103
- # action: enable_cluster
104
- payload = {}
105
- payload ['action' ] = 'enable_cluster'
106
- payload ['bind_address' ] = '0.0.0.0'
107
- payload ['username' ] = creds [0 ]
108
- payload ['password' ] = creds [1 ]
109
- payload ['port' ] = 5984
110
- payload ['node_count' ] = len (names )
111
- payload ['remote_node' ] = name
112
- payload ['remote_current_user' ] = creds [0 ]
113
- payload ['remote_current_password' ] = creds [1 ]
114
- setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json .dumps (payload ), auth = creds , headers = headers )
115
- payload ['password' ] = "**masked**"
116
- payload ['remote_current_password' ] = "**masked**"
117
- print ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
118
- print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
119
-
120
- # action: add_node
121
- payload = {}
122
- payload ['action' ] = 'add_node'
123
- payload ['username' ] = creds [0 ]
124
- payload ['password' ] = creds [1 ]
125
- payload ['port' ] = 5984
126
- payload ['host' ] = name
127
- setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json .dumps (payload ), auth = creds , headers = headers )
128
- payload ['password' ] = "**masked**"
129
- print ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
130
- print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
131
-
132
-
133
-
82
+ print ("== Get the cluster up and running ===" )
134
83
setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json = {"action" : "finish_cluster" }, auth = creds )
84
+ print ('\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}' )
85
+ print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
135
86
if (setup_resp .status_code == 201 ):
136
- print ("== CouchDB cluster setup done! ===" )
137
- print ('\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload {"action": "finish_cluster"}' )
138
- print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
87
+ print ("\t Sweet! Just a final check for the logs..." )
139
88
setup_resp = requests .get ("http://127.0.0.1:5984/_cluster_setup" , auth = creds )
140
89
print ('\t Request: GET http://127.0.0.1:5984/_cluster_setup' )
141
90
print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
142
-
143
91
print ("Time to relax!" )
144
92
else :
145
93
print ('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}' .format (setup_resp .status_code ))
@@ -157,7 +105,7 @@ def diff(first, second):
157
105
max_tries = 10
158
106
)
159
107
# Check if the _membership API on all (known) CouchDB nodes have the same values.
160
- # Returns true if sam . False in any other situation.
108
+ # Returns true if same . False in any other situation.
161
109
def are_nodes_in_sync (names ):
162
110
# Make sure that ALL (known) CouchDB cluster peers have been
163
111
# have the same _membership data.Use "this" nodes memebership as
@@ -207,6 +155,7 @@ def sleep_forever():
207
155
peer_names = discover_peers (construct_service_record ())
208
156
connect_the_dots (peer_names )
209
157
print ("Got the following peers' fqdm from DNS lookup:" ,peer_names ,flush = True )
158
+
210
159
# loop until all CouchDB nodes discovered
211
160
while not are_nodes_in_sync (peer_names ):
212
161
time .sleep (5 )
0 commit comments