#!/bin/bash
CURRENT_PATH=$(dirname $(readlink -f $0))
source ${CURRENT_PATH}/logs.sh
AI_ROOT=/ISM/AI
SYS_PYTHON=/usr/bin/python3
DB_PATH=/OSM/coffer_data/ISM/AI/smart.db
LOOP_NUM=0

ONLINE_FLAG_MONITOR=`ps -ef | grep -w "smart_gui_server.py" | grep -v grep | awk '{print $2}'`

if [ ! -d "/ISM/AI/gui/logs" ];then
    mkdir -p /ISM/AI/gui/logs
    logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] mkdir /ISM/AI/gui/logs"
fi

if [ ! -d "/OSM/coffer_data/ISM/AI" ];then
    mkdir -p /OSM/coffer_data/ISM/AI
    logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] mkdir /OSM/coffer_data/ISM/AI"
fi

if [ ! -d "/OSM/coffer_log/log/ismlog/smartguilog" ];then
    mkdir -p /OSM/coffer_log/log/ismlog/smartguilog
    logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] mkdir /OSM/coffer_log/log/ismlog/smartguilog"
fi

ln -sfn /OSM/coffer_log/log/ismlog/smartguilog/ /ISM/AI/gui/logs

####切用户
if [ -f /OSM/conf/unroot_flag ]; then
    chown -R -h OceanStorMgrUser:OceanStorCommonGroup /ISM/AI
else
    chown -R -h OceanStorMgrUser:mgrgroup /ISM/AI
fi
if [ -f /OSM/conf/unroot_flag ]; then
    chown -R -h OceanStorMgrUser:OceanStorCommonGroup /OSM/coffer_log/log/ismlog/smartguilog/
else
    chown -R -h OceanStorMgrUser:mgrgroup /OSM/coffer_log/log/ismlog/smartguilog/
fi
###切进程目录权限
chmod -Rf 550 /ISM/AI/
####修改日志目录权限
chmod -Rf 640 /OSM/coffer_log/log/ismlog/smartguilog/
chmod 750 /OSM/coffer_log/log/ismlog/smartguilog/
chmod 750 /ISM/AI/gui/logs/
# 切数据权限
if [ -f /OSM/conf/unroot_flag ]; then
    chown -R -h OceanStorMgrUser:mgrgroup /OSM/coffer_data/ISM/AI
else
    chown -R -h OceanStorMgrUser:mgrgroup /OSM/coffer_data/ISM/AI
fi
chmod 750 /OSM/coffer_data/ISM/AI
chmod 660 $DB_PATH
# 切配置文件权限
chmod 750 /ISM/AI/gui/conf
chmod 750 /ISM/AI/gui/conf/i18n
chmod 640 /ISM/AI/gui/conf/i18n/system_para_zh.xml

