#!/usr/bin/env bash
# 文件名: etcd_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 [ -z "${ARCH}" ]; then
  case $(uname -m) in
    x86_64) ARCH=amd64 ;;
    aarch64) ARCH=arm64 ;;
    *) printf "$err: 不支持该平台 $(uname -m)"; exit 1 ;;
  esac
fi

# 版本号
etcd_version=3.5.13

if [[ "$@" < "1" ]]; then
    echo """
用法: $(basename $0) [选项]
选项:
  --cluster         [必填] 是否使用集群部署
  --encrypt         [必填] 是否使用TLS加密部署
  --other_member    [选填] 其它集群成员(除自己),集群模式部署生效
  --data_dir        [选填] 数据存储目录, 缺省: /data/etcd
  --only_tls13      [选填] TLS加密版本,缺省: false

示例:
1. 单点部署，不使用TLS
# ./$(basename $0) --cluster false --encrypt false

2. 单点TLS加密部署,数据目录为/opt/etcd,仅支持tls_1.3版本
# ./$(basename $0) --cluster false --encrypt true --data_dir /opt/etcd --only_tls13 true

2. 集群TLS加密部署
# ./$(basename $0) --cluster true --encrypt true --other_member 192.168.88.11,192.168.88.12
"""
    exit 67
fi

# 读取命令行参数
ARGUMENT_LIST=(
    "cluster"
    "encrypt"
    "other_member"
    "data_dir"
    "only_tls13"
)

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

eval set --$opts

while true; do
    case "$1" in
    --cluster)
        shift
        cluster=$1
        ;;
    --encrypt)
        shift
        encrypt=$1
        ;;
    --other_member)
        shift
        other_member=$1
        ;;
    --data_dir)
        shift
        data_dir=$1
        ;;
    --only_tls13)
        shift
        only_tls13=$1
        ;;
      --)
        shift
        break
        ;;
    esac
    shift
done

# 定义默认变量
master_servers=$(echo ${masters} | sed 's/,/ /g')
master_server_opt=$(for ip in ${master_servers}; do echo -n " --rs ${ip}:6443"; done )
cluster=${cluster:-false}
encrypt=${encrypt:-false}
data_dir=${data_dir:-/data/etcd}
only_tls13=${only_tls13:-false}

# 输出当前部署信息
cat<< EOF
`echo -e " ====================== 环境部署指要 =========================="`
`echo -e " - 部署模式: ${cluster}"`
`echo -e " - TLS 加密: ${encrypt}"`
`echo -e " - 集群列表: [ $(hostname -i) ],${other_member}"`
`echo -e " - 数据目录: ${data_dir}"`
`echo -e " - 仅TLS1.3: ${only_tls13}"`
`echo -e " =============================================================="`
`echo -e ""`
EOF
read -p "确认无误后,请按Enter键进行部署..."


#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
