2018-02-22 18:01:05 +03:00
|
|
|
#!/bin/sh
|
2017-09-05 14:39:20 +03:00
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o pipefail
|
2018-03-08 16:59:41 +03:00
|
|
|
set -o nounset
|
2017-09-05 14:39:20 +03:00
|
|
|
|
|
|
|
|
2016-08-16 22:20:41 +03:00
|
|
|
cmd="$@"
|
|
|
|
|
2018-03-08 16:59:41 +03:00
|
|
|
if [ -z "${POSTGRES_USER}" ]; then
|
|
|
|
# the official postgres image uses 'postgres' as default user if not set explictly.
|
2016-03-08 12:12:55 +03:00
|
|
|
export POSTGRES_USER=postgres
|
|
|
|
fi
|
2018-03-08 16:59:41 +03:00
|
|
|
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}"
|
2016-08-16 22:20:41 +03:00
|
|
|
|
2018-02-22 18:01:05 +03:00
|
|
|
postgres_ready() {
|
2016-08-16 22:20:41 +03:00
|
|
|
python << END
|
|
|
|
import sys
|
2018-03-08 16:59:41 +03:00
|
|
|
|
2016-08-16 22:20:41 +03:00
|
|
|
import psycopg2
|
2018-03-08 16:59:41 +03:00
|
|
|
|
2016-08-16 22:20:41 +03:00
|
|
|
try:
|
2018-03-08 16:59:41 +03:00
|
|
|
psycopg2.connect(
|
|
|
|
dbname="${POSTGRES_DB}",
|
|
|
|
user="${POSTGRES_USER}",
|
|
|
|
password="${POSTGRES_PASSWORD}",
|
|
|
|
host="postgres"
|
|
|
|
)
|
2016-08-16 22:20:41 +03:00
|
|
|
except psycopg2.OperationalError:
|
|
|
|
sys.exit(-1)
|
|
|
|
sys.exit(0)
|
2018-03-08 16:59:41 +03:00
|
|
|
|
2016-08-16 22:20:41 +03:00
|
|
|
END
|
|
|
|
}
|
|
|
|
|
|
|
|
until postgres_ready; do
|
2018-03-08 16:59:41 +03:00
|
|
|
>&2 echo 'PostgreSQL is unavailable (sleeping)...'
|
2016-08-16 22:20:41 +03:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
2018-03-08 16:59:41 +03:00
|
|
|
>&2 echo 'PostgreSQL is up - continuing...'
|
|
|
|
|
2016-08-16 22:20:41 +03:00
|
|
|
exec $cmd
|