1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
set -euo pipefail
###
### Start a PostgreSQL docker container with the proper extensions installed
### for the full test suite.
###
### To kill an already running container, run the same command again with the
### `--kill` flag.
###
### Usage:
###
### ./postgres-docker [-h|--help] [-p|--port PORT] [-k|--kill]
###
### Options:
###
### -h, --help print (this) help and exit
###
### -p, --port PORT specify the host port to which PostgreSQL will be bound
### (defaults to 5432)
###
### -k, --kill given the container is already running, kill it
###
function help {
# Print this file's contents, but only the lines that start
# with `###` (documentation lines, above).
sed -rn 's/^### ?//;T;p' "$0"
}
function HAS {
# Check if the system has a certain program ($1) installed.
# Output is silenced, but the function returns the result of
# the `command` statement.
command -v $1 > /dev/null 2>&1
return $?
}
# Script variables
PROJECT_ROOT="$(dirname $(readlink -f "$0"))" # Same level as this file
# Dependency variables (e.g. PYTHON3_CLI=python3)
DOCKER=${DOCKER_CLI:-docker}
DEPS="$DOCKER"
# Arg defaults
KILL=false
CONTAINER_NAME=model-bakery-postgres
PORT=${PGPORT:-5432}
# Argument parsing using "getopt"
OPTIONS=h,k,p
LONGOPTS=help,kill,port
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
PARSE_EXIT=$?
if [[ $PARSE_EXIT -ne 0 ]]; then
exit $PARSE_EXIT
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-p|--port)
PGPORT=$2
shift
;;
-k|--kill)
KILL=true
shift
;;
-h|--help)
help
shift
exit 0
;;
--)
shift
break
;;
*)
echo "Error. Exiting."
exit 3
;;
esac
done
# Grab the remaining positional argument(s) (not needed here)
# if [[ $# -ne 0 ]]; then
# POSITIONAL_1=$1
# POSITIONAL_1=$2
# else
# echo "Missing positional args?"
# exit 1
# fi
echo "Checking dependencies [$(echo ${DEPS} | sed 's/ /, /g')]..."
for dep in $DEPS; do
MISSING_DEP=false
if ! HAS $dep; then
echo "You do not have '${dep}' installed, or it is not available on your PATH!"
echo "If '${dep}' is not what it is called on your system, set the ${dep^^}_CLI environment variable."
MISSING_DEP=true
fi
if [[ $MISSING_DEP = true ]]; then
echo "Missing dependencies."
exit 1
fi
done
# Check if already running
if $DOCKER container inspect $CONTAINER_NAME > /dev/null 2>&1; then
RUNNING_ALREADY=true
else
RUNNING_ALREADY=false
fi
# If stop desired, stop container
if [[ $KILL = true ]]; then
if [[ $RUNNING_ALREADY = true ]]; then
echo "Stopping '${CONTAINER_NAME}'..."
$DOCKER stop $CONTAINER_NAME
exit 0
else
echo "Container '${CONTAINER_NAME}' is not currently running."
exit 1
fi
fi
# If running already, do nothing but check port
if [[ $RUNNING_ALREADY = true ]]; then
RUNNING_PORT=$($DOCKER container inspect $CONTAINER_NAME | grep -m 1 -Po "(?<=HostPort\": \")\d+")
if [[ $RUNNING_PORT -ne $PORT ]]; then
echo "Container '${CONTAINER_NAME}' is already running, but on the wrong port!"
echo "Container is using port ${RUNNING_PORT} and the specified port is ${PORT}."
exit 1
else
echo "Container '${CONTAINER_NAME}' is already running."
exit 0
fi
fi
# Start postgres container
echo "Starting '${CONTAINER_NAME}'..."
$DOCKER run --rm --name ${CONTAINER_NAME} -e POSTGRES_HOST_AUTH_METHOD=trust -p ${PORT}:5432 -d postgis/postgis:12-3.3
# Wait a few seconds so the DB container can start up
echo "Waiting for DB container..."
sleep 5s
# Enable all of the extensions needed on the template1 database
$DOCKER exec $CONTAINER_NAME /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS citext;\" -U postgres"
$DOCKER exec $CONTAINER_NAME /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS hstore;\" -U postgres"
$DOCKER exec $CONTAINER_NAME /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS postgis;\" -U postgres"
|