-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some beautiful scripts to initialize a CouchDB 2.x cluster automa…
…gically.
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM alpine:edge | ||
|
||
RUN apk add --no-cache curl | ||
|
||
COPY wait-for /wait-for | ||
COPY await-nodes.sh /await-nodes.sh | ||
COPY init-cluster.sh /init-cluster.sh | ||
COPY docker-entrypoint.sh /docker-entrypoint.sh | ||
|
||
CMD [ "/docker-entrypoint.sh" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env sh | ||
|
||
available_nodes=0 | ||
|
||
./wait-for 172.16.238.11:5984 -t 10 | ||
result=$? | ||
if [ ${result} -eq 0 ] ; then | ||
available_nodes=$((available_nodes+1)) | ||
fi | ||
|
||
./wait-for 172.16.238.12:5984 -t 10 | ||
result=$? | ||
if [ ${result} -eq 0 ] ; then | ||
available_nodes=$((available_nodes+1)) | ||
fi | ||
|
||
./wait-for 172.16.238.13:5984 -t 10 | ||
result=$? | ||
if [ ${result} -eq 0 ] ; then | ||
available_nodes=$((available_nodes+1)) | ||
fi | ||
|
||
if [ ${available_nodes} -eq 3 ] | ||
then echo "alright" | ||
else echo "missing node, got $available_nodes nodes" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env sh | ||
|
||
#cat << EOB > .couchdb-env | ||
#COUCHDB.USERNAME=admin | ||
#COUCHDB.PASSWORD=password | ||
#COUCHDB_USER=admin | ||
#COUCHDB_PASSWORD=password | ||
#EOB | ||
#docker-compose -f docker-compose-cluster.yml -p db up | ||
|
||
/await-nodes.sh | ||
/init-cluster.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env sh | ||
|
||
# http://docs.couchdb.org/en/stable/cluster/setup.html | ||
# http://docs.couchdb.org/en/latest/cluster/setup.html | ||
|
||
curl -X PUT http://172.16.238.11:5984/_users | ||
curl -X PUT http://172.16.238.11:5984/_replicator | ||
curl -X PUT http://172.16.238.12:5984/_users | ||
curl -X PUT http://172.16.238.12:5984/_replicator | ||
curl -X PUT http://172.16.238.13:5984/_users | ||
curl -X PUT http://172.16.238.13:5984/_replicator | ||
|
||
#USERNAME=$(awk 'BEGIN {print ENVIRON["COUCHDB.USERNAME"]}') | ||
#PASSWORD=$(awk 'BEGIN {print ENVIRON["COUCHDB.PASSWORD"]}') | ||
|
||
curl -X PUT http://172.16.238.11:5984/_node/[email protected]/_config/admins/root -d '"a-secret"' | ||
curl -X PUT http://172.16.238.12:5984/_node/[email protected]/_config/admins/root -d '"a-secret"' | ||
curl -X PUT http://172.16.238.13:5984/_node/[email protected]/_config/admins/root -d '"a-secret"' | ||
|
||
#curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "content-type:application/json" -d '{"action":"enable_cluster","username":"root","password":"a-secret","bind_address":"0.0.0.0","node_count":3}' | ||
|
||
curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "content-type:application/json" -d '{"remote_node":"172.16.238.12","port":"5984","action":"enable_cluster","username":"root","password":"a-secret","bind_address":"0.0.0.0","node_count":3}' | ||
curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "content-type:application/json" -d '{"remote_node":"172.16.238.13","port":"5984","action":"enable_cluster","username":"root","password":"a-secret","bind_address":"0.0.0.0","node_count":3}' | ||
|
||
curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "content-type:application/json" -d '{"action":"add_node","host":"172.16.238.12","port":"5984","username":"root","password":"a-secret"}' | ||
curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "content-type:application/json" -d '{"action":"add_node","host":"172.16.238.13","port":"5984","username":"root","password":"a-secret"}' | ||
|
||
curl --user root:a-secret -X POST http://172.16.238.11:5984/_cluster_setup -H "Content-Type: application/json" -d '{"action": "finish_cluster"}' | ||
|
||
curl --user root:a-secret -X GET http://172.16.238.11:5984/_membership |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/bin/sh | ||
|
||
TIMEOUT=15 | ||
QUIET=0 | ||
|
||
echoerr() { | ||
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi | ||
} | ||
|
||
usage() { | ||
exitcode="$1" | ||
cat << USAGE >&2 | ||
Usage: | ||
$cmdname host:port [-t timeout] [-- command args] | ||
-q | --quiet Do not output any status messages | ||
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout | ||
-- COMMAND ARGS Execute command with args after the test finishes | ||
USAGE | ||
exit "$exitcode" | ||
} | ||
|
||
wait_for() { | ||
for i in `seq $TIMEOUT` ; do | ||
nc -z "$HOST" "$PORT" > /dev/null 2>&1 | ||
|
||
result=$? | ||
if [ $result -eq 0 ] ; then | ||
if [ $# -gt 0 ] ; then | ||
exec "$@" | ||
fi | ||
exit 0 | ||
fi | ||
sleep 1 | ||
done | ||
echo "Operation timed out" >&2 | ||
exit 1 | ||
} | ||
|
||
while [ $# -gt 0 ] | ||
do | ||
case "$1" in | ||
*:* ) | ||
HOST=$(printf "%s\n" "$1"| cut -d : -f 1) | ||
PORT=$(printf "%s\n" "$1"| cut -d : -f 2) | ||
shift 1 | ||
;; | ||
-q | --quiet) | ||
QUIET=1 | ||
shift 1 | ||
;; | ||
-t) | ||
TIMEOUT="$2" | ||
if [ "$TIMEOUT" = "" ]; then break; fi | ||
shift 2 | ||
;; | ||
--timeout=*) | ||
TIMEOUT="${1#*=}" | ||
shift 1 | ||
;; | ||
--) | ||
shift | ||
break | ||
;; | ||
--help) | ||
usage 0 | ||
;; | ||
*) | ||
echoerr "Unknown argument: $1" | ||
usage 1 | ||
;; | ||
esac | ||
done | ||
|
||
if [ "$HOST" = "" -o "$PORT" = "" ]; then | ||
echoerr "Error: you need to provide a host and port to test." | ||
usage 2 | ||
fi | ||
|
||
wait_for "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
version: "2.3" | ||
|
||
services: | ||
couchdb1: | ||
image: apache/couchdb:2.1.1 | ||
command: -name [email protected] -setcookie thecookie | ||
env_file: .couchdb-env | ||
restart: always | ||
networks: | ||
couchdb-cluster: | ||
ipv4_address: 172.16.238.11 | ||
ports: | ||
- "15984:5984" | ||
|
||
couchdb2: | ||
image: apache/couchdb:2.1.1 | ||
command: -name [email protected] -setcookie thecookie | ||
env_file: .couchdb-env | ||
restart: always | ||
networks: | ||
couchdb-cluster: | ||
ipv4_address: 172.16.238.12 | ||
ports: | ||
- "25984:5984" | ||
|
||
couchdb3: | ||
image: apache/couchdb:2.1.1 | ||
command: -name [email protected] -setcookie thecookie | ||
env_file: .couchdb-env | ||
restart: always | ||
networks: | ||
couchdb-cluster: | ||
ipv4_address: 172.16.238.13 | ||
ports: | ||
- "35984:5984" | ||
|
||
couchdbstats: | ||
image: gesellix/couchdb-prometheus-exporter | ||
command: -couchdb.uri=http://couchdb1:5984 -logtostderr | ||
env_file: .couchdb-env | ||
networks: | ||
couchdb-cluster: | ||
ipv4_address: 172.16.238.10 | ||
ports: | ||
- "9984:9984" | ||
|
||
cluster-init: | ||
image: cluster-init | ||
networks: | ||
couchdb-cluster: | ||
ipv4_address: 172.16.238.99 | ||
|
||
networks: | ||
couchdb-cluster: | ||
driver: overlay | ||
ipam: | ||
driver: default | ||
config: | ||
- subnet: "172.16.238.0/24" |