Created
November 13, 2018 16:08
-
-
Save ccampo133/95a0c6aaaaf3c0493a41641b17449b06 to your computer and use it in GitHub Desktop.
Utility scripts for running Couchbase in a container
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
#!/usr/bin/env bash | |
stopcouch | |
rm -rf ~/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
#!/usr/bin/env bash | |
# Default values | |
version="4.5.1" | |
username="Administrator" | |
password="password" | |
volume_path="$HOME/couchbase" | |
cpus="0.000" # this is max CPU usage. a good value is probably 0.25 (although this is slow) | |
config_url="http://localhost:8091" | |
no_buckets=false | |
db_path="/opt/couchbase/var" | |
usage() { | |
echo "Start and configure a Couchbase server locally, using Docker, with" | |
echo "minimal system resources. NOTE: for DEV ENVIRONMENTS ONLY!" | |
echo "" | |
echo "To completely stop/destroy, run:" | |
echo " docker stop couch && docker rm couch && rm -rf $volume_path" | |
echo "" | |
echo "Requirements:" | |
echo " Docker (https://www.docker.com/)" | |
echo " jq (https://stedolan.github.io/jq/)" | |
echo "" | |
echo "NOTE: default values are shown below where applicable" | |
echo "" | |
echo "startcouch" | |
echo " -h --help" | |
echo " --no_buckets" | |
echo " --version=$version" | |
echo " --username=$username" | |
echo " --password=$password" | |
echo " --volume_path=$volume_path" | |
echo " --cpus=$cpus" | |
echo "" | |
} | |
while [ "$1" != "" ]; do | |
param=$(echo "$1" | awk -F= '{print $1}') | |
value=$(echo "$1" | awk -F= '{print $2}') | |
case ${param} in | |
-h | --help) | |
usage | |
exit | |
;; | |
--version) | |
version=${value} | |
;; | |
--username) | |
username=${value} | |
;; | |
--password) | |
password=${value} | |
;; | |
--volume_path) | |
volume_path=${value} | |
;; | |
--no_buckets) | |
no_buckets=true | |
;; | |
--cpus) | |
cpus=${value} | |
;; | |
*) | |
echo "ERROR: unknown parameter \"$param\"" | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
if ! [ -x "$(command -v docker)" ]; then | |
echo 'Error: docker is not installed. Visit https://www.docker.com/' >&2 | |
exit 1 | |
fi | |
if ! [ -x "$(command -v jq)" ]; then | |
echo 'Error: jq is not installed. Visit https://stedolan.github.io/jq/' >&2 | |
exit 1 | |
fi | |
# Create volume directory if it doesn't yet exist | |
mkdir -p "${volume_path}" | |
# Start Couchbase if not already running | |
docker ps -a | grep couchbase > /dev/null 2>&1 | |
if [ $? -eq 1 ]; then | |
echo "Starting Couchbase Community Edition v$version..." | |
docker run -d --name couch \ | |
-p 8091-8094:8091-8094 \ | |
-p 11210:11210 \ | |
-v "$volume_path:/opt/couchbase/var" \ | |
--cpus="$cpus" \ | |
couchbase:community-"${version}" > /dev/null | |
else | |
echo "It looks like Couchbase is already running. Continuing with configuration..." | |
fi | |
# Wait for Couchbase to come up before configuring it. | |
# NOTE: curl times out after 2 minutes regardless of the --connect-timeout setting. | |
wait_for_couch() { | |
# If we get lucky, Couchbase will be up before the first curl is issued. | |
sleep 5 | |
response="000" | |
while true; do | |
response=$(curl --write-out %{http_code} -s -o /dev/null --connect-timeout 30 ${config_url}/pools ) | |
if [ "$response" = "200" ]; then | |
settings=$(curl -s ${config_url}/pools | jq .settings) | |
if [ "$settings" = "[]" ]; then | |
echo "Couchbase is up and ready to be configured." | |
break | |
fi | |
echo "It looks like Couchbase is already configured. Exiting." | |
exit 0 | |
fi | |
echo "Waiting for Couchbase to come up..." | |
sleep 5 | |
done | |
} | |
create_bucket() { | |
local name=$1 | |
# TODO: make all of this configurable via args -ccampo 2018-04-13 | |
curl -s -o /dev/null -X POST $config_url/pools/default/buckets \ | |
-H 'Content-Type: application/x-www-form-urlencoded' \ | |
-u "$username:$password" \ | |
--data-urlencode "name=$name" \ | |
--data-urlencode "ramQuotaMB=100" \ | |
--data-urlencode "conflictResolutionType=seqno" \ | |
--data-urlencode "threadsNumber=3" \ | |
--data-urlencode "authType=sasl" \ | |
--data-urlencode "bucketType=membase" \ | |
--data-urlencode "saslPassword=" \ | |
--data-urlencode "replicaIndex=0" \ | |
--data-urlencode "replicaNumber=1" \ | |
--data-urlencode "flushEnabled=1" \ | |
--data-urlencode "evictionPolicy=valueOnly" \ | |
--data-urlencode "autoCompactionDefined=false" | |
} | |
configure_couch() { | |
# Initialize node | |
curl -X POST "$config_url/nodes/self/controller/settings" \ | |
-H 'Content-Type: application/x-www-form-urlencoded' \ | |
--data-urlencode "path=$db_path/lib/couchbase/data" \ | |
--data-urlencode "index_path=$db_path/lib/couchbase/data" | |
# Setup hostname | |
curl -s -o /dev/null -X POST "$config_url/node/controller/rename" \ | |
-d 'hostname=127.0.0.1' | |
# Setup indexes | |
# TODO: is this necessary? -ccampo 2018-04-13 | |
curl -s -o /dev/null -X POST "$config_url/settings/indexes" \ | |
-d 'storageMode=forestdb' | |
# Setup RAM quotas | |
# TODO: make this configurable -ccampo 2018-04-13 | |
curl -s -o /dev/null -X POST "$config_url/pools/default" \ | |
-d 'indexMemoryQuota=512&memoryQuota=512' | |
# Setup services | |
curl -s -o /dev/null -X POST "$config_url/node/controller/setupServices" \ | |
-d 'services=kv%2Cn1ql%2Cindex' | |
# Opt out of phone-home | |
curl -s -o /dev/null -X POST "$config_url/settings/stats" \ | |
-d 'sendStats=false' | |
# Setup Administrator username and password | |
curl -s -o /dev/null -X POST "$config_url/settings/web" \ | |
-d "username=${username}&password=${password}&port=SAME" | |
if [ "$no_buckets" = false ]; then | |
# TODO: is it necessary to create the default bucket? -ccampo 2018-04-13 | |
create_bucket default | |
# Create all required buckets | |
#create_bucket commerce | |
#create_bucket read | |
#create_bucket write | |
#create_bucket localization | |
# TODO: customize this to allow arbitrary buckets -ccampo 2018-04-13 | |
fi | |
} | |
wait_for_couch | |
echo "Configuring Couchbase..." | |
configure_couch | |
echo "Couchbase is configured and ready to use at $config_url" |
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
#!/usr/bin/env bash | |
docker stop couch && docker rm couch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment