tests_jc2server.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #!/bin/bash
  2. # TravisCI Tests
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://gameservermanagers.com
  6. version="071115"
  7. #### Variables ####
  8. # Notification Email
  9. # (on|off)
  10. emailnotification="on"
  11. email="me@danielgibbs.co.uk"
  12. # Steam login
  13. steamuser="anonymous"
  14. steampass=""
  15. # Start Variables
  16. updateonstart="off"
  17. fn_parms(){
  18. parms=""
  19. }
  20. #### Advanced Variables ####
  21. # Steam
  22. appid="261140"
  23. # Server Details
  24. servicename="jc2-server"
  25. gamename="Just Cause 2"
  26. engine="avalanche"
  27. # Directories
  28. rootdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  29. selfname="$(basename $0)"
  30. lockselfname=".${servicename}.lock"
  31. filesdir="${rootdir}/serverfiles"
  32. systemdir="${filesdir}"
  33. executabledir="${filesdir}"
  34. executable="./Jcmp-Server"
  35. servercfg="config.lua"
  36. servercfgdir="${filesdir}"
  37. servercfgfullpath="${servercfgdir}/${servercfg}"
  38. servercfgdefault="${servercfgdir}/default_config.lua"
  39. backupdir="${rootdir}/backups"
  40. # Logging
  41. logdays="7"
  42. #gamelogdir="" # No server logs available
  43. scriptlogdir="${rootdir}/log/script"
  44. consolelogdir="${rootdir}/log/console"
  45. scriptlog="${scriptlogdir}/${servicename}-script.log"
  46. consolelog="${consolelogdir}/${servicename}-console.log"
  47. emaillog="${scriptlogdir}/${servicename}-email.log"
  48. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  49. consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
  50. ##### Script #####
  51. # Do not edit
  52. fn_runfunction(){
  53. # Functions are downloaded and run with this function
  54. if [ ! -f "${rootdir}/functions/${functionfile}" ]; then
  55. cd "${rootdir}"
  56. if [ ! -d "functions" ]; then
  57. mkdir functions
  58. fi
  59. cd functions
  60. echo -e " loading ${functionfile}...\c"
  61. wget -N /dev/null https://raw.githubusercontent.com/dgibbs64/linuxgsm/master/functions/${functionfile} 2>&1 | grep -F HTTP | cut -c45-
  62. chmod +x "${functionfile}"
  63. cd "${rootdir}"
  64. fi
  65. source "${rootdir}/functions/${functionfile}"
  66. }
  67. fn_functions(){
  68. # Functions are defined in fn_functions.
  69. functionfile="${FUNCNAME}"
  70. fn_runfunction
  71. }
  72. fn_functions
  73. fn_currentstatus_tmux(){
  74. pid=$(tmux list-sessions 2>&1 | awk '{print $1}' | grep -Ec "^${servicename}:")
  75. if [ "${pid}" != "0" ]; then
  76. currentstatus="ONLINE"
  77. else
  78. currentstatus="OFFLINE"
  79. fi
  80. }
  81. fn_currentstatus_ts3(){
  82. ts3status=$(${executable} status servercfgfullpathfile=${servercfgfullpath})
  83. if [ "${ts3status}" == "Server is running" ]; then
  84. currentstatus="ONLINE"
  85. else
  86. currentstatus="OFFLINE"
  87. fi
  88. }
  89. fn_setstatus(){
  90. fn_currentstatus_tmux
  91. echo""
  92. echo "Required status: ${requiredstatus}"
  93. counter=0
  94. echo "Current status: ${currentstatus}"
  95. while [ "${requiredstatus}" != "${currentstatus}" ]; do
  96. counter=$((counter+1))
  97. fn_currentstatus_tmux
  98. echo -ne "New status: ${currentstatus}\\r"
  99. if [ "${requiredstatus}" == "ONLINE" ]; then
  100. (fn_start > /dev/null 2>&1)
  101. else
  102. (fn_stop > /dev/null 2>&1)
  103. fi
  104. if [ "${counter}" -gt "5" ]; then
  105. currentstatus="FAIL"
  106. echo "Current status: ${currentstatus}"
  107. echo ""
  108. echo "Unable to start or stop server."
  109. exit 1
  110. fi
  111. done
  112. echo -ne "New status: ${currentstatus}\\r"
  113. echo -e "\n"
  114. echo "Test starting:"
  115. echo ""
  116. sleep 0.5
  117. }
  118. echo "================================="
  119. echo "TravisCI Tests"
  120. echo "Linux Game Server Manager"
  121. echo "by Daniel Gibbs"
  122. echo "http://gameservermanagers.com"
  123. echo "================================="
  124. echo ""
  125. sleep 1
  126. echo "================================="
  127. echo "Server Tests"
  128. echo "Using: ${gamename}"
  129. echo "================================="
  130. echo ""
  131. sleep 1
  132. mkdir ${rootdir}
  133. echo "1.0 - start - no files"
  134. echo "================================="
  135. echo "Description:"
  136. echo "test script reaction to missing server files."
  137. echo ""
  138. (fn_start)
  139. echo ""
  140. echo "Test complete!"
  141. sleep 1
  142. echo ""
  143. echo "1.1 - getopt"
  144. echo "================================="
  145. echo "Description:"
  146. echo "displaying options messages."
  147. echo ""
  148. (fn_getopt)
  149. echo ""
  150. echo "Test complete!"
  151. sleep 1
  152. echo ""
  153. echo "2.0 - install"
  154. echo "================================="
  155. echo "Description:"
  156. echo "install ${gamename} server."
  157. fn_autoinstall
  158. echo ""
  159. echo "Test complete!"
  160. sleep 1
  161. echo ""
  162. echo "3.1 - start"
  163. echo "================================="
  164. echo "Description:"
  165. echo "start ${gamename} server."
  166. requiredstatus="OFFLINE"
  167. fn_setstatus
  168. fn_start
  169. echo ""
  170. echo "Test complete!"
  171. sleep 1
  172. echo ""
  173. echo "3.2 - start - online"
  174. echo "================================="
  175. echo "Description:"
  176. echo "start ${gamename} server while already running."
  177. requiredstatus="ONLINE"
  178. fn_setstatus
  179. (fn_start)
  180. echo ""
  181. echo "Test complete!"
  182. sleep 1
  183. echo ""
  184. echo "3.3 - start - updateonstart"
  185. echo "================================="
  186. echo "Description:"
  187. echo "will update server on start."
  188. requiredstatus="OFFLINE"
  189. fn_setstatus
  190. (
  191. updateonstart="on"
  192. fn_start
  193. )
  194. echo ""
  195. echo "Test complete!"
  196. sleep 1
  197. echo ""
  198. echo "3.4 - stop"
  199. echo "================================="
  200. echo "Description:"
  201. echo "stop ${gamename} server."
  202. requiredstatus="ONLINE"
  203. fn_setstatus
  204. fn_stop
  205. echo ""
  206. echo "Test complete!"
  207. sleep 1
  208. echo ""
  209. echo "3.5 - stop - offline"
  210. echo "================================="
  211. echo "Description:"
  212. echo "stop ${gamename} server while already stopped."
  213. requiredstatus="OFFLINE"
  214. fn_setstatus
  215. (fn_stop)
  216. echo ""
  217. echo "Test complete!"
  218. sleep 1
  219. echo ""
  220. echo "3.6 - restart"
  221. echo "================================="
  222. echo "Description:"
  223. echo "restart ${gamename}."
  224. requiredstatus="ONLINE"
  225. fn_setstatus
  226. fn_restart
  227. echo ""
  228. echo "Test complete!"
  229. sleep 1
  230. echo ""
  231. echo "3.7 - restart - offline"
  232. echo "================================="
  233. echo "Description:"
  234. echo "restart ${gamename} while already stopped."
  235. requiredstatus="OFFLINE"
  236. fn_setstatus
  237. fn_restart
  238. echo ""
  239. echo "Test complete!"
  240. sleep 1
  241. echo ""
  242. echo "4.1 - update"
  243. echo "================================="
  244. echo "Description:"
  245. echo "check for updates."
  246. requiredstatus="OFFLINE"
  247. fn_setstatus
  248. fn_update_check
  249. echo ""
  250. echo "Test complete!"
  251. sleep 1
  252. echo ""
  253. echo "4.2 - update - change buildid"
  254. echo "================================="
  255. echo "Description:"
  256. echo "change the buildid tricking SteamCMD to update."
  257. requiredstatus="OFFLINE"
  258. fn_setstatus
  259. fn_printinfonl "changed buildid to 0."
  260. sed -i 's/[0-9]\+/0/' ${filesdir}/steamapps/appmanifest_${appid}.acf
  261. fn_update_check
  262. echo ""
  263. echo "Test complete!"
  264. sleep 1
  265. echo ""
  266. echo "4.3 - update - change buildid - online"
  267. echo "================================="
  268. echo "Description:"
  269. echo "change the buildid tricking SteamCMD to update server while already running."
  270. requiredstatus="ONLINE"
  271. fn_setstatus
  272. fn_printinfonl "changed buildid to 0."
  273. sed -i 's/[0-9]\+/0/' ${filesdir}/steamapps/appmanifest_${appid}.acf
  274. fn_update_check
  275. echo ""
  276. echo "Test complete!"
  277. sleep 1
  278. echo ""
  279. echo "4.4 - update - remove appmanifest file"
  280. echo "================================="
  281. echo "Description:"
  282. echo "removing appmanifest file will cause script to repair."
  283. requiredstatus="OFFLINE"
  284. fn_setstatus
  285. fn_printinfonl "removed appmanifest_${appid}.acf."
  286. rm --verbose "${filesdir}/steamapps/appmanifest_${appid}.acf"
  287. fn_update_check
  288. echo ""
  289. echo "Test complete!"
  290. sleep 1
  291. echo ""
  292. echo "4.5 - force-update"
  293. echo "================================="
  294. echo "Description:"
  295. echo "force-update bypassing update check."
  296. requiredstatus="OFFLINE"
  297. fn_setstatus
  298. fn_update_check
  299. echo ""
  300. echo "Test complete!"
  301. sleep 1
  302. echo ""
  303. echo "4.6 - force-update - online"
  304. echo "================================="
  305. echo "Description:"
  306. echo "force-update bypassing update check server while already running."
  307. requiredstatus="ONLINE"
  308. fn_setstatus
  309. fn_update_check
  310. echo ""
  311. echo "Test complete!"
  312. sleep 1
  313. echo ""
  314. echo "4.7 - validate"
  315. echo "================================="
  316. echo "Description:"
  317. echo "validate server files."
  318. requiredstatus="OFFLINE"
  319. fn_setstatus
  320. fn_validate
  321. echo ""
  322. echo "Test complete!"
  323. sleep 1
  324. echo ""
  325. echo "4.8 - validate - online"
  326. echo "================================="
  327. echo "Description:"
  328. echo "validate server files while server while already running."
  329. requiredstatus="ONLINE"
  330. fn_setstatus
  331. fn_validate
  332. echo ""
  333. echo "Test complete!"
  334. sleep 1
  335. echo ""
  336. echo "5.1 - monitor - online"
  337. echo "================================="
  338. echo "Description:"
  339. echo "run monitor server while already running."
  340. requiredstatus="ONLINE"
  341. fn_setstatus
  342. (fn_monitor)
  343. echo ""
  344. echo "Test complete!"
  345. sleep 1
  346. echo ""
  347. echo "5.2 - monitor - offline - no lockfile"
  348. echo "================================="
  349. echo "Description:"
  350. echo "run monitor while server is offline with no lockfile."
  351. requiredstatus="OFFLINE"
  352. fn_setstatus
  353. (fn_monitor)
  354. echo ""
  355. echo "Test complete!"
  356. sleep 1
  357. echo ""
  358. echo "5.3 - monitor - offline - with lockfile"
  359. echo "================================="
  360. echo "Description:"
  361. echo "run monitor while server is offline with no lockfile."
  362. requiredstatus="OFFLINE"
  363. fn_setstatus
  364. fn_printinfonl "creating lockfile."
  365. date > "${rootdir}/${lockselfname}"
  366. (fn_monitor)
  367. echo ""
  368. echo "Test complete!"
  369. sleep 1
  370. echo ""
  371. echo "5.4 - monitor - gsquery.py failure"
  372. echo "================================="
  373. echo "Description:"
  374. echo "gsquery.py will fail to query port."
  375. requiredstatus="ONLINE"
  376. fn_setstatus
  377. sed -i 's/[0-9]\+/0/' "${servercfgfullpath}"
  378. (fn_monitor)
  379. echo ""
  380. fn_printinfonl "Reseting ${servercfg}."
  381. fn_install_config
  382. echo ""
  383. echo "Test complete!"
  384. sleep 1
  385. echo ""
  386. echo "6.0 - details"
  387. echo "================================="
  388. echo "Description:"
  389. echo "display details."
  390. requiredstatus="ONLINE"
  391. fn_setstatus
  392. fn_details
  393. echo ""
  394. echo "Test complete!"
  395. sleep 1
  396. echo ""
  397. echo "================================="
  398. echo "Server Tests - Complete!"
  399. echo "Using: ${gamename}"
  400. echo "================================="
  401. echo ""
  402. requiredstatus="OFFLINE"
  403. fn_setstatus
  404. sleep 1
  405. fn_printinfo "Tidying up directories."
  406. sleep 1
  407. rm -rfv ${serverfiles}
  408. echo "END"