#!/bin/bash

namespace=edoc2

WorkNode01=rocky-k8s-node01
WorkNode02=rocky-k8s-node02
WorkNode03=rocky-k8s-node03
SSH_PORT=22
pgsql_ip01="172.17.188.180"
pgsql_ip02="172.17.188.181"
pgsql_ip03="172.17.188.182"
etcd_podip01="172.17.188.190"
etcd_podip02="172.17.188.191"
etcd_podip03="172.17.188.192"
bip_pool="172.17.0.0"

deploy_pgsql() {
    kubectl get pod -n $namespace | grep -v grep | grep pgsql
    if [ $? == 1 ];then
        echo "##### create remote pv dbmgr dir ##################################"
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "mkdir -p /home/data/pgsqldata && mkdir -p /home/data/pgbackup && mkdir -p /home/data/etcddata"
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p /home/data/pgsqldata && mkdir -p /home/data/pgbackup && mkdir -p /home/data/etcddata"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p /home/data/pgsqldata && mkdir -p /home/data/pgbackup && mkdir -p /home/data/etcddata"
        sleep 1
	sed -i "s#172.17.188.180#$pgsql_ip01#g" *.yaml
	sed -i "s#172.17.188.181#$pgsql_ip02#g" *.yaml
	sed -i "s#172.17.188.182#$pgsql_ip03#g" *.yaml
	sed -i "s#172.17.188.190#$etcd_podip01#g" *.yaml
	sed -i "s#172.17.188.191#$etcd_podip02#g" *.yaml
	sed -i "s#172.17.188.192#$etcd_podip03#g" *.yaml
	sed -i "s#172.17.0.0#$bip_pool#g" *.yaml

        sleep 1
        kubectl label nodes $WorkNode01 node.pgsql.labels=pgsql-node01
        kubectl label nodes $WorkNode02 node.pgsql.labels=pgsql-node02
        kubectl label nodes $WorkNode03 node.pgsql.labels=pgsql-node03

        kubectl label nodes $WorkNode01 node.plabels.nodelabels=pMiddleware
        kubectl label nodes $WorkNode02 node.plabels.nodelabels=pMiddleware
        kubectl label nodes $WorkNode03 node.plabels.nodelabels=pMiddleware

        kubectl apply -f 01pgsql-configmap.yaml
	sleep 3
	kubectl apply -f 02etcd1.yaml
	kubectl apply -f 03etcd2.yaml
	kubectl apply -f 04etcd3.yaml
	sleep 15
	kubectl apply -f 05pgsql0.yaml
	kubectl apply -f 06pgsql1.yaml
	kubectl apply -f 07pgsql2.yaml
	sleep 15
	kubectl apply -f 08pg_haproxy.yaml

        sleep 5

        echo "##### show pods status #####################################"
        kubectl get pod -n $namespace | grep -v grep | grep "pgsql"
    else
        echo "##### pgsqldb statefulset service has been exist! ###############"
    fi
}

delete_pgsql() {
    echo "##### start delete pgsqldb statefulset service ##################"
    kubectl delete -f 08pg_haproxy.yaml
    sleep 5
    kubectl delete -f 05pgsql0.yaml
    kubectl delete -f 06pgsql1.yaml
    kubectl delete -f 07pgsql2.yaml
    
    kubectl get pod -n $namespace | grep -v grep | grep "pgsql"
    echo "##### check delete status ##################################"
    while :
    do
        echo "##### check pgsqldb service alive! ###########################"
        kubectl get pod -n $namespace | grep -v grep | grep "pgsql"
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete pgsql statefulset service has successful! ########"
    kubectl delete -f 02etcd1.yaml
    kubectl delete -f 03etcd2.yaml
    kubectl delete -f 04etcd3.yaml
    sleep 1
}

cat <<eof 
****************************************
** pgsql operation start/stop/restart **
****************************************
eof

sleep 3
Action=$1
case $Action in 
    1|start|START)
        echo "begin deploy pgsql ........."
        deploy_pgsql
        ;;
    2|stop|STOP)
        echo "begin stop pgsql ..........."
        delete_pgsql
        ;;
    3|restart|RESTART)
        echo "begin restart pgsql ........"
        delete_pgsql
        sleep 3
        deploy_pgsql
        ;;
    *)
        echo "输入操作不存在 .........."
        #exit;
esac
