-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpgautoupgrade-healthcheck.sh
executable file
·38 lines (30 loc) · 1.12 KB
/
pgautoupgrade-healthcheck.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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"
# Check if an upgrade is in progress and keep the container alive
if [ -f "$UPGRADE_LOCK_FILE" ]; then
exit 0
fi
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=$?
if [ $PG_ISREADY_STATUS -eq 0 ]; then
exit 0
else
# Exit with the status of pg_isready
exit $PG_ISREADY_STATUS
fi