#!/bin/bash
# 命名空间名
namespace=edoc2
# 集群三个节点名称
WorkNode01=k8s-node01
WorkNode02=k8s-node02
WorkNode03=k8s-node03
SSH_PORT=22
pv_volume=1000Gi
#pv_path="/data/minio"
pv_yaml="minio-pv.yaml"


deploy_minio() {
    kubectl get pod -n $namespace | grep -v grep | grep -v ominio | grep minio
    if [ $? == 1 ];then
        echo "##### create remote pv minio dir ###############################"
        sshpass ssh -p $SSH_PORT root@$WorkNode01 'res=`ls -A /home/data/minio01`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio01;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode02 'res=`ls -A /home/data/minio02`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio02;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode03 'res=`ls -A /home/data/minio03`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio03;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode01 'res=`ls -A /home/data/minio04`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio04;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode02 'res=`ls -A /home/data/minio05`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio05;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode03 'res=`ls -A /home/data/minio06`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio06;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode01 'res=`ls -A /home/data/minio07`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio07;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode02 'res=`ls -A /home/data/minio08`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio08;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode03 'res=`ls -A /home/data/minio09`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio09;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode01 'res=`ls -A /home/data/minio10`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio10;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode02 'res=`ls -A /home/data/minio11`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio11;else echo "nothing exec";fi'
        sleep 1
        sshpass ssh -p $SSH_PORT root@$WorkNode03 'res=`ls -A /home/data/minio12`;if [ -z "${res:0:1}" ];then mkdir -p /home/data/minio12;else echo "nothing exec";fi'
        echo "################################################################"

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

        sleep 1
        kubectl label nodes $WorkNode01 node.minio01.labels=minio-node01
        kubectl label nodes $WorkNode02 node.minio02.labels=minio-node02
        kubectl label nodes $WorkNode03 node.minio03.labels=minio-node03
        kubectl label nodes $WorkNode01 node.minio04.labels=minio-node04
        kubectl label nodes $WorkNode02 node.minio05.labels=minio-node05
        kubectl label nodes $WorkNode03 node.minio06.labels=minio-node06
        kubectl label nodes $WorkNode01 node.minio07.labels=minio-node07
        kubectl label nodes $WorkNode02 node.minio08.labels=minio-node08
        kubectl label nodes $WorkNode03 node.minio09.labels=minio-node09
        kubectl label nodes $WorkNode01 node.minio10.labels=minio-node10
        kubectl label nodes $WorkNode02 node.minio11.labels=minio-node11
        kubectl label nodes $WorkNode03 node.minio12.labels=minio-node12

        kubectl label nodes $WorkNode01 node.dnlabels.nodelabels=DMiddleware
        kubectl label nodes $WorkNode02 node.dnlabels.nodelabels=DMiddleware
        kubectl label nodes $WorkNode03 node.dnlabels.nodelabels=DMiddleware

        sed -i "s#minio.edoc2#minio.$namespace#g" *.yaml
        sed -i "s#namespace:\ edoc2#namespace:\ $namespace#g" *.yaml
        # create sc pv
        kubectl apply -f minio-pv-pvc-mapping.yaml
        sleep 2
        kubectl apply -f minio-sc.yaml
        sleep 1
        kubectl apply -f minio-pv.yaml
        sleep 1
        kubectl apply -f minio-pvc.yaml
        sleep 1
        # create statefulset servcie
        kubectl apply -f edoc2_minio_cluster.yaml

        sleep 60
        minio_ip=`kubectl get svc -n $namespace | grep -v ominio | grep minio | awk -F " " '{print $3}'`
        mc config host add minio http://$minio_ip:9000 admin root123456 --api s3v4
        sleep 60
        mc mb minio/edoc2
        sleep 5
        mc mb minio/edoc2docs

        echo "##### show pods status #####################################"
        kubectl get pod -n $namespace | grep -v grep | grep -v ominio | grep minio
        sleep 5
        current_date=$(date -d "sunday" +"%Y%m%d%H%M%S")
        last_date=$(date -d "last sunday" +"%Y%m%d%H%M%S")
        now_date=$(date -d now +"%Y%m%d%H%M%S")
        sleep 1
        kubectl get pvc -n edoc2 | grep minio > "$now_date"-minio_pvc.info
        kubectl get pvc -n edoc2 | grep minio > /opt/"$now_date"-minio_pvc.info
    else
        echo "##### minio statefulset service has been exist! ###############"
    fi
}

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

    # echo "##### start delete pvc 不要开注释 #####################################"
    # delete pvc 不要开注释 彻底干掉minio用
    #kubectl delete pvc data-minio-0 -n $namespace
    #kubectl delete pvc data-minio-1 -n $namespace
    #kubectl delete pvc data-minio-2 -n $namespace
    #kubectl delete pvc data-minio-3 -n $namespace
    #kubectl delete pvc data-minio-4 -n $namespace
    #kubectl delete pvc data-minio-5 -n $namespace
    #kubectl delete pvc data-minio-6 -n $namespace
    #kubectl delete pvc data-minio-7 -n $namespace
    #kubectl delete pvc data-minio-8 -n $namespace
    #kubectl delete pvc data-minio-9 -n $namespace
    #kubectl delete pvc data-minio-10 -n $namespace
    #kubectl delete pvc data-minio-11 -n $namespace
    sleep 3
    
    #kubectl delete -f minio-pv.yaml
    #sleep 3
    #kubectl delete -f minio-sc.yaml
    #sleep 1
    echo "##### minio pvc pv sc has been delete successful! #############"
}

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

check_minio_iops() {
    io_dev=`df -Th /data | grep -v Filesystem | awk -F " " '{print $1}'`
    dd_output=$(time dd conv=fsync if=$io_dev of=/home/ecm6.2-k8s/pids/output bs=4k count=200000 2>&1)
    size=`echo $dd_output | awk -F " " '{print $14}'`
    uint=`echo $dd_output | awk -F " " '{print $15}' | awk -F "/" '{print $1}'`
    echo $size
    echo $uint
    if [ "${uint}" == "MB" ];then
        if [ "${size}" -gt "90" ];then
           echo "minio disk iops >= 90MB/s"
           deploy_minio
        else
           echo "minio disk iops <= 90MB/s please renew minio disk path!"
        fi
    elif [ "${uint}" == "GB" ];then
        if [ "${size}" -gt "0.1" ];then
           echo "minio disk iops >= 90MB/s"
           deploy_minio
        else
           echo "minio disk iops <= 90MB/s please renew minio disk path!"
        fi
    else
        deploy_minio
    fi
}

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