tests_jc2server.sh 10 KB

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