#!/usr/bin/env bash
#glusterfs8.6

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 heketi_ssh_copy_id() {
  expect -c "set timeout -1
  spawn ssh-copy-id -i /etc/heketi/heketi_key.pub $1
  expect {
    *yes/no* {send -- yes\r;exp_continue;}
    *password:* {send -- $2\r;exp_continue;}
    eof        {exit 0;}
  }"
}


function heketi_key() {
      #配置heketi密钥对
    if [[ ! -f /etc/heketi/heketi_key.pub ]];then
      if [[ `cat /etc/os-release` =~ CentOS ]];then
        ssh-keygen -f /etc/heketi/heketi_key -t rsa -N ''
        chown heketi:heketi /etc/heketi/heketi_key*
      else
        ssh-keygen -m PEM -t rsa -b 4096 -q -f /etc/heketi/heketi_key -N ''
        chown heketi:heketi /etc/heketi/heketi_key*
      fi
    fi
    password=$(grep "SERVER_PASSWORD" $BASE_DIR/ai.config)
    password=${password#*=}
    storage_type=$(grep "STORAGE_TYPE" $BASE_DIR/ai.config | awk -F '=' '{print $2}')

    if [ "$storage_type" == "glusterfs" ];then
      gfs_ips=$(grep "^STORAGE_NODE_IP" $BASE_DIR/ai.config | awk -F ' ' '{print $1}' | awk -F '=' '{print $2}')
      for ip in  $gfs_ips
      do
        logger info "glusterfs node ip: ${ip}"
        heketi_ssh_copy_id  ${ip} ${password}
      done
    fi
}

function heketi_install() {
   #3、安装heketi
   if [ `systemctl is-active heketi` != "active" ];then
       logger info "创建heketi目录：/var/lib/heketi /etc/heketi /var/log/heketi"
       mkdir -p /var/lib/heketi /etc/heketi /var/log/heketi
       logger info "创建heketi用户组"
       groupadd --system heketi
       useradd -s /sbin/nologin --system -g heketi heketi

       curl -o /opt/heketi-v9.0.0.linux.amd64.tar.gz  -C- -O --retry 3 https://minio.mopaas.com/ai-deployer/glusterfs8-6/heketi-v9.0.0.linux.amd64.tar.gz
       tar xvf /opt/heketi-v9.0.0.linux.amd64.tar.gz -C /opt
       logging info "复制heketi配置文件"
       cp /opt/heketi/heketi /usr/bin/heketi && cp /opt/heketi/heketi-cli /usr/bin/heketi-cli

       cp $BASE_DIR/ai/storage-install/glusterfs8-6/heketi.service /usr/lib/systemd/system/heketi.service
       cp $BASE_DIR/ai/storage-install/glusterfs8-6/heketi.json /etc/heketi/heketi.json
       logger info "修改heketi用户组"
       chown -R heketi:heketi /var/lib/heketi /var/log/heketi /etc/heketi
  fi
    #配置heketi密钥对
    logger info "配置heketi密钥对"
    heketi_key

    logger info "重启heketi"
    #systemctl start heketi
    systemctl enable heketi
    systemctl daemon-reload && systemctl restart heketi
    #判断heketi是否正常
    if [ `systemctl is-active heketi` == "active" ];then
      logger info "heketi正常启动"
    else
      logger error "heketi未正常启动"
      exit 1
    fi
}

function gfs_node_create() {
    #4、使用heketi创建glusterfs集群
    ######使用topology创建集群
    mgr_node_ip=$(grep "MGR_NODE_IP" $BASE_DIR/ai.config |awk '{print $1}'|awk -F '=' '{print $2}')
    node_str=""
    template="{\"node\": {\"hostnames\": {\"manage\": [\"_HOSTNAME_\"],\"storage\": [\"_HOSTNAME_\"]},\"zone\": 1},\"devices\": [\"_DEV_NAME_\"]},"

    storage_type=$(grep "STORAGE_TYPE" $BASE_DIR/ai.config | awk -F '=' '{print $2}')

    if [ "$storage_type" == "glusterfs" ];then
      gfs_ips=$(grep "^STORAGE_NODE_IP" $BASE_DIR/ai.config | awk -F ' ' '{print $1}' | awk -F '=' '{print $2}')
      for ip in  $gfs_ips
      do
        logger info "glusterfs node ip: ${ip}"
        disk=$(grep "^STORAGE_NODE_IP"  $BASE_DIR/ai.config |grep ${ip} | awk -F ' ' '{print $2}' | awk -F '=' '{print $2}')
        temp_str=`echo ${template} | sed "s#_HOSTNAME_#${ip}#g"`
        temp_str=`echo ${temp_str} | sed "s#_DEV_NAME_#${disk}#g"`
        node_str="${node_str}${temp_str}"
      done
    fi
    node_str=${node_str%?}
    echo $node_str
    cp $BASE_DIR/ai/storage-install/glusterfs8-6/topology.json.tmp $BASE_DIR/ai/storage-install/glusterfs8-6/topology.json
    sed -i "s#_NODES_#$node_str#g" $BASE_DIR/ai/storage-install/glusterfs8-6/topology.json

    /usr/bin/heketi-cli --server=http://${mgr_node_ip}:38089 --user admin --secret admin topology load --json=$BASE_DIR/ai/storage-install/glusterfs8-6/topology.json 2>&1
}

function storage_class_create() {
  mgr_node_ip=$(cat $BASE_DIR/ai.config|grep MGR_NODE_IP|awk '{print $1}'|awk -F '=' '{print $2}')
  #heketi-server端口问题-->改为38089

  cp $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml.tmp $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml
  sed -i "s#HEKETI_SVC_CLUSTER_IP#${mgr_node_ip}#g" $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml
#默认为一个volume无副本
#  host_num=$(grep "^STORAGE_NODE_IP" $BASE_DIR/ai.config |wc -l)
#  if [ "$host_num" == 1 ];then
#    sed -i "s#_VOLUMETYPE_#none#g" $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml
#  else
#    sed -i "s#_VOLUMETYPE_#replicate:$host_num#g" $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml
#  fi
  #kubectl命令的位置
  logger info "创建storage-class"
  $BASE_DIR/bin/kubectl create -f $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-storage-class.yaml
}


########运行###########################################################################
#BASE_DIR=/etc/ai-deployer
BASE_DIR=$(grep  "BASE" $PWD/ai.config | awk -F '=' '{print $2}')

  #单独安装或删除
  case "$1" in
    (install-glusterfs)
      #：1、为离线安装 2、判断节点是否安装过gfs8.6
      #需要修改hosts的获取方法
      ansible-playbook -i $BASE_DIR/clusters/ai/hosts $BASE_DIR/ai/storage-install/glusterfs8-6/gfs8-6.yaml
      ;;
    (install-heketi)
      heketi_install
      ;;
    (delete-glusterfs)
      ansible-playbook -i $BASE_DIR/clusters/ai/hosts $BASE_DIR/ai/storage-install/glusterfs8-6/del-glusterfs.yaml
      ;;
    (add-glusterfs-node)
      #在新存储节点安装gfs
       ansible-playbook -i $BASE_DIR/clusters/ai/hosts $BASE_DIR/ai/storage-install/glusterfs8-6/gfs8-6.yaml
      #在新存储节点添加heketi密钥
       heketi_key
       #导入新topology.json
       gfs_node_create
      ;;
    (set-all)
    #全部安装
      ansible-playbook -i $BASE_DIR/clusters/ai/hosts $BASE_DIR/ai/storage-install/glusterfs8-6/gfs8-6-install.yaml
      ansible-playbook -i $BASE_DIR/clusters/ai/hosts $BASE_DIR/ai/storage-install/glusterfs8-6/glusterfs-client.yaml
      heketi_install
      gfs_node_create
      storage_class_create
      ;;
  esac
