Skip to content

Commit

Permalink
Add support for running scripts on primary node every time container …
Browse files Browse the repository at this point in the history
…is started (#70606)

Signed-off-by: Thomas Lønskov Luther <rasmussen.thomas@gmail.com>
  • Loading branch information
ThomasRasmussen committed Aug 7, 2024
1 parent 891a5f6 commit 4074201
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ postgresql_custom_pre_init_scripts() {
fi
}

########################
# Run custom start scripts
# Globals:
# POSTGRESQL_*
# Arguments:
# None
# Returns:
# None
#########################
postgresql_custom_start_scripts() {
info "Loading custom start scripts..."
if [[ -d "$POSTGRESQL_STARTSCRIPTS_DIR" ]] && [[ -n $(find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)") ]] ; then
info "Loading user's custom files from $POSTGRESQL_STARTSCRIPTS_DIR ..."
postgresql_start_bg "false"
find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
debug "Executing $f"
"$f"
else
debug "Sourcing $f"
. "$f"
fi
;;
*.sql)
debug "Executing $f"
postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD" <"$f"
;;
*.sql.gz)
debug "Executing $f"
gunzip -c "$f" | postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD"
;;
*) debug "Ignoring $f" ;;
esac
done
touch "$POSTGRESQL_VOLUME_DIR"/.user_scripts_initialized
fi
}

########################
# Run custom initialization scripts
# Globals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ repmgr_initialize() {
else
debug "Skipping repmgr configuration..."
fi
# Allow running custom start scripts - always run after initialization
postgresql_custom_start_scripts
elif [[ "$REPMGR_ROLE" = "standby" ]]; then
local -r psql_major_version="$(postgresql_get_major_version)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export POSTGRESQL_TMP_DIR="$POSTGRESQL_BASE_DIR/tmp"
export POSTGRESQL_PID_FILE="$POSTGRESQL_TMP_DIR/postgresql.pid"
export POSTGRESQL_BIN_DIR="$POSTGRESQL_BASE_DIR/bin"
export POSTGRESQL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export POSTGRESQL_STARTSCRIPTS_DIR="/docker-entrypoint-startdb.d"
export POSTGRESQL_PREINITSCRIPTS_DIR="/docker-entrypoint-preinitdb.d"
export PATH="${POSTGRESQL_BIN_DIR}:${BITNAMI_ROOT_DIR}/common/bin:${PATH}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ postgresql_custom_pre_init_scripts() {
fi
}

########################
# Run custom start scripts
# Globals:
# POSTGRESQL_*
# Arguments:
# None
# Returns:
# None
#########################
postgresql_custom_start_scripts() {
info "Loading custom start scripts..."
if [[ -d "$POSTGRESQL_STARTSCRIPTS_DIR" ]] && [[ -n $(find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)") ]] ; then
info "Loading user's custom files from $POSTGRESQL_STARTSCRIPTS_DIR ..."
postgresql_start_bg "false"
find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
debug "Executing $f"
"$f"
else
debug "Sourcing $f"
. "$f"
fi
;;
*.sql)
debug "Executing $f"
postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD" <"$f"
;;
*.sql.gz)
debug "Executing $f"
gunzip -c "$f" | postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD"
;;
*) debug "Ignoring $f" ;;
esac
done
touch "$POSTGRESQL_VOLUME_DIR"/.user_scripts_initialized
fi
}

########################
# Run custom initialization scripts
# Globals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ repmgr_initialize() {
else
debug "Skipping repmgr configuration..."
fi
# Allow running custom start scripts - always run after initialization
postgresql_custom_start_scripts
elif [[ "$REPMGR_ROLE" = "standby" ]]; then
local -r psql_major_version="$(postgresql_get_major_version)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export POSTGRESQL_TMP_DIR="$POSTGRESQL_BASE_DIR/tmp"
export POSTGRESQL_PID_FILE="$POSTGRESQL_TMP_DIR/postgresql.pid"
export POSTGRESQL_BIN_DIR="$POSTGRESQL_BASE_DIR/bin"
export POSTGRESQL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export POSTGRESQL_STARTSCRIPTS_DIR="/docker-entrypoint-startdb.d"
export POSTGRESQL_PREINITSCRIPTS_DIR="/docker-entrypoint-preinitdb.d"
export PATH="${POSTGRESQL_BIN_DIR}:${BITNAMI_ROOT_DIR}/common/bin:${PATH}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ postgresql_custom_pre_init_scripts() {
fi
}

