#!/bin/bash

namespace=edoc2

WorkNode=k8s-node03
SSH_PORT=22
pv_volume=50Gi
pv_path="/data/apm"
pv_yaml="apm-pv.yaml"

deploy_apm() {
    kubectl get pod -n $namespace | grep -v grep | grep apm
    if [ $? == 1 ];then
        echo "##### create remote pv apm dir ###############################"

        # apm默认安装在 apm-node03 标签节点
        sshpass ssh -p $SSH_PORT root@$WorkNode "mkdir -p $pv_path\"03\" && mkdir -p $backup_path\"03\""

        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
        sed -i "s/$old_pv_path/$new_pv_path/g" $pv_yaml

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

        # create sc pv
        kubectl apply -f apm-sc.yaml
        sleep 1
        kubectl apply -f apm-pv.yaml
        sleep 1
        kubectl apply -f apm-pvc.yaml
        sleep 5

        # create statefulset servcie
        kubectl apply -f .

        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.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! ########"

    echo "##### start delete pvc #####################################"
    # delete pvc
    #kubectl delete pvc apm-skyes-volume-apm-0 -n $namespace
    sleep 5
    # delete pv
    #kubectl patch pv apm-storage-pv-1 -p '{"metadata":{"finalizers":null}}'
    sleep 3
    kubectl delete -f apm-pvc.yaml
    sleep 2
    kubectl delete -f apm-pv.yaml
    sleep 3
    kubectl delete -f apm-sc.yaml
    sleep 1
    kubectl delete -f .
    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

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
