#!/bin/bash
set -uo pipefail
#==============================================================#
# File      :   pgsql-svc
# Desc      :   Refresh PGSQL Cluster Services on HAProxy
# Ctime     :   2021-12-03
# Mtime     :   2025-01-11
# Path      :   bin/pgsql-svc
# Deps      :   ansible-playbook, pgsql.yml
# License   :   Apache-2.0 @ https://pigsty.io/docs/about/license/
# Copyright :   2018-2026  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#
APP_NAME="$(basename $0)"
APP_DIR="$(cd $(dirname $0) && pwd)"
PIGSTY_HOME=$(cd $(dirname ${APP_DIR}) && pwd)


#--------------------------------------------------------------#
# Usage
#--------------------------------------------------------------#
# bin/pgsql-svc <cls>          # refresh haproxy services of cluster 'cls'
# bin/pgsql-svc <cls> [ip...]  # refresh services on specific instances


#--------------------------------------------------------------#
# Utils
#--------------------------------------------------------------#
__CN='\033[0m';__CK='\033[0;30m';__CR='\033[0;31m';__CG='\033[0;32m';
__CY='\033[0;33m';__CB='\033[0;34m';__CM='\033[0;35m';__CC='\033[0;36m';__CW='\033[0;37m';
function log_info() {  printf "[${__CG} OK ${__CN}] ${__CG}$*${__CN}\n";   }
function log_warn() {  printf "[${__CY}WARN${__CN}] ${__CY}$*${__CN}\n";   }
function log_error() { printf "[${__CR}FAIL${__CN}] ${__CR}$*${__CN}\n";   }
function log_debug() { printf "[${__CB}HINT${__CN}] ${__CB}$*${__CN}\n"; }
function log_input() { printf "[${__CM} IN ${__CN}] ${__CM}$*\n=> ${__CN}"; }
function log_hint()  { printf "${__CB}$*${__CN}\n"; }
function log_line()  { printf "${__CM}[$*] ===========================================${__CN}\n"; }
function is_valid_ip(){
    if [[ "$1" =~ (([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]) ]]; then
        return 0
    else
        return 1
    fi
}

#--------------------------------------------------------------#
# Param
#--------------------------------------------------------------#
PG_CLUSTER=${1-''}
if [[ -z "${PG_CLUSTER}" ]]; then
	log_error "pg_cluster is empty"
	log_hint "Usage:"
	log_hint "    bin/pgsql-svc <cls>          # refresh haproxy services of cluster 'cls'"
	log_hint "    bin/pgsql-svc <cls> [ip...]  # refresh services on specific instances"
	exit 1
fi


#--------------------------------------------------------------#
# Create Cluster                     [ 1 arg = refresh cluster ]
#--------------------------------------------------------------#
# if only 1 arg is given, arg1 = pg_cluster
# the entire pgsql cluster will be refreshed
if (($# == 1)); then
	log_line "EXECUTE"
	log_warn "reload pgsql services for '${PG_CLUSTER}'"
  log_hint "$ ./pgsql.yml -l '${PG_CLUSTER}' -t pg_service"

  "${PIGSTY_HOME}/pgsql.yml" -l "${PG_CLUSTER}" -t pg_service

  if [[ $? -ne 0 ]]; then
    log_line "FAILURE"
    log_error "fail to reload pgsql services for '${PG_CLUSTER}'"
    exit 2
  fi
  log_line "SUMMARY"
	log_info "reload pgsql services for ${PG_CLUSTER} complete!"
  log_hint "check haproxy admin page: http://i.pigsty/haproxy/${PG_CLUSTER}-n/"
	exit 0
fi



#--------------------------------------------------------------#
# Refresh Specific Instances       [2+ args = refresh instances]
#--------------------------------------------------------------#
# if more than 1 args is given, arg1 = pg_cluster, arg2+ = ip list
# each instance (with corresponding ip) haproxy will be refreshed
IP_LIST=""
TARGET_PATTERN="&${PG_CLUSTER}"
EXISTS_PATTERN="${PG_CLUSTER}"
for ((i=2; i<=$#; i++))
do
  if ! is_valid_ip "${!i}"; then
    log_error "invalid ip address given: ${!i}"
    exit 3
  fi
  IP_LIST="${IP_LIST} ${!i}"
  TARGET_PATTERN="${!i},${TARGET_PATTERN}"
  EXISTS_PATTERN="${EXISTS_PATTERN},!${!i}"
done

# Reload Services
log_line "EXECUTE"
log_warn "reload pgsql services on specific ${PG_CLUSTER} members"
log_hint "$ ./pgsql.yml -l '${TARGET_PATTERN}' -t pg_service"

"${PIGSTY_HOME}/pgsql.yml" -l "${TARGET_PATTERN}" -t pg_service

if [[ $? -ne 0 ]]; then
  log_line "FAILURE"
  log_error "fail to reload pgsql services for '${PG_CLUSTER}'"
  log_warn "current traffic may not be affected immediately"
  log_warn "BUT IT'S VERY IMPORTANT TO FIX THIS ASAP!"
  exit 4
fi
log_line "SUMMARY"
log_info "reload pgsql services for ${IP_LIST} of ${PG_CLUSTER} complete"
log_hint "check haproxy admin page: https://i.pigsty/haproxy/${PG_CLUSTER}-n/"
exit 0