|
1 |
| -#!/usr/bin/env bash |
2 |
| - |
3 |
| -HOST=${DATABASE_HOST-${POSTGRES_PORT_5432_TCP_ADDR-localhost}} |
4 |
| -PORT=${DATABASE_PORT-${POSTGRES_PORT_5432_TCP_PORT-5432}} |
5 |
| -USER=${DATABASE_USER-${POSTGRES_ENV_POSTGRES_USER-postgres}} |
6 |
| -DATABASE=${DATABASE_NAME-${POSTGRES_ENV_POSTGRES_DATABASE-postgres}} |
7 |
| -PASS="${DATABASE_PASS-${POSTGRES_ENV_POSTGRES_PASSWORD}}" |
8 |
| -export PGPASSWORD="${PASS}" |
9 |
| -PGCONN="--username=${USER} --host=${HOST} --port=${PORT}" |
10 |
| -PSQL="psql ${PGCONN} --dbname=${DATABASE}" |
11 |
| -PGDUMP="pg_dump ${PGCONN}" |
12 |
| -GZIP="gzip --fast" |
13 |
| - |
14 |
| - |
15 |
| -function wait_postgres { |
16 |
| - # Wait for PostgreSQL to be available |
17 |
| - TIMEOUT=${3:-30} |
18 |
| - echo -n "Waiting to connect to PostgreSQL at ${1-$HOST}:${2-$PORT}" |
19 |
| - for (( i=0;; i++ )); do |
20 |
| - if [ ${i} -eq ${TIMEOUT} ]; then |
21 |
| - echo " timeout!" |
22 |
| - exit 99 |
| 1 | +# shellcheck disable=SC2034 shell=bash |
| 2 | + |
| 3 | +# The docker.io/postgres image uses the following env vars |
| 4 | +# POSTGRES_PASSWORD |
| 5 | +# POSTGRES_USER |
| 6 | +# POSTGRES_DB |
| 7 | + |
| 8 | +# Postgres also supports environment variables https://www.postgresql.org/docs/current/libpq-envars.html |
| 9 | +# PGPASSWORD |
| 10 | +# PGUSER |
| 11 | +# PGDATABASE |
| 12 | +# PGHOST |
| 13 | +# PGPORT |
| 14 | +# PGPASSFILE |
| 15 | + |
| 16 | +# Original env vars |
| 17 | +# HOST=${DATABASE_HOST-${POSTGRES_PORT_5432_TCP_ADDR-localhost}} |
| 18 | +# PORT=${DATABASE_PORT-${POSTGRES_PORT_5432_TCP_PORT-5432}} |
| 19 | +# USER=${DATABASE_USER-${POSTGRES_ENV_POSTGRES_USER-postgres}} |
| 20 | +# DATABASE=${DATABASE_NAME-${POSTGRES_ENV_POSTGRES_DATABASE-postgres}} |
| 21 | +# PASS="${DATABASE_PASS-${POSTGRES_ENV_POSTGRES_PASSWORD}}" |
| 22 | +# export PGPASSWORD="${PASS}" |
| 23 | +# PGCONN="--username=${USER} --host=${HOST} --port=${PORT}" |
| 24 | +# PSQL="psql ${PGCONN} --dbname=${DATABASE}" |
| 25 | +# PGDUMP="pg_dump ${PGCONN}" |
| 26 | +# GZIP="gzip --fast" |
| 27 | + |
| 28 | +# shellcheck disable=SC1091 |
| 29 | +. /panubo-functions.sh |
| 30 | + |
| 31 | +global_options() { |
| 32 | + echoerr "Global Options: (where possible these options match psql options)" |
| 33 | + echoerr " -h|--host host to connect to" |
| 34 | + echoerr " -p|--port post to connect to" |
| 35 | + echoerr " -U|--username user to connect with" |
| 36 | + echoerr " -d|--dbname database to connect to" |
| 37 | + echoerr " -W|--password password to connection to (not recommended. Use password-file)" |
| 38 | + echoerr " --password-file password file to read password from" |
| 39 | +} |
| 40 | + |
| 41 | +parse_options() { |
| 42 | + # Function parses the following options from both the command line and environment variables |
| 43 | + # Command line options take precedence and if neither is set commands should fall back to |
| 44 | + # default postgres environment variables. |
| 45 | + |
| 46 | + # Connection options |
| 47 | + # h|host |
| 48 | + # p|port |
| 49 | + # U|user |
| 50 | + # d|dbname |
| 51 | + |
| 52 | + # Password options |
| 53 | + # W|password |
| 54 | + # password-file |
| 55 | + |
| 56 | + # Create User DB options |
| 57 | + # no-create-database |
| 58 | + # no-revoke-public-create |
| 59 | + |
| 60 | + # Drop User DB options |
| 61 | + # drop-database |
| 62 | + |
| 63 | + # We don't want to pass these back to the caller |
| 64 | + local password |
| 65 | + local password_file |
| 66 | + |
| 67 | + # Pull in environment variables prefixed with DATABASE_ |
| 68 | + for item in host port username dbname password password_file no_create_database no_revoke_public_create drop_database; do |
| 69 | + local varname |
| 70 | + varname="DATABASE_${item^^}" |
| 71 | + if [[ -n "${!varname:-}" ]]; then |
| 72 | + eval ${item}="${!varname}" |
| 73 | + fi |
| 74 | + done |
| 75 | + |
| 76 | + # Options and long options |
| 77 | + local options="h:p:U:d:W:" |
| 78 | + local longopts="host:,port:,username:,dbname:,password:,password-file:,no-create-database,no-revoke-public-create,drop-database,format:,compression:,skip-globals" |
| 79 | + local parsed |
| 80 | + |
| 81 | + # Parse with getopt (not getopts) |
| 82 | + ! parsed=$(getopt --quiet --options=${options} --longoptions=${longopts} --name "${0}" -- "${@}") |
| 83 | + eval set -- "${parsed}" |
| 84 | + while true; do |
| 85 | + case "${1}" in |
| 86 | + -h|--host) |
| 87 | + host="${2}" |
| 88 | + shift 2 |
| 89 | + ;; |
| 90 | + -p|--port) |
| 91 | + port="${2}" |
| 92 | + shift 2 |
| 93 | + ;; |
| 94 | + -U|--username) |
| 95 | + user="${2}" |
| 96 | + shift 2 |
| 97 | + ;; |
| 98 | + -d|--database) |
| 99 | + database="${2}" |
| 100 | + shift 2 |
| 101 | + ;; |
| 102 | + -W|--password) |
| 103 | + password="${2}" |
| 104 | + shift 2 |
| 105 | + ;; |
| 106 | + --password-file) |
| 107 | + password_file="${2}" |
| 108 | + shift 2 |
| 109 | + ;; |
| 110 | + --no-create-database) |
| 111 | + no_create_database="true" |
| 112 | + shift |
| 113 | + ;; |
| 114 | + --no-revoke-public-create) |
| 115 | + no_revoke_public_create="true" |
| 116 | + shift |
| 117 | + ;; |
| 118 | + --drop-database) |
| 119 | + drop_database="true" |
| 120 | + shift |
| 121 | + ;; |
| 122 | + --format) |
| 123 | + format="${2}" |
| 124 | + shift 2 |
| 125 | + ;; |
| 126 | + --compression) |
| 127 | + compression="${2}" |
| 128 | + shift 2 |
| 129 | + ;; |
| 130 | + --skip-globals) |
| 131 | + skip_globals="true" |
| 132 | + shift |
| 133 | + ;; |
| 134 | + --) |
| 135 | + shift |
| 136 | + break |
| 137 | + ;; |
| 138 | + *) |
| 139 | + echo "Unrecognised option" |
| 140 | + exit 3 |
| 141 | + ;; |
| 142 | + esac |
| 143 | + done |
| 144 | + |
| 145 | + # Set remaining command line arguments into an array |
| 146 | + args=( "$@" ) |
| 147 | + |
| 148 | + # Setup connection string |
| 149 | + connection=() |
| 150 | + for item in host port user dbname; do |
| 151 | + if [[ -n "${!item:-}" ]]; then |
| 152 | + connection+=("--${item}" "${!item}") |
23 | 153 | fi
|
24 |
| - sleep 1 |
25 |
| - (exec 3<>/dev/tcp/${1-$HOST}/${2-$PORT}) &>/dev/null && break |
26 |
| - echo -n "." |
27 | 154 | done
|
28 |
| - echo " connected." |
29 |
| - exec 3>&- |
30 |
| - exec 3<&- |
| 155 | + |
| 156 | + # Read in the password file if set |
| 157 | + if [[ -n "${password_file:-}" ]]; then |
| 158 | + # Read password file if set on the command line or DATABASE_PASSWORD_FILE |
| 159 | + password="$(cat "${password_file}")" |
| 160 | + fi |
| 161 | + |
| 162 | + # If the password was set write it to .pgpass (or save a temporary file and set PGPASSFILE) |
| 163 | + if [[ -n "${password}" ]]; then |
| 164 | + local old_umask |
| 165 | + old_umask="$(umask)" |
| 166 | + umask 0077 |
| 167 | + PGPASSFILE="$(mktemp)" |
| 168 | + export PGPASSFILE |
| 169 | + echo "*:*:*:*:${password}" > "${PGPASSFILE}" |
| 170 | + umask "${old_umask}" |
| 171 | + echoerr ">>> Password written to ${PGPASSFILE}" |
| 172 | + fi |
31 | 173 | }
|
32 | 174 |
|
| 175 | +echoerr() { echo "$@" 1>&2; } |
| 176 | + |
| 177 | +genpasswd() { |
| 178 | + # Ambiguous characters have been been excluded |
| 179 | + CHARS="abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ" |
| 180 | + |
| 181 | + export LC_CTYPE=C # Quiet tr warnings |
| 182 | + local length |
| 183 | + length="${1:-16}" |
| 184 | + set +o pipefail |
| 185 | + strings < /dev/urandom | tr -dc "${CHARS}" | head -c "${length}" | xargs |
| 186 | + set -o pipefail |
| 187 | +} |
33 | 188 |
|
34 |
| -function genpasswd() { |
35 |
| - export LC_CTYPE=C # Quiet tr warnings |
36 |
| - local l=$1 |
37 |
| - [ "$l" == "" ] && l=16 |
38 |
| - set +o pipefail |
39 |
| - strings < /dev/urandom | tr -dc A-Za-z0-9_ | head -c ${l} |
40 |
| - set -o pipefail |
| 189 | +wait_postgres() { |
| 190 | + wait_tcp "${host:-${PGHOST:-127.0.0.1}}" "${port:-${PGPORT:-5432}}" |
41 | 191 | }
|
0 commit comments