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

[bitnami/postgresql-repmgr] Support running scripts on primary node when container starts #70737

Closed
11 changes: 10 additions & 1 deletion bitnami/moodle/4.1/debian-12/rootfs/post-init.d/sql-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ else
. /opt/bitnami/scripts/liblog.sh
fi

MARIADB_HOST=${MARIADB_HOST:-$MOODLE_DATABASE_HOST}
MARIADB_PORT_NUMBER=${MARIADB_PORT_NUMBER:-$MOODLE_DATABASE_PORT_NUMBER}
MARIADB_ROOT_USER=${MARIADB_ROOT_USER:-$MOODLE_DATABASE_USER}
MARIADB_DATABASE=${MARIADB_DATABASE:-$MOODLE_DATABASE_NAME}
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-$MOODLE_DATABASE_PASSWORD}
fi

mysql_execute() {
local -r sql_file="${1:?missing file}"
local failure=0
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER")
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER" "-D" "${MARIADB_DATABASE}")
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
mysql_cmd+=("-p${MARIADB_ROOT_PASSWORD}")
fi
Expand All @@ -33,6 +41,7 @@ mysql_execute() {
return "$failure"
}


# Loop through all input files passed via stdin
read -r -a custom_init_scripts <<< "$@"
failure=0
Expand Down
11 changes: 10 additions & 1 deletion bitnami/moodle/4.2/debian-12/rootfs/post-init.d/sql-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ else
. /opt/bitnami/scripts/liblog.sh
fi

MARIADB_HOST=${MARIADB_HOST:-$MOODLE_DATABASE_HOST}
MARIADB_PORT_NUMBER=${MARIADB_PORT_NUMBER:-$MOODLE_DATABASE_PORT_NUMBER}
MARIADB_ROOT_USER=${MARIADB_ROOT_USER:-$MOODLE_DATABASE_USER}
MARIADB_DATABASE=${MARIADB_DATABASE:-$MOODLE_DATABASE_NAME}
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-$MOODLE_DATABASE_PASSWORD}
fi

mysql_execute() {
local -r sql_file="${1:?missing file}"
local failure=0
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER")
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER" "-D" "${MARIADB_DATABASE}")
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
mysql_cmd+=("-p${MARIADB_ROOT_PASSWORD}")
fi
Expand All @@ -33,6 +41,7 @@ mysql_execute() {
return "$failure"
}


# Loop through all input files passed via stdin
read -r -a custom_init_scripts <<< "$@"
failure=0
Expand Down
11 changes: 10 additions & 1 deletion bitnami/moodle/4.3/debian-12/rootfs/post-init.d/sql-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ else
. /opt/bitnami/scripts/liblog.sh
fi

MARIADB_HOST=${MARIADB_HOST:-$MOODLE_DATABASE_HOST}
MARIADB_PORT_NUMBER=${MARIADB_PORT_NUMBER:-$MOODLE_DATABASE_PORT_NUMBER}
MARIADB_ROOT_USER=${MARIADB_ROOT_USER:-$MOODLE_DATABASE_USER}
MARIADB_DATABASE=${MARIADB_DATABASE:-$MOODLE_DATABASE_NAME}
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-$MOODLE_DATABASE_PASSWORD}
fi

mysql_execute() {
local -r sql_file="${1:?missing file}"
local failure=0
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER")
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER" "-D" "${MARIADB_DATABASE}")
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
mysql_cmd+=("-p${MARIADB_ROOT_PASSWORD}")
fi
Expand All @@ -33,6 +41,7 @@ mysql_execute() {
return "$failure"
}


# Loop through all input files passed via stdin
read -r -a custom_init_scripts <<< "$@"
failure=0
Expand Down
11 changes: 10 additions & 1 deletion bitnami/moodle/4.4/debian-12/rootfs/post-init.d/sql-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ else
. /opt/bitnami/scripts/liblog.sh
fi

MARIADB_HOST=${MARIADB_HOST:-$MOODLE_DATABASE_HOST}
MARIADB_PORT_NUMBER=${MARIADB_PORT_NUMBER:-$MOODLE_DATABASE_PORT_NUMBER}
MARIADB_ROOT_USER=${MARIADB_ROOT_USER:-$MOODLE_DATABASE_USER}
MARIADB_DATABASE=${MARIADB_DATABASE:-$MOODLE_DATABASE_NAME}
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD:-$MOODLE_DATABASE_PASSWORD}
fi

mysql_execute() {
local -r sql_file="${1:?missing file}"
local failure=0
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER")
mysql_cmd=("mysql" "-h" "$MARIADB_HOST" "-P" "$MARIADB_PORT_NUMBER" "-u" "$MARIADB_ROOT_USER" "-D" "${MARIADB_DATABASE}")
if [[ "${ALLOW_EMPTY_PASSWORD:-no}" != "yes" ]]; then
mysql_cmd+=("-p${MARIADB_ROOT_PASSWORD}")
fi
Expand All @@ -33,6 +41,7 @@ mysql_execute() {
return "$failure"
}


# Loop through all input files passed via stdin
read -r -a custom_init_scripts <<< "$@"
failure=0
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
Loading
Loading