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


deploy_minio() {
    if which mc;then 
       echo "mv 命令已存在"
    else 
      cp ../pkg/mc /usr/bin
    fi
    kubectl get pod -n $namespace | grep -v grep | grep -v ominio | grep minio
    if [ $? == 1 ];then
        sleep 1
        echo "################################################################"
        sleep 1
        kubectl label nodes $WorkNode01 node.drives01.nodelabels=DRMiddleware01
        kubectl label nodes $WorkNode02 node.drives02.nodelabels=DRMiddleware02
        kubectl label nodes $WorkNode03 node.drives03.nodelabels=DRMiddleware03

        sed -i "s#minio.edoc2#minio.$namespace#g" *.yaml
        sed -i "s#namespace:\ edoc2#namespace:\ $namespace#g" *.yaml
        sleep 2
        kubectl apply -f minio-svc.yaml
        sleep 5
        # create statefulset servcie
        kubectl apply -f edoc2_minio_cluster01.yaml
        kubectl apply -f edoc2_minio_cluster02.yaml
        kubectl apply -f edoc2_minio_cluster03.yaml

        sleep 60
        minio_ip=`kubectl get svc -n $namespace | grep -v ominio | grep -v minio0 | grep -v minio1 | grep -v minio2 | 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 -v minio0 | grep -v minio1 | grep -v minio2 | 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_cluster03.yaml
    kubectl delete -f edoc2_minio_cluster02.yaml
    kubectl delete -f edoc2_minio_cluster01.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! ########"
}    

cat <<eof 
****************************************
** minio operation start/stop/restart **
****************************************
eof

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
