Files
ocf-managed-container/refactored/managed-container.d/promote.sh
T

95 lines
2.6 KiB
Bash

# Sourced by managed-container; uses the shared OCF and parameter environment.
managed_promote()
{
local monitor_rc role i
managed_monitor
monitor_rc=$?
case "${monitor_rc}" in
"${OCF_RUNNING_PROMOTED}")
ocf_log info \
"PostgreSQL is already primary; reconciling primary invariants"
if ! ensure_local_ha_slots; then
ocf_exit_reason \
"HA slot reconciliation failed on existing primary"
return "${OCF_ERR_GENERIC}"
fi
if ! ensure_rewind_privileges; then
ocf_exit_reason \
"pg_rewind privilege reconciliation failed on existing primary"
return "${OCF_ERR_GENERIC}"
fi
return "${OCF_SUCCESS}"
;;
"${OCF_SUCCESS}")
# Healthy standby: continue with promotion.
;;
"${OCF_NOT_RUNNING}")
ocf_exit_reason \
"Cannot promote PostgreSQL because the resource is not running"
return "${OCF_ERR_GENERIC}"
;;
*)
ocf_exit_reason \
"Cannot promote unhealthy PostgreSQL instance (monitor rc=${monitor_rc})"
return "${monitor_rc}"
;;
esac
ocf_log info \
"Promoting PostgreSQL standby inside ${CONTAINER_NAME}"
if ! pg_command '
BINDIR="$(pg_config --bindir)" || exit 1
exec "$BINDIR/pg_ctl" \
-D "$PGDATA" \
promote -w
'
then
ocf_exit_reason "pg_ctl promote failed"
return "${OCF_ERR_GENERIC}"
fi
for i in $(seq 1 30); do
role="$(pg_role 2>/dev/null)" || {
sleep 1
continue
}
if [ "${role}" = "f" ]; then
ocf_log info \
"PostgreSQL became primary; reconciling primary invariants"
if ! ensure_local_ha_slots; then
ocf_exit_reason \
"PostgreSQL promoted but HA slot reconciliation failed"
return "${OCF_ERR_GENERIC}"
fi
if ! ensure_rewind_privileges; then
ocf_exit_reason \
"PostgreSQL promoted but pg_rewind privilege reconciliation failed"
return "${OCF_ERR_GENERIC}"
fi
ocf_log info \
"PostgreSQL promotion completed successfully"
return "${OCF_SUCCESS}"
fi
sleep 1
done
ocf_exit_reason \
"PostgreSQL did not become primary after promotion"
return "${OCF_ERR_GENERIC}"
}