#!/bin/bash

namespace=edoc2

WorkNode01=k8s-node01
WorkNode02=k8s-node02
WorkNode03=k8s-node03
SSH_PORT=22

base_dir=/home/data

deploy_mongodb() {
    kubectl get pod -n $namespace | grep -v grep | grep -v "mongodb-" | grep mongodb
    if [ $? == 1 ];then
        echo "##### create remote pv mongodb dir ##################################"
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "mkdir -p ${base_dir}/mongodb"
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p ${base_dir}/mongodb"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p ${base_dir}/mongodb"

        sleep 1

        old_namespace=`cat *.yaml | grep "namespace: " | grep -v log | uniq -c | awk -F " " '{print $3}'`
        sed -i "s#namespace: $old_namespace#namespace: $namespace#g" *.yaml
 
        echo "##### $old_base_dir ##############################################"

        sleep 1
        old_dir=`cat *.yaml | grep "path: " | grep -v log | uniq -c | awk -F " " '{print $3}'`
        sed -i "s#$old_dir#$base_dir/mongodb#g" *.yaml

        sleep 1

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

#        kubectl apply -f 01_mongodb-configmap.yaml
#        kubectl apply -f 02_mongodb-secert.yaml
        sleep 5
#        kubectl apply -f 03_mongodb01.yaml
#        kubectl apply -f 04_mongodb02.yaml
#        kubectl apply -f 05_mongodb03.yaml

        sleep 15

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

delete_mongodb() {
    echo "##### start delete mongodb deployment service ##################"
    # delete mongodb servcie
#    kubectl delete -f 05_mongodb03.yaml
#    kubectl delete -f 04_mongodb02.yaml
#    kubectl delete -f 03_mongodb01.yaml
    sleep 5
#    kubectl delete -f 02_mongodb-secert.yaml
#    kubectl delete -f 01_mongodb-configmap.yaml
    
    kubectl get pod -n $namespace | grep -v grep | grep -v "mongodb-" | grep mongodb
    echo "##### check delete status ##################################"
    while :
    do
        echo "##### check mongodb service alive! ###########################"
        kubectl get pod -n $namespace | grep -v grep | grep -v "mongodb-" | grep mongodb
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete mongodb deployment service has successful! ########"
    sleep 3
}

cat <<eof 
******************************************
** mongodb 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 mongodb ........."
        deploy_mongodb
        ;;
    2|stop|STOP)
        echo "begin stop mongodb ..........."
        delete_mongodb
        ;;
    3|restart|RESTART)
        echo "begin restart mongodb ........"
        delete_mongodb
        sleep 3
        deploy_mongodb
        ;;
    *)
        echo "输入操作不存在 .........."
        #exit;
esac
