Skip to content

Commit

Permalink
Add some beautiful scripts to initialize a CouchDB 2.x cluster automa…
Browse files Browse the repository at this point in the history
…gically.
  • Loading branch information
gesellix committed Mar 12, 2018
1 parent 91b2054 commit 73fae7b
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/compose/cluster-init/Dockerfile
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" ]
26 changes: 26 additions & 0 deletions examples/compose/cluster-init/await-nodes.sh
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
12 changes: 12 additions & 0 deletions examples/compose/cluster-init/docker-entrypoint.sh
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
30 changes: 30 additions & 0 deletions examples/compose/cluster-init/init-cluster.sh
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
79 changes: 79 additions & 0 deletions examples/compose/cluster-init/wait-for
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 "$@"
59 changes: 59 additions & 0 deletions examples/compose/docker-compose-cluster.yml
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"

0 comments on commit 73fae7b

Please sign in to comment.