########################
# Run custom start scripts
# Globals:
# POSTGRESQL_*
# Arguments:
# None
# Returns:
# None
#########################
postgresql_custom_start_scripts() {
info "Loading custom start scripts..."
if [[ -d "$POSTGRESQL_STARTSCRIPTS_DIR" ]] && [[ -n $(find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)") ]] ; then
info "Loading user's custom files from $POSTGRESQL_STARTSCRIPTS_DIR ..."
postgresql_start_bg "false"
find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
debug "Executing $f"
"$f"
else
debug "Sourcing $f"
. "$f"
fi
;;
*.sql)
debug "Executing $f"
postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD" <"$f"
;;
*.sql.gz)
debug "Executing $f"
gunzip -c "$f" | postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD"
;;
*) debug "Ignoring $f" ;;
esac
done
touch "$POSTGRESQL_VOLUME_DIR"/.user_scripts_initialized
fi
}

########################
# Run custom initialization scripts
# Globals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ repmgr_initialize() {
else
debug "Skipping repmgr configuration..."
fi
# Allow running custom start scripts - always run after initialization
postgresql_custom_start_scripts
elif [[ "$REPMGR_ROLE" = "standby" ]]; then
local -r psql_major_version="$(postgresql_get_major_version)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export POSTGRESQL_TMP_DIR="$POSTGRESQL_BASE_DIR/tmp"
export POSTGRESQL_PID_FILE="$POSTGRESQL_TMP_DIR/postgresql.pid"
export POSTGRESQL_BIN_DIR="$POSTGRESQL_BASE_DIR/bin"
export POSTGRESQL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export POSTGRESQL_STARTSCRIPTS_DIR="/docker-entrypoint-startdb.d"
export POSTGRESQL_PREINITSCRIPTS_DIR="/docker-entrypoint-preinitdb.d"
export PATH="${POSTGRESQL_BIN_DIR}:${BITNAMI_ROOT_DIR}/common/bin:${PATH}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ postgresql_custom_pre_init_scripts() {
fi
}

########################
# Run custom start scripts
# Globals:
# POSTGRESQL_*
# Arguments:
# None
# Returns:
# None
#########################
postgresql_custom_start_scripts() {
info "Loading custom start scripts..."
if [[ -d "$POSTGRESQL_STARTSCRIPTS_DIR" ]] && [[ -n $(find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)") ]] ; then
info "Loading user's custom files from $POSTGRESQL_STARTSCRIPTS_DIR ..."
postgresql_start_bg "false"
find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
debug "Executing $f"
"$f"
else
debug "Sourcing $f"
. "$f"
fi
;;
*.sql)
debug "Executing $f"
postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD" <"$f"
;;
*.sql.gz)
debug "Executing $f"
gunzip -c "$f" | postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD"
;;
*) debug "Ignoring $f" ;;
esac
done
touch "$POSTGRESQL_VOLUME_DIR"/.user_scripts_initialized
fi
}

########################
# Run custom initialization scripts
# Globals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ repmgr_initialize() {
else
debug "Skipping repmgr configuration..."
fi
# Allow running custom start scripts - always run after initialization
postgresql_custom_start_scripts
elif [[ "$REPMGR_ROLE" = "standby" ]]; then
local -r psql_major_version="$(postgresql_get_major_version)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export POSTGRESQL_TMP_DIR="$POSTGRESQL_BASE_DIR/tmp"
export POSTGRESQL_PID_FILE="$POSTGRESQL_TMP_DIR/postgresql.pid"
export POSTGRESQL_BIN_DIR="$POSTGRESQL_BASE_DIR/bin"
export POSTGRESQL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export POSTGRESQL_STARTSCRIPTS_DIR="/docker-entrypoint-startdb.d"
export POSTGRESQL_PREINITSCRIPTS_DIR="/docker-entrypoint-preinitdb.d"
export PATH="${POSTGRESQL_BIN_DIR}:${BITNAMI_ROOT_DIR}/common/bin:${PATH}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ postgresql_custom_pre_init_scripts() {
fi
}

