#!/bin/bash

namespace=edoc2

WorkNode01=k8s-node01
WorkNode02=k8s-node02
WorkNode03=k8s-node03
SSH_PORT=22
pv_volume=100Gi
pv_path="/data/es"
backup_volume=50Gi
backup_path="/data/nas/esback"
new_backup_nas_server="192.168.251.176"
pv_yaml="es-pv.yaml"
pvc_yaml="es-pvc.yaml"
backup_pv_yaml="backup-es-pv.yaml"
backup_pvc_yaml="backup-es-pvc.yaml"

deploy_es() {
    exportfs -rv
    systemctl restart rpcbind.service
    systemctl restart nfs.service
    systemctl restart nfs
    kubectl get pod -n $namespace | grep -v grep | grep -v kyes-0 | grep es0
    if [ $? == 1 ];then
        echo "##### create remote pv es dir ###############################"
        # es-node01 标签节点
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "mkdir -p $pv_path\"01\""

        # es-node02 标签节点
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p $pv_path\"02\""

        # es-node03 标签节点
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p $pv_path\"03\""

        sleep 1
        old_pv_volume=`cat $pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
        old_pvc_volume=`cat $pvc_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_pvc_volume/$pv_volume/g" $pvc_yaml
        sed -i "s/$old_pv_path/$new_pv_path/g" $pv_yaml

        old_backup_pv_volume=`cat $backup_pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
        old_backup_pv_path=`cat $backup_pv_yaml | grep "path:" | uniq -c | awk -F " " '{print $3}' | awk -F "0" '{print $1}' | uniq -c | awk -F " " '{print $2}'`
        new_backup_path=${backup_path//\//\\\/}
        old_backup_pv_path=${old_backup_pv_path//\//\\\/}
        old_backup_nas_server=`cat backup-es-pv.yaml | grep "server:" | uniq -c | awk -F " " '{print $3}' | uniq -c | awk -F " " '{print $2}'`

        old_backup_pvc_volume=`cat $backup_pvc_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`

        sed -i "s/$old_backup_pv_volume/$backup_volume/g" $backup_pv_yaml
        sed -i "s/$old_backup_pv_path/$new_backup_path/g" $backup_pv_yaml
        sed -i "s/$old_backup_nas_server/$new_backup_nas_server/g" $backup_pv_yaml
        sed -i "s/$old_backup_pvc_volume/$backup_volume/g" $backup_pvc_yaml

        sleep 1
        kubectl label nodes $WorkNode01 node.es.labels=es-node01
        kubectl label nodes $WorkNode02 node.es.labels=es-node02
        kubectl label nodes $WorkNode03 node.es.labels=es-node03

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

        # create sc pv
        kubectl apply -f es-sc.yaml
        sleep 1
        kubectl apply -f es-pv.yaml
        sleep 1
        kubectl apply -f es-pvc.yaml
        sleep 1
        #kubectl apply -f backup-es-sc.yaml
        #sleep 1
        #kubectl apply -f backup-es-pv.yaml
        #sleep 1
        #kubectl apply -f backup-es-pvc.yaml
        #sleep 1
        # create statefulset servcie
        kubectl apply -f es-configmap.yaml
        kubectl apply -f edoc2_es_cluster.yaml

        sleep 30

        echo "##### show pods status #################################################"
        kubectl get pod -n $namespace | grep -v grep | grep -v skyes-0 | grep es0
        echo "##### show es heath status #############################################"
        sleep 5
        #kubectl exec -it esa-0 -n $namespace -- curl -X GET "http://esa-0.esa.edoc2.svc.cluster.local:9200/_cat/health?v"
        #kubectl exec -it esa-0 -n $namespace -- curl -X GET "http://esa-0.esa.edoc2.svc.cluster.local:9200"
        #kubectl exec -it esb-0 -n $namespace -- curl -X GET "http://esb-0.esb.edoc2.svc.cluster.local:9200/_cat/health?v"
        #kubectl exec -it esb-0 -n $namespace -- curl -X GET "http://esb-0.esb.edoc2.svc.cluster.local:9200"
        #kubectl exec -it esc-0 -n $namespace -- curl -X GET "http://esc-0.esc.edoc2.svc.cluster.local:9200/_cat/health?v"
        #kubectl exec -it esc-0 -n $namespace -- curl -X GET "http://esc-0.esc.edoc2.svc.cluster.local:9200"
        #kubectl exec -it esa-0 -n $namespace -- curl -X GET "http://esa:9200/_cat/health?v"
        #kubectl exec -it esa-0 -n $namespace -- curl -X GET "http://esa:9200"
        #kubectl exec -it esb-0 -n $namespace -- curl -X GET "http://esb:9200/_cat/health?v"
        #kubectl exec -it esb-0 -n $namespace -- curl -X GET "http://esb:9200"
        #kubectl exec -it esc-0 -n $namespace -- curl -X GET "http://esc:9200/_cat/health?v"
        #kubectl exec -it esc-0 -n $namespace -- curl -X GET "http://esc:9200"
    else
        echo "##### es statefulset service has been exist! ###############"
    fi
}

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

    echo "##### start delete pvc #####################################"
    # delete pvc
    #kubectl delete pvc owner-es-volume-esa-0 -n $namespace
    #kubectl delete pvc owner-es-volume-esb-0 -n $namespace
    #kubectl delete pvc owner-es-volume-esc-0 -n $namespace
    #kubectl delete pvc es-dbbackup-volume-esa-0 -n $namespace
    #kubectl delete pvc es-dbbackup-volume-esb-0 -n $namespace
    #kubectl delete pvc es-dbbackup-volume-esc-0 -n $namespace
    sleep 5
    # delete pv
    #kubectl patch pv es-storage-pv-1 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv es-storage-pv-2 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv es-storage-pv-3 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv esbackup-storage-pv-1 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv esbackup-storage-pv-2 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv esbackup-storage-pv-3 -p '{"metadata":{"finalizers":null}}'
    sleep 1
    #kubectl delete -f backup-es-pvc.yaml
    kubectl delete -f es-pvc.yaml
    sleep 1
    #kubectl delete -f backup-es-pv.yaml
    kubectl delete -f es-pv.yaml
    sleep 1
    kubectl delete -f es-sc.yaml
    #kubectl delete -f backup-es-sc.yaml
    sleep 1
    echo "##### es pvc pv sc has been delete successful! #############"
}

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