Advertisement
aokmikey

Untitled

Sep 22nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.41 KB | None | 0 0
  1. #! /bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: csgo-server-launcher
  4. # Required-Start: $remote_fs $syslog
  5. # Required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Counter-Strike - Global Offensive Server Launcher
  9. ### END INIT INFO
  10.  
  11. ##################################################################################
  12. #                                                                                #
  13. #  Counter-Strike : Global Offensive Server Launcher v1.10                       #
  14. #                                                                                #
  15. #  A simple script to launch your Counter-Strike : Global Offensive              #
  16. #  Dedicated Server.                                                             #
  17. #                                                                                #
  18. #  Copyright (C) 2013-2015 Cr@zy <webmaster@crazyws.fr>                          #
  19. #                                                                                #
  20. #  Counter-Strike : Global Offensive Server Launcher is free software; you can   #
  21. #  redistribute it and/or modify it under the terms of the GNU Lesser General    #
  22. #  Public License as published by the Free Software Foundation, either version 3 #
  23. #  of the License, or (at your option) any later version.                        #
  24. #                                                                                #
  25. #  Counter-Strike : Global Offensive Server Launcher is distributed in the hope  #
  26. #  that it will be useful, but WITHOUT ANY WARRANTY; without even the implied    #
  27. #  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      #
  28. #  GNU Lesser General Public License for more details.                           #
  29. #                                                                                #
  30. #  You should have received a copy of the GNU Lesser General Public License      #
  31. #  along with this program. If not, see http://www.gnu.org/licenses/.            #
  32. #                                                                                #
  33. #  Related post: http://goo.gl/HFFGy                                             #
  34. #  Usage: ./csgo-server-launcher.sh {start|stop|status|restart|console|update}   #
  35. #    - start: start the server                                                   #
  36. #    - stop: stop the server                                                     #
  37. #    - status: display the status of the server (down or up)                     #
  38. #    - restart: restart the server                                               #
  39. #    - console: display the server console where you can enter commands.         #
  40. #     To exit the console without stopping the server, press CTRL + A then D.    #
  41. #    - update: update the server                                                 #
  42. #    - create: creates a new server                                              #
  43. #                                                                                #
  44. ##################################################################################
  45.  
  46. CONFIG_FILE="csgo-server-launcher.conf"
  47.  
  48. # No edits necessary beyond this line
  49.  
  50. function start {
  51.   if [ ! -d $DIR_ROOT ]; then echo "ERROR: $DIR_ROOT is not a directory"; exit 1; fi
  52.   if [ ! -x $DIR_ROOT/$DAEMON_GAME ]; then echo "ERROR: $DIR_ROOT/$DAEMON_GAME does not exist or is not executable"; exit 1; fi
  53.   if status; then echo "$SCREEN_NAME is already running"; exit 1; fi
  54.  
  55.   # Create runscript file for autoupdate
  56.   echo "Create runscript file '$STEAM_RUNSCRIPT' for autoupdate..."
  57.   cd $DIR_STEAMCMD
  58.   echo "login $STEAM_LOGIN $STEAM_PASSWORD" > $STEAM_RUNSCRIPT
  59.   echo "force_install_dir $DIR_ROOT" >> $STEAM_RUNSCRIPT
  60.   echo "app_update 740" >> $STEAM_RUNSCRIPT
  61.   echo "quit" >> $STEAM_RUNSCRIPT
  62.   chown $USER $STEAM_RUNSCRIPT
  63.   chmod 600 $STEAM_RUNSCRIPT
  64.  
  65.   # Generated misc args
  66.   GENERATED_ARGS="";
  67.   if [ -z "${API_AUTHORIZATION_KEY}" -a -f $DIR_GAME/webapi_authkey.txt ]; then API_AUTHORIZATION_KEY="`cat $DIR_GAME/webapi_authkey.txt`"; fi
  68.   if [ ! -z "${API_AUTHORIZATION_KEY}" ]
  69.   then
  70.     GENERATED_ARGS="-authkey ${API_AUTHORIZATION_KEY}"
  71.     if [ ! -z "${WORKSHOP_COLLECTION_ID}" ]; then GENERATED_ARGS="${GENERATED_ARGS} +host_workshop_collection ${WORKSHOP_COLLECTION_ID}"; fi
  72.     if [ ! -z "${WORKSHOP_START_MAP}" ]; then GENERATED_ARGS="${GENERATED_ARGS} +workshop_start_map ${WORKSHOP_START_MAP}"; fi
  73.   fi
  74.   if [ ! -z "${GSLT}" ]; then GENERATED_ARGS="${GENERATED_ARGS} +sv_setsteamaccount ${GSLT}"; fi
  75.  
  76.   # Start game
  77.   PARAM_START="${PARAM_START} ${GENERATED_ARGS}"
  78.   echo "Start command : $PARAM_START"
  79.  
  80.   if [ `whoami` = root ]
  81.   then
  82.     su - $USER -c "cd $DIR_ROOT ; screen -AmdS $SCREEN_NAME ./$DAEMON_GAME $PARAM_START"
  83.   else
  84.     cd $DIR_ROOT
  85.     screen -AmdS $SCREEN_NAME ./$DAEMON_GAME $PARAM_START
  86.   fi
  87. }
  88.  
  89. function stop {
  90.   if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi
  91.  
  92.   if [ `whoami` = root ]
  93.   then
  94.     tmp=$(su - $USER -c "screen -ls" | awk -F . "/\.$SCREEN_NAME\t/ {print $1}" | awk '{print $1}')
  95.     su - $USER -c "screen -r $tmp -X quit"
  96.   else
  97.     screen -r $(screen -ls | awk -F . "/\.$SCREEN_NAME\t/ {print $1}" | awk '{print $1}') -X quit
  98.   fi
  99. }
  100.  
  101. function status {
  102.   if [ `whoami` = root ]
  103.   then
  104.     su - $USER -c "screen -ls" | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
  105.   else
  106.     screen -ls | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
  107.   fi
  108. }
  109.  
  110. function console {
  111.   if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi
  112.  
  113.   if [ `whoami` = root ]
  114.   then
  115.     tmp=$(su - $USER -c "screen -ls" | awk -F . "/\.$SCREEN_NAME\t/ {print $1}" | awk '{print $1}')
  116.     su - $USER -c "screen -r $tmp"
  117.   else
  118.     screen -r $(screen -ls | awk -F . "/\.$SCREEN_NAME\t/ {print $1}" | awk '{print $1}')
  119.   fi
  120. }
  121.  
  122. function update {
  123.   # Create the log directory
  124.   if [ ! -d $DIR_LOGS ];
  125.   then
  126.     echo "$DIR_LOGS does not exist, creating..."
  127.     if [ `whoami` = root ]
  128.     then
  129.       su - $USER -c "mkdir -p $DIR_LOGS";
  130.     else
  131.       mkdir -p "$DIR_LOGS"
  132.     fi
  133.   fi
  134.  
  135.   if [ ! -d $DIR_LOGS ]
  136.   then
  137.     echo "ERROR: Could not create $DIR_LOGS"
  138.     exit 1
  139.   fi
  140.  
  141.   # Create the game root
  142.   if [ ! -d $DIR_ROOT ]
  143.   then
  144.     echo "$DIR_ROOT does not exist, creating..."
  145.     if [ `whoami` = root ]
  146.     then
  147.       su - $USER -c "mkdir -p $DIR_ROOT";
  148.     else
  149.       mkdir -p "$DIR_ROOT"
  150.     fi
  151.   fi
  152.  
  153.   if [ ! -d $DIR_ROOT ]
  154.   then
  155.     echo "ERROR: Could not create $DIR_ROOT"
  156.     exit 1
  157.   fi
  158.  
  159.   if [ -z "$1" ]; then retry=0; else retry=$1; fi
  160.  
  161.   if [ -z "$2" ]
  162.   then
  163.     if status
  164.     then
  165.       stop
  166.       echo "Stop $SCREEN_NAME before update..."
  167.       sleep 5
  168.       relaunch=1
  169.     else
  170.       relaunch=0
  171.     fi
  172.   else
  173.     relaunch=$2
  174.   fi
  175.  
  176.   # Save motd.txt before update
  177.   if [ -f "$DIR_GAME/motd.txt" ]; then cp $DIR_GAME/motd.txt $DIR_GAME/motd.txt.bck; fi
  178.   # Save maplist.txt before update
  179.   if [ -f "$DIR_GAME/maplist.txt" ]; then cp $DIR_GAME/maplist.txt $DIR_GAME/maplist.txt.bck; fi
  180.   # Save maplist.txt before update
  181.   if [ -f "$DIR_GAME/mapcycle.txt" ]; then cp $DIR_GAME/mapcycle.txt $DIR_GAME/mapcycle.txt.bck; fi
  182.  
  183.  
  184.   echo "Starting the $SCREEN_NAME update..."
  185.  
  186.   if [ `whoami` = root ]
  187.   then
  188.     su - $USER -c "cd $DIR_STEAMCMD ; ./steamcmd.sh $PARAM_UPDATE 2>&1 | tee $UPDATE_LOG"
  189.   else
  190.     cd $DIR_STEAMCMD
  191.     ./steamcmd.sh $PARAM_UPDATE 2>&1 | tee $UPDATE_LOG
  192.   fi
  193.  
  194.   # Restore motd.txt
  195.   if [ -f "$DIR_GAME/motd.txt.bck" ]; then mv $DIR_GAME/motd.txt.bck $DIR_GAME/motd.txt; fi
  196.   # Restore motd.txt
  197.   if [ -f "$DIR_GAME/maplist.txt.bck" ]; then mv $DIR_GAME/maplist.txt.bck $DIR_GAME/maplist.txt; fi
  198.   # Restore mapcycle.txt
  199.   if [ -f "$DIR_GAME/mapcycle.txt.bck" ]; then mv $DIR_GAME/mapcycle.txt.bck $DIR_GAME/mapcycle.txt; fi
  200.  
  201.   # Check for update
  202.   if [ `egrep -ic "Success! App '740' fully installed." "$UPDATE_LOG"` -gt 0 ] || [ `egrep -ic "Success! App '740' already up to date" "$UPDATE_LOG"` -gt 0 ]
  203.   then
  204.     echo "$SCREEN_NAME updated successfully"
  205.   else
  206.     if [ $retry -lt $UPDATE_RETRY ]
  207.     then
  208.       retry=`expr $retry + 1`
  209.       echo "$SCREEN_NAME update failed... retry $retry/3..."
  210.       update $retry $relaunch
  211.     else
  212.       echo "$SCREEN_NAME update failed... exit..."
  213.       exit 1
  214.     fi
  215.   fi
  216.  
  217.   # Send e-mail
  218.   if [ ! -z "$UPDATE_EMAIL" ]; then cat $UPDATE_LOG | mail -s "$SCREEN_NAME update for $(hostname -f)" $UPDATE_EMAIL; fi
  219.  
  220.   if [ $relaunch = 1 ]
  221.   then
  222.     echo "Restart $SCREEN_NAME..."
  223.     start
  224.     sleep 5
  225.     echo "$SCREEN_NAME restarted successfully"
  226.   else
  227.     exit 1
  228.   fi
  229. }
  230.  
  231. function create {
  232.   # IP should never exist: RFC 5735 TEST-NET-2
  233.   if [ "$IP" = "198.51.100.0" ]
  234.   then
  235.     echo "ERROR: You must configure the script before you create a server."
  236.     exit 1
  237.   fi
  238.  
  239.   # If steamcmd already exists just install the server
  240.   if [ -e "$DIR_STEAMCMD/steamcmd.sh" ]
  241.   then
  242.     echo "steamcmd already exists..."
  243.     echo "Installing/Updating $SCREEN_NAME"
  244.     update
  245.     return
  246.   fi
  247.  
  248.   # Install steamcmd in the specified directory
  249.   if [ ! -d "$DIR_STEAMCMD" ]
  250.   then
  251.     echo "$DIR_STEAMCMD does not exist, creating..."
  252.     if [ `whoami` = "root" ]
  253.     then
  254.       su - $USER -c "mkdir -p $DIR_STEAMCMD"
  255.     else
  256.       mkdir -p $DIR_STEAMCMD
  257.     fi
  258.     if [ ! -d "$DIR_STEAMCMD" ]
  259.     then
  260.       echo "ERROR: Could not create $DIR_STEAMCMD"
  261.       exit 1
  262.     fi
  263.   fi
  264.  
  265.   # Download steamcmd
  266.   echo "Downloading steamcmd from http://media.steampowered.com/client/steamcmd_linux.tar.gz"
  267.   if [ `whoami` = "root" ]
  268.   then
  269.     su - $USER -c "cd $DIR_STEAMCMD ; wget http://media.steampowered.com/client/steamcmd_linux.tar.gz"
  270.   else
  271.     cd $DIR_STEAMCMD ; wget http://media.steampowered.com/client/steamcmd_linux.tar.gz
  272.   fi
  273.   if [ "$?" -ne "0" ]
  274.   then
  275.     echo "ERROR: Unable to download steamcmd"
  276.     exit 1
  277.   fi
  278.  
  279.   # Extract it
  280.   echo "Extracting and removing the archive"
  281.   if [ `whoami` = "root" ]
  282.   then
  283.     su - $USER -c "cd $DIR_STEAMCMD ; tar xzvf ./steamcmd_linux.tar.gz"
  284.     su - $USER -c "cd $DIR_STEAMCMD ; rm ./steamcmd_linux.tar.gz"
  285.   else
  286.     cd $DIR_STEAMCMD ; tar xzvf ./steamcmd_linux.tar.gz
  287.     cd $DIR_STEAMCMD ; rm ./steamcmd_linux.tar.gz
  288.   fi
  289.  
  290.   # Did it install?
  291.   if [ ! -e "$DIR_STEAMCMD/steamcmd.sh" ]
  292.   then
  293.     echo "ERROR: Failed to install steamcmd"
  294.     exit 1
  295.   fi
  296.  
  297.   # Run steamcmd for the first time to update it, telling it to quit when it is done
  298.   echo "Updating steamcmd"
  299.   if [ `whoami` = "root" ]
  300.   then
  301.   su - $USER -c "echo quit | $DIR_STEAMCMD/steamcmd.sh"
  302.   else
  303.     echo quit | $DIR_STEAMCMD/steamcmd.sh
  304.   fi
  305.  
  306.   # Done installing steamcmd, install the server
  307.   echo "Done installing steamcmd. Installing the game"
  308.   echo "This will take a while"
  309.   update
  310. }
  311.  
  312. function usage {
  313.   echo "Usage: service csgo-server-launcher {start|stop|status|restart|console|update|create}"
  314.   echo "On console, press CTRL+A then D to stop the screen without stopping the server."
  315. }
  316.  
  317. ### BEGIN ###
  318.  
  319. # Read config file
  320. if [ ! -f "$CONFIG_FILE" ]
  321. then
  322.   echo "ERROR: Config file $CONFIG_FILE not found..."
  323.   exit 1
  324. else
  325.   source "$CONFIG_FILE"
  326. fi
  327.  
  328. # Check required packages
  329. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  330. if [ ! -x `which awk` ]; then echo "ERROR: You need awk for this script (try apt-get install awk)"; exit 1; fi
  331. if [ ! -x `which screen` ]; then echo "ERROR: You need screen for this script (try apt-get install screen)"; exit 1; fi
  332. if [ ! -x `which wget` ]; then echo "ERROR: You need wget for this script (try apt-get install wget)"; exit 1; fi
  333. if [ ! -x `which tar` ]; then echo "ERROR: You need tar for this script (try apt-get install tar)"; exit 1; fi
  334.  
  335. case "$1" in
  336.  
  337.   start)
  338.     echo "Starting $SCREEN_NAME..."
  339.     start
  340.     sleep 5
  341.     echo "$SCREEN_NAME started successfully"
  342.   ;;
  343.  
  344.   stop)
  345.     echo "Stopping $SCREEN_NAME..."
  346.     stop
  347.     sleep 5
  348.     echo "$SCREEN_NAME stopped successfully"
  349.   ;;
  350.  
  351.   restart)
  352.     echo "Restarting $SCREEN_NAME..."
  353.     status && stop
  354.     sleep 5
  355.     start
  356.     sleep 5
  357.     echo "$SCREEN_NAME restarted successfully"
  358.   ;;
  359.  
  360.   status)
  361.     if status
  362.     then echo "$SCREEN_NAME is UP"
  363.     else echo "$SCREEN_NAME is DOWN"
  364.     fi
  365.   ;;
  366.  
  367.   console)
  368.     echo "Open console on $SCREEN_NAME..."
  369.     console
  370.   ;;
  371.  
  372.   update)
  373.     echo "Updating $SCREEN_NAME..."
  374.     update
  375.   ;;
  376.  
  377.   create)
  378.     echo "Creating $SCREEN_NAME..."
  379.     create
  380.   ;;
  381.  
  382.   *)
  383.     usage
  384.     exit 1
  385.   ;;
  386.  
  387. esac
  388.  
  389. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement