awips2/rpms/awips2.edex/Installer.edex/scripts/edex_camel
2018-12-26 13:50:30 -07:00

269 lines
7.1 KiB
Bash
Executable file

#!/bin/bash
#
# edex_camel This shell script takes care of starting and stopping
# the AWIPS EDEX Camel instance.
#
# chkconfig: - 99 10
# description: Camel ESB System, which is the instance \
# used by AWIPS EDEX.
# processname: start.sh
# config: /awips/edex/esb/conf/global.xml
#skip systemctl
export SYSTEMCTL_SKIP_REDIRECT=true
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "${NETWORKING}" == "no" ] && exit 0
RETVAL=0
prog="start.sh"
MEM=( `free -g | grep "Mem:"` )
TOTAL_MEM=${MEM[1]}
if [ -f /etc/rc.d/init.d/edexServiceList ]; then
. /etc/rc.d/init.d/edexServiceList
else
SERVICES=( 'request' 'ingest' 'ingestGrib')
fi
# Available Service Modes
# ingest
# ingestGrib
# ingestDat
# request
# centralRegistry
# ingestHydro
# requestHydro
# Who to run EDEX server as, usually "awips". (NOT "root")
export EDEXUSER=awips
# Todays date in format of YYYYMMDD.
export TODAY=`/bin/date +%Y%m%d`
# We will no longer be using hard-coded paths that need to be replaced.
# Use rpm to find the paths that we need.
export JAVA_INSTALL="/awips2/java"
export PYTHON_INSTALL="/awips2/python"
export EDEX_INSTALL="/awips2/edex"
export PSQL_INSTALL="/awips2/psql"
# The path that is to be used for the script
export JAVA_HOME=${JAVA_INSTALL}
export PATH=${JAVA_INSTALL}/bin:${PYTHON_INSTALL}/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export LD_LIBRARY_PATH=${JAVA_INSTALL}/lib:${PYTHON_INSTALL}/lib
export LD_PRELOAD=${PYTHON_INSTALL}/lib/libpython2.7.so
export AMQP_SPEC=""
export DATA_ARCHIVE_ROOT=/awips2/data_store
# what to do to get pids of an EDEX instance
# $1 == instance token
getCamelAndWrapperPids() {
_camel_pid=`pgrep -f -u $EDEXUSER "java -Dedex.run.mode=${1} "`
if [ "$_camel_pid" != "" ]; then
# occasionally will get more than one running, grab parent for first one only
_camel_pid="${_camel_pid%% *}"
# grab wrapper pid from edex process, run through awk to throw away leading white space
_wrapper_pid=`ps --no-headers -p $_camel_pid -o ppid | awk '{print $1}'`
else
# camel not up, double check wrapper pid file
pidfile=${EDEX_INSTALL}/bin/${1}.pid
if [ -f $pidfile ]; then
_wrapper_pid=`cat $pidfile`
if [ "$_wrapper_pid" != "" ]; then
# double check process is indeed a wrapper process
check=`ps -p $_wrapper_pid -o pid,args | grep -c wrapper.conf`
if [ $check -eq 0 ]; then
_wrapper_pid=""
fi
fi
else
_wrapper_pid=""
fi
fi
}
# what to do to start an EDEX instance
# $1 == instance token
startEDEX() {
getCamelAndWrapperPids ${1}
if [ "$_wrapper_pid" != "" -o "$_camel_pid" != "" ]; then
echo "WARNING: EDEX ${1} instance already running, not starting another instance"
return 1
fi
local EXTRA_ARGS="-noConsole"
local DAEMON="${EDEX_INSTALL}/bin/start.sh ${EXTRA_ARGS} ${1}"
local LOG=${EDEX_INSTALL}/logs/start-edex-${1}-$TODAY.log
local TIME=`/bin/date "+%F %T"`
echo "$TIME: Service edex_camel Starting EDEX ${1}" >> $LOG
chmod a+r $LOG
su - $EDEXUSER -c "$DAEMON &" >> $LOG 2>&1
sleep 10
local pidfile=${EDEX_INSTALL}/bin/${1}.pid
if [ -f $pidfile ]; then
pid=`cat ${pidfile}`
if [ "$pid" == "" ]; then
echo "WARNING: No Wrapper Pid File Found, EDEX ${1} did not start properly"
else
checkStatus ${1}
fi
else
echo "WARNING: No Wrapper Pid File Found, EDEX ${1} did not start properly"
fi
}
# what to do to stop an EDEX instance
# $1 == instance token
stopEDEX() {
getCamelAndWrapperPids ${1}
if [ "$_wrapper_pid" == "" -a "$_camel_pid" == "" ]; then
echo "WARNING: EDEX ${1} instance not running, no shutdown attempted"
return 1
fi
local LOG=${EDEX_INSTALL}/logs/start-edex-${1}-$TODAY.log
local TIME=`/bin/date "+%F %T"`
echo "$TIME: Service edex_camel Stopping EDEX ${1}" >> $LOG
chmod a+r $LOG
if [ "$_wrapper_pid" != "" -a "$_wrapper_pid" != "1" ]; then
kill $_wrapper_pid
else
# occasionally wrapper dies and camel process is still running
kill $_camel_pid
fi
cnt=0
savepid=$_wrapper_pid
while [ "${_wrapper_pid}${_camel_pid}" != "" ]; do
if [ "$_wrapper_pid" != "$savepid" ]; then
# only display warning when other wrapper starts (not if wrapper died)
if [ "$_wrapper_pid" != "" ]; then
kill $_wrapper_pid
savepid=$_wrapper_pid
fi
fi
let cnt+=1
let mod10=cnt%10
if [ $cnt -ge 300 ]; then
echo "Force killing EDEX ${1}"
if [ "$_camel_pid" != "" ]; then
kill -9 $_camel_pid
fi
if [ "$_wrapper_pid" != "" ]; then
kill -9 $_wrapper_pid
fi
elif [ $mod10 -eq 0 ]; then
echo "Waiting for EDEX ${1} to shutdown"
fi
sleep 1
getCamelAndWrapperPids ${1}
done
echo "EDEX ${1} shutdown"
}
# what to use to check status
# $1 == instance token
checkStatus() {
getCamelAndWrapperPids ${1}
if [ "$_wrapper_pid" == "" ]; then
echo "EDEX Camel (${1}) is not running"
elif [ "$_wrapper_pid" == "1" ]; then
echo "WARNING: EDEX Camel (${1}) wrapper is not running"
echo "WARNING: EDEX Camel (${1}) is running (java PID $_camel_pid)"
echo "WARNING: Recommend restarting EDEX Camel ${1}"
else
echo "EDEX Camel (${1}) is running (wrapper PID $_wrapper_pid)"
echo "EDEX Camel (${1}) is running (java PID $_camel_pid)"
fi
}
# Verify root user
checkUser() {
REQUIREDUSER="root"
CURUSER=`whoami`
if [ "$CURUSER" != "$REQUIREDUSER" ]; then
echo "Insufficient privileges: must run script as $REQUIREDUSER"
exit 1
fi
}
func=$1
shift 1
if [ $# -gt 0 ]; then
SERVICES=("$@")
fi
# See how we were called.
case $func in
start)
checkUser
for service in ${SERVICES[*]};
do
echo "Starting EDEX Camel ($service): "
startEDEX $service &
done
wait
RETVAL=$?
;;
stop)
checkUser
for service in ${SERVICES[*]};
do
echo "Stopping EDEX Camel ($service): "
stopEDEX $service &
done
wait
RETVAL=$?
;;
restart)
checkUser
for service in ${SERVICES[*]};
do
echo "Stopping EDEX Camel ($service): "
stopEDEX $service &
done
wait
# small sleep for wrapper lock to disappear so that we don't make .1 version of log files
sleep 5
for service in ${SERVICES[*]};
do
echo "Starting EDEX Camel ($service): "
startEDEX $service &
done
wait
RETVAL=$?
;;
status)
for service in ${SERVICES[*]};
do
checkStatus $service
done
;;
*)
# Print help
echo "Usage: $0 {start|stop|restart|reload|status} {service} {service}..." 1>&2
echo "If service(s) blank it will start the default services of ${SERVICES[*]}" 1>&2
exit 1
;;
esac
exit $RETVAL