Advertisement
Guest User

Untitled

a guest
May 9th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.79 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # TODO: su $GAMEUSER -c "COMMANDS..."
  4.  
  5. # script to control the racesow servers
  6.  
  7. # The user which should run the gameservers
  8. GAMEUSER=warsow
  9.  
  10. SCREEN_NAME=warsow_race
  11.  
  12. # Folder to store PID files (writeable)
  13. PATH_PIDS=/home/warsow/pids
  14.  
  15. # Path to the ini-file configuring the gameservers
  16. PATH_CONFIG=/srv/apache2/warsow-race.net/www/config/config.ini
  17.  
  18. CHECKSCRIPT=/home/warsow/scripts/check_gameserver.pl
  19.  
  20. # Warsow root directory
  21. PATH_WARSOW=/home/warsow/warsow_race
  22.  
  23. # The gameserver executable
  24. GAMESERVER=wsw_server.x86_64
  25.  
  26. # The mod directory (ie. basewsw)
  27. MODDIR=racesow
  28.  
  29. # Path to location where the folder $MODDIR exists with pk3s of maps
  30. PATH_PK3S=/home/warsow/.warsow/
  31.  
  32. # Path to file with one (pass)word per line
  33. PATH_WORDS=/srv/apache2/warsow-race.net/www/library/freecap/freecap_words
  34.  
  35. # Path to kkrcon
  36. PATH_KKRCON=/home/warsow/kkrcon/
  37. KKRCON=./kkrcon.pl
  38. RCON_PASSWORD=*****
  39.  
  40. #Path quakestat
  41. QUAKESTAT=quakestat
  42.  
  43. #Hostname or IP for qstat queries
  44. HOST=localhost
  45.  
  46. # The start-stop-daemon executable
  47. DAEMON=/sbin/start-stop-daemon
  48.  
  49. # Database Conenction
  50. DB_NAME=warsow_racenet
  51. DB_USER=root
  52. DB_PASS=
  53.  
  54. # Save the location of this script
  55. THISFILE=$0
  56.  
  57. # Store the gameserver ports from the inifile in $PORTS
  58. eval "PORTS=\""$(cat $PATH_CONFIG | \
  59. awk -F= '$1 ~ /^gameservers\.[^\.]+\.port/ \
  60.     {{gsub("^ *\"*", "", $2)} \
  61.     {gsub("\"* *$", "", $2)} \
  62.     {print $2}}')"\""
  63.  
  64. # get the process id of the main screen
  65. function get_main_screen_pid
  66. {
  67.     return `screen -ls | pcregrep "\d+\.$SCREEN_NAME" | awk -F "." '{printf "%d",$1}'`
  68. }
  69.    
  70. # Start a server loop for the given port
  71. function gameserver_start
  72. {
  73.     get_main_screen_pid
  74.     if [ "$?" == "0" ]; then
  75.         echo "starting main screen."
  76.         `screen -dmS $SCREEN_NAME`
  77.     fi
  78.  
  79.     if [ "$1" == "" ]; then
  80.         for PORT in $PORTS; do
  81.             gameserver_start $PORT
  82.         done
  83.     else
  84.         PORTCHECK=$(echo $PORTS | grep $1)
  85.         if [ "$PORTCHECK" != "" ];then
  86.             if [ ! -f $PATH_WARSOW/$MODDIR/cfgs/port_$1.cfg ]; then
  87.                 echo "WARNING: no config found for $1"
  88.             fi
  89.  
  90.             gameserver_check_pid $1
  91.             if [ $? == 0 ]; then               
  92.                 PORT=$1
  93.                 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"
  94.                 `$CMD`
  95.             else
  96.                 echo "server $1 is already running"
  97.                 exit
  98.             fi
  99.             return 1
  100.         else
  101.             echo "server $1 is not configured"
  102.             exit
  103.         fi
  104.     fi
  105. }
  106.  
  107. # stop gameserver(s)
  108. function gameserver_stop
  109. {
  110.     if [ "$1" == "" ]; then
  111.             for PORT in $PORTS; do
  112.                 gameserver_start $PORT
  113.             done
  114.         else
  115.             PORTCHECK=$(echo $PORTS | grep $1)
  116.         if [ "$PORTCHECK" != "" ];then
  117.             if [ ! -f $PATH_WARSOW/$MODDIR/cfgs/port_$1.cfg ]; then
  118.                 echo "WARNING: no config found for $1"
  119.             fi
  120.  
  121.             gameserver_check_pid $1
  122.             if [ $? != 0 ]; then               
  123.                 PORT=$1
  124.                 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"
  125.                 rm -f $PATH_PIDS/$PORT.pid
  126.                 `$CMD`
  127.             else
  128.                 echo "server $1 is not running"
  129.                 exit
  130.             fi
  131.         else
  132.             echo "server $1 is not configured"
  133.             exit
  134.         fi
  135.     fi
  136. }
  137.  
  138. # Kill server loop with the given port
  139. function gameserver_kill
  140. {
  141.     if [ "$1" == "" ]; then
  142.         echo "you need to specify a port for killing a server"
  143.         exit
  144.     else
  145.         PORTCHECK=$(echo $PORTS | grep $1)
  146.         if [ "$PORTCHECK" != "" ]; then
  147.             gameserver_check_pid $1
  148.             if [ $? == 1 ]; then
  149.                 echo "killing server warsow://$HOST:$1"
  150.                 kill -n 15 `cat $PATH_PIDS/$1.pid`
  151.                 rm -f  $PATH_PIDS/$1.pid
  152.             else
  153.                 echo "server $1 is not running"
  154.                 exit
  155.             fi
  156.         else
  157.             echo "server $1 is not configured"
  158.             exit
  159.         fi
  160.     fi
  161. }
  162.  
  163. # check if server on port $1 is running, also removes old pidfiles if necesary
  164. function gameserver_check_pid
  165. {
  166.     if [ ! -f $PATH_PIDS/$1.pid ]; then
  167.         return 0
  168.     fi
  169.     SERVERPID=$(cat $PATH_PIDS/$1.pid)
  170.     TEST=$(echo $SERVERPID | xargs ps -fp | grep $GAMESERVER)
  171.     if [ "$TEST" == "" ]; then
  172.         rm -f $PATH_PIDS/$1.pid
  173.         return 0
  174.     else
  175.         return 1
  176.     fi
  177. }
  178.  
  179. # check if the server is running, kills hanging gameserver processes
  180. function gameserver_check_gamestate
  181. {
  182.     if [ "$1" == "" ]; then
  183.         for PORT in $PORTS; do
  184.             gameserver_check_gamestate $PORT
  185.         done
  186.     else
  187.         SERVERTEST=`$QUAKESTAT -warsows $HOST:$1 | pcregrep "$HOST:$1 +(DOWN|no response)"`
  188.         if [ "$SERVERTEST" != "" ]; then
  189.             echo "warsow://$HOST:$1 is not reachable via qstat"
  190.            
  191.             echo "* trying to find the process by it's pid"
  192.             gameserver_check_pid $1
  193.             if [ $? == 0 ]; then
  194.                 echo "* gameserver not found, starting on port $1"
  195.                 gameserver_start $1
  196.             else
  197.                 echo "* restarting gameserver on port $1"
  198.                 gameserver_stop $1
  199.                 gameserver_start $1
  200.             fi
  201.         fi
  202.     fi
  203. }
  204.  
  205.  
  206. # Change the default startup map for a server
  207. function gameserver_set_defaultmap
  208. {
  209.    
  210.     if [ "$1" == "" ]; then
  211.         for PORT in $PORTS; do
  212.             gameserver_set_defaultmap $PORT
  213.         done
  214.     else
  215.         PORT=$1
  216.         PORTCHECK=$(echo $PORTS | grep $PORT)
  217.         if [ "$PORTCHECK" != "" ]; then
  218.             MYSQL="mysql $DB_NAME --user=$DB_USER --password=$DB_PASS --column-names=FALSE"
  219.            
  220.             MAP=$2
  221.  
  222.             if [ "$MAP" == "" ]; then
  223.                 MAP=$(echo "SELECT name FROM map WHERE freestyle='false' AND status='enabled' ORDER BY RAND() LIMIT 1" | $MYSQL)
  224.             fi
  225.            
  226.             FILE=$(echo "SELECT file FROM map WHERE name LIKE '$MAP' LIMIT 1" | $MYSQL)
  227.             if [ ! -f $PATH_PK3S$MODDIR/$FILE ]; then
  228.                 echo "map $MAP not found on server"
  229.                 exit
  230.             fi
  231.        
  232.             echo "set sv_defaultmap $MAP" > $PATH_WARSOW/$MODDIR"/cfgs/port_"$PORT"_defaultmap.cfg"
  233.         else
  234.             echo "server $1 is not configured"
  235.             exit
  236.         fi
  237.     fi
  238.        
  239. }
  240.  
  241. # Set a new password in the servers password file and send the new password via rcon as well
  242. function gameserver_set_password
  243. {
  244.     if [ "$1" == "" ]; then
  245.         echo "no server port given"
  246.         exit
  247.     fi
  248.    
  249.     PORT=$1
  250.     PORTCHECK=$(echo $PORTS | grep $PORT)
  251.     if [ "$PORTCHECK" != "" ]; then
  252.         PATH_PASS=$PATH_WARSOW/$MODDIR"/cfgs/port_"$PORT"_password.cfg"
  253.            
  254.         gameserver_load_config $PORT
  255.         if [ $isProtected == 1 ]; then
  256.             if [ ! -f $PATH_PASS ]; then
  257.                 echo "ERROR: could not find PATH_PASS ($PATH_PASS)"
  258.                 exit
  259.             fi
  260.         else
  261.             echo "Warning: port $PORT is not configured as protected server. password not set"
  262.             exit
  263.         fi
  264.                
  265.         if [ "$2" == "" ]; then
  266.             if [ ! -f $PATH_WORDS ]; then
  267.                 echo "ERROR: PATH_WORDS ($PATH_WORDS) is not a file"
  268.                 exit
  269.             fi
  270.        
  271.             NUM_WORDS=$(wc -l $PATH_WORDS | awk '{print $1}')
  272.             WORD_POS=$RANDOM
  273.             let "WORD_POS %= $NUM_WORDS"
  274.             NEWPASS=$(awk 'NR=='$WORD_POS $PATH_WORDS)
  275.         else
  276.             NEWPASS=$2
  277.         fi
  278.        
  279.         CMD="set password $NEWPASS"
  280.         gameserver_check_pid $PORT
  281.         if [ $? == 1 ]; then
  282.             cd $PATH_KKRCON && $KKRCON -t 'old' -a $ip -p $PORT $RCON_PASSWORD $CMD &> /dev/null
  283.         fi
  284.         echo $CMD > $PATH_PASS
  285.     else
  286.         echo "server $1 is not configured"
  287.         exit
  288.     fi
  289. }
  290.  
  291. # Parses the serverconfig from raceent config.ini
  292. function gameserver_load_config
  293. {
  294.     eval `cat $PATH_CONFIG | \
  295.     grep gameservers.$1 | \
  296.     awk -F= '{sub("^gameservers\.[0-9]+\.", "", $1)} \
  297.         {gsub("\.", "_", $1)} \
  298.         {sub(" +$", "", $1)} \
  299.         {sub("^ *\"*", "", $2)} \
  300.         {sub("\"* *$", "", $2)} \
  301.         {printf "%s=\"%s\"\n", $1, $2}'`
  302. }
  303.  
  304. # returns pids of child processes and the input pid
  305. function find_depening_pids
  306. {
  307.         [ -n "$1" ] && echo $1;
  308.         for p in `pgrep -P $1`; do
  309.             fcp $p;
  310.     done;
  311. }
  312.  
  313.  
  314. # Check and run the action
  315. case $1 in
  316.     start)      gameserver_start $2;;
  317.     stop)       gameserver_stop $2;;
  318.     defaultmap) gameserver_set_defaultmap $2 $3;;
  319.     password)   gameserver_set_password $2 $3;;
  320.     check)      gameserver_check_gamestate $2;;
  321.     *)      echo "Usage: $0 {start|stop|defaultmap|password}";;
  322. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement