#!/bin/bash
install_dir=$1     # 包管理安装路径(自动化用对接装机Dashboard统一包路径管理)
install_type=$2    # 集群cluster或单机standalone
nas_type=$3        # 存储类型 localpath nfs linux_cifs win_cifs
namespace=$4       # k8s 命名空间名称
num=$5             # 装机只要一个存储默认填1  装机有多存储需求创建多个选2 3 4 5（新增几个执行多次）
nas_ip=$6          # 共享nas的server端ip地址
nas_path=$7        # 单机localpath填写本地路径  集群共享存储为server提供的服务别名（smb-cifs根据服务别名获取挂载点）
nas_data_size=$8   # 共享存存储pv pvc的资源大小申请 单位Gi（此资源为pv pvc request最小资源申请参数 最大limit资源不做限制 localpath无需填写）
nas_user=$9        # nas共享存储账号名（nfs localpath无需配置填写）
nas_passwd=${10}   # nas共享存储密码  （nfs localpath无需配置填写）

function local_path() {
    echo "************************ local_path *****************************"
    mkdir -p $nas_path
}

function nfs() {
    echo "************************* nfs ******************************"
    cd $install_dir
    kubectl delete -f ./exec-nfs-csi/03nfs-pvc.yaml &
    kubectl delete -f ./exec-nfs-csi/02nfs-pv.yaml &
    kubectl delete -f ./exec-nfs-csi/01nfs-sc.yaml &
    sleep 1
    rm -rf ./exec-nfs-csi/*
    sleep 1
    cp -rf ./nfs-static-demo/* ./exec-nfs-csi/
    sed -i "s#SPACENAME#$namespace#g" ./exec-nfs-csi/*
    sed -i "s#DATA_SIZE#$nas_data_size#g" ./exec-nfs-csi/*
    sed -i "s#NFS_IP#$nas_ip#g" ./exec-nfs-csi/*
    sed -i "s#NFS_PATH#$nas_path#g" ./exec-nfs-csi/*
    sed -i "s#NUM#$num#g" ./exec-nfs-csi/*
    sleep 1
    kubectl apply -f ./exec-nfs-csi/01nfs-sc.yaml
    kubectl apply -f ./exec-nfs-csi/02nfs-pv.yaml
    kubectl apply -f ./exec-nfs-csi/03nfs-pvc.yaml
}

function lsmb_cifs() {
    echo "************************* lsmb_cifs ******************************"
    cd $install_dir
    kubectl delete -f ./exec-smb-csi/02smb-cifs-pvc.yaml &
    kubectl delete -f ./exec-smb-csi/01smb-cifs-sc.yaml &
    kubectl delete secret smbcreds
    sleep 1
    rm -rf ./exec-smb-csi/*
    sleep 1
    for pvid in `kubectl get pvc -n $namespace | grep local-smbcifs | awk -F " " '{print $1}'`;do kubectl delete pv $pvid;done;
    for pvid in `kubectl get pv -n $namespace | grep local-smbcifs | awk -F " " '{print $1}'`;do kubectl delete pv $pvid;done;
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/csi-node-driver-registrar_v2.5.1.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/csi-provisioner_v3.2.0.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/livenessprobe_v2.7.0.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/smbplugin_v1.9.0.tar
    docker push registry.edoc2.com:5000/edoc2v5/csi-node-driver-registrar:v2.5.1
    docker push registry.edoc2.com:5000/edoc2v5/csi-provisioner:v3.2.0
    docker push registry.edoc2.com:5000/edoc2v5/livenessprobe:v2.7.0
    docker push registry.edoc2.com:5000/edoc2v5/smbplugin:v1.9.0
    kubectl apply -f ./csi-driver-smb-v1.9.0/01rbac-csi-smb.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/02csi-smb-driver.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/03csi-smb-controller.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/csi-smb-node.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/csi-smb-node-windows.yaml
    kubectl create secret generic smbcreds --from-literal username=$nas_user --from-literal password=$nas_passwd
    cp -rf ./linux-smb-cifs-static-demo/* ./exec-smb-csi/
    sed -i "s#SPACENAME#$namespace#g" ./exec-smb-csi/*
    sed -i "s#DATA_SIZE#$nas_data_size#g" ./exec-smb-csi/*
    sed -i "s#CIFS_IP#$nas_ip#g" ./exec-smb-csi/*
    sed -i "s#SHARE_NAME#$nas_path#g" ./exec-smb-csi/*
    sed -i "s#NUM#$num#g" ./exec-smb-csi/*
    sleep 1
    kubectl apply -f ./exec-smb-csi/01smb-cifs-sc.yaml
    kubectl apply -f ./exec-smb-csi/02smb-cifs-pvc.yaml
    sleep 1
    kubectl get pvc -n ecm-devuserv8000rc11 | grep local-smbcifs | grep smb-storage-pvc
}

function wsmb_cifs() {
    echo "************************* wsmb_cifs ******************************"
    cd $install_dir
    kubectl delete -f ./exec-wsmb-csi/02win_smb-cifs-pvc.yaml &
    kubectl delete -f ./exec-wsmb-csi/01win_smb-cifs-sc.yaml &
    kubectl delete secret wsmbcreds
    sleep 1
    rm -rf ./exec-wsmb-csi/*
    sleep 1
    for pvid in `kubectl get pvc -n $namespace | grep local-wsmbcifs | awk -F " " '{print $1}'`;do kubectl delete pv $pvid;done;
    for pvid in `kubectl get pv -n $namespace | grep local-wsmbcifs | awk -F " " '{print $1}'`;do kubectl delete pv $pvid;done;
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/csi-node-driver-registrar_v2.5.1.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/csi-provisioner_v3.2.0.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/livenessprobe_v2.7.0.tar
    docker load -i ./csi-driver-smb-v1.9.0/images_pkg/smbplugin_v1.9.0.tar
    docker push registry.edoc2.com:5000/edoc2v5/csi-node-driver-registrar:v2.5.1
    docker push registry.edoc2.com:5000/edoc2v5/csi-provisioner:v3.2.0
    docker push registry.edoc2.com:5000/edoc2v5/livenessprobe:v2.7.0
    docker push registry.edoc2.com:5000/edoc2v5/smbplugin:v1.9.0
    kubectl apply -f ./csi-driver-smb-v1.9.0/01rbac-csi-smb.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/02csi-smb-driver.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/03csi-smb-controller.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/csi-smb-node.yaml
    kubectl apply -f ./csi-driver-smb-v1.9.0/csi-smb-node-windows.yaml
    kubectl create secret generic wsmbcreds --from-literal username=$nas_user --from-literal password=$nas_passwd
    cp -rf ./win-smb-cifs-static-demo/* ./exec-wsmb-csi/
    sed -i "s#SPACENAME#$namespace#g" ./exec-wsmb-csi/*
    sed -i "s#DATA_SIZE#$nas_data_size#g" ./exec-wsmb-csi/*
    sed -i "s#CIFS_IP#$nas_ip#g" ./exec-wsmb-csi/*
    sed -i "s#SHARE_NAME#$nas_path#g" ./exec-wsmb-csi/*
    sed -i "s#NUM#$num#g" ./exec-wsmb-csi/*
    sleep 1
    kubectl apply -f ./exec-wsmb-csi/01win_smb-cifs-sc.yaml
    kubectl apply -f ./exec-wsmb-csi/02win_smb-cifs-pvc.yaml
    sleep 1
    kubectl get pvc -n ecm-devuserv8000rc11 | grep local-wsmbcifs | grep wsmb-storage-pvc
}

function main() {
   if [ ${install_type} == "standalone" ];then
       if [ ${nas_type} == "localpath" ];then
           local_path
       fi
   fi
   if [ $install_type == "cluster" ];then
       if [ ${nas_type} == "nfs" ];then
           nfs
       elif [ ${nas_type} == "linux_cifs" ];then
           lsmb_cifs
       elif [ ${nas_type} == "win_cifs" ];then
           wsmb_cifs
       fi
   fi
}

main

