@@ -69,7 +69,7 @@ def enable_cluster(nr_of_peers):
69
69
setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json .dumps (payload ), auth = creds , headers = headers )
70
70
payload ['password' ] = "**masked**"
71
71
print ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
72
- print ("\t Response:" , setup_resp .status_code , setup_resp .json ())
72
+ print ("\t \ t Response:" , setup_resp .status_code , setup_resp .json ())
73
73
74
74
# Compare (json) objects - order does not matter. Credits to:
75
75
# https://stackoverflow.com/a/25851972
@@ -93,6 +93,41 @@ def finish_cluster(names):
93
93
# on the "first" pod only with this hack:
94
94
if (os .getenv ("HOSTNAME" ).endswith ("-0" )):
95
95
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 ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
117
+ print ("\t \t Response:" , 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 ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
129
+ print ("\t \t Response:" , setup_resp .status_code , setup_resp .json ())
130
+
96
131
# Make sure that ALL CouchDB cluster peers have been
97
132
# primed with _nodes data before /_cluster_setup
98
133
# Use the _membership of "this" pod's CouchDB as reference
@@ -121,65 +156,32 @@ def finish_cluster(names):
121
156
if creds [0 ] and creds [1 ]:
122
157
remote_resp = requests .get (remote_membership_uri , auth = creds )
123
158
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 ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
145
- print ("\t Response:" , 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 ("\t Request: POST http://127.0.0.1:5984/_cluster_setup , payload:" ,json .dumps (payload ))
157
- print ("\t Response:" , setup_resp .status_code , setup_resp .json ())
159
+
158
160
159
161
print ('CouchDB cluster peer {} added to "setup coordination node"' .format (name ))
160
162
# At this point ALL peers have _nodes populated. Finish the cluster setup!
161
163
setup_resp = requests .post ("http://127.0.0.1:5984/_cluster_setup" , json = {"action" : "finish_cluster" }, auth = creds )
162
164
if (setup_resp .status_code == 201 ):
163
165
print ("== CouchDB cluster setup done! ===" )
164
166
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." )
166
168
print ("\t Request: GET http://127.0.0.1:5984/_cluster_setup" )
167
-
169
+ print ( " \t \t Response:" , setup_resp . status_code , setup_resp . json ())
168
170
print ("== Creating default databases ===" )
169
171
170
172
setup_resp = requests .put ("http://127.0.0.1:5984/_users" , auth = creds , headers = headers )
171
173
print ("\t Request: PUT http://127.0.0.1:5984/_users" )
172
- print ("\t Response:" , setup_resp .status_code , setup_resp .json ())
174
+ print ("\t \ t Response:" , setup_resp .status_code , setup_resp .json ())
173
175
174
176
setup_resp = requests .put ("http://127.0.0.1:5984/_replicator" , auth = creds , headers = headers )
175
177
print ("\t Request: PUT http://127.0.0.1:5984/_replicator" )
176
- print ("\t Response:" , setup_resp .status_code , setup_resp .json ())
178
+ print ("\t \ t Response:" , setup_resp .status_code , setup_resp .json ())
177
179
178
180
setup_resp = requests .put ("http://127.0.0.1:5984/_global_changes" , auth = creds , headers = headers )
179
181
print ("\t Request: PUT http://127.0.0.1:5984/_global_changes" )
180
- print ("\t Response:" , setup_resp .status_code , setup_resp .json ())
182
+ print ("\t \ t Response:" , setup_resp .status_code , setup_resp .json ())
181
183
182
- print ( " \t Response:" , setup_resp . status_code , setup_resp . json () )
184
+ print ( "Time to relax!" )
183
185
else :
184
186
print ('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}' .format (setup_resp .status_code ))
185
187
else :
0 commit comments