#!/bin/bash
# 修改为你自己的namespace名称
namespace=edoc2
# 必须修改位置自己k8s单机的IP地址
WorkNode01=
SSH_PORT=22
# 根据实际挂载点为磁盘大小分配pv磁盘空间
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 ###############################"
        sleep 1
        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'
        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.minio.labels=minio-node

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

        sed -i "s#namespace:\ edoc2#namespace:\ $namespace#g" *.yaml
        sed -i "s#minio.edoc2#minio.$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
    sleep 3
    # delete pv 不要开注释
    #kubectl patch pv minio-storage-pv-1 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv minio-storage-pv-2 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv minio-storage-pv-3 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv minio-storage-pv-4 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv minio-storage-pv-5 -p '{"metadata":{"finalizers":null}}'
    #kubectl patch pv minio-storage-pv-6 -p '{"metadata":{"finalizers":null}}'
    
    #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
