#!/bin/bash

CONFIG_FILE=deploy-config/all.conf
show_env() {
    source scripts/utils.sh
    source scripts/config_get.sh
    source scripts/global_variables.sh
    echo "==================================="
    echo $NAMESPACE
    echo "==================================="
}

get_deploy_env() {
    namespace=$NAMESPACE
    WorkNode01=$WorkNode01
    WorkNode02=$WorkNode02
    WorkNode03=$WorkNode03
    WorkNode04=$WorkNode04
    SSH_PORT=$SSH_PORT
    es_pv_volume=$ES_PV_VOLUME
    es_pv_path=$ES_PV_PATH
    es_backup_volume=$ES_BACKUP_VOLUME
    es_backup_path=$ES_BACKUP_PATH
    rabbit_pv_volume=$MQ_PV_VOLUME
    rabbit_pv_path=$MQ_PV_PATH
    redis_pv_volume=$REDIS_PV_VOLUME
    redis_pv_path=$REDIS_PV_PATH
    base_dir=$BASE_DIR
    minio_pv_volume=$MINIO_PV_VOLUME
    minio_pv_path=$MINIO_PV_PATH
}

get_install_path() {
    find_path=`find / -name "auto_deploy_edoc2.sh" -type f | awk -F "auto_deploy_edoc2.sh" '{print $1}'`
    install_path="${find_path%%/}"
    echo "${install_path}"
    cd "${install_path}"
}


cat <<eof 
**************************************
** logs operation start/stop/restart **
**************************************
eof

sleep 1
show_env
get_deploy_env
Action=$1
# get_deploy_env
case $Action in 
    1|start|START)
        get_install_path
        echo "begin deploy ecm ........."
        echo "==============================================="
        echo "${install_path}"
        echo "${APM_STATUS}"
        echo "${TYPE}"
        echo "${STORAGE_MODE}"
        echo "==============================================="
        if [ "${APM_STATUS}" == "false" ];then
           echo "should start local collect logs"
           if [ "${TYPE}" == "cluster" ];then
               kill -9 `cat "${install_path}"/pids/bakckup.pid`
               sleep 1
               rm -rf "${install_path}"/pids/bakckup.pid
               mkdir -p "${install_path}"/Template/cluster/logs
               logs_base_path="${install_path}/Template/cluster"
               #nohup scripts/kube_logs.sh daemonize $logs_base_path 2>&1 &
               #daemonize "${install_path}"/scripts/kube_logs.sh daemonize $logs_base_path
               #nohup scripts/kube_logs.sh daemonize $logs_base_path 2>&1 &
               source scripts/kube_logs.sh daemonize $logs_base_path &
               jobs_process=`jobs -l | grep -v grep | awk -F " " '{print $2}'`
               echo $jobs_process
               echo $jobs_process > "${install_path}"/pids/bakckup.pid
           elif [ "${TYPE}" == "allinone" ];then
               kill -9 `cat "${install_path}"/pids/bakckup.pid`
               sleep 1
               rm -rf "${install_path}"/pids/bakckup.pid
               mkdir -p "${install_path}"/Template/standalone/logs
               logs_base_path="${install_path}/Template/standalone"
               #nohup scripts/kube_logs.sh daemonize $logs_base_path 2>&1 &
               #daemonize -a -e "${install_path}"/scripts/kube_logs.sh daemonize $logs_base_path
               #nohup scripts/kube_logs.sh daemonize $logs_base_path > /dev/null 2>&1 &
               source scripts/kube_logs.sh daemonize $logs_base_path &
               echo $jobs_process
               echo $jobs_process > "${install_path}"/pids/bakckup.pid
           fi
        elif [ "${APM_STATUS}" == "true" ];then
           echo "nothing to do"
        fi
        ;;
    2|stop|STOP)
        get_install_path
        echo "begin stop ecm ..........."
        if [ "${APM_STATUS}" == "false" ];then
           echo "should start local collect logs"
           if [ "${TYPE}" == "cluster" ];then
               mkdir -p "${install_path}"/Template/cluster/logs
               logs_base_path="${install_path}/Template/cluster"
               source scripts/kube_logs.sh stop $logs_base_path
               kill -9 `cat "${install_path}"/pids/bakckup.pid`
               sleep 1
               rm -rf "${install_path}"/pids/bakckup.pid
           elif [ "${TYPE}" == "allinone" ];then
               mkdir -p "${install_path}"/Template/standalone/logs
               logs_base_path="${install_path}/Template/standalone"
               source scripts/kube_logs.sh stop $logs_base_path
               kill -9 `cat "${install_path}"/pids/bakckup.pid`
               sleep 1
               rm -rf "${install_path}"/pids/bakckup.pid
           fi
        elif [ "${APM_STATUS}" == "true" ];then
           echo "nothing to do"
        fi
        ;;
    3|restart|RESTART)
        get_install_path
        echo "begin restart ecm ........"
        sleep 3
        ;;
    *)
        echo "输入操作不存在 .........."
        #exit;
esac
