#!/usr/bin/env bash #===========================================# # Author : Alawn # # LastUpdated: 2020-04-08 # # Dependency : bash, expect # # Discription: Management Menu for NCBA MSL # #===========================================# ### Variables cur_dir=$( pwd ) base_usr=`whoami` base_home=`cat /etc/passwd | grep $(whoami) | cut -d ':' -f 6` usr_sh=`cat /etc/passwd | grep $(whoami) | cut -d ':' -f 7` war_dir=${base_home}/apps/ app_list=(`ls $war_dir | sed 's/\.war$//g'| sed 's/\-1\.0\-SNAPSHOT//g'`) echo -e ${app_list[0]} echo -e ${app_list[1]} echo -e ${app_list[2]} echo -e ${app_list[3]} echo -e ${app_list[4]} echo -e ${app_list[5]} echo -e ${app_list[6]} echo -e ${app_list[7]} #exit cur_app="" app_num=${#app_list[@]} myip=`hostname -I | sed 's/ //g'` domain=`hostname|sed 's/ //g'` ### User define conf_common_dataid="common.properties" conf_group="DEFAULT_GROUP" conf_directory="/home/ncbamsl/conf/" conf_center_host="10.0.0.13:9084" profile_common="common.properties" startup_script="${base_home}/bin/startApp-configclient.sh" inject_script=${base_home}/.inject.script initrc=${base_home}/.injectrc ### Constants red='\033[0;31m' green='\033[0;32m' yellow='\033[0;33m' blue='\033[36m' gray='\033[37m' plain='\033[0m' declare -A module_map=() module_map["gateway-openapi"]="ogw" module_map["manage-openapi"]="oam" module_map["gateway-external"]="egw" module_map["gateway-internal"]="igw" module_map["industry-application"]="ias" module_map["customer"]="urm" module_map["customer-campaign"]="ccs" module_map["savings"]="sas" module_map["savings-service"]="sas" module_map["loan-service"]="lns" module_map["loan-account"]="lon" module_map["wallet-service"]="wls" module_map["account"]="acm" module_map["accounting"]="act" module_map["basic-service"]="bap" module_map["product"]="prd" module_map["boss"]="bui" module_map["batch"]="bat" module_map["data-feed"]="dfs" module_map["ecp-message"]="ems" module_map["ecp-message-app"]="msc" module_map["notification-center"]="ntc" module_map["ecp-ops"]="ops" ### Function main_menu() { while true do title=" Please Enter Your Application:[1-$app_num]" clear echo -e echo -e "${blue}NCBA MSL Management Menu (RWA)${plain}" echo -e "----------------------------------------------" echo -e $title echo -e "----------------------------------------------" echo -e "" ## render app menu for ((i=1;i<=${#app_list[@]};i++ )); do hint="${app_list[$i-1]}" state=`get_app_state ${hint}` printf "${green}%-4s${plain} %-30s [$([ "$state" == "running" ] && echo ${green} || echo ${red})%-7s${plain}]\n" ${i}")" ${hint} ${state} done ## Ask user which choice and press key echo -e "" && read -p "Please enter a number:" selected case "${selected}" in [1-9]*) if [ "${selected}" -gt $app_num ]; then title="[${red}Error${plain}] Please only enter a number [1-$app_num]" continue fi echo echo "You choose = ${app_list[${selected}-1]}" cur_app=${app_list[${selected}-1]} echo function_menu ;; q) exit 0 ;; a) clear for ((j=1;j<=${#app_list[@]};j++ )); do hint="${app_list[$j-1]}" echo -e "${green}${j}${plain}) ${hint} status" done echo -e && read -s -n1 -p "Press any key go back" ;; *) title="[${red}Error${plain}] Please only enter a number [1-$app_num]" ;; esac done } create_initrc() { if [ ! -f ${initrc} ]; then cat > ${initrc} < ${inject_script} spawn /usr/bin/env ${usr_sh} send ". ${initrc}\n" interact EOF fi } inject() { ## Define environment variable and it can be inherited into sub shell export HOME=${base_home}/${cur_app} export HWORKDIR=$HOME export LOGNAME=$cur_app ## Create RC file if it not exists create_initrc ## Create expect script if it not exists create_inject_script ## Execute expect script for app as interact mode expect ${inject_script} ## Recover $HOME environment after sub shell has been logout export HOME=${base_home} } function_menu() { #title=" Please Enter Your Operation:[1-4]" while true do pid=`get_app_pid ${cur_app}` state=`get_app_state ${cur_app}` if [ "running" == "$state" ]; then title="$cur_app is [${green}running($pid)${plain}] " else title="$cur_app is [${red}dead${plain}]" fi clear echo -e "----------------------------------------------" echo -e $title echo -e "----------------------------------------------" echo -e "" echo -e "${green}1${plain}) Startup/Restart" echo -e "${green}2${plain}) Shutdown" echo -e "${green}3${plain}) Login" echo -e "" echo -e "${green}b${plain}) Go back" echo -e "" echo -e && read -p "Please enter a number :" selected [ -z "${selected}" ] && selected="status" case "${selected}" in 1) ## Startup Application module_name=${module_map["${cur_app}"]} profile_app="" if [ "" != "$module_name" ]; then profile_app=${module_name}.properties else profile_app=${profile_common} fi node_id=${module_name}_${domain} cmd="${startup_script} ${cur_app} -serverAddr ${conf_center_host} -appDataId ${profile_app} -commonDataId ${profile_common} -group ${conf_group} -filedir ${conf_directory}${moddule_name}/ -nodeId ${node_id}" #echo ${cmd} clear echo -e "" echo -e "${blue}>> Now prepare startup application, please wait a moment...${plain}" sh ${cmd} #exit 0 echo -e "" && read -s -n1 -p "Press any key continue..." break ;; 2) ## Stop application clear echo -e "" echo -e "${blue}>> Now beginning kill the application with PID:${pid}...${plain}" echo -e "" if [ "${pid}" != "" ]; then kill -9 ${pid} sleep 1 state=get_app_state [ "${state}" == "running" ] && echo -e "${red}Application not been kill...${plain}" || echo -e "${green}Application has been killed...${plain}" else echo -e "${yellow}Application is not running...${plain}" fi echo -e "" && read -s -n1 -p "Press any key continue..." break ;; 3) ## Login as application user echo -e "${green}1${plain}) 进入系统" inject break ;; b|q) break ;; *) ## Echo error message when other key pressed title="[${red}Error${plain}] Please only enter a number [1-4]" ;; esac done } get_app_state() { app=$1 pid=`ps -ef | grep "/${app}.war" | grep -v grep|awk '{print $2}'` echo $([ ! -z "${pid}" ] && echo "running" || echo "dead") return 0 } get_app_pid() { app=$1 echo $(ps -ef | grep "/${app}.war" | grep -v grep|awk '{print $2}') return 0 } main_menu exit 0