Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # TODO: su $GAMEUSER -c "COMMANDS..."
- # script to control the racesow servers
- # The user which should run the gameservers
- GAMEUSER=warsow
- SCREEN_NAME=warsow_race
- # Folder to store PID files (writeable)
- PATH_PIDS=/home/warsow/pids
- # Path to the ini-file configuring the gameservers
- PATH_CONFIG=/srv/apache2/warsow-race.net/www/config/config.ini
- CHECKSCRIPT=/home/warsow/scripts/check_gameserver.pl
- # Warsow root directory
- PATH_WARSOW=/home/warsow/warsow_race
- # The gameserver executable
- GAMESERVER=wsw_server.x86_64
- # The mod directory (ie. basewsw)
- MODDIR=racesow
- # Path to location where the folder $MODDIR exists with pk3s of maps
- PATH_PK3S=/home/warsow/.warsow/
- # Path to file with one (pass)word per line
- PATH_WORDS=/srv/apache2/warsow-race.net/www/library/freecap/freecap_words
- # Path to kkrcon
- PATH_KKRCON=/home/warsow/kkrcon/
- KKRCON=./kkrcon.pl
- RCON_PASSWORD=*****
- #Path quakestat
- QUAKESTAT=quakestat
- #Hostname or IP for qstat queries
- HOST=localhost
- # The start-stop-daemon executable
- DAEMON=/sbin/start-stop-daemon
- # Database Conenction
- DB_NAME=warsow_racenet
- DB_USER=root
- DB_PASS=
- # Save the location of this script
- THISFILE=$0
- # Store the gameserver ports from the inifile in $PORTS
- eval "PORTS=\""$(cat $PATH_CONFIG | \
- awk -F= '$1 ~ /^gameservers\.[^\.]+\.port/ \
- {{gsub("^ *\"*", "", $2)} \
- {gsub("\"* *$", "", $2)} \
- {print $2}}')"\""
- # get the process id of the main screen
- function get_main_screen_pid
- {
- return `screen -ls | pcregrep "\d+\.$SCREEN_NAME" | awk -F "." '{printf "%d",$1}'`
- }
- # Start a server loop for the given port
- function gameserver_start
- {
- get_main_screen_pid
- if [ "$?" == "0" ]; then
- echo "starting main screen."
- `screen -dmS $SCREEN_NAME`
- fi
- if [ "$1" == "" ]; then
- for PORT in $PORTS; do
- gameserver_start $PORT
- done
- else
- PORTCHECK=$(echo $PORTS | grep $1)
- if [ "$PORTCHECK" != "" ];then
- if [ ! -f $PATH_WARSOW/$MODDIR/cfgs/port_$1.cfg ]; then
- echo "WARNING: no config found for $1"
- fi
- gameserver_check_pid $1
- if [ $? == 0 ]; then
- PORT=$1
- CMD="screen -S $SCREEN_NAME -X screen -t "$GAMEUSER"_"$PORT" $DAEMON --pidfile $PATH_PIDS/$PORT.pid --make-pidfile --start --chdir $PATH_WARSOW --exec $PATH_WARSOW/$GAMESERVER +set fs_game $MODDIR +exec cfgs/port_"$PORT".cfg"
- `$CMD`
- else
- echo "server $1 is already running"
- exit
- fi
- return 1
- else
- echo "server $1 is not configured"
- exit
- fi
- fi
- }
- # stop gameserver(s)
- function gameserver_stop
- {
- if [ "$1" == "" ]; then
- for PORT in $PORTS; do
- gameserver_start $PORT
- done
- else
- PORTCHECK=$(echo $PORTS | grep $1)
- if [ "$PORTCHECK" != "" ];then
- if [ ! -f $PATH_WARSOW/$MODDIR/cfgs/port_$1.cfg ]; then
- echo "WARNING: no config found for $1"
- fi
- gameserver_check_pid $1
- if [ $? != 0 ]; then
- PORT=$1
- CMD="$DAEMON --pidfile $PATH_PIDS/$PORT.pid --make-pidfile --chuid $GAMEUSER:$GAMEUSER --stop --chdir $PATH_WARSOW --exec $PATH_WARSOW/$GAMESERVER +set fs_game $MODDIR +exec cfgs/port_"$PORT".cfg"
- rm -f $PATH_PIDS/$PORT.pid
- `$CMD`
- else
- echo "server $1 is not running"
- exit
- fi
- else
- echo "server $1 is not configured"
- exit
- fi
- fi
- }
- # Kill server loop with the given port
- function gameserver_kill
- {
- if [ "$1" == "" ]; then
- echo "you need to specify a port for killing a server"
- exit
- else
- PORTCHECK=$(echo $PORTS | grep $1)
- if [ "$PORTCHECK" != "" ]; then
- gameserver_check_pid $1
- if [ $? == 1 ]; then
- echo "killing server warsow://$HOST:$1"
- kill -n 15 `cat $PATH_PIDS/$1.pid`
- rm -f $PATH_PIDS/$1.pid
- else
- echo "server $1 is not running"
- exit
- fi
- else
- echo "server $1 is not configured"
- exit
- fi
- fi
- }
- # check if server on port $1 is running, also removes old pidfiles if necesary
- function gameserver_check_pid
- {
- if [ ! -f $PATH_PIDS/$1.pid ]; then
- return 0
- fi
- SERVERPID=$(cat $PATH_PIDS/$1.pid)
- TEST=$(echo $SERVERPID | xargs ps -fp | grep $GAMESERVER)
- if [ "$TEST" == "" ]; then
- rm -f $PATH_PIDS/$1.pid
- return 0
- else
- return 1
- fi
- }
- # check if the server is running, kills hanging gameserver processes
- function gameserver_check_gamestate
- {
- if [ "$1" == "" ]; then
- for PORT in $PORTS; do
- gameserver_check_gamestate $PORT
- done
- else
- SERVERTEST=`$QUAKESTAT -warsows $HOST:$1 | pcregrep "$HOST:$1 +(DOWN|no response)"`
- if [ "$SERVERTEST" != "" ]; then
- echo "warsow://$HOST:$1 is not reachable via qstat"
- echo "* trying to find the process by it's pid"
- gameserver_check_pid $1
- if [ $? == 0 ]; then
- echo "* gameserver not found, starting on port $1"
- gameserver_start $1
- else
- echo "* restarting gameserver on port $1"
- gameserver_stop $1
- gameserver_start $1
- fi
- fi
- fi
- }
- # Change the default startup map for a server
- function gameserver_set_defaultmap
- {
- if [ "$1" == "" ]; then
- for PORT in $PORTS; do
- gameserver_set_defaultmap $PORT
- done
- else
- PORT=$1
- PORTCHECK=$(echo $PORTS | grep $PORT)
- if [ "$PORTCHECK" != "" ]; then
- MYSQL="mysql $DB_NAME --user=$DB_USER --password=$DB_PASS --column-names=FALSE"
- MAP=$2
- if [ "$MAP" == "" ]; then
- MAP=$(echo "SELECT name FROM map WHERE freestyle='false' AND status='enabled' ORDER BY RAND() LIMIT 1" | $MYSQL)
- fi
- FILE=$(echo "SELECT file FROM map WHERE name LIKE '$MAP' LIMIT 1" | $MYSQL)
- if [ ! -f $PATH_PK3S$MODDIR/$FILE ]; then
- echo "map $MAP not found on server"
- exit
- fi
- echo "set sv_defaultmap $MAP" > $PATH_WARSOW/$MODDIR"/cfgs/port_"$PORT"_defaultmap.cfg"
- else
- echo "server $1 is not configured"
- exit
- fi
- fi
- }
- # Set a new password in the servers password file and send the new password via rcon as well
- function gameserver_set_password
- {
- if [ "$1" == "" ]; then
- echo "no server port given"
- exit
- fi
- PORT=$1
- PORTCHECK=$(echo $PORTS | grep $PORT)
- if [ "$PORTCHECK" != "" ]; then
- PATH_PASS=$PATH_WARSOW/$MODDIR"/cfgs/port_"$PORT"_password.cfg"
- gameserver_load_config $PORT
- if [ $isProtected == 1 ]; then
- if [ ! -f $PATH_PASS ]; then
- echo "ERROR: could not find PATH_PASS ($PATH_PASS)"
- exit
- fi
- else
- echo "Warning: port $PORT is not configured as protected server. password not set"
- exit
- fi
- if [ "$2" == "" ]; then
- if [ ! -f $PATH_WORDS ]; then
- echo "ERROR: PATH_WORDS ($PATH_WORDS) is not a file"
- exit
- fi
- NUM_WORDS=$(wc -l $PATH_WORDS | awk '{print $1}')
- WORD_POS=$RANDOM
- let "WORD_POS %= $NUM_WORDS"
- NEWPASS=$(awk 'NR=='$WORD_POS $PATH_WORDS)
- else
- NEWPASS=$2
- fi
- CMD="set password $NEWPASS"
- gameserver_check_pid $PORT
- if [ $? == 1 ]; then
- cd $PATH_KKRCON && $KKRCON -t 'old' -a $ip -p $PORT $RCON_PASSWORD $CMD &> /dev/null
- fi
- echo $CMD > $PATH_PASS
- else
- echo "server $1 is not configured"
- exit
- fi
- }
- # Parses the serverconfig from raceent config.ini
- function gameserver_load_config
- {
- eval `cat $PATH_CONFIG | \
- grep gameservers.$1 | \
- awk -F= '{sub("^gameservers\.[0-9]+\.", "", $1)} \
- {gsub("\.", "_", $1)} \
- {sub(" +$", "", $1)} \
- {sub("^ *\"*", "", $2)} \
- {sub("\"* *$", "", $2)} \
- {printf "%s=\"%s\"\n", $1, $2}'`
- }
- # returns pids of child processes and the input pid
- function find_depening_pids
- {
- [ -n "$1" ] && echo $1;
- for p in `pgrep -P $1`; do
- fcp $p;
- done;
- }
- # Check and run the action
- case $1 in
- start) gameserver_start $2;;
- stop) gameserver_stop $2;;
- defaultmap) gameserver_set_defaultmap $2 $3;;
- password) gameserver_set_password $2 $3;;
- check) gameserver_check_gamestate $2;;
- *) echo "Usage: $0 {start|stop|defaultmap|password}";;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement