#!/bin/env bash
##Description: precheck before tidb dr swtich over 

function f_syntax
{
    [ ! -z "$1" ] && echo ">>[$(date "+%Y%m%d.%H%M%S")] $1"
    echo " "
    echo "ksh ${ShellName}  [-hostname <hostname>]"
    echo "  [-clsname <clsname>] [-node <ip:[prt>] [-role <role>] "
    echo "  [-action <start|stop|start>] "
    echo " "
    echo " #sample:"
    echo " #sh ${ShellName} -clsname sa2 -role tidb -node 172.16.xxx:4100 -action restart"
    exit 22
}

function f_PrintLog
{
    _LogFlag=$(echo "$1"|tr "a-z" "A-Z")
    _LogMessage=$2
    declare LogFile="${LogFile:-"/tmp/${PShellName:-${ShellName}}.log"}"
    touch "${LogFile}" 2>/dev/null
    if [ -w "${LogFile}" ]
    then
        echo "[${_LogFlag}]:$(date "+%Y%m%d.%H%M%S"):${UserName}@${HostName}:${2}"|tee -a "${LogFile}";
    else
        echo "[${_LogFlag}]:$(date "+%Y%m%d.%H%M%S"):${UserName}@${HostName}:${2}";
    fi
}


###############################################################################
##Init##
ShellName="$(echo $0|awk -F / '{print $NF}')";ShellOption="$@";
WorkDir="$(echo $0|sed s/${ShellName}//g)";[ -z "${WorkDir}" ] && WorkDir=${PWD};cd ${WorkDir};WorkDir=${PWD}
LogDir="${WorkDir%/}/logs";mkdir -p ${LogDir};chmod ugo+rwx ${LogDir} 2>/dev/null
LogFile="${LogDir}/${ShellName}.$(date "+%Y%m%d.%H%M%S").log";LogLevel=3;
touch "${LogFile}" 2>/dev/null;chmod ugo+rw "${LogFile}" 2>/dev/null
declare Shellcksum=$(cksum ${WorkDir%/}/${ShellName} 2>/dev/null|awk '{printf "%d",$1}')
declare -i UserID=$(id -u);declare UserName=$(whoami);declare HostName=$(hostname);
###############################################################################
##Get inopt
[ -z "${ShellOption}" ] && f_syntax;
for inopt in ${ShellOption}
do
    case $(echo $inopt|tr a-z A-Z) in
    -HOSTNAME|-HOST) CurOpt="-HOSTNAME";continue;;
    -CLSNAME|-C) CurOpt="-CLSNAME";continue;;
    -NODE|-N) CurOpt="-NODE";continue;;
    -ROLE|-R) CurOpt="-ROLE";continue;;
    -ACTION|-A) CurOpt="-ACTION";continue;;
    -HELP|--HELP|-H) f_syntax;;
    -*) CurOpt="";continue;;
    esac
    case "${CurOpt}" in
    -HOSTNAME) declare LocalHost="${inopt}";continue;;
    -CLSNAME) declare clsname="${inopt}";continue;;
    -NODE) declare tidbnode="${inopt}";continue;;
    -ROLE) declare tidbrole="$(echo "${inopt}"|tr A-Z a-z)";continue;;
    -ACTION) declare action="$(echo "${inopt}"|tr A-Z a-z)";continue;;
    esac
done

f_PrintLog "Info" "Start to run Script ${ShellName}, the Shellcksum is ${Shellcksum}"
f_PrintLog "Debug" "Script Options:${ShellOption}"

function f_tiupcmd
{
    f_PrintLog "Info" "Start to run f_checkdrainer."
    cls_name="$1"
    component_name="$2"
    action="$3"
    
    f_PrintLog "Info" "Start to ${action} ${component_name} on cluster ${cls_name} ..."
    sleep 3
    #clsinfo 
    if [ -z "$(echo ${component_name} | grep :)" ]
    then
        tiup cluster ${action} ${cls_name} -R${component_name} --yes --wait-timeout 30
    else
        tiup cluster ${action} ${cls_name} -N${component_name} --yes --wait-timeout 30
    fi
    rt_code=$?
    if [ ${rt_code} -eq 0 ]
    then
        f_PrintLog "Info" "${action} ${component_name} on cluster ${cls_name} successfully!"
        return ${rt_code}
    else
        f_PrintLog "Error" "${action} ${component_name} on cluster ${cls_name} failed!!!"
        return ${rt_code}
    fi
}


###################
####    Main   ####
###################



#display only
if [ -z "${action}" -o "${action}" = "display" ] 
then
    f_PrintLog "Info" "Not provide the action parameter:start/stop/restart, run display!";
    tiup cluster display ${clsname} 
    f_PrintLog "Info" "Run display finished!";
elif [ -z "${tidbrole}" -a -z "${tidbnode}" ]
then
    f_PrintLog "Error" "Pls input the nodeid or noderole!"
    exit
else
    #priority nodeid>role
    [ -z "${tidbnode}" ] && nodeName=${tidbrole} || nodeName=${tidbnode}
    f_tiupcmd ${clsname} "${nodeName}" ${action}
    rt_code=$?
    tiup cluster display ${clsname} 
    
    if [ ${rt_code} -eq 0 ]
    then
        f_PrintLog "Info" "cluster ${clsname} node ${nodeName} ${action} finish!"    
    else
        f_PrintLog "Error" "cluster ${clsname} node ${nodeName} ${action} failed !!!"
        exit 1
    fi
fi



