Skip to content

Instantly share code, notes, and snippets.

@ericlowry
Last active August 11, 2023 05:41
Show Gist options
  • Save ericlowry/e89a961d8a501e98b1f22d03fa33e9cf to your computer and use it in GitHub Desktop.
Save ericlowry/e89a961d8a501e98b1f22d03fa33e9cf to your computer and use it in GitHub Desktop.
Create a couchdb container for testing
#!/usr/bin/env bash
#
# couchdb.sh - manage the docker development environment for couchdb
#
COMMAND="$1"
CONTAINER="$2"
IMAGE="apache/couchdb:latest"
VOLUME="${CONTAINER}-data"
PORT="5984"
USER="admin"
PASS="password" # change me!
NODE_NAME="nonode@nohost"
function usage {
echo -e "\nUsage:\n"
echo -e " $0 ps list couchdb containers"
echo -e " $0 make [name] make the couchdb container"
echo -e " $0 init [name] initialize the couchdb container"
echo -e " $0 drop [name] drop the couchdb container"
echo -e " $0 clean [name] drop the couchdb container and delete data files"
echo -e " $0 start [name] start the couchdb container"
echo -e " $0 stop [name] stop the couchdb container"
echo -e ""
}
if [ "$CONTAINER" = "" ]; then
if [ "$COMMAND" != "ps" ]; then
usage
exit
fi
fi
if [ "$COMMAND" = "ps" ]; then
docker ps -a --filter "label=simple-couchdb"
elif [ "$COMMAND" = "make" ]; then
echo -e "creating docker container ${CONTAINER} from image ${IMAGE}"
docker pull ${IMAGE}
docker volume create ${VOLUME}
docker run -d \
--publish ${PORT}:5984 \
--volume ${VOLUME}:/opt/couchdb/data \
--name ${CONTAINER} \
--env COUCHDB_USER=${USER} \
--env COUCHDB_PASSWORD=${PASS} \
--label simple-couchdb \
${IMAGE}
elif [ "$COMMAND" = "init" ]; then
echo -e "\ninitializing couchdb instance ${CONTAINER}"
curl -H 'Content-Type: application/json' \
-X POST http://${USER}:${PASS}@localhost:${PORT}/_cluster_setup \
--data-binary @- <<EOF
{
"action":"enable_single_node",
"singlenode":true,
"bind_address":"0.0.0.0",
"port":5984,
"username":"${USER}",
"password":"${PASS}"
}
EOF
function setting {
echo
echo "setting: $1 $2 \"$3\""
echo -n " prev: "
curl -H 'Content-Type: application/json' \
-X PUT http://${USER}:${PASS}@localhost:${PORT}/_node/${NODE_NAME}/_config/$1/$2 \
-d "\"$3\""
}
setting uuids algorithm "random"
setting cors credentials "true"
setting cors headers "accept, authorization, content-type, origin, referer"
setting cors methods "GET, PUT, POST, HEAD, DELETE"
setting cors origins "http://localhost:3000,http://localhost:3001"
setting httpd enable_cors "true"
# Set session timeout to 8 hours (default is 10 mins)
setting couch_httpd_auth timeout "28800"
echo
echo
elif [ "$COMMAND" = "drop" ]; then
echo -e "dropping docker container ${CONTAINER}"
docker rm --force ${CONTAINER}
elif [ "$COMMAND" = "clean" ]; then
echo -e "dropping docker container ${CONTAINER}"
docker rm --force ${CONTAINER}
echo -e "deleting couchdb data files at ${VOLUME}"
docker volume rm ${VOLUME}
elif [ "$COMMAND" = "start" ]; then
echo -e "starting couchdb docker container ${CONTAINER}"
docker start ${CONTAINER}
elif [ "$COMMAND" = "stop" ]; then
echo -e "stopping couchdb docker container ${CONTAINER}"
docker stop ${CONTAINER}
else
usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment