Skip to content

Commit b8ce200

Browse files
committed
random password and add kartoza figlet
1 parent fcac40b commit b8ce200

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ RUN set -eux \
2020
apt-transport-https curl gettext \
2121
&& dpkg-divert --local --rename --add /sbin/initctl
2222

23-
RUN apt-get -y update; apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat gdal-bin
23+
RUN apt-get -y update; apt-get -y install build-essential autoconf libxml2-dev zlib1g-dev netcat gdal-bin \
24+
figlet toilet
2425

2526

2627

scripts/docker-entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ source /scripts/setup-ssl.sh
1414
# Setup pg_hba.conf
1515

1616
source /scripts/setup-pg_hba.sh
17+
# Function to add figlet
18+
advertise
19+
figlet -t "Kartoza Docker PostGIS"
20+
21+
POSTGRES_PASS=$(cat /tmp/PGPASSWORD.txt)
22+
echo -e "[Entrypoint] GENERATED Postgres PASSWORD: \e[1;31m $POSTGRES_PASS"
23+
echo -e "\033[0m PGPASSWORD Generated above: "
1724

1825
if [[ -z "$REPLICATE_FROM" ]]; then
1926
# This means this is a master instance. We check that database exists
@@ -43,4 +50,5 @@ if [[ "${1:0:1}" = '-' ]]; then
4350
set -- postgres "$@"
4451
fi
4552

53+
4654
exec su - "$@"

scripts/env-data.sh

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ POSTGRES_MAJOR_VERSION=$(cat /tmp/pg_version.txt)
33
POSTGIS_MAJOR=$(cat /tmp/pg_major_version.txt)
44
POSTGIS_MINOR_RELEASE=$(cat /tmp/pg_minor_version.txt)
55
DEFAULT_DATADIR="/var/lib/postgresql/${POSTGRES_MAJOR_VERSION}/main"
6+
POSTGRES_INITDB_WALDIR="/opt/${POSTGRES_MAJOR_VERSION}/pg_waldir"
67
ROOT_CONF="/etc/postgresql/${POSTGRES_MAJOR_VERSION}/main"
78
PG_ENV="$ROOT_CONF/environment"
89
CONF="$ROOT_CONF/postgresql.conf"
@@ -67,20 +68,26 @@ then
6768
mkdir -p ${DATA_PATH}
6869
fi
6970
}
71+
72+
73+
if [ -z "${POSTGRES_INITDB_WALDIR}" ]; then
74+
POSTGRES_INITDB_WALDIR=${POSTGRES_INITDB_WALDIR}
75+
fi
76+
7077
# Make sure we have a user set up
7178
if [ -z "${POSTGRES_USER}" ]; then
7279
POSTGRES_USER=docker
7380
fi
74-
if [ -z "${POSTGRES_PASS}" ]; then
75-
POSTGRES_PASS=docker
76-
fi
81+
82+
7783
if [ -z "${POSTGRES_DBNAME}" ]; then
7884
POSTGRES_DBNAME=gis
7985
fi
8086
# If datadir is not defined, then use this
8187
if [ -z "${DATADIR}" ]; then
8288
DATADIR=${DEFAULT_DATADIR}
8389
fi
90+
8491
# RECREATE_DATADIR flag default value
8592
# Always assume that we don't want to recreate datadir if not explicitly defined
8693
# For issue: https://github.com/kartoza/docker-postgis/issues/226
@@ -377,3 +384,23 @@ until su - postgres -c "${PG_BASEBACKUP} -X stream -h ${REPLICATE_FROM} -p ${REP
377384
done
378385

