#!/bin/bash

namespace=$2

WorkNode01=$3
WorkNode02=$4
WorkNode03=$5
SSH_PORT=$6
mgr_yaml01=mgr-pod0.yaml
mgr_yaml02=mgr-pod1.yaml
mgr_yaml03=mgr-pod2.yaml
base_dir="/home/data"
pv_volume="250Gi"
backup_pv_volume="250Gi"
pv_yaml="mysql-pv.yaml"
backup_pv_yaml="backup-mysql-pv.yaml"

echo "########################"
echo $base_dir
echo $pv_volume
echo $backup_pv_volume
echo "#######################"

deploy_mgr() {
    kubectl get pod -n $namespace | grep -v grep | grep mysql
    if [ $? == 1 ];then
        echo "##### create remote pv dbmgr dir ##################################"
        sshpass ssh -p $SSH_PORT root@$WorkNode01 "mkdir -p ${base_dir}/mysql && mkdir -p ${base_dir}/mysqlbackup"
        sshpass ssh -p $SSH_PORT root@$WorkNode02 "mkdir -p ${base_dir}/mysql && mkdir -p ${base_dir}/mysqlbackup"
        sshpass ssh -p $SSH_PORT root@$WorkNode03 "mkdir -p ${base_dir}/mysql && mkdir -p ${base_dir}/mysqlbackup"

        sleep 1
        old_base_dir01=`cat $mgr_yaml01 |awk '/volumes/{p=4}p-->0' | grep path | awk -F "/" '{print $2}'`
        old_base_dir02=`cat $mgr_yaml02 |awk '/volumes/{p=4}p-->0' | grep path | awk -F "/" '{print $2}'`
        old_base_dir03=`cat $mgr_yaml03 |awk '/volumes/{p=4}p-->0' | grep path | awk -F "/" '{print $2}'`
        old_pv_volume=`cat $pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`
        old_backup_pv_volume=`cat $backup_pv_yaml | grep "storage:" | uniq -c | awk -F " " '{print $3}'`

        new_base_dir=${base_dir//\//\\\/}
        echo "##### $new_base_dir ################################################"
        old_base_dir01="\/""${old_base_dir01}"
        old_base_dir02="\/""${old_base_dir02}"
        old_base_dir03="\/""${old_base_dir03}"
 
        echo "##### $old_base_dir01 ##############################################"
        echo "##### $old_base_dir02 ##############################################"
        echo "##### $old_base_dir03 ##############################################"
        #sed -i "s/$old_base_dir01/$new_base_dir/g" $mgr_yaml01
        #sed -i "s/$old_base_dir02/$new_base_dir/g" $mgr_yaml02
        #sed -i "s/$old_base_dir03/$new_base_dir/g" $mgr_yaml03
        sed -i "s/$old_pv_volume/$pv_volume/g" $pv_yaml
        sed -i "s/$old_backup_pv_volume/$backup_pv_volume/g" $backup_pv_yaml

        sleep 1
        kubectl label nodes $WorkNode01 mysqlrole=mysql-mgr-0
        kubectl label nodes $WorkNode02 mysqlrole=mysql-mgr-1
        kubectl label nodes $WorkNode03 mysqlrole=mysql-mgr-2

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

        kubectl label nodes $WorkNode01 node.mysqlbackup.labels=mysqlbackup-node01
        kubectl label nodes $WorkNode02 node.mysqlbackup.labels=mysqlbackup-node02
        kubectl label nodes $WorkNode03 node.mysqlbackup.labels=mysqlbackup-node03

        # create sc pv
        kubectl apply -f backup-mysql-sc.yaml
        kubectl apply -f backup-mysql-pv.yaml
        kubectl apply -f mysql-sc.yaml
        kubectl apply -f mysql-pv.yaml

        # create configmap and secret
        kubectl apply -f mysql-secret.yaml
        kubectl apply -f mysql-mgr-cnf-0.yaml
        kubectl apply -f mysql-mgr-cnf-1.yaml
        kubectl apply -f mysql-mgr-cnf-2.yaml

        # create statefulset servcie
        kubectl apply -f mgr-pod0.yaml
        sleep 15
        kubectl apply -f mgr-pod1.yaml
        sleep 15
        kubectl apply -f mgr-pod2.yaml
        #sleep 15
        #kubectl apply -f mgr-pod0.yaml

        sleep 9

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

delete_mgr() {
    echo "##### start delete dbmgr statefulset service ##################"
    # delete statefulset servcie
    kubectl delete -f mgr-pod0.yaml
    kubectl delete -f mgr-pod1.yaml
    kubectl delete -f mgr-pod2.yaml
    
    kubectl get pod -n $namespace | grep -v grep | grep mysql
    echo "##### check delete status ##################################"
    while :
    do
        echo "##### check dbmgr service alive! ###########################"
        kubectl get pod -n $namespace | grep -v grep | grep mysql
        if [ $? == 1 ];then
            break
        fi
        sleep 2
    done;
    echo "##### delete dbmgr statefulset service has successful! ########"
    kubectl delete -f mysql-mgr-cnf-0.yaml
    kubectl delete -f mysql-mgr-cnf-1.yaml
    kubectl delete -f mysql-mgr-cnf-2.yaml
    kubectl delete -f mysql-secret.yaml
    kubectl delete pvc owner-mysql-volume-mysql-mgra-0 -n $namespace
    kubectl delete pvc owner-mysql-volume-mysql-mgrb-0 -n $namespace
    kubectl delete pvc owner-mysql-volume-mysql-mgrc-0 -n $namespace
    kubectl delete pvc mysql-dbbackup-volume-mysql-mgra-0 -n $namespace
    kubectl delete pvc mysql-dbbackup-volume-mysql-mgrb-0 -n $namespace
    kubectl delete pvc mysql-dbbackup-volume-mysql-mgrc-0 -n $namespace
    sleep 3
    kubectl delete -f mysql-pv.yaml
    kubectl delete -f backup-mysql-pv.yaml
    kubectl delete -f mysql-sc.yaml
    kubectl delete -f backup-mysql-sc.yaml
    sleep 1
}

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