Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # The MIT License (MIT)
- #
- # Copyright (c) 2014-2018 Greg Messner, greg@messners.com
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in all
- # copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- #
- # This is the service utility script for running Postgres in a docker container. It is intended
- # to be called from upstart or systemd service scripts, but can also be used from the command line
- # to start, stop, and check the status of a running Postgres container.
- #
- # Load the docker utility functions
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
- source $SCRIPT_DIR/docker-functions.sh
- #
- # Print the help information for the Postgres service container.
- #
- function help {
- echo
- echo " This script performs one or more actions on the Postgres service container."
- echo
- echo " Usage : $(basename $0) action ..."
- echo
- echo " Available actions"
- echo " ------------------------------------------------------------------------"
- echo " attach Attaches the current process to the running Postgres service,"
- echo " usually only used by actual OS services"
- echo " cleanup Cleans up (removes) the Postgres service container"
- echo " destroy Stops and removes the Postgres service container, and deletes"
- echo " the associated database data (docker volume)"
- echo " help Prints this help information and exits"
- echo " start Starts the Postgres service container. When starting the service"
- echo " the following environment variables must be set:"
- echo " POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD"
- echo " status Returns a one word status for the Postgres service"
- echo " stop Stops the Postgres service container"
- echo
- echo " NOTE: Multiple actions may be combined on a single command line, for example:"
- echo " $(basename $0) start attach"
- echo
- }
- #
- # Stops the service container if it is running and deletes the associated docker volume.
- #
- destroy() {
- # Make sure the container is stopped and removed
- df_kill_container ${POSTGRES_CONTAINER}
- # Remove the docker volume if it is present
- if docker volume inspect ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1 ; then
- echo -n "Removing ${POSTGRES_DATA_VOLUME} data volume..."
- docker volume rm --force ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1
- echo "done"
- fi
- }
- #
- # Launches the container and waits for it to be fully started. After calling 'start', you can
- # call the 'attach' method to attach a service script to the log output of the container.
- #
- start() {
- # Make sure the database name, username, and password is set, if not, abort
- ENV_ERRORS="false"
- if [ -z "$POSTGRES_DB" ]; then
- echo "POSTGRES_DB env variable is not set!" 1>&2
- ENV_ERRORS="true"
- fi
- if [ -z "$POSTGRES_USER" ]; then
- echo "POSTGRES_USER env variable is not set!" 1>&2
- ENV_ERRORS="true"
- fi
- if [ -z "$POSTGRES_PASSWORD" ]; then
- echo "POSTGRES_PASSWORD env variable is not set!" 1>&2
- ENV_ERRORS="true"
- fi
- if [ "$ENV_ERRORS" == "true" ]; then
- exit 1
- fi
- # If the data volume does not exist, create it
- if ! docker volume inspect ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1 ; then
- echo "Creating ${POSTGRES_DATA_VOLUME} data volume"
- docker volume create ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1
- fi
- # If the local postgres config file is set and exists, set the container up to use it
- LOCAL_POSTGRES_CONFIG=()
- if [[ ! -z $LOCAL_POSTGRES_CONFIG_FILE ]] && [[ -f $LOCAL_POSTGRES_CONFIG_FILE ]]; then
- echo "Setting up container to use local postgres configuration"
- LOCAL_POSTGRES_CONFIG=("-c" "config_file=$LOCAL_POSTGRES_CONFIG_FILE")
- fi
- # Make sure that docker does not have the service container present
- df_kill_container ${POSTGRES_CONTAINER}
- # Run the docker container
- CONTAINER_ID=$(docker run --name "${POSTGRES_CONTAINER}" \
- --detach --restart=no \
- --health-cmd="pg_isready -h localhost" \
- -p ${POSTGRES_PORT}:5432 \
- -e POSTGRES_DB=${POSTGRES_DB} \
- -e POSTGRES_USER=${POSTGRES_USER} \
- -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
- -v ${POSTGRES_DATA_VOLUME}:/var/lib/postgresql/data \
- ${POSTGRES_IMAGE_NAME}:${POSTGRES_IMAGE_VERSION} ${LOCAL_POSTGRES_CONFIG[*]})
- if [ $? -ne 0 ]; then
- echo "Error starting ${POSTGRES_CONTAINER}"
- exit 1
- fi
- # Wait a maximum of 60 seconds for the container to be fully started and healthy
- echo "Started ${POSTGRES_CONTAINER}, container ID=${CONTAINER_ID}"
- echo "Waiting for ${POSTGRES_CONTAINER} to become healthy"
- df_wait_for_container 60 ${POSTGRES_CONTAINER}
- }
- #############################################################################
- # Need at least a single argumet (action), if none are provided putput usage info and exit with error.
- if [ $# -lt 1 ]; then
- echo "Usage : $(basename $0) attach|cleanup|destroy|help|status|start|stop"
- exit 1
- fi
- if [ "$1" == "help" ]; then
- help
- exit 0
- fi
- # Default the env variables if they are not set.
- POSTGRES_IMAGE_NAME=${POSTGRES_IMAGE_NAME:-postgres}
- POSTGRES_IMAGE_VERSION=${POSTGRES_IMAGE_VERSION:-"9.6.8"}
- POSTGRES_CONTAINER=${POSTGRES_CONTAINER:-postgres-db}
- POSTGRES_DATA_VOLUME=${POSTGRES_DATA_VOLUME:-postgres-db-data}
- POSTGRES_PORT=${POSTGRES_PORT:-5432}
- #
- # Loop thru the arguments (actions). Some actions may be paired together, for example:
- #
- # start attach - Will start the container and attach the current process to it
- # stop cleanup - Will stop the container and then clean it up (remove it)
- #
- for ARG in "$@"
- do
- case "$ARG" in
- "attach")
- df_attach_container $POSTGRES_CONTAINER
- ;;
- "cleanup")
- df_cleanup_container $POSTGRES_CONTAINER
- ;;
- "destroy")
- destroy
- ;;
- "start")
- start
- ;;
- "status")
- df_container_status $POSTGRES_CONTAINER
- ;;
- "stop")
- df_stop_container $POSTGRES_CONTAINER
- ;;
- *)
- echo "'$1' is not a valid $(basename $0 '.sh') command, aborting!"
- exit 1
- ;;
- esac
- RESULTS=$?
- if [ $RESULTS -ne 0 ]; then
- exit $RESULTS
- fi
- done
- # vim: syntax=sh ts=4 sw=4 sts=4 sr noet
Add Comment
Please, Sign In to add comment