initPythonEnv()
{
    if [ ! -d "$AI_ROOT/smart_gui_virtual/lib/" ];then
        cd $AI_ROOT
        $SYS_PYTHON -m venv smart_gui_virtual --without-pip --copies
        if [ $? != 0 ]; then
            logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] initPythonEnv failed"
            exit 1
        fi
        if [ ! -d "$AI_ROOT/smart_gui_virtual/lib/python3.7" ];then
            mv $AI_ROOT/smart_gui_virtual/lib/python3.8 $AI_ROOT/smart_gui_virtual/lib/python3.7
        fi
        source smart_gui_virtual/bin/activate
        cp -rf $AI_ROOT/smart_gui_env/site-packages/* $AI_ROOT/smart_gui_virtual/lib/python3.7/site-packages/
        deactivate
    fi
    chmod -R 750 $AI_ROOT/smart_gui_virtual/bin
    chmod -R 750 $AI_ROOT/smart_gui_virtual/lib
}

startSmartGUI()
{
    ONLINE_FLAG_MONITOR=`ps -ef | grep -w "smart_gui_server.py" | grep -v grep | awk '{print $2}'`
    if [ -n "$ONLINE_FLAG_MONITOR" ]; then
        logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] smart gui is already started."
        exit 0
    fi
    logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] Begin to start smart_gui_server.py"
    initPythonEnv
    # cgroup_manage.sh
    sudo /OSM/bin/cgroup_manage.sh -g omm -i $$ start
        PYTHONPATH=$PYTHONPATH:/ISM/AI/gui/ /ISM/AI/smart_gui_virtual/bin/python3.7 /ISM/AI/gui/gui_server/smart_gui_server.py >> /ISM/AI/gui/logs/smartguilog/deploy.log &
    sudo /OSM/bin/cgroup_manage.sh -i $$  end
    sleep 20  # wait start for rest service init
    if [ $? -ne 0 ];then
        logAndEchoError "[PATCH][SMART_GUI_DAEMON] Run smart_gui_server.py failed."
    fi
    logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] Start smart_gui_server.py success"
}

function kill_smart_gui()
{
    retryTimes=3
    i=0
    while [[ $i -lt $retryTimes ]];do
        ONLINE_FLAG_MONITOR=`ps -ef | grep -w "smart_gui_server.py" | grep -v grep | awk '{print $2}'`
        logAndEchoInfo "[PATCH][SMART_GUI_DAEMON]  ps -ef | grep -w 'smart_gui_server.py' | grep -v grep | awk '{print $2}' result=$ONLINE_FLAG_MONITOR"
        if [ -z "$ONLINE_FLAG_MONITOR" ];then
            logAndEchoInfo "[SMART_GUI_DAEMON] smart gui stopped"
            break
        fi
        kill -9 `ps -wwef | grep "smart_gui_server.py" | grep -v "grep" | awk '{print $2}'` >/dev/null 2>&1
        logAndEchoInfo "[PATCH][SMART_GUI_DAEMON] smart gui still alive, times=$i."
        let i++
    done
}

function check_main_control()
{
    PYTHONPATH=$PYTHONPATH:/ISM/AI/gui/ /ISM/AI/smart_gui_virtual/bin/python3.7 /ISM/AI/gui/script/check_main_controller.py $1
    if [ $? -eq 0 ];then
        return 0
    fi
    return 1
}

function check_smart_gui_service()
{
    netstat -anp | grep :8408 | grep LISTEN | grep python
    if [ $? -eq 0 ];then
        return 0
    fi
    return 1
}

function check_smart_gui_status()
{
    ONLINE_FLAG_MONITOR=`ps -ef | grep -w "smart_gui_server.py" | grep -v grep | awk '{print $2}'`
    if [ -z "$ONLINE_FLAG_MONITOR" ];then
        return 0
    fi
    return 1
}

function check_db_permit()
{
    is_check=$(( LOOP_NUM % 200 ))
    if [ $is_check -eq 0 ]; then
        permit_smart=$(find -L $DB_PATH -maxdepth 0 -perm 660)
        if [ -z "$permit_smart" ]; then
            chmod 660 $DB_PATH >> /ISM/AI/gui/logs/smartguilog/smart_gui_runtime.log &
        fi
    fi
}

LOOP_TIME=3
remote_cnt=0
while :
do
    ((LOOP_NUM++))
    check_db_permit
    # master is not the node, return 0
    check_main_control $remote_cnt
    if [ $? -eq 0 ];then
        remote_cnt=0
        logAndEchoInfo "[PATCH][check_main_control] Current is not main control, now kill smart gui"
        kill_smart_gui
        sleep ${LOOP_TIME}
        continue
    fi
    remote_cnt=1
    check_smart_gui_status
    if [ $? -eq 0 ];then
        logAndEchoInfo "[PATCH][check_smart_gui_status] Current is main control and start smart gui"
        startSmartGUI
        sleep ${LOOP_TIME}
        continue
    fi

    check_smart_gui_service
    if [ $? -eq 1 ];then
        logAndEchoInfo "[PATCH][check_port_status] 8408 not bind error and kill smart gui and restart smart gui"
        kill_smart_gui
        startSmartGUI
        sleep ${LOOP_TIME}
        check_smart_gui_status
        if [ $? -eq 0 ];then
            logAndEchoError "[PATCH]smart gui server restart failed"
        fi
        check_smart_gui_service
        if [ $? -eq 1 ];then
            logAndEchoError "[PATCH]smart gui server restart not bind"
        fi
        continue
    fi

    sleep 1
done

