#!/bin/bash
#调用此脚本的用法   "?.sh config文件路径"；如果传入的是目录，默认读取该目录下的config文件；如果传入的是文件则为指定的文件

set -u -e

dir=$(dirname $0)
COSMIC_CONFIG_DIR=${1}
# k8s集群配置路径
ANSIBLE_HOSTS=${2}
if [ ! -f ${ANSIBLE_HOSTS} ]
then
  ANSIBLE_HOSTS=${3}
fi

echo "k8s config path: ${ANSIBLE_HOSTS}"

context='apiVersion: v1
kind: ConfigMap
metadata:
  name: init
  namespace: init
data:
  cosmic.conf: |'

YAML_FILE_DIR=${dir}/../ansible/roles/00-copy_all/templates

function generate_yaml() {
  echo -e "生成configmap.yaml"
  if [ -d ${COSMIC_CONFIG_DIR} ]
  then
    echo "$context" > ${YAML_FILE_DIR}/configmap.yaml
    # 设置shell分隔符
    IFS=$'\n'
    for i in $(cat ${COSMIC_CONFIG_DIR}/*);do echo "    ""$i" >> ${YAML_FILE_DIR}/configmap.yaml;done
  fi

  # 表示有密钥存在
  if cat ${YAML_FILE_DIR}/configmap.yaml | grep ansible_ssh_private_key_file >/dev/null; then
    key_file_dir=$(pwd)/ssh_keys
    init_key_path=/opt/gowww/singularity/runtime/ssh_keys
    is_use_key=1
    echo "key_file_dir=$key_file_dir" >> ${dir}/cosmic_config
    echo "init_key_path=$init_key_path" >> ${dir}/cosmic_config
    echo "is_use_key=$is_use_key" >> ${dir}/cosmic_config
    # 替换成init job任务pod的路径
    sed -i "s#$key_file_dir#$init_key_path#g" ${YAML_FILE_DIR}/configmap.yaml
  fi

  # 合成到一个文件
  echo -e "合成manifest.yaml.j2"
  > ${YAML_FILE_DIR}/manifest.yaml.j2
  cat ${YAML_FILE_DIR}/0namespace.yaml >> ${YAML_FILE_DIR}/manifest.yaml.j2
  echo "---" >> ${YAML_FILE_DIR}/manifest.yaml.j2
  cat ${YAML_FILE_DIR}/clusterrolebinding.yaml >> ${YAML_FILE_DIR}/manifest.yaml.j2
  echo "---" >> ${YAML_FILE_DIR}/manifest.yaml.j2
  cat ${YAML_FILE_DIR}/serviceaccount.yaml >> ${YAML_FILE_DIR}/manifest.yaml.j2
  echo "---" >> ${YAML_FILE_DIR}/manifest.yaml.j2
  cat ${YAML_FILE_DIR}/configmap.yaml >> ${YAML_FILE_DIR}/manifest.yaml.j2
  echo "---" >> ${YAML_FILE_DIR}/manifest.yaml.j2
  cat ${YAML_FILE_DIR}/job.yaml.j2 >> ${YAML_FILE_DIR}/manifest.yaml.j2
}


function merge_config() {
  > ${dir}/cosmic_config
  cat ${ANSIBLE_HOSTS} | awk '/\[K8S_MASTER\]/,/\[all.*\]/' | grep -v "\[all\:vars\]" >> ${dir}/cosmic_config
#  cat ${ANSIBLE_HOSTS} >> ${dir}/cosmic_config
#  cat ${COSMIC_CONFIG_DIR}/* >> ${dir}/cosmic_config
  echo "[all:vars]" >> ${dir}/cosmic_config
  mysql=`cat ${COSMIC_CONFIG_DIR}/*|grep "^db_user=*"|awk '{print}'`
  if [ ! -z "$mysql" ]
  then
    echo "use mysql as database"
    echo "$mysql" >> ${dir}/cosmic_config
  fi
  pg=`cat ${COSMIC_CONFIG_DIR}/*|grep "^PG_USER=*"|awk '{print}'`
  if [ ! -z "$pg" ]
  then
    echo "use pg as database"
    echo "$pg" >> ${dir}/cosmic_config
  fi
  pg_provide=`cat ${COSMIC_CONFIG_DIR}/*|grep "^pgconfig=*"|awk '{print}'`
  if [ ! -z "$pg_provide" ]
  then
    echo "use pg_provide as database"
    echo "$pg_provide" >> ${dir}/cosmic_config
  fi
  cosmic_install_dir=`cat ${COSMIC_CONFIG_DIR}/*|grep "^cosmic_install_dir=*"|awk '{print}'`
  if [ ! -z "$cosmic_install_dir" ]
  then
    echo "$cosmic_install_dir" >> ${dir}/cosmic_config
  fi
}

#不单独使用这个脚本，不需要重复部署ansible
#echo -e "\033[38;4;94m>>>>部署ansible\033[0m"
#${dir}/shell/install-ansible.sh

#合并配置
merge_config

#生成yaml文件
generate_yaml

#执行
ansible-playbook -i ${dir}/cosmic_config ${dir}/../ansible/00* --tags=cosmic  --become  --become-method=sudo --become-user=root
ansible-playbook -i ${dir}/cosmic_config ${dir}/../ansible/01* --tags=cosmic  --become  --become-method=sudo --become-user=root
