Skip to content

Commit

Permalink
Be a drop-in replacement for upstream
Browse files Browse the repository at this point in the history
- Updates environment variables to have the same names
- Based on current docker-entrypoint.sh
- Fixes DanielDent#2
  • Loading branch information
DanielDent committed Oct 17, 2016
2 parents d8ccc1c + d308f7e commit 32727b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ services:
image: 'danieldent/postgres-replication'
restart: 'always'
environment:
PGUSER: 'postgres'
PGPASSWORD: 'postgres'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
PGDATA: '/var/lib/postgresql/data/pgdata'
volumes:
- '/var/lib/postgresql/data'
Expand All @@ -20,8 +20,8 @@ services:
image: 'danieldent/postgres-replication'
restart: 'always'
environment:
PGUSER: 'postgres'
PGPASSWORD: 'postgres'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
PGDATA: '/var/lib/postgresql/data/pgdata'
REPLICATE_FROM: 'pg-master'
volumes:
Expand Down
89 changes: 50 additions & 39 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#!/bin/bash
set -e

# Backwards compatibility for old variable names (deprecated)
if [ "x$PGUSER" != "x" ]; then
POSTGRES_USER=$PGUSER
fi
if [ "x$PGPASSWORD" != "x" ]; then
POSTGRES_PASSWORD=$PGPASSWORD
fi

# Based on official postgres package's entrypoint script (https://hub.docker.com/_/postgres/)
# Modified to be able to set up a slave. The docker-entrypoint-initdb.d hook provided is inadequate.

set_listen_addresses() {
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
}
set -e

if [ "${1:0:1}" = '-' ]; then
set -- postgres "$@"
fi

if [ "$1" = 'postgres' ]; then
mkdir -p "$PGDATA"
chmod 700 "$PGDATA"
chown -R postgres "$PGDATA"

chmod g+s /run/postgresql
Expand All @@ -19,25 +28,24 @@ if [ "$1" = 'postgres' ]; then
# look specifically for PG_VERSION, as it is expected in the DB dir
if [ ! -s "$PGDATA/PG_VERSION" ]; then
if [ "x$REPLICATE_FROM" == "x" ]; then
gosu postgres initdb
else
until ping -c 1 -W 1 ${REPLICATE_FROM}
do
echo "Waiting for master to ping..."
sleep 1s
done
until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${PGUSER} -vP
do
echo "Waiting for master to connect..."
sleep 1s
done
chmod 700 ${PGDATA}
fi
eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"
else
until ping -c 1 -W 1 ${REPLICATE_FROM}
do
echo "Waiting for master to ping..."
sleep 1s
done
until gosu postgres pg_basebackup -h ${REPLICATE_FROM} -D ${PGDATA} -U ${POSTGRES_USER} -vP
do
echo "Waiting for master to connect..."
sleep 1s
done
fi

# check password first so we can output the warning before postgres
# messes it up
if [ "$PGPASSWORD" ]; then
pass="PASSWORD '$PGPASSWORD'"
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
# The - option suppresses leading tabs but *not* spaces. :)
Expand All @@ -50,7 +58,7 @@ if [ "$1" = 'postgres' ]; then
effectively any other container on the same
system.
Use "-e PGPASSWORD=password" to set
Use "-e POSTGRES_PASSWORD=password" to set
it in "docker run".
****************************************************
EOWARN
Expand All @@ -65,49 +73,52 @@ if [ "$1" = 'postgres' ]; then
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"

# internal start of server in order to allow set-up using psql-client
# does not listen on TCP/IP and waits until start finishes
# does not listen on external TCP/IP and waits until start finishes
gosu postgres pg_ctl -D "$PGDATA" \
-o "-c listen_addresses=''" \
-o "-c listen_addresses='localhost'" \
-w start

: ${PGUSER:=postgres}
: ${POSTGRES_DB:=$PGUSER}
export PGUSER POSTGRES_DB
: ${POSTGRES_USER:=postgres}
: ${POSTGRES_DB:=$POSTGRES_USER}
export POSTGRES_USER POSTGRES_DB

psql=( psql -v ON_ERROR_STOP=1 )

if [ "$POSTGRES_DB" != 'postgres' ]; then
psql --username postgres <<-EOSQL
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$POSTGRES_DB" ;
EOSQL
echo
fi

if [ "$PGUSER" = 'postgres' ]; then
if [ "$POSTGRES_USER" = 'postgres' ]; then
op='ALTER'
else
op='CREATE'
fi

psql --username postgres <<-EOSQL
$op USER "$PGUSER" WITH SUPERUSER $pass ;
"${psql[@]}" --username postgres <<-EOSQL
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
EOSQL
echo

fi

fi
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )

echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; psql --username "$PGUSER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
*) echo "$0: ignoring $f" ;;
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done

if [ "x$REPLICATE_FROM" == "x" ]; then
if [ "x$REPLICATE_FROM" == "x" ]; then
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
fi
set_listen_addresses '*'
fi

echo
echo 'PostgreSQL init process complete; ready for start up.'
Expand Down
2 changes: 1 addition & 1 deletion setup-replication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else

cat > ${PGDATA}/recovery.conf <<EOF
standby_mode = on
primary_conninfo = 'host=${REPLICATE_FROM} port=5432 user=${PGUSER} password=${PGPASSWORD}'
primary_conninfo = 'host=${REPLICATE_FROM} port=5432 user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}'
trigger_file = '/tmp/touch_me_to_promote_to_me_master'
EOF
chown postgres ${PGDATA}/recovery.conf
Expand Down

0 comments on commit 32727b4

Please sign in to comment.