#!/bin/bash

# k8s 安装配置信息变量获取
TYPE=$(get_config install_type TYPE)
SYSTEM=$(get_config system_type SYSTEM)
SVERSION=$(get_config system_version SVERSION)
TIMESERVER=$(get_config time_server TIMESERVER)
K8S_HOSTNAME=$(get_config all-in-one_hostname K8S_HOSTNAME)
ONE_IP=$(get_config all-in-one_ip ONE_IP)
ONE_AUTH=$(get_config all-in-one_account ONE_AUTH)
ONE_PASSWORD=$(get_config all-in-one_account ONE_PASSWORD)
K8S_LB=$(get_config group_hostname K8S_LB)
K8S_MASTER01=$(get_config group_hostname K8S_MASTER01)
K8S_MASTER02=$(get_config group_hostname K8S_MASTER02)
K8S_MASTER03=$(get_config group_hostname K8S_MASTER03)
K8S_NODE01=$(get_config group_hostname K8S_NODE01)
K8S_NODE02=$(get_config group_hostname K8S_NODE02)
K8S_NODE03=$(get_config group_hostname K8S_NODE03)
K8S_MASTER_LB_IP=$(get_config group_ip K8S_MASTER_LB_IP)
K8S_MASTER_IP01=$(get_config group_ip K8S_MASTER_IP01)
K8S_MASTER_IP02=$(get_config group_ip K8S_MASTER_IP02)
K8S_MASTER_IP03=$(get_config group_ip K8S_MASTER_IP03)
K8S_NODE_IP01=$(get_config group_ip K8S_NODE_IP01)
K8S_NODE_IP02=$(get_config group_ip K8S_NODE_IP02)
K8S_NODE_IP03=$(get_config group_ip K8S_NODE_IP03)
KVERSION=$(get_config k8s_version KVERSION)
ACTION_TYPE=$(get_config action_type ACTION_TYPE)
REGISTRY=$(get_config harbor_ip REGISTRY)
MASTER_ETH01=$(get_config haproxy_eth MASTER_ETH01)
MASTER_ETH02=$(get_config haproxy_eth MASTER_ETH02)
MASTER_ETH03=$(get_config haproxy_eth MASTER_ETH03)

NAMESPACE=$(get_config deployenv NAMESPACE)
WorkNode01=$(get_config deployenv WorkNode01)
WorkNode02=$(get_config deployenv WorkNode02)
WorkNode03=$(get_config deployenv WorkNode03)
aWorkNode01=$(get_config deployenv aWorkNode01)
aWorkNode02=$(get_config deployenv aWorkNode02)
aWorkNode03=$(get_config deployenv aWorkNode03)
ES_PV_VOLUME=$(get_config deployenv ES_PV_VOLUME)
ES_PV_PATH=$(get_config deployenv ES_PV_PATH)
ES_BACKUP_VOLUME=$(get_config deployenv ES_BACKUP_VOLUME)
ES_BACKUP_PATH=$(get_config deployenv ES_BACKUP_PATH)
MQ_PV_VOLUME=$(get_config deployenv MQ_PV_VOLUME)
MQ_PV_PATH=$(get_config deployenv MQ_PV_PATH)
REDIS_PV_VOLUME=$(get_config deployenv REDIS_PV_VOLUME)
REDIS_PV_PATH=$(get_config deployenv REDIS_PV_PATH)
BASE_DIR=$(get_config deployenv BASE_DIR)
MGR_PV_VOLUME=$(get_config deployenv MGR_PV_VOLUME)
MGR_BACKUP_VOLUME=$(get_config deployenv MGR_BACKUP_VOLUME)
MINIO_PV_VOLUME=$(get_config deployenv MINIO_PV_VOLUME)
MINIO_PV_PATH=$(get_config deployenv MINIO_PV_PATH)
ES_JAVA_OPTS=$(get_config deployenv ES_JAVA_OPTS)
STORAGE_MODE=$(get_config deployenv STORAGE_MODE)
WEBOFFICE=$(get_config deployenv WEBOFFICE)
S3SECRET=$(get_config deployenv S3SECRET)
S3URL=$(get_config deployenv S3URL)
S3ACCESS=$(get_config deployenv S3ACCESS)
S3BUCKET=$(get_config deployenv S3BUCKET)
S3HealthUrl=$(get_config deployenv S3HealthUrl)
DOMAIN_NAME=$(get_config deployenv DOMAIN_NAME)
CONTAINERD=$(get_config deployenv CONTAINERD)
RUNENV=$(get_config deployenv RUNENV)
STG_MountType=$(get_config deployenv STG_MountType)

K8S_SERVICE_IP=$(get_config k8s_network K8S_SERVICE_IP)
K8S_CLUSTER_DNS=$(get_config k8s_network K8S_CLUSTER_DNS)
K8S_CLUSTER_NET=$(get_config k8s_network K8S_CLUSTER_NET)

