#!/bin/bash

namespace=edoc2

WorkNode01=k8s-node01
WorkNode02=k8s-node02
WorkNode03=k8s-node03
SSH_PORT=22
#pv_volume=50Gi
pv_path="/data/redis"
pv_yaml="edoc2-redis-sentinel-master.yaml edoc2-redis-sentinel-slave1.yaml edoc2-redis-sentinel-slave2.yaml"


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 $pv_path"
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p $pv_path"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $pv_path"

        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
        #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" $pv_yaml
        for yaml in $pv_yaml;do
            sed -i 's/$old_pv_path/$new_pv_path/g' $pv_yaml
        done
        sleep 1
        # create sc pv
        #kubectl apply -f redis-sentinel-sc.yaml
        #kubectl apply -f redis-sentinel-pv.yaml
        kubectl apply -f redis-sentinel-configmap.yaml
        kubectl apply -f redis-sentinel-rbac.yaml

        # create statefulset servcie
        kubectl apply -f edoc2-redis-sentinel-master.yaml
        kubectl apply -f edoc2-redis-sentinel-slave1.yaml
        kubectl apply -f edoc2-redis-sentinel-slave2.yaml
        kubectl apply -f redis-sentinel-svc-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 redis-sentinel-svc-sentinel.yaml
    kubectl delete -f edoc2-redis-sentinel-slave2.yaml
    kubectl delete -f edoc2-redis-sentinel-slave1.yaml
    kubectl delete -f edoc2-redis-sentinel-master.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! ########"
    kubectl delete -f redis-sentinel-configmap.yaml
    kubectl delete -f redis-sentinel-rbac.yaml
    sleep 1

    echo "##### start delete pvc #####################################"
    # delete pvc
    #kubectl delete pvc redis-storage-master-redis-master-0 -n $namespace
    #kubectl delete pvc redis-storage-slave-redis-slavea-0 -n $namespace
    #kubectl delete pvc redis-storage-slave-redis-slaveb-0 -n $namespace
    sleep 3

    # delete pv
    ##kubectl patch pv redis-storage-pv-1 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv redis-storage-pv-2 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv redis-storage-pv-3 -p '{"metadata":{"finalizers":null}}'
    sleep 3
    
    # delete sc pv
    #kubectl delete -f redis-sentinel-sc.yaml
    sleep 1
    #kubectl delete -f redis-sentinel-pv.yaml
    sleep 1
    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