379386
}
387+
388+
function pg_password() {
389+
SETUP_LOCKFILE="${EXTRA_CONF_DIR}/.pass.lock"
390+
if [ -z "${POSTGRES_PASS}" ] && [ ! -f ${SETUP_LOCKFILE} ]; then
391+
POSTGRES_PASS=$(openssl rand -base64 15)
392+
touch ${SETUP_LOCKFILE}
393+
echo "$POSTGRES_PASS" >> /tmp/PGPASSWORD.txt
394+
else
395+
echo "$POSTGRES_PASS" >> /tmp/PGPASSWORD.txt
396+
fi
397+
398+
}
399+
400+
function advertise() {
401+
SETUP_LOCKFILE="${EXTRA_CONF_DIR}/.bash.lock"
402+
if [[ ! -f ${SETUP_LOCKFILE} ]]; then
403+
echo 'figlet -t "Kartoza Docker PostGIS"' >> ~/.bashrc
404+
touch ${SETUP_LOCKFILE}
405+
fi
406+
}

scripts/setup-database.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
source /scripts/env-data.sh
44

5+
POSTGRES_PASS=$(cat /tmp/PGPASSWORD.txt)
6+
57
# test if DATADIR has content
68
# Do initialization if DATADIR is empty, or RECREATE_DATADIR is true
79
if [[ -z "$(ls -A ${DATADIR} 2> /dev/null)" || "${RECREATE_DATADIR}" == 'TRUE' ]]; then
810
# Only attempt reinitializations if ${RECREATE_DATADIR} is true
911
# No Replicate From settings. Assume that this is a master database.
1012
# Initialise db
1113
echo "Initializing Postgres Database at ${DATADIR}"
12-
create_dir ${DATADIR}
14+
create_dir ${DATADIR} ${POSTGRES_INITDB_WALDIR}
1315
rm -rf ${DATADIR}/*
14-
chown -R postgres:postgres ${DATADIR}
16+
chown -R postgres:postgres ${DATADIR} ${POSTGRES_INITDB_WALDIR}
1517
echo "Initializing with command:"
16-
echo "postgres" > /tmp/superuser_pass.txt
17-
command="$INITDB -U postgres --pwfile "/tmp/superuser_pass.txt" -E ${DEFAULT_ENCODING} --lc-collate=${DEFAULT_COLLATION} --lc-ctype=${DEFAULT_CTYPE} --wal-segsize=${WAL_SEGSIZE} --auth=${PASSWORD_AUTHENTICATION} -D ${DATADIR} ${INITDB_EXTRA_ARGS}"
18+
command="$INITDB -U postgres --pwfile=<(echo "$POSTGRES_PASS") -E ${DEFAULT_ENCODING} --lc-collate=${DEFAULT_COLLATION} --lc-ctype=${DEFAULT_CTYPE} --wal-segsize=${WAL_SEGSIZE} --auth=${PASSWORD_AUTHENTICATION} -D ${DATADIR} --waldir=${POSTGRES_INITDB_WALDIR} ${INITDB_EXTRA_ARGS}"
1819
su - postgres -c "$command"
19-
rm /tmp/superuser_pass.txt
2020
fi;
2121

2222
# Set proper permissions

scripts/setup-pg_hba.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ if [ -f "${SETUP_LOCKFILE}" ]; then
77
return 0
88
fi
99

10+
# Setup Postgresql password
11+
pg_password
12+
1013
# This script will setup pg_hba.conf
1114

1215
# Reconfigure pg_hba if environment settings changed

scripts/setup-user.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ source /scripts/env-data.sh
1414

1515
# Only create credentials if this is a master database
1616
# Slave database will just mirror from master users
17-
echo "Setup postgres User:Password"
18-
echo "postgresql user: $POSTGRES_USER" > /tmp/PGPASSWORD.txt
19-
echo "postgresql password: $POSTGRES_PASS" >> /tmp/PGPASSWORD.txt
17+
18+
19+
POSTGRES_PASS=$(cat /tmp/PGPASSWORD.txt)
2020

2121
# Check user already exists
2222
echo "Creating superuser $POSTGRES_USER"

0 commit comments

Comments
 (0)