#!/usr/bin/env bash
# 文件名: lvscare_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}

name=lvscare
temp_path=$(mktemp -d)
version=4.3.2

sed -n -e '1,/^exit 0$/!p' $0 > ${temp_path}/${name}-${version}.tar.xz 2>/dev/null

cd ${temp_path}
printf "$info: [ ${bldgre}${name}-${version}${txtrst} ] 安装中...\n"


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

# 动态声明环境变量
case "$ID" in
    "debian")
        OPTS_CONFIG=/etc/default/lvscare ;;
    "ubuntu")
        OPTS_CONFIG=/etc/default/lvscare ;;
    "centos")
        OPTS_CONFIG=/etc/sysconfig/lvscare ;;
    "kylin")
        OPTS_CONFIG=/etc/sysconfig/lvscare ;;
    *)
        printf "$err: 不支持该OS发行发支." && exit 1
        ;;
esac

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

if [[ "$@" < "1" ]]; then
    echo """
用法: $(basename $0) [选项]
选项:
  --masters         [必填] Kubernetes 控制平面节点IP地址
  --vip             [选填] 虚拟IP地址(默认: 168.254.88.48)
  --port            [选填] 虚拟IP地址监听端口(默认: 6443)
  --logger          [选填] 打印的日志级别(默认: DEBG)
  --interval        [选填] 探测后端服务的间隔时间(默认：6秒)
  --iface           [选填] 虚拟网口名称(默认：kube-vip)

示例:
1. 安装示例
# ./$(basename $0) --masters 192.168.88.11,192.168.88.12,192.168.88.13
"""
    exit 67
fi

# 读取命令行参数
ARGUMENT_LIST=(
    "masters"
    "vip"
    "port"
    "logger"
    "interval"
    "iface"
)

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

eval set --$opts

while true; do
    case "$1" in
    --masters)  
        shift
        masters=$1
        ;;
    --vip)  
        shift
        vip=$1
        ;;
    --port)  
        shift
        port=$1
        ;;
    --logger)  
        shift
        logger=$1
        ;;
    --interval)
        shift
        interval=$1
        ;;
    --iface)
        shift
        iface=$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 )
vip=${vip:-169.254.88.48}
port=${port:-6443}
logger=${logger:-DEBG}
interval=${interval:-6}
iface=${iface:-kube-vip}

if [[ ! -e /usr/sbin/${name} ]]
then
    tar axvf ${temp_path}/${name}-${version}.tar.xz -C /tmp > /dev/null
    cp /tmp/${name}-${ARCH} /usr/sbin/${name} >/dev/null
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

rm -rf ${temp_path}
exit 0
