Skip to content

Commit

Permalink
Add upgrade lock file and healthcheck to the container (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Wuast94 <[email protected]>
Co-authored-by: Wuast94 <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 2649352 commit 57fa939
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Dockerfile.alpine
Original file line number Diff line number Diff line change
Expand Up @@ -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 pgautoupgrade-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/pgautoupgrade-healthcheck.sh

CMD ["postgres"]
4 changes: 3 additions & 1 deletion Dockerfile.bookworm
Original file line number Diff line number Diff line change
Expand Up @@ -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 pgautoupgrade-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/pgautoupgrade-healthcheck.sh

CMD ["postgres"]
23 changes: 23 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/data}/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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ###
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions pgautoupgrade-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -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/data}/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

0 comments on commit 57fa939

Please sign in to comment.