Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Add upgrade lock file and healthcheck to the container #60

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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.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 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.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}/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 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}/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
Loading