Skip to content

Commit 2d8e5c2

Browse files
author
Matthew Fisher
committed
Merge pull request #36 from bacongobbler/fixup-ci
ref(postgres): move username and password to secrets
2 parents bf182fb + 262cdbf commit 2d8e5c2

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
contrib/ci/tmp

contrib/ci/test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ set -x
55

66
JOB=$(docker run -d $1)
77
# wait for postgres to boot
8+
CURRENT_DIR=$(cd $(dirname $0); pwd)
9+
mkdir -p $CURRENT_DIR/tmp
10+
echo "testuser" > $CURRENT_DIR/tmp/user
11+
echo "icanttellyou" > $CURRENT_DIR/tmp/password
12+
JOB=$(docker run -dv $CURRENT_DIR/tmp:/var/run/secrets/deis/database/creds $1)
813
sleep 10
14+
# display logs for debugging purposes
15+
docker logs $JOB
916
docker exec $JOB is_master
1017
docker rm -f $JOB

rootfs/docker-entrypoint.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
#
3+
# Originally copied and modified from
4+
# https://github.com/docker-library/postgres/blob/ec5ce80ca914e02c2d5eb9fde424039d4cee032e/9.4/docker-entrypoint.sh
5+
#
6+
set -e
7+
8+
set_listen_addresses() {
9+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
10+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
11+
}
12+
13+
POSTGRES_USER="$(cat /var/run/secrets/deis/database/creds/user)"
14+
POSTGRES_PASSWORD="$(cat /var/run/secrets/deis/database/creds/password)"
15+
16+
if [ "$1" = 'postgres' ]; then
17+
mkdir -p "$PGDATA"
18+
chmod 700 "$PGDATA"
19+
chown -R postgres "$PGDATA"
20+
21+
chmod g+s /run/postgresql
22+
chown -R postgres /run/postgresql
23+
24+
# look specifically for PG_VERSION, as it is expected in the DB dir
25+
if [ ! -s "$PGDATA/PG_VERSION" ]; then
26+
gosu postgres initdb
27+
28+
# check password first so we can output the warning before postgres
29+
# messes it up
30+
if [ "$POSTGRES_PASSWORD" ]; then
31+
pass="PASSWORD '$POSTGRES_PASSWORD'"
32+
authMethod=md5
33+
else
34+
# The - option suppresses leading tabs but *not* spaces. :)
35+
cat >&2 <<-'EOWARN'
36+
****************************************************
37+
WARNING: No password has been set for the database.
38+
This will allow anyone with access to the
39+
Postgres port to access your database. In
40+
Docker's default configuration, this is
41+
effectively any other container on the same
42+
system.
43+
44+
Use "-e POSTGRES_PASSWORD=password" to set
45+
it in "docker run".
46+
****************************************************
47+
EOWARN
48+
49+
pass=
50+
authMethod=trust
51+
fi
52+
53+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
54+
55+
# internal start of server in order to allow set-up using psql-client
56+
# does not listen on TCP/IP and waits until start finishes
57+
gosu postgres pg_ctl -D "$PGDATA" \
58+
-o "-c listen_addresses=''" \
59+
-w start
60+
61+
: ${POSTGRES_USER:=postgres}
62+
: ${POSTGRES_DB:=$POSTGRES_USER}
63+
export POSTGRES_USER POSTGRES_DB
64+
65+
if [ "$POSTGRES_DB" != 'postgres' ]; then
66+
psql --username postgres <<-EOSQL
67+
CREATE DATABASE "$POSTGRES_DB" ;
68+
EOSQL
69+
echo
70+
fi
71+
72+
if [ "$POSTGRES_USER" = 'postgres' ]; then
73+
op='ALTER'
74+
else
75+
op='CREATE'
76+
fi
77+
78+
psql --username postgres <<-EOSQL
79+
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
80+
EOSQL
81+
echo
82+
83+
echo
84+
for f in /docker-entrypoint-initdb.d/*; do
85+
case "$f" in
86+
*.sh) echo "$0: running $f"; . "$f" ;;
87+
*.sql)
88+
echo "$0: running $f";
89+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f"
90+
echo
91+
;;
92+
*) echo "$0: ignoring $f" ;;
93+
esac
94+
echo
95+
done
96+
97+
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
98+
set_listen_addresses '*'
99+
100+
echo
101+
echo 'PostgreSQL init process complete; ready for start up.'
102+
echo
103+
fi
104+
105+
exec gosu postgres "$@"
106+
fi
107+
108+
exec "$@"

0 commit comments

Comments
 (0)