updates on pwrd, simplified

This commit is contained in:
fbsanchez 2018-12-04 18:22:11 +01:00
parent 4fa3f967f6
commit b2758e6738
2 changed files with 183 additions and 102 deletions

View File

@ -9,24 +9,33 @@ if [ "$1" == "" ] || [ "$1" != "--install" ]; then
To install the Pandora web robot daemon (pwrd)
Please launch this script as root:
$0 --install
$0 --install [directory]
_HELP
exit 0
fi
if [ "`rpm -qa | grep xorg-x11-server-Xvfb | wc -l`" == "0" ]; then
echo "Package xorg-x11-server-Xvfb is required"
exit 0
if [ "`which rpm`" ]; then
if [ "`rpm -qa | grep xorg-x11-server-Xvfb | wc -l`" == "0" ]; then
echo "Package xorg-x11-server-Xvfb is required"
exit 0
fi
else
echo "Xvfb is required, please confirm is installed in your system"
fi
if [ "$2" != "" ] && [ -d "$2" ]; then
GLOBAL_INST_DIR=$2
fi
chmod +x pwrd
PWR_SERVER_DEST=/usr/lib/pwr
PWR_SERVER_RSC=/etc/pwr/tmp
PWR_SERVER_LOG=/var/log/pwr
PWR_FIREFOX_INSTALLDIR=/opt
PWR_SERVER_DEST=$GLOBAL_INST_DIR/usr/lib/pwr
PWR_SERVER_RSC=$GLOBAL_INST_DIR/etc/pwr/tmp
PWR_SERVER_LOG=$GLOBAL_INST_DIR/var/log/pwr
PWR_FIREFOX_INSTALLDIR=$GLOBAL_INST_DIR/opt
mkdir -p $PWR_SERVER_DEST
mkdir -p $PWR_SERVER_LOG
@ -34,19 +43,22 @@ mkdir -p $PWR_SERVER_RSC
mkdir -p $PWR_FIREFOX_INSTALLDIR
tar xvf firefox-43.0.tar >/dev/null
tar xvf firefox-47.0.1.tar >/dev/null
mv firefox $PWR_FIREFOX_INSTALLDIR/
tar xvzf firefox_profile.tar.gz >/dev/null
chown -R `whoami`. firefox_profile
mv firefox_profile $PWR_FIREFOX_INSTALLDIR
mkdir -P $PWR_FIREFOX_INSTALLDIR/selenium
mv config.json $PWR_FIREFOX_INSTALLDIR/selenium/
ln -s $PWR_FIREFOX_INSTALLDIR/firefox/firefox /usr/bin/firefox
# Generate logrotate configuration
cat > /etc/logrotate.d/pwrd <<EO_LROTATE
echo <<EO_LROTATE > /etc/logrotate.d/pwrd
/var/log/pwr/pwr_std.log
/var/log/pwr/xvfb.log
/var/log/pwr/pwr_error.log {
weekly
missingok
@ -60,6 +72,10 @@ cat > /etc/logrotate.d/pwrd <<EO_LROTATE
EO_LROTATE
# Update pwrd daemon
if [ "$GLOBAL_INST_DIR" != "" ]; then
sed -i "s/PWR_GLOBAL_DIR=\"\"/PWR_GLOBAL_DIR=\"\\$GLOBAL_INST_DIR\"/g" ./pwrd
fi
cp ./selenium-server-standalone-2.53.1.jar $PWR_SERVER_DEST/
cp ./pwrd /etc/init.d/pwrd
chmod +x /etc/init.d/pwrd
@ -77,4 +93,3 @@ Please start the service with:
EOF

View File

@ -1,9 +1,26 @@
#!/bin/bash
# pwr - this script starts and stops the pwr grid
# **********************************************************************
# Pandora FMS PWR Server Daemon
# (c) 2008-2018 Artica ST
#
# chkconfig: - 85 1# description: PWRD Grid is a distributed testing platform for browser-based automation.
# Please see http://www.pandorafms.org
# **********************************************************************
### BEGIN INIT INFO
# Provides: pwrd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: Startup script daemon for PWR server
# Description: Startup script daemon for PWR server (linux)
### END INIT INFO
# chkconfig: - 85 1
# description: PWRD Grid is a distributed platform for browser-based automation.
# processname: pwr
# pidfile: $base_dir/pwr.pid
# pidfile: $BASE_DIR/pwr.pid
# Source function library.
@ -12,21 +29,55 @@ if [ -e /etc/rc.d/init.d/functions ]; then
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
# **********************************************************************
# Configuration options
# Example node capabilities: -browser browserName=firefox,maxInstances=1,platform=LINUX
# Customize Java binary path
JAVA=/usr/bin/java
# Customize PWR global installation directory
PWR_GLOBAL_DIR=""
# Customize PWR running user
USER=root
# Customize PWR application files directory
PWR_INST_DIR="$PWR_GLOBAL_DIR/opt"
# Customize port for PWR
PWR_PORT=4444
PWR_HUB_PORT=4444
PWR_NODE_PORT=5555
# Extra options for PWR
PWR_OPTS=" -firefoxProfileTemplate $PWR_INST_DIR/firefox_profile "
PWR_NODE_OPTS=" -nodeConfig $PWR_INST_DIR/selenium/config.json "
PWR_HUB_OPTS=""
# **********************************************************************
# Default paths
PWR_DIR="$PWR_GLOBAL_DIR/usr/lib/pwr"
LOG_DIR="$PWR_GLOBAL_DIR/var/log/pwr"
STD_LOG="$LOG_DIR/pwr_std.log"
BASE_DIR="$PWR_GLOBAL_DIR/etc/pwr/tmp"
PWR="$PWR_DIR/selenium-server-standalone-2.53.1.jar"
XVFB_PID_FILE="$BASE_DIR/xvfb.pid"
# **********************************************************************
# Variables
# Variable
host=`ip a show $(route -n | awk '/^0\.0\.0\.0/ {print $NF}') | grep "inet " | awk '{print $2}' | cut -f1 -d'/' | head -1`
# **********************************************************************
# Functions
# Start Xvfb, required for firefox in server without X
function start_xvfb() {
`which Xvfb >/dev/null 2>&1`
@ -37,45 +88,47 @@ function start_xvfb() {
echo 1
fi
else
Xvfb :99 -ac > /dev/null 2>&1 &
Xvfb :99 -screen 0 1400x768x24 -ac +extension RANDR > $LOG_DIR/xvfb.log 2>&1 & echo $! > $XVFB_PID_FILE
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
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
if [ ! -e "$BASE_DIR/pwrd_node.pid" ] && [ ! -e "$BASE_DIR/pwrd.pid" ]; then
if [ ! -e /etc/rc.d/init.d/functions ]; then
echo -n "Stopping Xvfb..."
fi
PID=$(cat $XVFB_PID_FILE)
if [ "$PID" != "" ] && [ `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
[ -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"
[ -e $BASE_DIR/pwrd_node.pid ] && echo -n `cat $BASE_DIR/pwrd_node.pid`
[ -e $BASE_DIR/pwrd.pid ] && echo -n `cat $BASE_DIR/pwrd.pid`
echo "pwrd processes left. Skipping Xvfb"
fi
fi
}
################################################################################
########################################################################
# START
################################################################################
########################################################################
# Start Selenium Standalone server
function start() {
if test -f $pid_file
then
if test -f $pid_file; then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
if [ "$PID" != "" ] && ps --pid $PID >/dev/null; then
echo "PWRD is already running: $PID"
exit 2
else
@ -91,11 +144,12 @@ function start() {
return 1
fi
echo -n "Starting PWRD..."
if [ ! -e /etc/rc.d/init.d/functions ]; then
echo -n "Starting PWRD..."
fi
export DISPLAY=:99
su $user -c "$java -jar $pwr -host $host $PWROPTS > $std_log 2>&1 &"
PID=$(su $USER -c "$JAVA -jar $PWR -host $host -port $PWR_PORT $PWR_OPTS > $STD_LOG 2>&1 & echo \$!")
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
@ -109,28 +163,29 @@ function start() {
fi
# Store PID
ps -eo pid,cmd | grep $pwr | grep -v "node" | grep -v "hub" | grep -v grep | awk {'print $1 '} > $pid_file
echo -n $PID > $pid_file
}
# Start Selenium GRID server: HUB component
function start-hub() {
if test -f $pid_file
then
if test -f $pid_file; then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
if [ "$PID" != "" ] && 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..."
if [ ! -e /etc/rc.d/init.d/functions ]; then
echo -n "Starting PWRD..."
fi
# No Xvfb needed in hub mode
su $user -c "$java -jar $pwr -host $host -role hub $PWR_HUB_OPTS > $std_log 2>&1 &"
PID=$(su $USER -c "$JAVA -jar $PWR -host $host -port $PWR_HUB_PORT -role hub $PWR_HUB_OPTS > $STD_LOG 2>&1 & echo \$!")
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
@ -144,15 +199,17 @@ function start-hub() {
fi
# Store PID
ps -eo pid,cmd | grep $pwr | grep "hub" | grep -v grep | awk {'print $1 '} > $pid_file
echo -n $PID > $pid_file
echo
echo -e "Add nodes to HUB executing:\n $0 start-node http://$host:$PWR_HUB_PORT/grid/register"
}
# Start Selenium GRID server: Node Connect to hub
function start-node() {
if test -f $pid_file
then
if test -f $pid_file ; then
PID=`cat $pid_file`
if ps --pid $PID >/dev/null; then
if [ "$PID" != "" ] && ps --pid $PID >/dev/null; then
echo "PWRD is already running: $PID"
exit 2
else
@ -168,11 +225,12 @@ function start-node() {
return 1
fi
echo -n "Starting PWRD..."
if [ ! -e /etc/rc.d/init.d/functions ]; then
echo -n "Starting PWRD..."
fi
export DISPLAY=:99
su $user -c "$java -jar $pwr -host $host -role node -hub $1 $PWROPTS > $std_log 2>&1 &"
PID=$(su $USER -c "$JAVA -jar $PWR -host $host -port $PWR_NODE_PORT -role node -hub $1 $PWR_OPTS > $STD_LOG 2>&1 & echo \$!")
if [ $? == "0" ]; then
if [ -e /etc/rc.d/init.d/functions ]; then
success
@ -186,32 +244,36 @@ function start-node() {
fi
# Store PID
ps -C java -eo pid,cmd | grep $pwr | grep "node" | grep -v grep | awk {'print $1 '} > $pid_file
echo -n $PID > $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
[ "$PID" == "" ] && PID=`ps aux | grep "$JAVA -jar $PWR" | grep -v grep | awk '{print $2}'`
[ "$PID" != "" ] && su $USER -c "kill -3 $PID"
[ "$PID" != "" ] && kill -9 $PID >/dev/null 2>&1
if [ $? -eq 0 ]; then
sleep 5
sleep 2
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..."
[ "$PID" == "" ] && [ -f $pid_file ] && rm -f $pid_file
if [ -e /etc/rc.d/init.d/functions ]; then
failure
else
echo "Failed"
fi
echo "Failed"
fi
stop_xvfb
else
@ -224,84 +286,94 @@ function stop() {
echo
}
################################################################################
########################################################################
# STATUS
################################################################################
########################################################################
# Status of Selenium Standalone server
function status() {
mode=$1
if test -f $pid_file ; then
echo "checking: $pid_file"
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
if [ "$PID" != "" ] && ps --pid $PID >/dev/null ; then
echo "PWRD$mode 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..."
echo "PWRD$mode isn't running..."
fi
else
echo "PWRD isn't running..."
echo "PWRD$mode isn't running..."
fi
}
########################################################################
# MAIN
########################################################################
if [ ! -x "$JAVA" ]; then
echo "Please verify $JAVA is a valid java" 2>&1
exit 1
fi
case "$1" in
start) # Standalone
pid_file=$base_dir/pwrd.pid
pid_file=$BASE_DIR/pwrd.pid
start
;;
start-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
pid_file=$BASE_DIR/pwrd_hub.pid
start-hub
;;
start-node) # node
pid_file=$base_dir/pwrd_node.pid
pid_file=$BASE_DIR/pwrd_node.pid
start-node $2
;;
stop) # standalone
pid_file=$base_dir/pwrd.pid
pid_file=$BASE_DIR/pwrd.pid
stop
;;
stop-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
pid_file=$BASE_DIR/pwrd_hub.pid
stop
;;
stop-node) # node
pid_file=$base_dir/pwrd_node.pid
pid_file=$BASE_DIR/pwrd_node.pid
stop
;;
restart) # standalone
pid_file=$base_dir/pwrd.pid
pid_file=$BASE_DIR/pwrd.pid
stop
start
;;
restart-hub) # hub
pid_file=$base_dir/pwrd_hub.pid
pid_file=$BASE_DIR/pwrd_hub.pid
stop
start-hub
;;
restart-node) # node
pid_file=$base_dir/pwrd_node.pid
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
pid_file=$BASE_DIR/pwrd.pid
status ""
pid_file=$BASE_DIR/pwrd_hub.pid
status " HUB"
pid_file=$BASE_DIR/pwrd_node.pid
status " Node"
;;
*)
echo "Usage: $SELF start*|stop*|restart*|status*"
echo "Usage: $SELF start*|stop*|restart*|status"
echo " start* (standalone, hub or node)"
echo " start"
echo " start-hub"
@ -314,12 +386,6 @@ case "$1" in
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]