#!/bin/bash
#  Create & manage k8s clusters

set -o nounset
set -o errexit
#set -o xtrace

# storage-install <>   输入一个参数
function usage() {
    echo -e "\033[33mUsage:\033[0m storage-install COMMAND [args]"
    cat <<EOF
-------------------------------------------------------------------------------------
1.command
Setups:
    glusterfs-install            to quickly setup a glusterfs cluster and create glusterfs storage class
    glusterfs-remove             to remove glusterfs storage class and destroy the glusterfs cluster
    longhorn-install             to quickly setup a longhorn cluster
2.BASE_DIR

storage-install <commad> <BASE_DIR>
EOF
}

function logger() {
  TIMESTAMP=$(date +'%Y-%m-%d %H:%M:%S')
  case "$1" in
    debug)
      echo -e "$TIMESTAMP \033[36mDEBUG\033[0m $2"
      ;;
    info)
      echo -e "$TIMESTAMP \033[32mINFO\033[0m $2"
      ;;
    warn)
      echo -e "$TIMESTAMP \033[33mWARN\033[0m $2"
      ;;
    error)
      echo -e "$TIMESTAMP \033[31mERROR\033[0m $2"
      ;;
    *)
      ;;
  esac
}

function cmd() {
 #   [[ -d "clusters/$1" ]] || { logger error "invalid config, run 'ezctl new $1' first"; return 1; }

    PLAY_BOOK="dummy.yml"
    case "$1" in
      (i-glusterfs)
          PLAY_BOOK="12.glusterfs.yml"
          ;;
      (r-glusterfs)
          PLAY_BOOK="40.delglusterfs.yml"
          ;;
      (i-longhorn)
          PLAY_BOOK="13.longhorn.yml"
          ;;
      (i-mce)
          PLAY_BOOK="50.mce.yml"
          ;;
      (*)
          usage
          exit 1
          ;;
    esac

    logger info "$1  begins in 3s, press any key to abort:\n"
    ! (read -r -t3 -n1) || { logger warn "$1 abort"; return 1; }

    cp /etc/kubeasz/clusters/ai/hosts $BASE/ai/storage-install/
    sed -i "s#/etc/kubeasz#$BASE/ai/storage-install#g" $BASE/ai/storage-install/hosts

    cat  "$BASE"/ai.config | grep ^STORAGE_NODE_IP > /tmp/storage-nodes.tmp
#    storage_num=$(cat ../../ai.config | grep ^STORAGE_NODE_IP | wc -l)

    if [ "$PLAY_BOOK" ==  "12.glusterfs.yml" ] || [ "$PLAY_BOOK" ==  "40.delglusterfs.yml" ];then
      echo "[glusterfs]" >> $BASE/ai/storage-install/hosts
    elif [ "$PLAY_BOOK" ==  "13.longhorn.yml" ]; then
      echo "[longhorn]" >> $BASE/ai/storage-install/hosts
    fi
    IFS=$'\n'
    for storage_node in `cat /tmp/storage-nodes.tmp`
    do
      storage_node_ip=$(echo $storage_node | awk -F " " '{ print $1 }'| awk -F "=" '{ print $2 }')
      storage_node_disk=$(echo $storage_node | awk -F " " '{ print $2 }')
      echo "${storage_node_ip} ${storage_node_disk}" >> $BASE/ai/storage-install/hosts
    done
    
    cp $BASE/ai/storage-install/ansible.cfg.tmp $BASE/ai/storage-install/ansible.cfg
    sed -i "s#ROLES_PATH_PWD#$BASE/ai/storage-install#g" $BASE/ai/storage-install/ansible.cfg
    ansible-playbook -i "$BASE/ai/storage-install/hosts"  "$BASE/ai/storage-install/playbooks/$PLAY_BOOK" || return 1
}


### Main Lines ##################################################
function main() {
  #BASE=/etc/ai-deployer
  BASE="$2"
  [[ -d "$BASE" ]] || { logger error "invalid dir:$BASE"; exit 1; } #判断$BASE目录是否存在
  # check bash shell
  #readlink 获得文件的绝对路径
  # $$获得shell的PID
  readlink /proc/$$/exe|grep -q "dash" && { logger error "you should use bash shell only"; exit 1; }

  # check 'ansible' executable
  which ansible > /dev/null 2>&1 || { logger error "need 'ansible', try: 'pip install ansible'"; usage; exit 1; }

  [ "$#" -gt 0 ] || { usage >&2; exit 2; } # 2>&1 、 >&2 输出错误日志 $#: 传入脚本的参数个数

  case "$1" in
      ### in-cluster operations #####################
      (glusterfs-install)
          [ "$#" -eq 2 ] || { usage >&2; exit 2; } #判断参数数量，如果参数数量不等于2，usage exit
          cmd  i-glusterfs #./storage-install i-glusterfs   # cmd cluster i-glusterfs
          ;;
      (glusterfs-remove)
          [ "$#" -eq 2 ] || { usage >&2; exit 2; }
          cmd  r-glusterfs
          ;;
      (longhorn-install)
          [ "$#" -eq 2 ] || { usage >&2; exit 2; }
          cmd  i-longhorn
          ;;
      (*)
          usage
          exit 0
          ;;
  esac
 }

main "$@"
