# Sourced by managed-container; uses the shared OCF and parameter environment. managed_promote() { local monitor_rc role i # Belt-and-suspenders safety: promotion scoring already returns # -INFINITY while this marker exists, but refuse the action itself too. # This protects against an administrative/manual promotion request or a # future scoring regression. if pg_rejoin_pending; then ocf_exit_reason \ "Cannot promote PostgreSQL while rejoin-required is set; instance must rejoin the elected primary first" return "${OCF_ERR_GENERIC}" fi 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 pg_clear_rejoin_pending >/dev/null 2>&1 || true 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 if ! pg_clear_rejoin_pending; then ocf_log warn \ "PostgreSQL promoted successfully but provisional rejoin marker could not be cleared" 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}" }