#!/usr/bin/env bash

### 需要修改以下的内容  ###
### 需要修改以下的内容  ###
### 需要修改以下的内容  ###

# 理论上来说，能访问公网的服务器 用来部署Harbor服务器
# 所有的主机均可以访问公网的话，填写  除了harbor服务器的 其他所有主机的地址
PrivateServerIPs=(10.215.125.15 10.215.125.16 10.215.125.17) # 内网服务器的IP地址，不包括可以访问公网IP的服务器

### 需要修改以上的内容  ###
### 需要修改以上的内容  ###
### 需要修改以上的内容  ###

RED="31m"    ## 姨妈红
GREEN="32m"  ## 水鸭青
YELLOW="33m" ## 鸭屎黄
PURPLE="35m" ## 基佬紫
BLUE="36m"   ## 天依蓝

colorEcho() {
  # shellcheck disable=SC2145
  echo -e "\033[${1}${@:2}\033[0m" 1>&2
}

check_root() {
  if [[ $EUID != 0 ]]; then
    colorEcho ${RED} "当前非root账号(或没有root权限)，无法继续操作，请更换root账号!"
    colorEcho ${YELLOW} "使用sudo -命令获取临时root权限（执行后可能会提示输入root密码）"
    exit 1
  fi
}

startFunc(){
  colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
  colorEcho ${BLUE} "开始执行 启动RKE集群的操作 ………"
  echo ""

  colorEcho ${BLUE} "本脚本的运行有一些列的前提依赖，请确定以下的项目都已完成！！！！"
  colorEcho ${YELLOW} "----------------------------------------------------------"
  colorEcho ${RED} "1. 完成基础环境初始化，将rke系统镜像均上传至私有Harbor中！"
  colorEcho ${RED} "2. 配置并修改好 rke集群的模板文件，命名为 cluster.yml !!"
  colorEcho ${RED} "3. ……"
  colorEcho ${YELLOW} "----------------------------------------------------------"

  while true; do
      colorEcho ${RED} "请确保您已经将上述的项目完成！！"
      read -r -p "请输入yes进行确认，脚本才可继续运行！！" input
      case $input in
      yes)
        colorEcho ${GREEN} "您已确认上述的项目均已完成！！"
        colorEcho ${GREEN} "----------------------------------------------------------"
        echo ""
        colorEcho ${BLUE} "开始执行 RKE集群的启动过程！！！"
        echo ""
        main
        break
        ;;
      *)
        echo ""
        colorEcho ${RED} "输入有误！！！ 请输入 >> yes << 进行确认"
        break
        colorEcho ${RED} "-----------------------------------------------------"
        echo ""
        ;;
      esac
  done
}

installRKE(){
  colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
  colorEcho ${BLUE} "开始下载并安装 RKE 工具 ………"
  echo ""

  colorEcho ${BLUE} "开始从rancher镜像下载rke工具……"
  wget http://rancher-mirror.cnrancher.com/rke/v1.2.6/rke_linux-amd64
  if [ -s rke_linux-amd64 ]; then
    colorEcho ${GREEN} "rke工具下载完成！"
    chmod +x rke_linux-amd64
    mv ./rke_linux-amd64 /usr/local/bin/rke
    colorEcho ${GREEN} "----------------------------------------------------------"
    rke --version
    colorEcho ${GREEN} "----------------------------------------------------------"
    rke config --list-version --all
    echo ""
    colorEcho ${BLUE} "开始从rancher镜像下载 kubectl 工具……"
    wget http://rancher-mirror.cnrancher.com/kubectl/v1.20.4/linux-amd64-v1.20.4-kubectl
    chmod +x linux-amd64-v1.20.4-kubectl
    mv linux-amd64-v1.20.4-kubectl /usr/local/bin/kubectl
    colorEcho ${GREEN} "----------------------------------------------------------"
    kubectl version
    colorEcho ${GREEN} "----------------------------------------------------------"
  else
    colorEcho ${RED} "rke工具下载失败！！！脚本无法继续运行，请手动下载rke工具！"
    colorEcho ${RED} "rke工具下载失败！！！脚本无法继续运行，请手动下载rke工具！"
    colorEcho ${RED} "rke工具下载失败！！！脚本无法继续运行，请手动下载rke工具！"
    return 1
  fi
}

