#!/bin/bash

namespace=edoc2

WorkNode03=k8s-node03
SSH_PORT=22

mssql_path=/home/data/sqlserver
sqlbak_path=/hdd/sqlbak
manual_bak_path=/hdd/manual_backup
oos_bak_path=/hdd/oos_backup

deploy_sqlserver() {
    kubectl get pod -n $namespace | grep -v grep | grep sqlserver
    if [ $? == 1 ];then
        echo "##### create remote pv es dir ###############################"
        # es-node03 标签节点
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $mssql_path"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $sqlbak_path"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $manual_bak_path"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $oos_bak_path"

        sleep 1
        kubectl label nodes $WorkNode03 node.sqlserver.labels=sqlserver-node03

        sleep 1
        # create statefulset servcie
        kubectl apply -f sqlserver-secret.yaml
        sleep 1
        kubectl apply -f edoc2_sqlserver_allinone.yaml

        sleep 30

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

delete_sqlserver() {
    echo "##### start delete sqlserver deployment service ###################################"
    # delete statefulset servcie
    kubectl delete -f edoc2_sqlserver_allinone.yaml
    kubectl get pod -n $namespace | grep -v grep | grep sqlserver
    echo "##### check delete status #########################################################"
    while :
    do
        kubectl get pod -n $namespace | grep -v grep | grep sqlserver
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete sqlserver deployment service has successful! ###############################"

    sleep 5
    #kubectl patch pv esbackup-storage-pv-3 -p '{"metadata":{"finalizers":null}}'
    sleep 1
    kubectl delete -f sqlserver-secret.yaml
    sleep 1
    echo "##### sqlserver deploy and secret has been delete successful! #############"
}

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