Last active
July 7, 2022 14:42
-
-
Save HodGreeley/fa88c74baf55115ef83135d4d069e796 to your computer and use it in GitHub Desktop.
Docker Scripts to Simplify Setup with Couchbase
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
#!/bin/bash | |
script_home="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" | |
export DOCKER=docker | |
source ${script_home}/server.env | |
source ${script_home}/app.env | |
${script_home}/network start | |
${script_home}/server < ${script_home}/${COUCHBASE_NODE_NAME}-cluster | |
# Create query indexes | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" cbq --script="CREATE INDEX \`resource-idx\` ON \`health\`(\`resourceType\`,\`id\`);" \ | |
-u "$COUCHBASE_RBAC_USERNAME" -p "$COUCHBASE_RBAC_PASSWORD" -q | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" cbq --script="CREATE INDEX \`observation-idx\` ON \`health\`((\`subject\`.\`reference\`),\`issued\` DESC,(\`valueQuantity\`.\`value\`))" \ | |
-u "$COUCHBASE_RBAC_USERNAME" -p "$COUCHBASE_RBAC_PASSWORD" -q | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" cbq --script="CREATE INDEX \`location-idx\` ON \`health\`(type.coding[0].code) WHERE resourceType = 'Location';" \ | |
-u "$COUCHBASE_RBAC_USERNAME" -p "$COUCHBASE_RBAC_PASSWORD" -q | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" mkdir /opt/couchbase/var/lib/couchbase/n1qlcerts | |
"$DOCKER" cp ${script_home}/curl_whitelist.json "${COUCHBASE_NODE_NAME}_0":/opt/couchbase/var/lib/couchbase/n1qlcerts/ | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" chown couchbase:couchbase /opt/couchbase/var/lib/couchbase/n1qlcerts/curl_whitelist.json | |
"$DOCKER" cp ${script_home}/fts-index.json "${COUCHBASE_NODE_NAME}_0":/tmp/ | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" curl -T /tmp/fts-index.json http://"$COUCHBASE_RBAC_USERNAME":"$COUCHBASE_RBAC_PASSWORD"@localhost:8094/api/index/diagnosis | |
"$DOCKER" cp ${script_home}/augment-data.json "${COUCHBASE_NODE_NAME}_0":/tmp/ | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" cbimport json -c couchbase://127.0.0.1:8091 -d file:///tmp/augment-data.json -g '%id%' -f lines \ | |
-b ${COUCHBASE_BUCKET} -u "$COUCHBASE_RBAC_USERNAME" -p "$COUCHBASE_RBAC_PASSWORD" | |
${script_home}/sync-gateway create | |
${script_home}/sync-gateway start | |
${script_home}/app |
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
#!/bin/bash | |
# This script intends to simplify launching, configuring and joining a set of | |
# Couchbase Server instances running in Docker containers on a single physical host | |
# into a cluster. | |
# Configuration happens via environment variables taken from the runtime environment, | |
# redirected input, or provided on the command line. | |
Usage() { | |
echo "Usage: $0 [VAR=value] ... [< file]" | |
} | |
# Read configuration from stdin when redirected (e.g. $0 < config) | |
[[ ! -t 0 ]] && source /dev/stdin | |
# Override configuration based on supplied arguments | |
until [ -z "$1" ] | |
do | |
[[ "$1" =~ ^[^=]+=[^=]+$ ]] || { echo Malformed argument "$1"; Usage; exit 1; } | |
eval "$1" || { echo Failed processing argument "$1"; Usage; exit 1; } | |
shift | |
done | |
# Use supplied parameters or try for sensible defaults | |
: ${DOCKER:=docker} | |
: ${COUCHBASE_NETWORK:=cbnetwork} | |
: ${COUCHBASE_NODE_NAME:=cbserver} | |
: ${COUCHBASE_NODE_COUNT:=1} | |
: ${COUCHBASE_CLUSTER_NAME:=cluster} | |
: ${COUCHBASE_BUCKET:=default} | |
: ${COUCHBASE_ADMINISTRATOR_USERNAME:?Please supply an administrator username} | |
: ${COUCHBASE_ADMINISTRATOR_PASSWORD:?Please supply an administrator password} | |
: ${COUCHBASE_RBAC_USERNAME:?Please supply an RBAC username} | |
: ${COUCHBASE_RBAC_PASSWORD:?Please supply an RBAC password} | |
: ${COUCHBASE_RBAC_NAME:=} | |
: ${COUCHBASE_RBAC_ROLES:="bucket_admin[*]"} | |
: ${COUCHBASE_SERVICES:="data,index,query,fts"} | |
: ${COUCHBASE_SERVER_PORTS:="8091-8094:8091-8094::11210:11210"} | |
cluster_url="couchbase://127.0.0.1" | |
read -r -d '' ports_script << EOF || true | |
{ | |
split(\$1, maps, /::/) | |
for (map in maps) { | |
split(maps[map], ranges, /:/) | |
count = split(ranges[1], ports, "-") | |
for (port in ports) { | |
ports[port] += offset | |
} | |
ranges[1] = ports[1] | |
if (count > 1) ranges[1] = ports[1] "-" ports[2] | |
printf "-p " ranges[1] ":" ranges[2] " " | |
} | |
} | |
EOF | |
for ((node = 0; node < $COUCHBASE_NODE_COUNT; ++node)) | |
do | |
echo "Starting node ${COUCHBASE_NODE_NAME}_${node}" | |
let offset=${node}*1000 || true | |
ports=$(awk -v offset=$offset "$ports_script" <<< "${COUCHBASE_SERVER_PORTS}") | |
"$DOCKER" run -d --name "${COUCHBASE_NODE_NAME}_${node}" --network "$COUCHBASE_NETWORK" $ports couchbase | |
done | |
sleep 15 | |
# Setup initial cluster/initialize node | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" couchbase-cli cluster-init --cluster ${cluster_url} --cluster-name "$COUCHBASE_CLUSTER_NAME" \ | |
--cluster-username "$COUCHBASE_ADMINISTRATOR_USERNAME" --cluster-password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--services ${COUCHBASE_SERVICES} --cluster-ramsize 256 \ | |
--cluster-index-ramsize 256 --cluster-fts-ramsize 256 --index-storage-setting default | |
# Setup Bucket | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" couchbase-cli bucket-create --cluster ${cluster_url} \ | |
--username "$COUCHBASE_ADMINISTRATOR_USERNAME" --password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--bucket "$COUCHBASE_BUCKET" --bucket-type couchbase --bucket-ramsize 256 | |
# Setup RBAC user using CLI | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" couchbase-cli user-manage --cluster ${cluster_url} \ | |
--username "$COUCHBASE_ADMINISTRATOR_USERNAME" --password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--set --rbac-username "$COUCHBASE_RBAC_USERNAME" --rbac-password "$COUCHBASE_RBAC_PASSWORD" \ | |
--rbac-name "$COUCHBASE_RBAC_NAME" --roles "$COUCHBASE_RBAC_ROLES" --auth-domain local | |
# Add nodes | |
docker_ip() { | |
"$DOCKER" inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@" | |
} | |
for ((node = 1; node < $COUCHBASE_NODE_COUNT; ++node)) | |
do | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_${node}" couchbase-cli server-add \ | |
--cluster $(docker_ip "${COUCHBASE_NODE_NAME}_0"):8091 \ | |
--username "$COUCHBASE_ADMINISTRATOR_USERNAME" --password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--server-add $(docker_ip "${COUCHBASE_NODE_NAME}_${node}"):8091 \ | |
--server-add-username "$COUCHBASE_ADMINISTRATOR_USERNAME" --server-add-password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--services "$COUCHBASE_SERVICES" | |
done | |
# Rebalance (needed to fully enable added nodes) | |
"$DOCKER" exec "${COUCHBASE_NODE_NAME}_0" couchbase-cli rebalance --cluster ${cluster_url} \ | |
--username "$COUCHBASE_ADMINISTRATOR_USERNAME" --password "$COUCHBASE_ADMINISTRATOR_PASSWORD" \ | |
--no-wait |
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
#!/bin/bash | |
# Configuration happens via environment variables taken from the runtime environment, | |
# redirected input, or provided on the command line. | |
Usage() { | |
echo "Usage: $0 {create|start|stop|remove} [VAR=value] ... [< file]" | |
} | |
CMD="$1" | |
shift | |
# Read configuration from stdin when redirected (e.g. $0 < config) | |
[[ ! -t 0 ]] && source /dev/stdin | |
# Override configuration based on supplied arguments | |
until [ -z "$1" ] | |
do | |
[[ "$1" =~ ^[^=]+=[^=]+$ ]] || { echo Malformed argument "$1"; Usage; exit 1; } | |
eval "$1" || { echo Failed processing argument "$1"; Usage; exit 1; } | |
shift | |
done | |
# Use supplied parameters or try for sensible defaults | |
: ${DOCKER:=docker} | |
: ${COUCHBASE_NETWORK:=cbnetwork} | |
: ${SYNC_GATEWAY:=sync_gateway} | |
: ${SYNC_GATEWAY_CONFIG:=$(pwd)/sync-gateway-config.json} | |
: ${SYNC_GATEWAY_PORTS:="4984-4985:4984-4985"} | |
: ${SYNC_GATEWAY_ADMIN_PORT:=":4985"} | |
: ${COUCHBASE_NODE_NAME:=cbserver} | |
docker_ip() { | |
"$DOCKER" inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@" | |
} | |
case "$CMD" in | |
create) | |
echo "Creating Sync Gateway container" | |
server_url=$(docker_ip "${COUCHBASE_NODE_NAME}_0"):8091 | |
export sg_config=$(<${SYNC_GATEWAY_CONFIG}) | |
sg_config=${sg_config//\$\{COUCHBASE_SERVER_URL\}/${server_url}} | |
SYNC_GATEWAY_PORTS=${SYNC_GATEWAY_PORTS//::/ -p } | |
"$DOCKER" create --name "$SYNC_GATEWAY" --network "$COUCHBASE_NETWORK" -p "$SYNC_GATEWAY_PORTS" \ | |
--entrypoint /bin/bash -e sg_config \ | |
couchbase/sync-gateway -c "sync_gateway -adminInterface ${SYNC_GATEWAY_ADMIN_PORT} <(echo \$sg_config)" | |
;; | |
start) | |
"$DOCKER" start "$SYNC_GATEWAY" | |
;; | |
stop) | |
echo "Stopping Sync Gateway container $SYNC_GATEWAY" | |
"$DOCKER" stop "$SYNC_GATEWAY" | |
;; | |
remove) | |
echo "Removing Sync Gateway container $SYNC_GATEWAY" | |
"$DOCKER" stop "$SYNC_GATEWAY" | |
"$DOCKER" rm "$SYNC_GATEWAY" | |
;; | |
*) | |
Usage | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If network does not exist: