pandorafms/pandora_server/util/pwrd/pwrd

326 lines
8.5 KiB
Bash

#!/bin/bash
# pwr - this script starts and stops the pwr grid
#
# chkconfig: - 85 1# description: PWRD Grid is a distributed testing platform for browser-based automation.
# processname: pwr
# pidfile: $base_dir/pwr.pid
# Source function library.
if [ -e /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
pwr_dir=/usr/lib/pwr
log_dir=/var/log/pwr
std_log=$log_dir/pwr_std.log
base_dir=/etc/pwr/tmp
java=/usr/bin/java
pwr="$pwr_dir/selenium-server-standalone-2.53.1.jar"
# Following parameters will control node capabilities: -browser browserName=firefox,maxInstances=1,platform=LINUX
PWROPTS=" -firefoxProfileTemplate /opt/firefox_profile"
PWR_HUB_OPTS=""
user=root
xvfb_pid_file=$base_dir/xvfb.pid
# Variable
host=`ip a show $(route -n | awk '/^0\.0\.0\.0/ {print $NF}') | grep "inet " | awk '{print $2}' | cut -f1 -d'/' | head -1`
# Start Xvfb, required for firefox in server without X
function start_xvfb() {
`which Xvfb >/dev/null 2>&1`
if [ $? -ne 0 ]; then
echo "Xvfb not found, please install it"
if [ -e /etc/rc.d/init.d/functions ]; then
failure
echo 1
fi
else
Xvfb :99 -ac > /dev/null 2>&1 &
export DISPLAY=:99
ps -eo pid,cmd | grep Xvfb | grep -v grep | awk {'print $1 '} > $xvfb_pid_file
echo 0
fi
}
# Stop Xvfb
function stop_xvfb() {
if test -f $xvfb_pid_file ; then
# Check if there's more instances of pwrd running i.e. standalone + node
if [ ! -e "$base_dir/pwrd_node.pid" ] && [ ! -e "$base_dir/pwrd.pid" ]; then
echo -n "Stopping Xvfb..."
PID=$(cat $xvfb_pid_file)
if [ `kill -9 $PID 2>&1 | grep "No such process" | wc -l` == "1" ]; then
# PID file exists but no process
test -f $xvfb_pid_file && rm -f $xvfb_pid_file
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
return 0
fi
else
echo "(1) pwrd process left. Skipping Xvfb"
fi
fi
}
################################################################################
# START
################################################################################
# Start Selenium Standalone server
function start() {
if test -f $pid_file
then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
echo "PWRD is already running: $PID"
exit 2
else
echo "Removing stale pid file: $pid_file"
fi
fi
if [ "`start_xvfb`" != "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed... Check Xvfb"
return 1
fi
echo -n "Starting PWRD..."
export DISPLAY=:99
su $user -c "$java -jar $pwr -host $host $PWROPTS > $std_log 2>&1 &"
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
echo "Success"
else
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed"
fi
# Store PID
ps -eo pid,cmd | grep $pwr | grep -v "node" | grep -v "hub" | grep -v grep | awk {'print $1 '} > $pid_file
}
# Start Selenium GRID server: HUB component
function start-hub() {
if test -f $pid_file
then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
echo "PWRD is already running: $PID"
exit 2
else
echo "Removing stale pid file: $pid_file"
fi
fi
echo -n "Starting PWRD..."
# No Xvfb needed in hub mode
su $user -c "$java -jar $pwr -host $host -role hub $PWR_HUB_OPTS > $std_log 2>&1 &"
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
echo "Success"
else
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed"
fi
# Store PID
ps -eo pid,cmd | grep $pwr | grep "hub" | grep -v grep | awk {'print $1 '} > $pid_file
}
# Start Selenium GRID server: Node Connect to hub
function start-node() {
if test -f $pid_file
then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
echo "PWRD is already running: $PID"
exit 2
else
echo "Removing stale pid file: $pid_file"
fi
fi
if [ "`start_xvfb`" != "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed... Check Xvfb"
return 1
fi
echo -n "Starting PWRD..."
export DISPLAY=:99
su $user -c "$java -jar $pwr -host $host -role node -hub $1 $PWROPTS > $std_log 2>&1 &"
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
echo "Success"
else
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed"
fi
# Store PID
ps -C java -eo pid,cmd | grep $pwr | grep "node" | grep -v grep | awk {'print $1 '} > $pid_file
}
################################################################################
# STOP
################################################################################
# Stop Selenium Standalone server
function stop() {
if test -f $pid_file ; then
echo -n "Stopping PWRD..."
PID=$(cat $pid_file)
su $user -c "kill -3 $PID"
kill -9 $PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
sleep 5
test -f $pid_file && rm -f $pid_file
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
else
echo "PWRD could not be stopped..."
if [ -e /etc/rc.d/init.d/functions ]; then
failure
fi
echo "Failed"
fi
stop_xvfb
else
echo "PWRD is not running."
stop_xvfb
if [ -e /etc/rc.d/init.d/functions ]; then
success
fi
fi
echo
}
################################################################################
# STATUS
################################################################################
# Status of Selenium Standalone server
function status() {
if test -f $pid_file ; then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null ; then
echo "PWRD is running: $PID"
if test -f $xvfb_pid_file ; then
PID=`cat $xvfb_pid_file`
if test -f $xvfb_pid_file ; then
echo "Xvfb is running: $PID"
fi
fi
else
echo "PWRD isn't running..."
fi
else
echo "PWRD isn't running..."
fi
}
case "$1" in
start) # Standalone
pid_file=$base_dir/pwrd.pid
start
;;
start-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
start-hub
;;
start-node) # node
pid_file=$base_dir/pwrd_node.pid
start-node $2
;;
stop) # standalone
pid_file=$base_dir/pwrd.pid
stop
;;
stop-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
stop
;;
stop-node) # node
pid_file=$base_dir/pwrd_node.pid
stop
;;
restart) # standalone
pid_file=$base_dir/pwrd.pid
stop
start
;;
restart-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
stop
start-hub
;;
restart-node) # node
pid_file=$base_dir/pwrd_node.pid
stop
start-node $2
;;
status)
pid_file=$base_dir/pwrd.pid
status
;;
status-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
status
;;
status-node) # node
pid_file=$base_dir/pwrd_node.pid
status
;;
*)
echo "Usage: $SELF start*|stop*|restart*|status*"
echo " start* (standalone, hub or node)"
echo " start"
echo " start-hub"
echo " start-node http://hub:4444/grid/register"
echo " stop* (standalone, hub or node)"
echo " stop"
echo " stop-hub"
echo " stop-node"
echo " restart* (standalone, hub or node)"
echo " restart"
echo " restart-hub"
echo " restart-node http://hub:4444/grid/register"
echo " status* (standalone, hub or node)"
echo " status"
echo " status-hub"
echo " status-node"
exit 9
;;
esac
echo FIN [$1]