#!/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

# gpaas配置文件路径
GPAAS_CONFIG=${4}

if [ -d ${GPAAS_CONFIG} ]; then
  GPAAS_CONFIG=${GPAAS_CONFIG}/config
fi

install_gpaas=0
if [ -f ${GPAAS_CONFIG} ]; then
  install_gpaas=1
  # 加载gpaas环境变量
  . ${GPAAS_CONFIG}
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
    echo "    [gpaas]" >> ${YAML_FILE_DIR}/configmap.yaml
    echo "    [gpaas:vars]" >> ${YAML_FILE_DIR}/configmap.yaml
    echo "    install_gpaas=${install_gpaas}" >> ${YAML_FILE_DIR}/configmap.yaml
    if [ ${install_gpaas} -eq 1 ]; then
      echo "    gpaas_password=${GPAAS_PASSWORD}" >> ${YAML_FILE_DIR}/configmap.yaml
      echo "    http_protocol=${HTTP_PROTOCOL}" >> ${YAML_FILE_DIR}/configmap.yaml
      echo "    url_domain=${URL_DOMAIN}" >> ${YAML_FILE_DIR}/configmap.yaml
    fi
  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
  echo "[all:vars]" >> ${dir}/cosmic_config
  cat ${ANSIBLE_HOSTS} | grep "PUSH_IMAGES" >> ${dir}/cosmic_config
  cat ${ANSIBLE_HOSTS} | grep "ingress_https_port" >> ${dir}/cosmic_config
  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
}

function check_merge_config() {
  > ${dir}/check_cosmic_config
  if [ -d ${COSMIC_CONFIG_DIR} ]; then
    cat ${ANSIBLE_HOSTS} | awk '/\[K8S_MASTER\]/,/\[all.*\]/' | grep -v "\[all\:vars\]" >> ${dir}/check_cosmic_config
    cat ${ANSIBLE_HOSTS} | awk '/\[K8S_ALL:children\]/,/*/' | grep -v "\[all\:vars\]" >> ${dir}/check_cosmic_config
    cat ${COSMIC_CONFIG_DIR}/cosmic-app >> ${dir}/check_cosmic_config
    if [ -f ${COSMIC_CONFIG_DIR}/cosmic-postgresql ]; then
      cat ${COSMIC_CONFIG_DIR}/cosmic-postgresql | grep "PG_PORT" >> ${dir}/check_cosmic_config
    fi
    cat ${COSMIC_CONFIG_DIR}/cosmic-nginx | grep "nginx_vip" >> ${dir}/check_cosmic_config
    cat ${COSMIC_CONFIG_DIR}/cosmic-nginx | grep "elb_host" >> ${dir}/check_cosmic_config
    cat ${COSMIC_CONFIG_DIR}/* >> ${dir}/check_cosmic_config
  fi
}

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

#合并配置
merge_config
check_merge_config

#生成yaml文件
generate_yaml

# check
ansible-playbook -i ${dir}/check_cosmic_config ${dir}/../ansible/03* --become --become-method=sudo --become-user=root

#执行
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
