@@ -63,13 +63,16 @@ def ordered(obj):
63
63
else :
64
64
return obj
65
65
66
+ # Run action:finish_cluster on one and only one CouchDB cluster node
66
67
def finish_cluster (names ):
67
68
# The HTTP POST to /_cluster_setup should be done to
68
69
# 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
69
72
# Given that K8S has a standardized naming for the pods:
70
73
# 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:
73
76
print ('HOSTNAME={0}' .format (os .getenv ("HOSTNAME" )))
74
77
if (os .getenv ("HOSTNAME" ).endswith ("-0" )):
75
78
creds = (os .getenv ("COUCHDB_USER" ), os .getenv ("COUCHDB_PASSWORD" ))
@@ -88,8 +91,6 @@ def finish_cluster(names):
88
91
remote_membership_uri = "http://{0}:5984/_membership" .format (name )
89
92
if creds [0 ] and creds [1 ]:
90
93
remote_resp = requests .get (remote_membership_uri , auth = creds )
91
- else :
92
- remote_resp = requests .get (remote_membership_uri )
93
94
# Compare local and remote _mebership data. Make sure the set
94
95
# of nodes match. This will ensure that the remote nodes
95
96
# are fully primed with nodes data before progressing with
@@ -102,21 +103,37 @@ def finish_cluster(names):
102
103
time .sleep (5 )
103
104
if creds [0 ] and creds [1 ]:
104
105
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 ))
108
120
# At this point ALL peers have _nodes populated. Finish the cluster setup!
109
121
if creds [0 ] and creds [1 ]:
110
122
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" })
113
123
if (setup_resp .status_code == 201 ):
114
124
print ("CouchDB cluster setup done. Time to relax!" )
115
125
else :
116
126
print ('Ouch! Failed the final step: http://127.0.0.1:5984/_cluster_setup returned {0}' .format (setup_resp .status_code ))
117
127
else :
118
128
print ("This pod is intentionally skipping the call to http://127.0.0.1:5984/_cluster_setup" )
119
129
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 )
120
137
121
138
def sleep_forever ():
122
139
while True :
@@ -127,5 +144,9 @@ def sleep_forever():
127
144
print ("Got the following peers' fqdm from DNS lookup:" ,peer_names ,flush = True )
128
145
connect_the_dots (peer_names )
129
146
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' )
131
152
sleep_forever ()
0 commit comments