#!/bin/bash
# Copyright © Huawei Technologies Co., Ltd.2019-2022. All rights reserved.
# 加载索引
ls ./env.properties >/dev/null 2>&1
if [ $? -ne 0 ]; then
    ARBITRATION_INSTALL_LOG=/var/log/arbitration_install
else
    . ./env.properties
fi
mkdir -p ${ARBITRATION_INSTALL_LOG}
ARBITRATION_INSTALL_LOG_FILE_PATH="${ARBITRATION_INSTALL_LOG}/arbitration_install.log"

###################################################################
#
#
#    记录日志公共方法
#     level：级别：DEBUG、INFO、WARN、ERROR
#     func_name:方法名
#     info：日志信息
#
#     example:  arbitration_log ERROR "SET_PRIMARY_LOCATION" "Set primary location failed."
#
#
###################################################################
function arbitration_log() {
    level=$1
    func_name=$2
    info=$3
    echo "[$(date "+%Y-%m-%d %H:%M:%S")] [$level] [$func_name] [$info]" >>${ARBITRATION_INSTALL_LOG_FILE_PATH}
}

#--------------------------------------------------------------------------
#   Name:           get_os_version
#   Input:          NA
#   Output:         NA
#   Description:    get OS type
#--------------------------------------------------------------------------
function get_os_version() {
    if [ -e /etc/SuSE-release ]; then
        grep "VERSION = 11" /etc/SuSE-release >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "SuSE11"
            return 11
        fi
        grep "VERSION = 12" /etc/SuSE-release >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "SuSE12"
            return 12
        fi
    fi
    if [ -e /etc/centos-release ]; then
        grep "CentOS release 6.2" /etc/centos-release >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "centos6.2"
            return 2
        fi
        cat /etc/centos-release | grep "CentOS" | grep "release 7" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "CentOS7"
            return 77
        fi
    fi
    if [ -e /etc/redhat-release ]; then
        cat /etc/redhat-release | grep "Red Hat" | grep "release 6" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "Red Hat6"
            return 6
        fi
        cat /etc/redhat-release | grep "Red Hat" | grep "release 7" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "Red Hat7"
            return 7
        fi
    fi
    if [ -e /etc/euleros-release ]; then
        cat /etc/euleros-release | grep "EulerOS" | grep "release 2.0" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "EulerOS2.0"
            return 3
        fi
    fi
    if [ -e /etc/kylin-release ]; then
        cat /etc/kylin-release | grep "Kylin" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "Kylin"
            return 20
        fi
    fi
    echo "Unsupport"
    exit 1
}

###################################################################
#
#
#    创建结果文件公共方法
#     result_code：返回值
#     script_name:脚本文件名
#
#     example:  create_check_file 100 "check_pre_install_result.sh"
#
#
###################################################################
function create_check_result_file() {
    local result_code=$1
    local script_name=$2
    echo "#!/bin/bash
echo  RESULT:${result_code} && rm -f /opt/arbitration_file/${script_name}
   " >"/opt/arbitration_file/${script_name}"

    chmod 500 "/opt/arbitration_file/${script_name}"
}

###################################################################
#     获取OMP01节点通讯IP，仅适用于NCE节点
###################################################################
function get_first_omp_ip() {
    ARBITRATION_TEMP_PATH=/opt/arbitration_file/arbitration_temp
    bash /opt/arbitration_file/get_arbitration_python.sh ${ARBITRATION_TEMP_PATH} >/dev/null
    TEMP_ARBITRATION_PYTHON_HOME=${ARBITRATION_TEMP_PATH}/arbitration_python/bin/python
    if [ ! -f "${TEMP_ARBITRATION_PYTHON_HOME}" ]; then
        TEMP_ARBITRATION_PYTHON_HOME=$(find /opt/arbitration-common* -name "python" -type f | head -n 1)
    fi
    $TEMP_ARBITRATION_PYTHON_HOME 2>/dev/null << EOF
# coding=utf-8
import json
with open(f'/opt/oss/manager/etc/sysconf/nodelists.json', mode='r') as node_file:
    node_data = json.load(node_file)
for omp_ip_item in node_data.get('nodeList', {}).get('0', {}).get('IPAddresses', []):
    if 'maintenance' in omp_ip_item.get('usage', ''):
        print(omp_ip_item.get('IP'))
        sys.exit(0)
sys.exit(1)
EOF
}