#!/usr/bin/env bash
# 文件名: kubernetes_deploy.sh
# 作者: 树辉<shuhui@vqiu.cn>
# vim:sw=4:ts=4:et

set -e

# 终端颜色定义
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}


# 引入系统发行版本配置
if [ -f "/etc/os-release" ];
then
    . /etc/os-release
else
    printf "$err: 不支持该操作系统."
    exit 1
fi

# 判断环境平台
if [ -z "${ARCH}" ]; then
  case $(uname -m) in
    x86_64) ARCH=amd64 ;;
    aarch64) ARCH=arm64 ;;
    *) printf "$err: 不支持该平台 $(uname -m)"; exit 1 ;;
  esac
fi

# 版本号
lvscare_version=4.3.2

# 下载链接
package_url="http://gitee:gitee@dl.gitee.work:8848/package/lvscare/lvscare-${lvscare_version}-${ARCH}.tar.xz"


if [[ "$@" < "1" ]]; then
    echo """
用法: $(basename $0) [选项]
选项:
  --master          [必填] Kubernetes 控制平面节点IP地址
  --worker          [必填] Kubernetes 数据平面节点IP地址
  --nfs_server      [必填] 共享存储节点(默认: master)
  --image_address   [必填] 共享存储节点(默认: master)
  --lbless          [选填] 高可用模式是否使用无负载均衡模式(默认: true)
  --ingress         [选填] 集群ingress组件(默认istio)
  --offline         [选填] 是否为离线部署(默认: false)

示例:
1. 单控制平面在线部署
# ./$(basename $0) --master 192.168.88.11 --worker 192.168.88.12,192.168.88.13 --ingress istio

2. 单控制平面离线部署
# ./$(basename $0) --master 192.168.88.11 --worker 192.168.88.12,192.168.88.13 --offline true
"""
    exit 67
fi

# 读取命令行参数
ARGUMENT_LIST=(
    "master"
    "worker"
    "nfs_server"
    "registry_server"
    "registry_port"
    "lbless"
    "ingress"
    "offline"
)

# 读取参数
opts=$(getopt \
    --longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
    --name "$(basename "$0")" \
    --options "" \
    -- "$@"
)

eval set --$opts

while true; do
    case "$1" in
    --master)  
        shift
        master=$1
        ;;
    --worker)  
        shift
        worker=$1
        ;;
    --nfs_server)  
        shift
        nfs_server=$1
        ;;
    --registry_server)
        shift
        registry_server=$1
        ;;
    --registry_port)  
        shift
        registry_port=$1
        ;;
    --lbless)
        shift
        lbless=$1
        ;;
    --ingress)
        shift
        ingress=$1
        ;;
    --offline)
        shift
        offline=$1
        ;;
      --)
        shift
        break
        ;;
    esac
    shift
done

# 定义默认变量

first_master=$(echo ${master} | awk -F',' '{print $1}')
nfs_server=${nfs_server:-$first_master}
registry_server=${registry_server:-$first_master}
registry_port=${registry_port:-5000}

if [[ "${registry_port}" -eq "80" ]] || [[ "${registry_port}" -eq "443" ]]; then
    registry_addr=${registry_server}
else
    registry_addr="${registry_server}:${registry_port}"
fi
api_addr=kube-api.gitee.local
lbless=${lbless:-true}
ingress=${ingress:-istio}
offline=${offline:-false}

if [[ "${offline}" == "false" ]]
then
    cat<< EOF
`echo -e " ====================== 环境部署指要 =========================="`
`echo -e " - 控制平面IP: ${master}"` 
`echo -e " - 数据平面IP: ${worker}"`
`echo -e " - NFS存储节点: ${nfs_server}"`
`echo -e " - Ingress组件: ${ingress}"`
`echo -e " - 是否离线部署: ${offline}"`
`echo -e " =============================================================="`
`echo -e ""`
EOF
    read -p "确认无误后,请按Enter键进行部署..."
else
    cat<< EOF
`echo -e " ====================== 环境部署指要 =========================="`
`echo -e " - 控制平面IP: ${master}"` 
`echo -e " - 数据平面IP: ${worker}"`
`echo -e " - NFS存储节点: ${nfs_server}"`
`echo -e " - 本地镜像仓库: ${registry_addr}"`
`echo -e " - 无负载均衡模式: ${lbless}"`
`echo -e " - Ingress控制器: ${ingress}"`
`echo -e " - 是否离线部署: ${offline}"`
`echo -e " =============================================================="`
`echo -e ""`
EOF
    read -p "确认无误后,请按Enter键进行部署..."
fi

#if [[ "${offline}" == "false" ]]; then
#    if [[ ! -e ${package_url##*/} ]]; then
#        wget -O ${package_url##*/} ${package_url} || curl -o ${package_url##*/} ${package_url}
#        tar axvf ${package_url##*/} -C /usr/sbin >/dev/null
#    fi
#else
#    if [[ -e ${package_url##*/} ]]; then
#        tar axvf ${package_url##*/} -C /usr/sbin >/dev/null
#    else
#        printf "$err: ${package_url##*/} 文件不存在~"
#        exit 67
#    fi
#fi
#
#cat >${OPTS_CONFIG} <<EOF
#OPTS="--vs ${vip}:${port} ${master_server_opt} --logger ${logger}  --interval ${interval} --mode link  --iface ${iface}"
#
#EOF
#
#cat >/etc/systemd/system/lvscare.service<<EOF
#[Unit]
#Description=lvscare
#After=network.target
#
#[Service]
#User=root
#LimitNOFILE=10240
#LimitNPROC=10240
#LimitCORE=infinity
#LimitMEMLOCK=infinity
#EnvironmentFile=${OPTS_CONFIG}
#ExecStart=/usr/sbin/lvscare care \$OPTS
#ExecStop=/usr/sbin/lvscare care \$OPTS --clean
#Restart=on-failure
#KillMode=process
#
#[Install]
#WantedBy=multi-user.target
#EOF
#
#systemctl daemon-reload
#systemctl enable lvscare.service
#systemctl restart lvscare.service