########################
# Run custom start scripts
# Globals:
# POSTGRESQL_*
# Arguments:
# None
# Returns:
# None
#########################
postgresql_custom_start_scripts() {
info "Loading custom start scripts..."
if [[ -d "$POSTGRESQL_STARTSCRIPTS_DIR" ]] && [[ -n $(find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)") ]] ; then
info "Loading user's custom files from $POSTGRESQL_STARTSCRIPTS_DIR ..."
postgresql_start_bg "false"
find "$POSTGRESQL_STARTSCRIPTS_DIR/" -type f -regex ".*\.\(sh\|sql\|sql.gz\)" | sort | while read -r f; do
case "$f" in
*.sh)
if [[ -x "$f" ]]; then
debug "Executing $f"
"$f"
else
debug "Sourcing $f"
. "$f"
fi
;;
*.sql)
debug "Executing $f"
postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD" <"$f"
;;
*.sql.gz)
debug "Executing $f"
gunzip -c "$f" | postgresql_execute "$POSTGRESQL_DATABASE" "$POSTGRESQL_INITSCRIPTS_USERNAME" "$POSTGRESQL_INITSCRIPTS_PASSWORD"
;;
*) debug "Ignoring $f" ;;
esac
done
touch "$POSTGRESQL_VOLUME_DIR"/.user_scripts_initialized
fi
}

########################
# Run custom initialization scripts
# Globals:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ repmgr_initialize() {
else
debug "Skipping repmgr configuration..."
fi
# Allow running custom start scripts - always run after initialization
postgresql_custom_start_scripts
elif [[ "$REPMGR_ROLE" = "standby" ]]; then
local -r psql_major_version="$(postgresql_get_major_version)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export POSTGRESQL_TMP_DIR="$POSTGRESQL_BASE_DIR/tmp"
export POSTGRESQL_PID_FILE="$POSTGRESQL_TMP_DIR/postgresql.pid"
export POSTGRESQL_BIN_DIR="$POSTGRESQL_BASE_DIR/bin"
export POSTGRESQL_INITSCRIPTS_DIR="/docker-entrypoint-initdb.d"
export POSTGRESQL_STARTSCRIPTS_DIR="/docker-entrypoint-startdb.d"
export POSTGRESQL_PREINITSCRIPTS_DIR="/docker-entrypoint-preinitdb.d"
export PATH="${POSTGRESQL_BIN_DIR}:${BITNAMI_ROOT_DIR}/common/bin:${PATH}"

Expand Down
1 change: 1 addition & 0 deletions bitnami/postgresql-repmgr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ Refer to [issues/27124](https://github.com/bitnami/containers/issues/27124) for
| `POSTGRESQL_PID_FILE` | PostgreSQL PID file | `$POSTGRESQL_TMP_DIR/postgresql.pid` |
| `POSTGRESQL_BIN_DIR` | PostgreSQL executables directory | `$POSTGRESQL_BASE_DIR/bin` |
| `POSTGRESQL_INITSCRIPTS_DIR` | Init scripts directory | `/docker-entrypoint-initdb.d` |
| `POSTGRESQL_STARTSCRIPTS_DIR` | Start scripts directory | `/docker-entrypoint-startdb.d` |
| `POSTGRESQL_PREINITSCRIPTS_DIR` | Pre-init scripts directory | `/docker-entrypoint-preinitdb.d` |
| `POSTGRESQL_DAEMON_USER` | PostgreSQL system user | `postgres` |
| `POSTGRESQL_DAEMON_GROUP` | PostgreSQL system group | `postgres` |
Expand Down

0 comments on commit 4074201

Please sign in to comment.