#!/bin/bash
set -uo pipefail
#==============================================================#
# File      :   pgmon-rm
# Desc      :   remove monitor targets from infra nodes
# Ctime     :   2023-01-11
# Mtime     :   2025-12-31
# Path      :   bin/pgmon-rm
# Deps      :   ansible
# 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/pgmon-rm <cls|ins> ...  # remove monitor targets from infra nodes


#--------------------------------------------------------------#
# 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
}

if (($# < 1)); then
    log_error "missing arguments: cluster or instance name to be removed from prometheus"
    log_hint "bin/pgmon-rm <cls|ins> ..."
    exit 1
fi

#--------------------------------------------------------------#
# Execute
#--------------------------------------------------------------#
log_line "PLANNING"
for ((i=1; i<=$#; i++))
do
    log_info "remove postgres monitor target: ${!i}"
done

log_info "check current postgres monitor targets: "
log_hint "$ ansible infra -b -a 'ls /infra/targets/{pgsql,pgrds,patroni}/'"
cd "${PIGSTY_HOME}" && ansible infra -b -a 'ls /infra/targets/pgsql/ /infra/targets/pgrds/ /infra/targets/patroni/'

log_line "EXECUTE"
for ((i=1; i<=$#; i++))
do
    log_warn "remove postgres monitor cluster: ${!i}.yml"
    log_hint "$ ansible infra -m file -b -a 'path=/infra/targets/pgsql/${!i}.yml state=absent'"
    cd "${PIGSTY_HOME}" && ansible infra -m file -b -a "path=/infra/targets/pgsql/${!i}.yml state=absent"

    log_hint "$ ansible infra -m file -b -a 'path=/infra/targets/pgrds/${!i}.yml state=absent'"
    cd "${PIGSTY_HOME}" && ansible infra -m file -b -a "path=/infra/targets/pgrds/${!i}.yml state=absent"

    log_hint "$ ansible infra -m file -b -a 'path=/infra/targets/patroni/${!i}.yml state=absent'"
    cd "${PIGSTY_HOME}" && ansible infra -m file -b -a "path=/infra/targets/patroni/${!i}.yml state=absent"
done

log_info "if you want to add them back:"
log_hint "$ ./pgsql.yml         -t pg_register -l <cls|ip>        # regular pgsql cluster"
log_hint "$ ./pgsql-monitor.yml -t pg_register -e clsname=<cls>  # remote rds cluster"
exit 0