Skip to content

Commit

Permalink
Allow to read Postgres username and database from Swarm secret files
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso authored and justinclift committed Feb 12, 2025
1 parent 5ca1f1b commit 485a528
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pgautoupgrade-healthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/usr/bin/env bash

read_env_or_file() {
local var_name="$1"
local file_var_name="${var_name}_FILE"

# Check if the *_FILE environment variable is set and points to a valid file
if [[ -n "${!file_var_name}" && -f "${!file_var_name}" && -s "${!file_var_name}" ]]; then
# Read the content of the file and assign it to the variable
echo "$(cat "${!file_var_name}")"
else
# Fallback to the normal environment variable
echo "${!var_name}"
fi
}

# 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"

Expand All @@ -8,7 +22,10 @@ if [ -f "$UPGRADE_LOCK_FILE" ]; then
exit 0
fi

pg_isready -d "${POSTGRES_DB}" -U "${POSTGRES_USER}"
POSTGRES_DB_VALUE=$(read_env_or_file "POSTGRES_DB")
POSTGRES_USER_VALUE=$(read_env_or_file "POSTGRES_USER")

pg_isready -d "${POSTGRES_DB_VALUE}" -U "${POSTGRES_USER_VALUE}"

# Capture the exit status of pg_isready
PG_ISREADY_STATUS=$?
Expand Down

0 comments on commit 485a528

Please sign in to comment.