#!/bin/bash

namespace=$2

WorkNode01=$3
WorkNode02=$4
WorkNode03=$5
SSH_PORT=$6
pv_volume=$7
pv_path="$8"
pv_yaml="rabbitmq-pv.yaml"


deploy_mq() {
    kubectl get pod -n $namespace | grep -v grep | grep rabbitmq
    if [ $? == 1 ];then
        echo "##### create remote pv rabbitmq dir ###############################"
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "mkdir -p $pv_path\"01\""
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p $pv_path\"02\""
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $pv_path\"03\""

        sleep 1
        old_pv_volume=`cat $pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
        old_pv_path=`cat $pv_yaml | grep "path:" | uniq -c | awk -F " " '{print $3}' | awk -F "0" '{print $1}' | uniq -c | awk -F " " '{print $2}'`
        new_pv_path=${pv_path//\//\\\/}
        old_pv_path=${old_pv_path//\//\\\/}

        sed -i "s/$old_pv_volume/$pv_volume/g" *.yaml
        sed -i "s/$old_pv_path/$new_pv_path/g" $pv_yaml

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

        kubectl label nodes $WorkNode01 node.labels.nodelabels=Middleware
        kubectl label nodes $WorkNode02 node.labels.nodelabels=Middleware
        kubectl label nodes $WorkNode03 node.labels.nodelabels=Middleware

        # create sc pv
        kubectl apply -f rabbitmq-sc.yaml
        kubectl apply -f rabbitmq-pv.yaml
        kubectl apply -f rabbitmq-configmap.yaml
        kubectl apply -f rabbitmq-rbac.yaml

        # create statefulset servcie
        result01=$(sshpass ssh -p $SSH_PORT root@$WorkNode01 "ls $pv_path\"01\"/.erlang.cookie 2>/dev/null")
        result02=$(sshpass ssh -p $SSH_PORT root@$WorkNode02 "ls $pv_path\"02\"/.erlang.cookie 2>/dev/null")
        result03=$(sshpass ssh -p $SSH_PORT root@$WorkNode03 "ls $pv_path\"03\"/.erlang.cookie 2>/dev/null")
        if [ -n "$result01" ] && [ -n "$result02" ] && [ -n "$result03" ]; then
            kubectl apply -f after_edoc2_rabbitmq_cluster.yaml
        else
            kubectl apply -f edoc2_rabbitmq_cluster.yaml
        fi

        sleep 30
        echo "##### check edoc2 first rabbitmq status ##################################"
        while :
        do
            echo "##### wait check edoc2 rabbitmq service count = 3 ! ###########################"
            rest_count=$(kubectl get pod -n $namespace | grep rabbitmq | grep "1/1" | wc -l)
            if [ "$rest_count" == "3" ];then
                echo "##### check after_edoc2_rabbitmq_cluster.yaml! ###########################"
                kubectl apply -f after_edoc2_rabbitmq_cluster.yaml
                sleep 3
                break
            fi
            sleep 10
        done;

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

delete_mq() {
    echo "##### start delete rabbitmq statefulset service ##################"
    # delete statefulset servcie
    result01=$(sshpass ssh -p $SSH_PORT root@$WorkNode01 "ls $pv_path\"01\"/.erlang.cookie 2>/dev/null")
    result02=$(sshpass ssh -p $SSH_PORT root@$WorkNode02 "ls $pv_path\"02\"/.erlang.cookie 2>/dev/null")
    result03=$(sshpass ssh -p $SSH_PORT root@$WorkNode03 "ls $pv_path\"03\"/.erlang.cookie 2>/dev/null")
    if [ -n "$result01" ] && [ -n "$result02" ] && [ -n "$result03" ]; then
        kubectl delete -f after_edoc2_rabbitmq_cluster.yaml
    else
        kubectl delete -f edoc2_rabbitmq_cluster.yaml
        sleep 10
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "rm -rf $pv_path\"01\""
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "rm -rf $pv_path\"02\""
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "rm -rf $pv_path\"03\""
    fi
    
    kubectl get pod -n $namespace | grep -v grep | grep rabbitmq
    echo "##### check delete status ##################################"
    while :
    do
        echo "##### check rabbitmq service alive! ###########################"
        kubectl get pod -n $namespace | grep -v grep | grep rabbitmq
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete rabbitmq statefulset service has successful! ########"

    echo "##### start delete pvc #####################################"
    # delete pvc
    kubectl delete pvc rabbitmq-storage-rabbitmq-0 -n $namespace
    kubectl delete pvc rabbitmq-storage-rabbitmq-1 -n $namespace
    kubectl delete pvc rabbitmq-storage-rabbitmq-2 -n $namespace
    sleep 3
    # delete sc pv
    kubectl delete -f rabbitmq-configmap.yaml
    kubectl delete -f rabbitmq-rbac.yaml
    sleep 1
    kubectl delete -f rabbitmq-pv.yaml
    sleep 1
    kubectl delete -f rabbitmq-sc.yaml
    echo "##### rabbitmq pvc pv sc has been delete successful! #############"
}

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

caliconode=`cat /etc/hosts | grep node | awk -F " " '{print $2}'`
if [ "${caliconode:0:1}" == "" ];then
    echo "##### caliconode host is empty #############################"
else
    echo "##### caliconode check status exist ########################"
    for cnode in $caliconode;do
        echo ${cnode};
        calicoip=`cat /etc/hosts | grep ${cnode} | awk -F " " '{print $1}'`
        calicoctl patch node ${cnode} --patch="{\"spec\":{\"bgp\": {\"ipv4Address\": \"$calicoip/24\"}}}" --allow-version-mismatch
    done
fi

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