#!/bin/bash

namespace=edoc2
KubeAPIServer=https://192.168.11.185:6443
WorkNode=192.168.11.185
SSH_PORT=22
ES_DIR=/opt/apm_region/es
ES_BACKDIR=/opt/apm_region/esbackup


deploy_apm() {
    kubectl get pod -n $namespace | grep -v grep | grep apm
    if [ $? == 1 ];then

        mkdir -p ${ES_DIR}
        mkdir -p ${ES_BACKUPDIR}

        sleep 1


        sleep 1
        echo "====================================================="
        kubectl label nodes $WorkNode node.apm.labels=apm-node01
        echo "====================================================="
        #kubectl label nodes $WorkNode node.labels.nodelabels=Middleware

        # setup kube-apiserver
        find . -name "*.yaml" -type f -exec sed -i "s#https://192.168.251.87:8443#${KubeAPIServer}#g" {} +

        # create statefulset servcie
        kubectl apply -f .
        kubectl apply -f kube-state-metric/.

        sleep 30

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

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

    sleep 1
    kubectl delete -f .
    sleep 3
    kubectl delete -f kube-state-metric/.
    echo "##### apm pvc pv sc has been delete successful! #############"
}

cat <<eof 
**************************************
** apm 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

if [ ${namespace} != "edoc2" ];then
    find . -name "*.yaml" -type f -exec sed -i "s#namespace: edoc2#namespace: ${namespace}#g" {} + 
fi

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