From 90d61411063487778d0531815419cb503368e3ac Mon Sep 17 00:00:00 2001 From: Wuast94 Date: Thu, 10 Oct 2024 20:31:23 +0000 Subject: [PATCH 1/4] Add upgrade lock file and fail if existing on fresh run --- docker-entrypoint.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 538ad9f..d1ef62e 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -2,6 +2,9 @@ set -Eeo pipefail # TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables) +# Define the path to the upgrade lock file using PGDATA if set, otherwise default +UPGRADE_LOCK_FILE="${PGDATA:-/var/lib/postgresql}/upgrade_in_progress.lock" + # usage: file_env VAR [DEFAULT] # ie: file_env 'XYZ_DB_PASSWORD' 'example' # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of @@ -308,6 +311,18 @@ get_bin_path() { fi } +# Function to create the upgrade lock file +create_upgrade_lock_file() { + echo "Creating upgrade lock file at $UPGRADE_LOCK_FILE" + touch "$UPGRADE_LOCK_FILE" +} + +# Function to remove the upgrade lock file +remove_upgrade_lock_file() { + echo "Removing upgrade lock file at $UPGRADE_LOCK_FILE" + rm -f "$UPGRADE_LOCK_FILE" +} + _main() { # if first arg looks like a flag, assume we want to run postgres server if [ "${1:0:1}" = '-' ]; then @@ -382,6 +397,7 @@ _main() { # If the version of PostgreSQL data files doesn't match our desired version, then upgrade them if [ "${PGVER}" != "${PGTARGET}" ]; then + create_upgrade_lock_file # Ensure the database files are a version we can upgrade local RECOGNISED=0 local OLDPATH=unset @@ -581,6 +597,7 @@ _main() { echo "The database has not yet been reindexed nor updated the query planner stats. Those " echo "will be done by a background task shortly. " echo "***************************************************************************************" + remove_upgrade_lock_file fi ### The main pgautoupgrade scripting ends here ### @@ -625,6 +642,12 @@ _main() { sync } +# Check if an upgrade lock file exists at script start and exit if it does +if [ -f "$UPGRADE_LOCK_FILE" ]; then + echo "Upgrade lock file already exists, indicating an incomplete previous upgrade. Exiting." + exit 1 +fi + if ! _is_sourced; then _main "$@" fi From 0538cc884af4cfc3684528ee208fbfb0ebcdcce9 Mon Sep 17 00:00:00 2001 From: Wuast94 Date: Thu, 10 Oct 2024 20:47:35 +0000 Subject: [PATCH 2/4] add healthcheck --- Dockerfile.alpine | 4 +++- healthcheck.sh | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 healthcheck.sh diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 59bd432..6e99fc1 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -180,11 +180,13 @@ RUN apk update && \ ENV PGTARGET=${PGTARGET} # Copy across the post-upgrade shell script -COPY pgautoupgrade-postupgrade.sh /usr/local/bin/ +COPY pgautoupgrade-postupgrade.sh healthcheck.sh /usr/local/bin/ # Set up the script run by the container when it starts WORKDIR /var/lib/postgresql COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] +HEALTHCHECK CMD /usr/local/bin/healthcheck-script.sh + CMD ["postgres"] diff --git a/healthcheck.sh b/healthcheck.sh new file mode 100644 index 0000000..cd98ebc --- /dev/null +++ b/healthcheck.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +# Define the path to the upgrade lock file using PGDATA if set, otherwise default +UPGRADE_LOCK_FILE="${PGDATA:-/var/lib/postgresql}/upgrade_in_progress.lock" + +# Check if an upgrade is in progress and keep the container alive +if [ -f "$UPGRADE_LOCK_FILE" ]; then + exit 0 +fi + +pg_isready -d "${POSTGRES_DB}" -U "${POSTGRES_USER}" + +# Capture the exit status of pg_isready +PG_ISREADY_STATUS=$? + +if [ $PG_ISREADY_STATUS -eq 0 ]; then + exit 0 +else + # Exit with the status of pg_isready + exit $PG_ISREADY_STATUS +fi From 77dc10acdd3f30a3bfd04ea4bf784c5e9645f0d7 Mon Sep 17 00:00:00 2001 From: Wuast94 Date: Thu, 10 Oct 2024 21:00:34 +0000 Subject: [PATCH 3/4] added healthcheck to bookworm container --- Dockerfile.bookworm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile.bookworm b/Dockerfile.bookworm index 4e7aecb..9a1aa40 100644 --- a/Dockerfile.bookworm +++ b/Dockerfile.bookworm @@ -175,11 +175,13 @@ RUN apt update && \ ENV PGTARGET=${PGTARGET} # Copy across the post-upgrade shell script -COPY pgautoupgrade-postupgrade.sh /usr/local/bin/ +COPY pgautoupgrade-postupgrade.sh healthcheck.sh /usr/local/bin/ # Set up the script run by the container when it starts WORKDIR /var/lib/postgresql COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] +HEALTHCHECK CMD /usr/local/bin/healthcheck-script.sh + CMD ["postgres"] From edf838f5233ed8e59559bdfd87a2462ad5827a58 Mon Sep 17 00:00:00 2001 From: Wuast94 Date: Fri, 11 Oct 2024 08:59:56 +0000 Subject: [PATCH 4/4] Fixed healthcheck.sh path --- Dockerfile.alpine | 2 +- Dockerfile.bookworm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 6e99fc1..fb377de 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -187,6 +187,6 @@ WORKDIR /var/lib/postgresql COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] -HEALTHCHECK CMD /usr/local/bin/healthcheck-script.sh +HEALTHCHECK CMD /usr/local/bin/healthcheck.sh CMD ["postgres"] diff --git a/Dockerfile.bookworm b/Dockerfile.bookworm index 9a1aa40..b11f689 100644 --- a/Dockerfile.bookworm +++ b/Dockerfile.bookworm @@ -182,6 +182,6 @@ WORKDIR /var/lib/postgresql COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] -HEALTHCHECK CMD /usr/local/bin/healthcheck-script.sh +HEALTHCHECK CMD /usr/local/bin/healthcheck.sh CMD ["postgres"]