APM_STATUS=$(get_config common APM)
RANCHER_STATUS=$(get_config common RANCHER)
SKYWALKING_STATUS=$(get_config common SKYWALKING)
SSH_PORT=$(get_config common SSH_PORT)
PRODUCTION=$(get_config common PRODUCTION)
DEPLOY_MODE=$(get_config common DEPLOY_MODE)
# VERSION=$(cat version)
DOMAIN_NAME=$(get_config deployenv DOMAIN_NAME)
NODE_PORT=$(get_config common SERVICE_PORT)
REGION=$(get_config common REGION)
SSL=$(get_config common SSL)
MINIOTYPE=$(get_config common MINIOTYPE)
WF=$(get_config common WF)
# ENABLED_SERVICE=$(get_config common ENABLED_SERVICE)

DOCKER0_NETWORK=$(get_config docker DOCKER0_NETWORK)
DOCKER_GWBRIDGE_NETWORK=$(get_config docker DOCKER_GWBRIDGE_NETWORK)
MACROWING_NETWORK=$(get_config docker MACROWING_NETWORK)
DOCKER_DATA_ROOT=$(get_config docker DOCKER_DATA_ROOT)

# subregion config
REGIONNAME=$(get_config subregionconf REGIONNAME)
PRIMARYURL=$(get_config subregionconf PRIMARYURL)
INTRANETADDRESS=$(get_config subregionconf INTRANETADDRESS)
REGIONURL=$(get_config subregionconf REGIONURL)
REGISTERCODE=$(get_config subregionconf REGISTERCODE)
REGION_TSAP=$(get_config subregionconf REGION_TSAP)

# database config
#UsedDatabase=$(get_config database UsedDatabase)
#DatabaseType=$(get_config database DatabaseType)
#DatabaseServerName=$(get_config database DatabaseServerName)
#DatabaseUserName=$(get_config database DatabaseUserName)
#DatabasePassword=$(get_config database DatabasePassword)
#DatabaseServerPort=$(get_config database DatabaseServerPort)
#DatabaseName=$(get_config database DatabaseName)

ENVFILE=envfile.env
SSH_OPT='-o StrictHostKeyChecking=no'

IP_LIST=($(get_node_ip $REGION))
INDRIVE_NODE=($(get_ip_by_attrvalue $REGION labels InDrive))
MIDDLEWARE_NODE=($(get_ip_by_attrvalue $REGION labels Middleware))
APM_NODE=($(get_ip_by_attrvalue $REGION labels APM))
MANAGER_IP=($(get_ip_by_attrvalue $REGION role manager))
WORKER_IP=($(get_ip_by_attrvalue $REGION role worker))
MANAGER_IP_1=${MANAGER_IP[0]}
MANAGER_IP_1_USER=$(get_node_attribute "$REGION" $MANAGER_IP_1 user)
DEPLOYED_MANAGER="${MANAGER_IP_1_USER}@${MANAGER_IP_1}"

function get_s3_common_config() {
    S3SECRET=$(get_config $STORAGE_MODE S3SECRET)
    S3ACCESS=$(get_config $STORAGE_MODE S3ACCESS)
    S3URL=$(get_config $STORAGE_MODE S3URL)
    S3BUCKET=$(get_config $STORAGE_MODE S3BUCKET)
    DATA_PATH=$(get_config $STORAGE_MODE STORAGE_PATH)
    WEBOFFICE_DATA_PATH=$(get_config $STORAGE_MODE WEBOFFICE_DATA_PATH) 
}

function get_storage_config() {
    case $STORAGE_MODE in
    "local" | "nas")
        DATA_PATH=$(get_config $STORAGE_MODE STORAGE_PATH)
        WEBOFFICE_DATA_PATH=$(get_config $STORAGE_MODE WEBOFFICE_DATA_PATH) 
        ;;
    "oss" | "s3")
        get_s3_common_config
        ;;
    "ceph")
        get_s3_common_config
        S3HealthUrl=$(get_config $STORAGE_MODE S3HealthUrl)
        ;;
    "swift")
        get_s3_common_config
        S3HealthUrl=$(get_config $STORAGE_MODE S3HealthUrl)
        SWIFTUSERNAME=$(get_config $STORAGE_MODE SWIFTUSERNAME)
        CONTAINERID=$(get_config $STORAGE_MODE CONTAINERID)
        ;;
    "minio")
        MINIO_PV_VOLUME=$(get_config deployenv MINIO_PV_VOLUME)
        MINIO_PV_PATH=$(get_config deployenv MINIO_PV_PATH)       
        ;;
    "*")
        log_error 'Invalid params, available values "local", "nas", "oss", "ceph", "swift"'
        exit 1000
        ;;
    esac
}

get_storage_config

