fofserver 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. #!/bin/bash
  2. # Fistful Of Frags
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://danielgibbs.co.uk
  6. # Version: 170914
  7. #### Variables ####
  8. # Notification Email
  9. # (on|off)
  10. emailnotification="off"
  11. email="email@example.com"
  12. # Steam login
  13. steamuser="anonymous"
  14. steampass=""
  15. # Start Variables
  16. defaultmap="fof_depot"
  17. maxplayers="16"
  18. port="27015"
  19. sourcetvport="27020"
  20. clientport="27005"
  21. ip="0.0.0.0"
  22. # https://developer.valvesoftware.com/wiki/Command_Line_Options#Source_Dedicated_Server
  23. fn_parms(){
  24. parms="-game fof -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +tv_port ${sourcetvport} +map ${defaultmap} +servercfgfile ${servercfg} -maxplayers ${maxplayers}"
  25. }
  26. #### Advanced Variables ####
  27. # Steam
  28. appid="295230"
  29. # Server Details
  30. servicename="fof-server"
  31. gamename="Fistful of Frags"
  32. engine="source"
  33. # Directories
  34. rootdir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
  35. selfname="$0"
  36. lockselfname=$(echo ".${servicename}.lock")
  37. filesdir="${rootdir}/serverfiles"
  38. systemdir="${filesdir}/fof"
  39. executabledir="${filesdir}"
  40. executable="./srcds_run"
  41. servercfgdir="${systemdir}/cfg"
  42. servercfg="${servicename}.cfg"
  43. servercfgfullpath="${servercfgdir}/${servercfg}"
  44. defaultcfg="${servercfgdir}/server.cfg"
  45. backupdir="backups"
  46. steamclient="${rootdir}/steamcmd/linux32/steamclient.so"
  47. # Server Details
  48. servername=$(grep -s hostname "${servercfgfullpath}"|sed 's/hostname //g'|sed 's/"//g')
  49. rcon=$(grep -s rcon_password "${servercfgfullpath}"|sed 's/rcon_password //g'|sed 's/"//g')
  50. # Logging
  51. logdays="7"
  52. gamelogdir="${systemdir}/logs"
  53. scriptlogdir="${rootdir}/log/script"
  54. consolelogdir="${rootdir}/log/console"
  55. scriptlog="${scriptlogdir}/${servicename}-script.log"
  56. consolelog="${consolelogdir}/${servicename}-console.log"
  57. emaillog="${scriptlogdir}/${servicename}-email.log"
  58. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  59. consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
  60. ##### Script #####
  61. # Do not edit
  62. # unless you know
  63. # what you are doing
  64. fn_scriptlog(){
  65. echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: '$1'" >> ${scriptlog}
  66. }
  67. # [ FAIL ]
  68. fn_printfail(){
  69. echo -en "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  70. }
  71. fn_printfailnl(){
  72. echo -e "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  73. }
  74. fn_printok(){
  75. echo -en "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  76. }
  77. # [ OK ]
  78. fn_printoknl(){
  79. echo -e "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  80. }
  81. fn_printinfo(){
  82. echo -en "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  83. }
  84. fn_printinfonl(){
  85. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  86. }
  87. # [ INFO ]
  88. fn_printokinfonl(){
  89. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  90. }
  91. fn_printwarn(){
  92. echo -en "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  93. }
  94. fn_printwarnnl(){
  95. echo -e "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  96. }
  97. # [ .... ]
  98. fn_printdots(){
  99. echo -en "\r\033[K[ .... ] $@"
  100. }
  101. fn_rootcheck(){
  102. if [ `whoami` = "root" ]; then
  103. fn_printfailnl "Script will not run as root!"
  104. exit
  105. fi
  106. }
  107. fn_syscheck(){
  108. if [ ! -e "${systemdir}" ]; then
  109. fn_printfailnl "Cannot access ${systemdir}: No such directory"
  110. exit
  111. fi
  112. }
  113. fn_autoip(){
  114. # Identifies the server interface IP
  115. # If multiple interfaces this will need to be set manually
  116. getip=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -v 127.0.0.1)
  117. getipwc=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -v 127.0.0.1|wc -l)
  118. if [ "${ip}" == "0.0.0.0" ]||[ "${ip}" == "" ]; then
  119. if [ "${getipwc}" -ge "2" ]; then
  120. fn_printwarn "Multiple active network interfaces.\n\n"
  121. echo -en "Manually specify the IP you want to use within the ${selfname} script.\n"
  122. echo -en "Set ip=\"0.0.0.0\" to one of the following:\n"
  123. echo -en "${getip}\n"
  124. exit
  125. else
  126. ip=${getip}
  127. fi
  128. fi
  129. }
  130. fn_logmanager(){
  131. if [ ! -e "${consolelog}" ]; then
  132. touch "${consolelog}"
  133. fi
  134. # log manager will active if finds logs older than ${logdays}
  135. if [ `find "${scriptlogdir}"/* -mtime +${logdays}|wc -l` -ne "0" ]; then
  136. fn_printdots "Starting log cleaner"
  137. sleep 1
  138. fn_printok "Starting log cleaner"
  139. sleep 1
  140. fn_scriptlog "Starting log cleaner"
  141. sleep 1
  142. echo -en "\n"
  143. fn_printinfo "Removing logs older than ${logdays} days"
  144. sleep 1
  145. echo -en "\n"
  146. fn_scriptlog "Removing logs older than ${logdays} days"
  147. sleep 1
  148. find "${scriptlogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  149. find "${consolelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  150. scriptcount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l)
  151. consolecount=$(find "${consolelogdir}"/* -mtime +${logdays}|wc -l)
  152. count=$((${scriptcount} + ${consolecount}))
  153. find "${scriptlogdir}"/* -mtime +${logdays} -exec rm {} \;
  154. find "${consolelogdir}"/* -mtime +${logdays} -exec rm {} \;
  155. fn_printok "Log cleaner removed ${count} log files"
  156. sleep 1
  157. echo -en "\n"
  158. fn_scriptlog "Log cleaner removed ${count} log files"
  159. fi
  160. }
  161. fn_debugserver(){
  162. fn_rootcheck
  163. fn_syscheck
  164. fn_autoip
  165. fn_distro
  166. fn_uptime
  167. fn_load
  168. fn_parms
  169. echo ""
  170. echo "${gamename} Debug"
  171. echo "============================"
  172. echo ""
  173. echo -e "Distro: ${os}"
  174. echo -e "Arch: ${arch}"
  175. echo -e "Kernel: ${kernel}"
  176. echo -e "Hostname: $HOSTNAME"
  177. echo ""
  178. echo "Start parameters:"
  179. echo ${parms}
  180. echo ""
  181. echo -e "Use for identifying server issues only!"
  182. echo -e "Press CTRL+c to drop out of debug mode"
  183. echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
  184. echo ""
  185. echo "Start parameters:"
  186. echo ${parms}
  187. echo ""
  188. while true; do
  189. read -p "Continue? [y/N]" yn
  190. case $yn in
  191. [Yy]* ) break;;
  192. [Nn]* ) echo Exiting; return 1;;
  193. * ) echo "Please answer yes or no.";;
  194. esac
  195. done
  196. fn_stopserver
  197. fn_printdots "Starting debug mode ${servicename}: ${servername}"
  198. sleep 1
  199. fn_printok "Starting debug mode ${servicename}: ${servername}"
  200. sleep 1
  201. fn_scriptlog "Started debug mode ${servername}"
  202. echo -en "\n"
  203. cd "${executabledir}"
  204. ${executable} ${parms} -debug
  205. }
  206. fn_console(){
  207. fn_rootcheck
  208. fn_syscheck
  209. echo ""
  210. echo "${gamename} Console"
  211. echo "============================"
  212. echo ""
  213. echo "Press \"CTRL+b d\" to exit console"
  214. echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
  215. echo ""
  216. while true; do
  217. read -p "Continue? [y/N]" yn
  218. case $yn in
  219. [Yy]* ) break;;
  220. [Nn]* ) echo Exiting; return 1;;
  221. * ) echo "Please answer yes or no.";;
  222. esac
  223. done
  224. fn_printdots "Starting ${servicename} console"
  225. sleep 1
  226. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  227. if [ ${tmuxwc} -eq 1 ]; then
  228. fn_printoknl "Starting ${servicename} console"
  229. sleep 1
  230. fn_scriptlog "Console accessed"
  231. tmux attach-session -t ${servicename}
  232. else
  233. fn_printfailnl "Starting ${servicename} console: ${servername} not running"
  234. sleep 1
  235. while true; do
  236. read -p "Do you want to start the server? [y/N]" yn
  237. case $yn in
  238. [Yy]* ) fn_startserver; break;;
  239. [Nn]* ) break;;
  240. * ) echo "Please answer yes or no.";;
  241. esac
  242. done
  243. fi
  244. }
  245. fn_backupserver(){
  246. fn_rootcheck
  247. fn_syscheck
  248. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  249. echo ""
  250. echo "${gamename} Backup"
  251. echo "============================"
  252. echo ""
  253. echo "The following backup will be created."
  254. echo ""
  255. echo "${backupdir}/${backupname}.tar.gz"
  256. echo ""
  257. while true; do
  258. read -p "Continue? [y/N]" yn
  259. case $yn in
  260. [Yy]* ) break;;
  261. [Nn]* ) echo Exiting; return 1;;
  262. * ) echo "Please answer yes or no.";;
  263. esac
  264. done
  265. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  266. if [ ${tmuxwc} -eq 1 ]; then
  267. echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
  268. while true; do
  269. read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
  270. case $yn in
  271. [Yy]* ) fn_stopserver; break;;
  272. [Nn]* ) break;;
  273. * ) echo "Please answer yes or no.";;
  274. esac
  275. done
  276. fi
  277. fn_printdots "Starting backup ${servicename}: ${servername}"
  278. sleep 1
  279. fn_printok "Starting backup ${servicename}: ${servername}"
  280. sleep 1
  281. fn_scriptlog "Backup started"
  282. echo -en "\n"
  283. cd "${rootdir}"
  284. mkdir -pv "${backupdir}" > /dev/null 2>&1
  285. tar -cvzf "${backupdir}/${backupname}.tar.gz" --exclude "${backupdir}" *
  286. echo -en "\r\033[K${servicename} Backup complete"
  287. fn_scriptlog "Backup complete"
  288. }
  289. fn_distro(){
  290. arch=$(uname -m)
  291. kernel=$(uname -r)
  292. if [ -f /etc/lsb-release ]; then
  293. os=$(lsb_release -s -d)
  294. elif [ -f /etc/debian_version ]; then
  295. os="Debian $(cat /etc/debian_version)"
  296. elif [ -f /etc/redhat-release ]; then
  297. os=$(cat /etc/redhat-release)
  298. else
  299. os="$(uname -s) $(uname -r)"
  300. fi
  301. }
  302. fn_uptime(){
  303. uptime=$(</proc/uptime)
  304. uptime=${uptime%%.*}
  305. minutes=$(( uptime/60%60 ))
  306. hours=$(( uptime/60/60%24 ))
  307. days=$(( uptime/60/60/24 ))
  308. }
  309. fn_load(){
  310. load=$(uptime|awk -F 'load average' '{ print $2 }')
  311. }
  312. fn_emailnotification(){
  313. fn_distro
  314. fn_uptime
  315. fn_load
  316. {
  317. echo -e "========================================\n${servicename} details\n========================================\n"
  318. echo -e "Service: ${servicename}"
  319. echo -e "Server: ${servername}"
  320. echo -e "Game: ${gamename}"
  321. echo -e "Failure reason: ${failurereason}"
  322. echo -e "Action Taken: ${actiontaken}\n"
  323. echo -e "========================================\nServer details\n========================================\n"
  324. echo -e "Date: $(date)"
  325. echo -e "Distro: ${os}"
  326. echo -e "Arch: ${arch}"
  327. echo -e "Kernel: ${kernel}"
  328. echo -e "Hostname: $HOSTNAME"
  329. echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
  330. echo -e "Avg Load${load}\n"
  331. echo -e "========================================\nLogs\n========================================\n"
  332. echo -e "Script log\n===================\n"
  333. }|tee "${scriptlogdir}/${servicename}-email.log" > /dev/null 2>&1
  334. tail -25 "${scriptlog}" >> "${emaillog}"
  335. if [ ! -z "${consolelog}" ]; then
  336. echo -e "\n\nConsole log\n====================\n" >> "${emaillog}"
  337. tail -25 "${consolelog}" >> "${emaillog}"
  338. fi
  339. if [ ! -z "${gamelogdir}" ]; then
  340. echo -e "\n\nServer log\n====================\n" >> "${emaillog}"
  341. tail "${gamelogdir}"/*|grep -v "==>"|sed '/^$/d'|tail -25 >> "${emaillog}"
  342. fi
  343. mail -s "${subject}" ${email} < "${emaillog}"
  344. fn_printinfo "Sent email notification to ${email}"
  345. sleep 1
  346. echo -en "\n"
  347. fn_scriptlog "Sent email notification to ${email}"
  348. }
  349. fn_emailtest(){
  350. fn_rootcheck
  351. fn_syscheck
  352. fn_scriptlog "Emailing test notification"
  353. if [ "${emailnotification}" = "on" ]; then
  354. subject="${servicename} Email Test Notification - Testing ${servername}"
  355. failurereason="Testing ${servicename} email notification"
  356. actiontaken="Sent test email...hello is this thing on?"
  357. fn_emailnotification
  358. else
  359. fn_printfailnl "Email notification not enabled"
  360. fn_scriptlog "Email notification not enabled"
  361. fi
  362. sleep 1
  363. echo -en "\n"
  364. }
  365. fn_serverquery(){
  366. # uses gsquery.py to directly query the server
  367. # detects if the server locks up
  368. if [ -f gsquery.py ]; then
  369. if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
  370. gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
  371. port=$((${gameport} + 1))
  372. elif [ "${engine}" == "spark" ]; then
  373. port=$((${port} + 1))
  374. fi
  375. fn_printinfo "Monitoring ${servicename}: Detected gsquery.py"
  376. sleep 1
  377. fn_scriptlog "Detected gsquery.py"
  378. fn_printdots "Monitoring ${servicename}: Querying port: ${ip}:${port}: QUERYING"
  379. sleep 1
  380. fn_scriptlog "Querying port: ${ip}:${port}: QUERYING"
  381. serverquery=$(./gsquery.py -a ${ip} -p ${port} -e ${engine} 2>&1)
  382. exitcode=$?
  383. if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then
  384. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ${serverquery}"
  385. sleep 1
  386. echo -en "\n"
  387. fn_scriptlog "Querying port: ${ip}:${port}: ${serverquery}"
  388. if [[ -z "${secondquery}" ]]; then
  389. fn_printinfo "Monitoring ${servicename}: Waiting 30 seconds to re-query"
  390. sleep 1
  391. fn_scriptlog "Waiting 30 seconds to re-query"
  392. sleep 29
  393. secondquery=1
  394. fn_serverquery
  395. fi
  396. if [ "${emailnotification}" = "on" ]; then
  397. subject="${servicename} Monitor - Starting ${servername}"
  398. failurereason="Failed to query ${servicename}: ${serverquery}"
  399. actiontaken="restarted ${servicename}"
  400. fn_emailnotification
  401. fi
  402. fn_restartserver
  403. exit
  404. elif [ "${exitcode}" == "0" ]; then
  405. fn_printok "Monitoring ${servicename}: Querying port: ${ip}:${port}: OK"
  406. sleep 1
  407. fn_scriptlog "Querying port: ${ip}:${port}: OK"
  408. echo -en "\n"
  409. exit
  410. elif [ "${exitcode}" == "126" ]; then
  411. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ERROR: ./gsquery.py: Permission denied"
  412. sleep 1
  413. fn_scriptlog "Querying port: ${ip}:${port}: ./gsquery.py: Permission denied"
  414. echo -en "\n"
  415. echo "Attempting to resolve automatically"
  416. chmod +x -v gsquery.py
  417. exitcode=$?
  418. if [ "${exitcode}" == "0" ]; then
  419. fn_serverquery
  420. else
  421. echo -en "\nUnable to resolve automatically. Please manually fix permissions\n"
  422. owner=$(ls -al gsquery.py|awk '{ print $3 }')
  423. echo "As user ${owner} or root run the following command"
  424. whoami=$(whoami)
  425. echo -en "\nchown ${whoami}:${whoami} gsquery.py\n\n"
  426. exit
  427. fi
  428. else
  429. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: UNKNOWN ERROR"
  430. sleep 1
  431. echo -en "\n"
  432. fn_scriptlog "Querying port: ${ip}:${port}: UNKNOWN ERROR"
  433. ./gsquery.py -a ${ip} -p ${port} -e ${engine}
  434. exit
  435. fi
  436. fi
  437. }
  438. fn_monitorserver(){
  439. fn_rootcheck
  440. fn_syscheck
  441. fn_autoip
  442. fn_printdots "Monitoring ${servicename}: ${servername}"
  443. sleep 1
  444. if [ ! -f ${lockselfname} ]; then
  445. fn_printinfo "Monitoring ${servicename}: No lock file found: Monitor disabled"
  446. sleep 1
  447. echo -en "\n"
  448. echo "To enable monitor run ${selfname} start"
  449. exit
  450. fi
  451. fn_scriptlog "Monitoring ${servername}"
  452. updatecheck=$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l)
  453. if [ "${updatecheck}" = "0" ]; then
  454. fn_printdots "Monitoring ${servicename}: Checking session: CHECKING"
  455. sleep 1
  456. fn_scriptlog "Checking session: CHECKING"
  457. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  458. if [ ${tmuxwc} -eq 1 ]; then
  459. fn_printok "Monitoring ${servicename}: Checking session: OK"
  460. sleep 1
  461. echo -en "\n"
  462. fn_scriptlog "Checking session: OK"
  463. fn_serverquery
  464. exit
  465. else
  466. fn_printfail "Monitoring ${servicename}: Checking session: FAIL"
  467. fn_scriptlog "Checking session: FAIL"
  468. sleep 1
  469. echo -en "\n"
  470. if [ "${emailnotification}" = "on" ]; then
  471. subject="${servicename} Monitor - Starting ${servername}"
  472. failurereason="${servicename} process not running"
  473. actiontaken="${servicename} has been restarted"
  474. fn_emailnotification
  475. fi
  476. fn_scriptlog "Monitor is starting ${servername}"
  477. fn_startserver
  478. fi
  479. else
  480. fn_printinfonl "Monitoring ${servicename}: Detected SteamCMD is checking for updates"
  481. sleep 1
  482. fn_scriptlog "Detected SteamCMD is checking for updates"
  483. fn_printinfonl "Monitoring ${servicename}: When updates complete ${servicename} will start"
  484. sleep 1
  485. fn_scriptlog "When updates complete ${servicename} will start"
  486. fi
  487. }
  488. fn_updateserver(){
  489. fn_rootcheck
  490. fn_syscheck
  491. fn_printdots "Updating ${servicename}: ${servername}"
  492. sleep 1
  493. fn_printok "Updating ${servicename}: ${servername}"
  494. sleep 1
  495. fn_scriptlog "Updating ${servername}"
  496. cd "${rootdir}"
  497. cd "steamcmd"
  498. ./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir "${filesdir}" +app_update ${appid} +quit|tee -a "${scriptlog}"
  499. }
  500. fn_validateserver(){
  501. fn_rootcheck
  502. fn_syscheck
  503. fn_printwarn "Validating may overwrite some customised files."
  504. sleep 1
  505. echo -en "\n"
  506. echo -en "https://developer.valvesoftware.com/wiki/SteamCMD#Validate"
  507. sleep 5
  508. echo -en "\n"
  509. fn_printdots "Validating ${servicename}: ${servername}"
  510. sleep 1
  511. fn_printok "Validating ${servicename}: ${servername}"
  512. sleep 1
  513. fn_scriptlog "Validating ${servername}"
  514. cd "${rootdir}"
  515. cd "steamcmd"
  516. ./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir "${filesdir}" +app_update ${appid} validate +quit|tee -a "${scriptlog}"
  517. }
  518. fn_restartserver(){
  519. fn_scriptlog "Restarting ${servername}"
  520. fn_stopserver
  521. fn_startserver
  522. }
  523. fn_stopserver(){
  524. fn_rootcheck
  525. fn_syscheck
  526. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  527. fn_printdots "Stopping ${servicename}: ${servername}"
  528. sleep 1
  529. fn_scriptlog "Stopping ${servername}"
  530. if [ "${pid}" == "0" ]; then
  531. fn_printfail "Stopping ${servicename}: ${servername} is already stopped"
  532. fn_scriptlog "${servername} is already stopped"
  533. else
  534. tmux kill-session -t ${servicename}
  535. fn_printok "Stopping ${servicename}: ${servername}"
  536. fn_scriptlog "Stopped ${servername}"
  537. fi
  538. # Remove lock file
  539. rm -f ${lockselfname}
  540. sleep 1
  541. echo -en "\n"
  542. }
  543. fn_startserver(){
  544. fn_rootcheck
  545. fn_syscheck
  546. fn_autoip
  547. fn_parms
  548. fn_logmanager
  549. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  550. if [ ${tmuxwc} -eq 0 ]; then
  551. mv "${scriptlog}" "${scriptlogdate}"
  552. mv "${consolelog}" "${consolelogdate}"
  553. fi
  554. fn_printdots "Starting ${servicename}: ${servername}"
  555. sleep 1
  556. fn_scriptlog "Starting ${servername}"
  557. if [ ${tmuxwc} -eq 1 ]; then
  558. fn_printinfo "Starting ${servicename}: ${servername} is already running"
  559. sleep 1
  560. echo -en "\n"
  561. fn_scriptlog "${servername} is already running"
  562. exit
  563. fi
  564. # Create lock file
  565. date > ${lockselfname}
  566. cd "${executabledir}"
  567. tmux new-session -d -s ${servicename} "${executable} ${parms}"
  568. tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'"
  569. sleep 1
  570. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  571. if [ ${tmuxwc} -eq 0 ]; then
  572. fn_printfail "Starting ${servicename}: Failed to start ${servername}"
  573. fn_scriptlog "failed to start ${servername}"
  574. else
  575. fn_printok "Starting ${servicename}: ${servername}"
  576. fn_scriptlog "Started ${servername}"
  577. fi
  578. sleep 1
  579. echo -en "\n"
  580. }
  581. fn_details(){
  582. fn_autoip
  583. servername=$(grep -s hostname "${servercfgfullpath}"|sed 's/hostname //g'|sed 's/"//g')
  584. rcon=$(grep -s rcon_password "${servercfgfullpath}"|sed 's/rcon_password //g'|sed 's/"//g')
  585. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  586. echo ""
  587. echo "${gamename} Server Details"
  588. echo "============================"
  589. echo "Server name: ${servername}"
  590. echo "Server IP: ${ip}:${port}"
  591. echo "RCON password: ${rcon}"
  592. echo "Config file: ${servercfgfullpath}"
  593. echo ""
  594. echo "${servername} Ports"
  595. echo "============================"
  596. echo "Ports the server is currently using."
  597. echo ""
  598. echo "DIRECTION DESCRIPTION PORT"
  599. echo "INBOUND Game/RCON port ${port}"
  600. if [ ! -z ${sourcetvport} ]; then
  601. echo "INBOUND SourceTV port ${sourcetvport}"
  602. fi
  603. echo "OUTBOUND Client port ${clientport}"
  604. echo ""
  605. echo "You can change ports by editing the"
  606. echo "start parameters in ${selfname}."
  607. echo ""
  608. if [ "${pid}" == "0" ]; then
  609. echo -e "Status:\e[0;31m OFFLINE\e[0;39m"
  610. else
  611. echo -e "Status:\e[0;32m ONLINE\e[0;39m"
  612. fi
  613. echo ""
  614. }
  615. #
  616. ## Installer
  617. #
  618. fn_fofdeps(){
  619. echo "Downloading libm.so.6"
  620. echo "================================="
  621. cd "${filesdir}"
  622. wget -nv https://github.com/dgibbs64/linuxgameservers/raw/master/FistfulOfFrags/dependencies/libm.so.6
  623. sleep 1
  624. echo ""
  625. }
  626. fn_header(){
  627. clear
  628. echo "================================="
  629. echo "${gamename}"
  630. echo "Linux Game Server Manager"
  631. echo "by Daniel Gibbs"
  632. echo "http://danielgibbs.co.uk"
  633. echo "================================="
  634. echo ""
  635. }
  636. fn_steamdl(){
  637. echo "Installing SteamCMD"
  638. echo "================================="
  639. cd "${rootdir}"
  640. mkdir -pv "steamcmd"
  641. sleep 1
  642. cd "steamcmd"
  643. if [ ! -f steamcmd.sh ]; then
  644. wget http://media.steampowered.com/client/steamcmd_linux.tar.gz
  645. tar --verbose -zxf steamcmd_linux.tar.gz
  646. rm -v steamcmd_linux.tar.gz
  647. chmod +x steamcmd.sh
  648. sleep 1
  649. else
  650. echo ""
  651. echo "Steam already installed!"
  652. fi
  653. sleep 1
  654. echo ""
  655. }
  656. fn_steaminstall(){
  657. echo "Installing ${gamename} Server"
  658. echo "================================="
  659. sleep 1
  660. mkdir -pv "${filesdir}"
  661. cd "${rootdir}/steamcmd"
  662. STEAMEXE=steamcmd ./steamcmd.sh +login ${steamuser} ${steampass} +force_install_dir "${filesdir}" +app_update ${appid} validate +quit
  663. echo ""
  664. echo "================================="
  665. while true; do
  666. read -p "Was the install successful? [y/N]" yn
  667. case $yn in
  668. [Yy]* ) break;;
  669. [Nn]* ) fn_retryinstall;;
  670. * ) echo "Please answer yes or no.";;
  671. esac
  672. done
  673. echo ""
  674. }
  675. fn_steamfix(){
  676. echo "Applying steamclient.so fix"
  677. echo "================================="
  678. sleep 1
  679. mkdir -pv "${HOME}/.steam"
  680. mkdir -pv "${HOME}/.steam/sdk32"
  681. cp -v "${steamclient}" "${HOME}/.steam/sdk32/steamclient.so"
  682. sleep 1
  683. echo ""
  684. }
  685. fn_loginstall(){
  686. echo "Creating log directorys"
  687. echo "================================="
  688. sleep 1
  689. mkdir -pv "${rootdir}/log"
  690. mkdir -pv "${scriptlogdir}"
  691. touch "${scriptlog}"
  692. mkdir -pv "${consolelogdir}"
  693. touch "${consolelog}"
  694. if [ ! -h ${rootdir}/log/server ]; then
  695. ln -sv "${gamelogdir}" "${rootdir}/log/server"
  696. else
  697. echo "Symbolic link ${gamelogdir} => ${rootdir}/log/server already exists!"
  698. fi
  699. sleep 1
  700. echo ""
  701. }
  702. fn_getquery(){
  703. echo "GameServerQuery"
  704. echo "============================"
  705. while true; do
  706. read -p "Do you want to install GameServerQuery? [y/N]" yn
  707. case $yn in
  708. [Yy]* ) wget -nv "http://danielgibbs.co.uk/dl/gsquery.py"; chmod +x gsquery.py;break;;
  709. [Nn]* ) echo -e "Not installing GameServerQuery.";break;;
  710. * ) echo "Please answer yes or no.";;
  711. esac
  712. done
  713. echo ""
  714. }
  715. fn_retryinstall(){
  716. while true; do
  717. read -p "Retry install? [y/N]" yn
  718. case $yn in
  719. [Yy]* ) fn_install; exit;;
  720. [Nn]* ) echo Exiting; exit;;
  721. * ) echo "Please answer yes or no.";;
  722. esac
  723. done
  724. }
  725. fn_install(){
  726. fn_rootcheck
  727. fn_header
  728. if [ -d "${filesdir}" ]; then
  729. echo "${gamename} Server is already installed here:"
  730. pwd
  731. echo ""
  732. while true; do
  733. read -p "Continue [y/N]" yn
  734. case $yn in
  735. [Yy]* ) fn_header; break;;
  736. [Nn]* ) echo Exiting; return 1;;
  737. * ) echo "Please answer yes or no.";;
  738. esac
  739. done
  740. fi
  741. echo "Install Directory:"
  742. pwd
  743. echo ""
  744. while true; do
  745. read -p "Continue [y/N]" yn
  746. case $yn in
  747. [Yy]* ) break;;
  748. [Nn]* ) echo Exiting; return 1;;
  749. * ) echo "Please answer yes or no.";;
  750. esac
  751. done
  752. fn_header
  753. fn_steamdl
  754. fn_steaminstall
  755. fn_steamfix
  756. fn_loginstall
  757. fn_getquery
  758. fn_fofdeps
  759. echo "Configuring ${gamename} Server"
  760. echo "================================="
  761. sleep 1
  762. read -p "Enter server name: " servername
  763. read -p "Enter rcon password: " rconpass
  764. sleep 1
  765. echo "Creating server.cfg."
  766. touch "${defaultcfg}"
  767. echo "exec ${servicename}.cfg" > "${defaultcfg}"
  768. sleep 1
  769. echo "Creating ${servicename}.cfg config file."
  770. touch "${servercfgfullpath}"
  771. {
  772. echo -e "// server name"
  773. echo -e "hostname \"${servername}\""
  774. echo -e ""
  775. echo -e "// rcon passsword"
  776. echo -e "rcon_password \"${rconpass}\""
  777. echo -e ""
  778. echo -e "// Server password"
  779. echo -e "sv_password \"\""
  780. echo -e ""
  781. echo -e "// server logging"
  782. echo -e "log on"
  783. echo -e "sv_logbans 1"
  784. echo -e "sv_logecho 1"
  785. echo -e "sv_logfile 1"
  786. echo -e "sv_log_onefile 0"
  787. echo -e "//Fistful of Frags"
  788. echo -e ""
  789. echo -e "//// Game Modes"
  790. echo -e "// 1 = Shootout"
  791. echo -e "// 2 = Teamplay/Objectives"
  792. echo -e ""
  793. echo -e "fof_sv_currentmode 1 "
  794. echo -e ""
  795. echo -e ""
  796. echo -e "//// Teamplay"
  797. echo -e "// 0 = Free-for-all"
  798. echo -e "// 1 = Team Deathmatch"
  799. echo -e ""
  800. echo -e "mp_teamplay 0"
  801. echo -e ""
  802. echo -e ""
  803. echo -e "//// Friendly Fire"
  804. echo -e "// 0 = Friendly fire disabled"
  805. echo -e "// 1 = Friendly fire enabled"
  806. echo -e ""
  807. echo -e "mp_friendlyfire 0"
  808. echo -e ""
  809. echo -e ""
  810. echo -e "//// Team numbers"
  811. echo -e "// 2 = Vigilantes & Desperados"
  812. echo -e "// 3 = Vigilantes, Desperados & Bandidos"
  813. echo -e "// 4 = Vigilantes, Desperados, Bandidos & Rangers"
  814. echo -e ""
  815. echo -e "fof_sv_maxteams 4"
  816. echo -e ""
  817. echo -e "//// Scrable Teams"
  818. echo -e "// Teams get scrambled when 75% of kills come from same team"
  819. echo -e "fof_sv_scrambleteams 1"
  820. echo -e "fof_sv_scrambleteams_maxkillratio 0.75"
  821. }|tee "${servercfgfullpath}" > /dev/null 2>&1
  822. sleep 1
  823. echo ""
  824. fn_header
  825. sleep 1
  826. fn_details
  827. sleep 1
  828. echo "================================="
  829. echo "Install Complete!"
  830. echo ""
  831. echo "To start server type:"
  832. echo "${selfname} start"
  833. echo ""
  834. }
  835. case "$1" in
  836. start)
  837. fn_startserver;;
  838. stop)
  839. fn_stopserver;;
  840. restart)
  841. fn_restartserver;;
  842. update)
  843. fn_updateserver;;
  844. update-restart)
  845. fn_stopserver
  846. fn_updateserver
  847. fn_startserver;;
  848. validate)
  849. fn_validateserver;;
  850. validate-restart)
  851. fn_stopserver
  852. fn_validateserver
  853. fn_startserver;;
  854. monitor)
  855. fn_monitorserver;;
  856. email-test)
  857. fn_emailtest;;
  858. details)
  859. fn_details;;
  860. backup)
  861. fn_backupserver;;
  862. console)
  863. fn_console;;
  864. debug)
  865. fn_debugserver;;
  866. install)
  867. fn_install;;
  868. *)
  869. echo "Usage: $0 {start|stop|restart|update|update-restart|validate|validate-restart|monitor|email-test|details|backup|console|debug|install}"
  870. exit 1;;
  871. esac
  872. exit