Skip to content

Commit 9360fb2

Browse files
committed
ROX-19980 fix
1 parent 80f102f commit 9360fb2

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

image/db/rhel/scripts/custom-entrypoint.sh

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,57 @@
66

77
set -e
88

9-
echo "Creating /var/lib/postgresql/data/pgdata..."
10-
mkdir -p /var/lib/postgresql/data/pgdata
9+
if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then
1110

12-
echo "Uncompressing into /var/lib/pgsql/data/pgdata..."
13-
tar -xzf /tmp/data.tar.gz -C /var/lib/postgresql/data/pgdata
11+
echo "Creating /var/lib/postgresql/data/pgdata..."
12+
mkdir -p /var/lib/postgresql/data/pgdata
1413

15-
echo "Starting database..."
16-
POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf &
14+
echo "Moving archive to target directory..."
15+
mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz
1716

18-
echo "Waiting for database to be ready..."
19-
while ! pg_isready -U postgres -h localhost -p 5432; do
20-
sleep 1
21-
done
17+
echo "Uncompressing into /var/lib/pgsql/data/pgdata..."
18+
tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata
2219

23-
echo "Changing password..."
24-
if [ "$POSTGRES_PASSWORD" != "postgres" ]; then
25-
PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';"
26-
fi
20+
echo "Removing archive..."
21+
rm /var/lib/postgresql/data/pgdata/data.tar.gz
22+
23+
echo "Starting database..."
24+
POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf &
25+
26+
echo "Waiting for database to be ready..."
27+
while ! pg_isready -U postgres -h localhost -p 5432; do
28+
sleep 1
29+
done
30+
31+
echo "Changing password if POSTGRES_PASSWORD is set..."
32+
if [ -n "$POSTGRES_PASSWORD" ]; then
33+
PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';"
34+
elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then
35+
PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';"
36+
fi
2737

28-
echo "Renaming postgres user if necessary..."
29-
if [ "$POSTGRES_USER" != "postgres" ]; then
30-
PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;"
38+
echo "Renaming postgres user if necessary..."
39+
if [ -n "$POSTGRES_USER" ]; then
40+
if [ "$POSTGRES_USER" != "postgres" ]; then
41+
PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;"
42+
fi
43+
fi
44+
45+
echo "Stopping database..."
46+
pg_ctl -w stop
47+
48+
else
49+
echo "Database already initialized. Skipping initialization..."
3150
fi
3251

33-
echo "Stopping database..."
34-
pg_ctl -w stop
52+
if [ "${ROX_SCANNER_DB_INIT}" == "true" ]; then
53+
echo "ROX_SCANNER_DB_INIT is set to true. Exiting..."
54+
exit 0
55+
else
56+
echo "Database initialized."
57+
fi
3558

3659
# Now we can start the database for real. But we will
3760
# forward any arguments to the actual entrypoint script
3861
echo "Starting database for real..."
39-
40-
exec /usr/local/bin/docker-entrypoint.sh "$@"
62+
exec /usr/local/bin/docker-entrypoint.sh "$@"

image/db/rhel/scripts/docker-entrypoint.sh

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
###
77
### [1]: https://github.com/docker-library/postgres/blob/master/12/bullseye/docker-entrypoint.sh
88

9-
if [ -n "$ROX_SCANNER_DB_INIT" ]; then
10-
exit 0
11-
fi
12-
139
set -Eeo pipefail
1410
# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables)
1511

@@ -310,18 +306,6 @@ _main() {
310306
fi
311307

312308
if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then
313-
### STACKROX MODIFIED - If we are initializing, then ensure we start from scratch.
314-
if [ -n "$ROX_SCANNER_DB_INIT" ]; then
315-
echo
316-
echo 'Initializing... Clearing any previous data from directories'
317-
echo
318-
319-
rm -rf "$PGDATA"
320-
if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then
321-
rm -rf "$POSTGRES_INITDB_WALDIR"
322-
fi
323-
fi
324-
325309
docker_setup_env
326310
# setup data directories and permissions (when run as root)
327311
docker_create_db_directories
@@ -333,16 +317,6 @@ _main() {
333317
exec gosu postgres "$BASH_SOURCE" "$@"
334318
fi
335319

336-
### STACKROX MODIFIED - Sanity check the database does not exist
337-
### upon initialization.
338-
if [ -n "$ROX_SCANNER_DB_INIT" ] && [ -n "$DATABASE_ALREADY_EXISTS" ]; then
339-
echo
340-
echo 'PostgreSQL Database appears to already exist upon initialization; Exiting with error...'
341-
echo
342-
343-
exit 1
344-
fi
345-
346320
# only run initialization on an empty data directory
347321
if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
348322
docker_verify_minimum_env

0 commit comments

Comments
 (0)