createRKEInstallerUser(){
  colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
  colorEcho ${BLUE} "开始创建 rke-installer 用户………"
  echo ""
  useradd rke-installer
  echo "rke-installer
  rke-installer
  " | passwd rke-installer
  #将登陆用户develop加入到docker用户组中
  gpasswd -a rke-installer docker
  #更新用户组
  newgrp docker
  echo ""
  if [ -d /home/rke-installer ]; then
      colorEcho ${GREEN} "rke-installer 用户创建成功！！ "
      echo ""
  else
    mkdir -p /home/rke-installer
    chown rke-installer:rke-installer -R /home/rke-installer
    usermod -d /home/rke-installer rke-installer
    colorEcho ${YELLOW} "检测到 rke-installer 用户已经存在"
  fi

  if [[ -s cluster.yaml || -s cluster.yml ]]; then
      colorEcho ${BLUE} "开始将 cluster.yaml文件复制到 rke-installer目录下…………"
      mv cluster.y* /home/rke-installer/cluster.yml
      if [ -s /home/rke-installer/cluster.yml ]; then
          colorEcho ${BLUE} "cluster.yml文件已经放置完成！"
          chown rke-installer:rke-installer /home/rke-installer/cluster.yml
      else
          colorEcho ${RED} "当前目录下未检测到 rke集群的模板文件！！"
          colorEcho ${RED} "程序无法继续，将退出！！"
          return 1
      fi
  else
      colorEcho ${RED} "当前目录下未检测到 rke集群的模板文件！！"
      colorEcho ${RED} "程序无法继续，将退出！！"
      echo ""
      colorEcho ${YELLOW} "--------------------------------------------------"
      colorEcho ${RED} "请创建RKE集群的模板文件，并命名为 cluster.yml "
      colorEcho ${RED} "请创建RKE集群的模板文件，并命名为 cluster.yml "
      colorEcho ${RED} "请创建RKE集群的模板文件，并命名为 cluster.yml "
      colorEcho ${YELLOW} "--------------------------------------------------"
      return 1
  fi

  colorEcho ${BLUE} "开始切换当前用户至 rke-installer "
  su rke-installer
  echo ""
  colorEcho ${BLUE} "请检查rke-installer用户能否执行 docker ps 命令！！"
  docker ps
  colorEcho ${BLUE} "----------------------------------------------------------"
}

generateRKEUserKey(){
  colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
  colorEcho ${BLUE} "开始创建 rke-installer用户的 ssh key ……"
  echo ""
  su rke-installer
  ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
  cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  chmod 600 ~/.ssh/authorized_keys
  colorEcho ${GREEN} "--------------------------------------------------------------"
  colorEcho ${GREEN} "-----------本机配置完成！-------------"
  echo ""

  for ip in "${PrivateServerIPs[@]}"; do
    colorEcho ${BLUE} "请手动将如下的命令，以 root 权限在主机 ${ip} 上运行"
    colorEcho ${BLUE} "请手动将如下的命令，以 root 权限在主机 ${ip} 上运行"
    colorEcho ${BLUE} "请手动将如下的命令，以 root 权限在主机 ${ip} 上运行"
    colorEcho ${BLUE} "-----------------------------------------------"
    echo ""
    echo ""
    colorEcho ${RED} "                        请以 root 角色 运行！！！                    "
    colorEcho ${RED} "                        请以 root 角色 运行！！！                    "
    colorEcho ${RED} "                        请以 root 角色 运行！！！                    "
    echo ""
    colorEcho ${YELLOW} "useradd rke-installer && echo \"rke-installer
rke-installer
\" | passwd rke-installer && gpasswd -a rke-installer docker && newgrp docker && su rke-installer && docker ps "
    echo ""
    colorEcho ${YELLOW} "clear && ssh-keygen -t rsa -P \"\" -f ~/.ssh/id_rsa && echo \"$(cat ~/.ssh/id_rsa.pub)\" >> ~/.ssh/authorized_keys && echo \"\" && cat ~/.ssh/authorized_keys"
    echo ""
    echo ""
    while true; do
      colorEcho ${RED} "请确保您已经将上述的命令在主机${ip}上执行了！！"
      read -r -p "请输入yes进行确认，脚本才可继续运行！！" input
      case $input in
      yes)
        colorEcho ${GREEN} "您已确认在主机${ip}上添加了私有的ssh key！"
        echo ""
        break
        ;;
      *)
        echo ""
        colorEcho ${RED} "输入有误！！！ 请输入 >> yes << 进行确认"
        colorEcho ${RED} "请在主机${ip}上执行上述命令！！！"
        colorEcho ${RED} "否则本脚本的功能会失效！！"
        colorEcho ${RED} "-----------------------------------------------------"
        echo ""
        ;;
      esac
    done
  done
}

startRKECLuster(){
  colorEcho ${PURPLE} "---------------------------------------------------------------------------------"
  colorEcho ${BLUE} "开始 启动 rke集群 ！！！"
  colorEcho ${BLUE} "开始 启动 rke集群 ！！！"
  colorEcho ${BLUE} "开始 启动 rke集群 ！！！"
  echo ""
  if [[ $(pwd) == "/home/rke-installer" ]]; then
      colorEcho ${BLUE} "检测到当前目录为 /home/rke-installer"
      echo ""
      colorEcho ${BLUE} "开始执行 RKE 集群的启动过程 ！！！"
      colorEcho ${BLUE} "-------------------------------------------------------------"
      for i in {3..1..-1}; do
        colorEcho ${BLUE} "倒计时开始 ->> $i 秒 <<-，准备启动RKE，上文的日志输出将会消失！！"
        sleep 2
      done
      clear
      rke up
  else
    colorEcho ${BLUE} "当前目录不为 /home/rke-installer，开始跳转目录！"
    cd /home/rke-installer
    startRKECLuster
  fi
}

main(){
  check_root
  installRKE || return $?
  generateRKEUserKey || return $?
  startRKECLuster || return $?
}

startFunc