From e0edb6732cb3de83fd3824997018b0de7f9c58f4 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Thu, 26 Oct 2023 16:17:27 +0200 Subject: [PATCH 01/10] ROX-19980 pre-built scanner-db image --- image/db/rhel/Dockerfile | 11 ++++++- image/db/rhel/scripts/custom-entrypoint.sh | 34 ++++++++++++++++++++++ image/db/rhel/scripts/docker-entrypoint.sh | 4 +++ image/db/rhel/scripts/start-db.sh | 16 ++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100755 image/db/rhel/scripts/custom-entrypoint.sh create mode 100755 image/db/rhel/scripts/start-db.sh diff --git a/image/db/rhel/Dockerfile b/image/db/rhel/Dockerfile index 9f4d1b62c..4befca5bf 100644 --- a/image/db/rhel/Dockerfile +++ b/image/db/rhel/Dockerfile @@ -59,7 +59,16 @@ USER 70:70 COPY --from=extracted_bundle /bundle/docker-entrypoint-initdb.d/definitions.sql.gz /docker-entrypoint-initdb.d/ -ENTRYPOINT ["docker-entrypoint.sh"] +COPY scripts/custom-entrypoint.sh /usr/local/bin/ +COPY scripts/start-db.sh /usr/local/bin/ + +RUN /usr/local/bin/start-db.sh +USER root +RUN rm -rf /usr/local/bin/start-db.sh && \ + rm -rf /docker-entrypoint-initdb.d/definitions.sql +USER 70:70 +ENV DATABASE_ALREADY_EXISTS=true +ENTRYPOINT ["custom-entrypoint.sh"] EXPOSE 5432 CMD ["postgres", "-c", "config_file=/etc/postgresql.conf"] diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh new file mode 100755 index 000000000..2ae57be56 --- /dev/null +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# The postgres server has been started once during the build process in the Dockerfile. +# Now we need to start it again, but this time with the correct password. +# So we need to issue a command to change the password. + +set -e + +echo "Starting database..." +POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & + +echo "Waiting for database to be ready..." +while ! pg_isready -U postgres -h localhost -p 5432; do + sleep 1 +done + +echo "Changing password..." +if [ "$POSTGRES_PASSWORD" != "postgres" ]; then + PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" +fi + +echo "Renaming postgres user if necessary..." +if [ "$POSTGRES_USER" != "postgres" ]; then + PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" +fi + +echo "Stopping database..." +pg_ctl -D /var/lib/postgresql/data/pgdata -w stop + +# Now we can start the database for real. But we will +# forward any arguments to the actual entrypoint script +echo "Starting database for real..." + +exec /usr/local/bin/docker-entrypoint.sh "$@" \ No newline at end of file diff --git a/image/db/rhel/scripts/docker-entrypoint.sh b/image/db/rhel/scripts/docker-entrypoint.sh index 27491bb91..fbc63774b 100755 --- a/image/db/rhel/scripts/docker-entrypoint.sh +++ b/image/db/rhel/scripts/docker-entrypoint.sh @@ -6,6 +6,10 @@ ### ### [1]: https://github.com/docker-library/postgres/blob/master/12/bullseye/docker-entrypoint.sh +if [ -n "$ROX_SCANNER_DB_INIT" ]; then + exit 0 +fi + set -Eeo pipefail # TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) diff --git a/image/db/rhel/scripts/start-db.sh b/image/db/rhel/scripts/start-db.sh new file mode 100755 index 000000000..dc8497e52 --- /dev/null +++ b/image/db/rhel/scripts/start-db.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -eu + +echo "Creating postgres.conf for initialization..." +cat < /tmp/postgres.conf +listen_addresses = '*' +EOF + +echo "Starting database..." +POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf + +echo "Waiting for database to stop..." +pg_ctl -D /var/lib/postgresql/data/pgdata -w stop + +rm /tmp/postgres.conf \ No newline at end of file From 9e85cd154f80832cd86b1e3a21411290d961c207 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Thu, 26 Oct 2023 16:33:11 +0200 Subject: [PATCH 02/10] ROX-19980 fix --- image/db/rhel/scripts/start-db.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/image/db/rhel/scripts/start-db.sh b/image/db/rhel/scripts/start-db.sh index dc8497e52..f5023c4ce 100755 --- a/image/db/rhel/scripts/start-db.sh +++ b/image/db/rhel/scripts/start-db.sh @@ -11,6 +11,8 @@ echo "Starting database..." POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf echo "Waiting for database to stop..." -pg_ctl -D /var/lib/postgresql/data/pgdata -w stop +while [ -f /var/lib/postgresql/data/pgdata/postmaster.pid ]; do + sleep 1 +done rm /tmp/postgres.conf \ No newline at end of file From 80f102fbe2d6447e374a06e701732c1e671ed5d1 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 12:40:05 +0200 Subject: [PATCH 03/10] ROX-19980 fix --- image/db/rhel/Dockerfile | 4 ++-- image/db/rhel/scripts/custom-entrypoint.sh | 8 +++++++- image/db/rhel/scripts/start-db.sh | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/image/db/rhel/Dockerfile b/image/db/rhel/Dockerfile index 4befca5bf..7fe7f8cae 100644 --- a/image/db/rhel/Dockerfile +++ b/image/db/rhel/Dockerfile @@ -36,7 +36,7 @@ RUN curl -sSLf https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-${ groupadd -g 70 postgres && \ adduser postgres -u 70 -g 70 -d /var/lib/postgresql -s /bin/sh && \ microdnf install \ - ca-certificates libicu systemd-sysv glibc-locale-source glibc-langpack-en \ + ca-certificates tar libicu systemd-sysv glibc-locale-source glibc-langpack-en \ postgresql12-server && \ # The removal of /usr/share/zoneinfo from UBI minimal images is intentional. # After building the image, the image is reduced in size as much as possible, @@ -65,7 +65,7 @@ COPY scripts/start-db.sh /usr/local/bin/ RUN /usr/local/bin/start-db.sh USER root RUN rm -rf /usr/local/bin/start-db.sh && \ - rm -rf /docker-entrypoint-initdb.d/definitions.sql + rm -rf /docker-entrypoint-initdb.d/* USER 70:70 ENV DATABASE_ALREADY_EXISTS=true ENTRYPOINT ["custom-entrypoint.sh"] diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index 2ae57be56..ec74f59e9 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -6,6 +6,12 @@ set -e +echo "Creating /var/lib/postgresql/data/pgdata..." +mkdir -p /var/lib/postgresql/data/pgdata + +echo "Uncompressing into /var/lib/pgsql/data/pgdata..." +tar -xzf /tmp/data.tar.gz -C /var/lib/postgresql/data/pgdata + echo "Starting database..." POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & @@ -25,7 +31,7 @@ if [ "$POSTGRES_USER" != "postgres" ]; then fi echo "Stopping database..." -pg_ctl -D /var/lib/postgresql/data/pgdata -w stop +pg_ctl -w stop # Now we can start the database for real. But we will # forward any arguments to the actual entrypoint script diff --git a/image/db/rhel/scripts/start-db.sh b/image/db/rhel/scripts/start-db.sh index f5023c4ce..e3f2d00bd 100755 --- a/image/db/rhel/scripts/start-db.sh +++ b/image/db/rhel/scripts/start-db.sh @@ -5,14 +5,24 @@ set -eu echo "Creating postgres.conf for initialization..." cat < /tmp/postgres.conf listen_addresses = '*' +max_wal_size = 1GB EOF +echo "Creating temporary PGDATA directory..." +mkdir -p /tmp/data + echo "Starting database..." -POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf +PGDATA=/tmp/data POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf echo "Waiting for database to stop..." -while [ -f /var/lib/postgresql/data/pgdata/postmaster.pid ]; do +while [ -f /tmp/data/pgdata/postmaster.pid ]; do sleep 1 done -rm /tmp/postgres.conf \ No newline at end of file +rm /tmp/postgres.conf + +echo "Compressing database data folder..." +tar -czf /tmp/data.tar.gz -C /tmp/data . + +echo "Removing temporary PGDATA directory..." +rm -rf /tmp/data From 9360fb22c240a7781000642ebce7dd67a582fcf2 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 14:01:39 +0200 Subject: [PATCH 04/10] ROX-19980 fix --- image/db/rhel/scripts/custom-entrypoint.sh | 64 +++++++++++++++------- image/db/rhel/scripts/docker-entrypoint.sh | 26 --------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index ec74f59e9..da37245a3 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -6,35 +6,57 @@ set -e -echo "Creating /var/lib/postgresql/data/pgdata..." -mkdir -p /var/lib/postgresql/data/pgdata +if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then -echo "Uncompressing into /var/lib/pgsql/data/pgdata..." -tar -xzf /tmp/data.tar.gz -C /var/lib/postgresql/data/pgdata + echo "Creating /var/lib/postgresql/data/pgdata..." + mkdir -p /var/lib/postgresql/data/pgdata -echo "Starting database..." -POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & + echo "Moving archive to target directory..." + mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz -echo "Waiting for database to be ready..." -while ! pg_isready -U postgres -h localhost -p 5432; do - sleep 1 -done + echo "Uncompressing into /var/lib/pgsql/data/pgdata..." + tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata -echo "Changing password..." -if [ "$POSTGRES_PASSWORD" != "postgres" ]; then - PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" -fi + echo "Removing archive..." + rm /var/lib/postgresql/data/pgdata/data.tar.gz + + echo "Starting database..." + POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & + + echo "Waiting for database to be ready..." + while ! pg_isready -U postgres -h localhost -p 5432; do + sleep 1 + done + + echo "Changing password if POSTGRES_PASSWORD is set..." + if [ -n "$POSTGRES_PASSWORD" ]; then + PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" + elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then + PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" + fi -echo "Renaming postgres user if necessary..." -if [ "$POSTGRES_USER" != "postgres" ]; then - PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" + echo "Renaming postgres user if necessary..." + if [ -n "$POSTGRES_USER" ]; then + if [ "$POSTGRES_USER" != "postgres" ]; then + PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" + fi + fi + + echo "Stopping database..." + pg_ctl -w stop + +else + echo "Database already initialized. Skipping initialization..." fi -echo "Stopping database..." -pg_ctl -w stop +if [ "${ROX_SCANNER_DB_INIT}" == "true" ]; then + echo "ROX_SCANNER_DB_INIT is set to true. Exiting..." + exit 0 +else + echo "Database initialized." +fi # Now we can start the database for real. But we will # forward any arguments to the actual entrypoint script echo "Starting database for real..." - -exec /usr/local/bin/docker-entrypoint.sh "$@" \ No newline at end of file +exec /usr/local/bin/docker-entrypoint.sh "$@" diff --git a/image/db/rhel/scripts/docker-entrypoint.sh b/image/db/rhel/scripts/docker-entrypoint.sh index fbc63774b..8163322f7 100755 --- a/image/db/rhel/scripts/docker-entrypoint.sh +++ b/image/db/rhel/scripts/docker-entrypoint.sh @@ -6,10 +6,6 @@ ### ### [1]: https://github.com/docker-library/postgres/blob/master/12/bullseye/docker-entrypoint.sh -if [ -n "$ROX_SCANNER_DB_INIT" ]; then - exit 0 -fi - set -Eeo pipefail # TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) @@ -310,18 +306,6 @@ _main() { fi if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then - ### STACKROX MODIFIED - If we are initializing, then ensure we start from scratch. - if [ -n "$ROX_SCANNER_DB_INIT" ]; then - echo - echo 'Initializing... Clearing any previous data from directories' - echo - - rm -rf "$PGDATA" - if [ -n "${POSTGRES_INITDB_WALDIR:-}" ]; then - rm -rf "$POSTGRES_INITDB_WALDIR" - fi - fi - docker_setup_env # setup data directories and permissions (when run as root) docker_create_db_directories @@ -333,16 +317,6 @@ _main() { exec gosu postgres "$BASH_SOURCE" "$@" fi - ### STACKROX MODIFIED - Sanity check the database does not exist - ### upon initialization. - if [ -n "$ROX_SCANNER_DB_INIT" ] && [ -n "$DATABASE_ALREADY_EXISTS" ]; then - echo - echo 'PostgreSQL Database appears to already exist upon initialization; Exiting with error...' - echo - - exit 1 - fi - # only run initialization on an empty data directory if [ -z "$DATABASE_ALREADY_EXISTS" ]; then docker_verify_minimum_env From 1163c59a387916c21d1f780c48c3416161579b57 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 14:31:36 +0200 Subject: [PATCH 05/10] ROX-19980 fix --- image/db/rhel/Dockerfile.slim | 11 ++++++++++- image/db/rhel/scripts/custom-entrypoint.sh | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/image/db/rhel/Dockerfile.slim b/image/db/rhel/Dockerfile.slim index 4faa80a37..f3ec9628c 100644 --- a/image/db/rhel/Dockerfile.slim +++ b/image/db/rhel/Dockerfile.slim @@ -59,7 +59,16 @@ USER 70:70 ENV ROX_SLIM_MODE="true" -ENTRYPOINT ["docker-entrypoint.sh"] +COPY scripts/custom-entrypoint.sh /usr/local/bin/ +COPY scripts/start-db.sh /usr/local/bin/ + +RUN /usr/local/bin/start-db.sh +USER root +RUN rm -rf /usr/local/bin/start-db.sh && \ + rm -rf /docker-entrypoint-initdb.d/* +USER 70:70 +ENV DATABASE_ALREADY_EXISTS=true +ENTRYPOINT ["custom-entrypoint.sh"] EXPOSE 5432 CMD ["postgres", "-c", "config_file=/etc/postgresql.conf"] diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index da37245a3..1bb1b2a1c 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -32,7 +32,7 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then if [ -n "$POSTGRES_PASSWORD" ]; then PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then - PGPASSWORD=$(cat "$POSTGRES_PASSWORD_FILE") psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" + PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$(cat "$POSTGRES_PASSWORD_FILE")';" fi echo "Renaming postgres user if necessary..." From 7e434d144b6c47ff974911fe35ace33aaa899d86 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 14:50:54 +0200 Subject: [PATCH 06/10] ROX-19980 fix --- image/db/rhel/Dockerfile.slim | 2 +- image/db/rhel/scripts/custom-entrypoint.sh | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/image/db/rhel/Dockerfile.slim b/image/db/rhel/Dockerfile.slim index f3ec9628c..5be32572d 100644 --- a/image/db/rhel/Dockerfile.slim +++ b/image/db/rhel/Dockerfile.slim @@ -36,7 +36,7 @@ RUN curl -sSLf https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-${ groupadd -g 70 postgres && \ adduser postgres -u 70 -g 70 -d /var/lib/postgresql -s /bin/sh && \ microdnf install \ - ca-certificates libicu systemd-sysv glibc-locale-source glibc-langpack-en \ + ca-certificates tar libicu systemd-sysv glibc-locale-source glibc-langpack-en \ postgresql12-server && \ # The removal of /usr/share/zoneinfo from UBI minimal images is intentional. # After building the image, the image is reduced in size as much as possible, diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index 1bb1b2a1c..3fa66955f 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -38,7 +38,11 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then echo "Renaming postgres user if necessary..." if [ -n "$POSTGRES_USER" ]; then if [ "$POSTGRES_USER" != "postgres" ]; then - PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" + if [ -n "$POSTGRES_PASSWORD" ]; then + PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" + elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then + PGPASSWORD="$(cat "$POSTGRES_PASSWORD_FILE")" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;" + fi fi fi From d710e771b413dbb6b8b1879dffba4087777b8ec5 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 16:54:21 +0200 Subject: [PATCH 07/10] ROX-19980 fix --- image/db/rhel/scripts/custom-entrypoint.sh | 26 ++++++++++++++-------- image/db/rhel/scripts/start-db.sh | 13 ++++++----- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index 3fa66955f..d18e87914 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -8,17 +8,25 @@ set -e if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then - echo "Creating /var/lib/postgresql/data/pgdata..." - mkdir -p /var/lib/postgresql/data/pgdata - - echo "Moving archive to target directory..." - mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz + # This uses the "archival" methoc + # echo "Creating /var/lib/postgresql/data/pgdata..." + # mkdir -p /var/lib/postgresql/data/pgdata + # + # echo "Moving archive to target directory..." + # mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz + # + # echo "Uncompressing into /var/lib/pgsql/data/pgdata..." + # tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata + # + # echo "Removing archive..." + # rm /var/lib/postgresql/data/pgdata/data.tar.gz - echo "Uncompressing into /var/lib/pgsql/data/pgdata..." - tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata + # SYMLINK METHOD + echo "Creating /var/lib/postgresql/data/pgdata..." + mkdir -p /var/lib/postgresql/data - echo "Removing archive..." - rm /var/lib/postgresql/data/pgdata/data.tar.gz + echo "Create a symbolic link from /var/lib/postgresql/data/pgdata to /tmp/data" + ln -s /tmp/data /var/lib/postgresql/data/pgdata echo "Starting database..." POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & diff --git a/image/db/rhel/scripts/start-db.sh b/image/db/rhel/scripts/start-db.sh index e3f2d00bd..91e0e3738 100755 --- a/image/db/rhel/scripts/start-db.sh +++ b/image/db/rhel/scripts/start-db.sh @@ -15,14 +15,15 @@ echo "Starting database..." PGDATA=/tmp/data POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf echo "Waiting for database to stop..." -while [ -f /tmp/data/pgdata/postmaster.pid ]; do +while [ -f /tmp/data/postmaster.pid ]; do sleep 1 done rm /tmp/postgres.conf -echo "Compressing database data folder..." -tar -czf /tmp/data.tar.gz -C /tmp/data . - -echo "Removing temporary PGDATA directory..." -rm -rf /tmp/data +# ARCHIVE METHOD +# echo "Compressing database data folder..." +# tar -czf /tmp/data.tar.gz -C /tmp/data . +# +# echo "Removing temporary PGDATA directory..." +# rm -rf /tmp/data From d453bf03cc6f91c0a681d5fbc0b61d3544834d28 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 17:49:30 +0200 Subject: [PATCH 08/10] ROX-19980 fix --- image/db/rhel/scripts/custom-entrypoint.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index d18e87914..b61e9900f 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -8,7 +8,7 @@ set -e if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then - # This uses the "archival" methoc + # ARCHIVAL METHOD # echo "Creating /var/lib/postgresql/data/pgdata..." # mkdir -p /var/lib/postgresql/data/pgdata # @@ -20,13 +20,14 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then # # echo "Removing archive..." # rm /var/lib/postgresql/data/pgdata/data.tar.gz + # END ARCHIVAL METHOD # SYMLINK METHOD echo "Creating /var/lib/postgresql/data/pgdata..." mkdir -p /var/lib/postgresql/data - echo "Create a symbolic link from /var/lib/postgresql/data/pgdata to /tmp/data" ln -s /tmp/data /var/lib/postgresql/data/pgdata + # END SYMLINK METHOD echo "Starting database..." POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & @@ -36,11 +37,14 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then sleep 1 done - echo "Changing password if POSTGRES_PASSWORD is set..." if [ -n "$POSTGRES_PASSWORD" ]; then + echo "Changing password via POSTGRES_PASSWORD environment variable..." PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then + echo "Changing password via POSTGRES_PASSWORD_FILE environment variable..." PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$(cat "$POSTGRES_PASSWORD_FILE")';" + else + echo "No password set. Skipping password change..." fi echo "Renaming postgres user if necessary..." From 641e9e7233eff04fcaac757fd668cf176c2e0c70 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 18:31:25 +0200 Subject: [PATCH 09/10] Revert "ROX-19980 fix" This reverts commit d453bf03cc6f91c0a681d5fbc0b61d3544834d28. --- image/db/rhel/scripts/custom-entrypoint.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index b61e9900f..d18e87914 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -8,7 +8,7 @@ set -e if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then - # ARCHIVAL METHOD + # This uses the "archival" methoc # echo "Creating /var/lib/postgresql/data/pgdata..." # mkdir -p /var/lib/postgresql/data/pgdata # @@ -20,14 +20,13 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then # # echo "Removing archive..." # rm /var/lib/postgresql/data/pgdata/data.tar.gz - # END ARCHIVAL METHOD # SYMLINK METHOD echo "Creating /var/lib/postgresql/data/pgdata..." mkdir -p /var/lib/postgresql/data + echo "Create a symbolic link from /var/lib/postgresql/data/pgdata to /tmp/data" ln -s /tmp/data /var/lib/postgresql/data/pgdata - # END SYMLINK METHOD echo "Starting database..." POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & @@ -37,14 +36,11 @@ if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then sleep 1 done + echo "Changing password if POSTGRES_PASSWORD is set..." if [ -n "$POSTGRES_PASSWORD" ]; then - echo "Changing password via POSTGRES_PASSWORD environment variable..." PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" elif [ -n "$POSTGRES_PASSWORD_FILE" ]; then - echo "Changing password via POSTGRES_PASSWORD_FILE environment variable..." PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$(cat "$POSTGRES_PASSWORD_FILE")';" - else - echo "No password set. Skipping password change..." fi echo "Renaming postgres user if necessary..." From 4328d2f40225d135bd0267883630529f39513219 Mon Sep 17 00:00:00 2001 From: Ludovic Cleroux Date: Fri, 27 Oct 2023 18:31:25 +0200 Subject: [PATCH 10/10] Revert "ROX-19980 fix" This reverts commit d710e771b413dbb6b8b1879dffba4087777b8ec5. --- image/db/rhel/scripts/custom-entrypoint.sh | 26 ++++++++-------------- image/db/rhel/scripts/start-db.sh | 13 +++++------ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/image/db/rhel/scripts/custom-entrypoint.sh b/image/db/rhel/scripts/custom-entrypoint.sh index d18e87914..3fa66955f 100755 --- a/image/db/rhel/scripts/custom-entrypoint.sh +++ b/image/db/rhel/scripts/custom-entrypoint.sh @@ -8,25 +8,17 @@ set -e if [ ! -d "/var/lib/postgresql/data/pgdata" ]; then - # This uses the "archival" methoc - # echo "Creating /var/lib/postgresql/data/pgdata..." - # mkdir -p /var/lib/postgresql/data/pgdata - # - # echo "Moving archive to target directory..." - # mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz - # - # echo "Uncompressing into /var/lib/pgsql/data/pgdata..." - # tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata - # - # echo "Removing archive..." - # rm /var/lib/postgresql/data/pgdata/data.tar.gz - - # SYMLINK METHOD echo "Creating /var/lib/postgresql/data/pgdata..." - mkdir -p /var/lib/postgresql/data + mkdir -p /var/lib/postgresql/data/pgdata + + echo "Moving archive to target directory..." + mv /tmp/data.tar.gz /var/lib/postgresql/data/pgdata/data.tar.gz + + echo "Uncompressing into /var/lib/pgsql/data/pgdata..." + tar -xzf /var/lib/postgresql/data/pgdata/data.tar.gz -C /var/lib/postgresql/data/pgdata - echo "Create a symbolic link from /var/lib/postgresql/data/pgdata to /tmp/data" - ln -s /tmp/data /var/lib/postgresql/data/pgdata + echo "Removing archive..." + rm /var/lib/postgresql/data/pgdata/data.tar.gz echo "Starting database..." POSTGRES_PASSWORD_FILE="" POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf & diff --git a/image/db/rhel/scripts/start-db.sh b/image/db/rhel/scripts/start-db.sh index 91e0e3738..e3f2d00bd 100755 --- a/image/db/rhel/scripts/start-db.sh +++ b/image/db/rhel/scripts/start-db.sh @@ -15,15 +15,14 @@ echo "Starting database..." PGDATA=/tmp/data POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf echo "Waiting for database to stop..." -while [ -f /tmp/data/postmaster.pid ]; do +while [ -f /tmp/data/pgdata/postmaster.pid ]; do sleep 1 done rm /tmp/postgres.conf -# ARCHIVE METHOD -# echo "Compressing database data folder..." -# tar -czf /tmp/data.tar.gz -C /tmp/data . -# -# echo "Removing temporary PGDATA directory..." -# rm -rf /tmp/data +echo "Compressing database data folder..." +tar -czf /tmp/data.tar.gz -C /tmp/data . + +echo "Removing temporary PGDATA directory..." +rm -rf /tmp/data