#!/bin/bash

namespace=edoc2

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

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

        kubectl label nodes $WorkNode01 node.redis1.labels=redis-node01
        kubectl label nodes $WorkNode02 node.redis2.labels=redis-node02
        kubectl label nodes $WorkNode03 node.redis3.labels=redis-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

        sleep 1
        kubectl apply -f 01-redis-configmap.yaml
        sleep 1
        kubectl apply -f 02-redis-secret.yaml
        sleep 1
        kubectl apply -f 03-redis-service.yaml
        sleep 1
        kubectl apply -f 04-redis-statefulset.yaml
        sleep 15
        kubectl apply -f 05-redis-sentinel-deployment-sentinel.yaml

        sleep 9

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

delete_redis() {
    echo "##### start delete redis statefulset service ##################"
    # delete statefulset servcie
    kubectl delete -f 05-redis-sentinel-deployment-sentinel.yaml
    sleep 2
    kubectl delete -f 04-redis-statefulset.yaml
    sleep 1
    kubectl delete -f 03-redis-service.yaml
    sleep 1
    kubectl delete -f 02-redis-secret.yaml
    sleep 1
    kubectl delete -f 01-redis-configmap.yaml
    
    kubectl get pod -n $namespace | grep -v grep | grep redis
    echo "##### check delete status ##################################"
    while :
    do
        echo "##### check redis service alive! ###########################"
        kubectl get pod -n $namespace | grep -v grep | grep redis
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete redis statefulset service has successful! ########"

    sleep 5

    echo "##### redis pvc pv sc has been delete successful! #############"
}

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