tests_jc2server.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. # 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. fn_functions(){
  100. # Functions are defined in fn_functions.
  101. functionfile="${FUNCNAME}"
  102. fn_runfunction
  103. }
  104. core_functions.sh
  105. getopt=$1
  106. core_getopt.sh
  107. fn_currentstatus_tmux(){
  108. pid=$(tmux list-sessions 2>&1 | awk '{print $1}' | grep -Ec "^${servicename}:")
  109. if [ "${pid}" != "0" ]; then
  110. currentstatus="ONLINE"
  111. else
  112. currentstatus="OFFLINE"
  113. fi
  114. }
  115. fn_currentstatus_ts3(){
  116. ts3status=$(${executable} status servercfgfullpathfile=${servercfgfullpath})
  117. if [ "${ts3status}" == "Server is running" ]; then
  118. currentstatus="ONLINE"
  119. else
  120. currentstatus="OFFLINE"
  121. fi
  122. }
  123. fn_setstatus(){
  124. fn_currentstatus_tmux
  125. echo""
  126. echo "Required status: ${requiredstatus}"
  127. counter=0
  128. echo "Current status: ${currentstatus}"
  129. while [ "${requiredstatus}" != "${currentstatus}" ]; do
  130. counter=$((counter+1))
  131. fn_currentstatus_tmux
  132. echo -ne "New status: ${currentstatus}\\r"
  133. if [ "${requiredstatus}" == "ONLINE" ]; then
  134. (command_start.sh > /dev/null 2>&1)
  135. else
  136. (command_stop.sh > /dev/null 2>&1)
  137. fi
  138. if [ "${counter}" -gt "5" ]; then
  139. currentstatus="FAIL"
  140. echo "Current status: ${currentstatus}"
  141. echo ""
  142. echo "Unable to start or stop server."
  143. exit 1
  144. fi
  145. done
  146. echo -ne "New status: ${currentstatus}\\r"
  147. echo -e "\n"
  148. echo "Test starting:"
  149. echo ""
  150. sleep 0.5
  151. }
  152. echo "================================="
  153. echo "TravisCI Tests"
  154. echo "Linux Game Server Manager"
  155. echo "by Daniel Gibbs"
  156. echo "http://gameservermanagers.com"
  157. echo "================================="
  158. echo ""
  159. sleep 1
  160. echo "================================="
  161. echo "Server Tests"
  162. echo "Using: ${gamename}"
  163. echo "================================="
  164. echo ""
  165. sleep 1
  166. mkdir ${rootdir}
  167. echo "1.0 - start - no files"
  168. echo "================================="
  169. echo "Description:"
  170. echo "test script reaction to missing server files."
  171. echo ""
  172. (command_start.sh)
  173. echo ""
  174. echo "Test complete!"
  175. sleep 1
  176. echo ""
  177. echo "1.1 - getopt"
  178. echo "================================="
  179. echo "Description:"
  180. echo "displaying options messages."
  181. echo ""
  182. (fn_getopt)
  183. echo ""
  184. echo "Test complete!"
  185. sleep 1
  186. echo ""
  187. echo "2.0 - install"
  188. echo "================================="
  189. echo "Description:"
  190. echo "install ${gamename} server."
  191. fn_autoinstall
  192. echo ""
  193. echo "Test complete!"
  194. sleep 1
  195. echo ""
  196. echo "3.1 - start"
  197. echo "================================="
  198. echo "Description:"
  199. echo "start ${gamename} server."
  200. requiredstatus="OFFLINE"
  201. fn_setstatus
  202. command_start.sh
  203. echo ""
  204. echo "Test complete!"
  205. sleep 1
  206. echo ""
  207. echo "3.2 - start - online"
  208. echo "================================="
  209. echo "Description:"
  210. echo "start ${gamename} server while already running."
  211. requiredstatus="ONLINE"
  212. fn_setstatus
  213. (command_start.sh)
  214. echo ""
  215. echo "Test complete!"
  216. sleep 1
  217. echo ""
  218. echo "3.3 - start - updateonstart"
  219. echo "================================="
  220. echo "Description:"
  221. echo "will update server on start."
  222. requiredstatus="OFFLINE"
  223. fn_setstatus
  224. (
  225. updateonstart="on"
  226. command_start.sh
  227. )
  228. echo ""
  229. echo "Test complete!"
  230. sleep 1
  231. echo ""
  232. echo "3.4 - stop"
  233. echo "================================="
  234. echo "Description:"
  235. echo "stop ${gamename} server."
  236. requiredstatus="ONLINE"
  237. fn_setstatus
  238. command_stop.sh
  239. echo ""
  240. echo "Test complete!"
  241. sleep 1
  242. echo ""
  243. echo "3.5 - stop - offline"
  244. echo "================================="
  245. echo "Description:"
  246. echo "stop ${gamename} server while already stopped."
  247. requiredstatus="OFFLINE"
  248. fn_setstatus
  249. (command_stop.sh)
  250. echo ""
  251. echo "Test complete!"
  252. sleep 1
  253. echo ""
  254. echo "3.6 - restart"
  255. echo "================================="
  256. echo "Description:"
  257. echo "restart ${gamename}."
  258. requiredstatus="ONLINE"
  259. fn_setstatus
  260. fn_restart
  261. echo ""
  262. echo "Test complete!"
  263. sleep 1
  264. echo ""
  265. echo "3.7 - restart - offline"
  266. echo "================================="
  267. echo "Description:"
  268. echo "restart ${gamename} while already stopped."
  269. requiredstatus="OFFLINE"
  270. fn_setstatus
  271. fn_restart
  272. echo ""
  273. echo "Test complete!"
  274. sleep 1
  275. echo ""
  276. echo "4.1 - update"
  277. echo "================================="
  278. echo "Description:"
  279. echo "check for updates."
  280. requiredstatus="OFFLINE"
  281. fn_setstatus
  282. update_check.sh
  283. echo ""
  284. echo "Test complete!"
  285. sleep 1
  286. echo ""
  287. echo "4.2 - update - change buildid"
  288. echo "================================="
  289. echo "Description:"
  290. echo "change the buildid tricking SteamCMD to update."
  291. requiredstatus="OFFLINE"
  292. fn_setstatus
  293. fn_printinfonl "changed buildid to 0."
  294. sed -i 's/[0-9]\+/0/' ${filesdir}/steamapps/appmanifest_${appid}.acf
  295. update_check.sh
  296. echo ""
  297. echo "Test complete!"
  298. sleep 1
  299. echo ""
  300. echo "4.3 - update - change buildid - online"
  301. echo "================================="
  302. echo "Description:"
  303. echo "change the buildid tricking SteamCMD to update server while already running."
  304. requiredstatus="ONLINE"
  305. fn_setstatus
  306. fn_printinfonl "changed buildid to 0."
  307. sed -i 's/[0-9]\+/0/' ${filesdir}/steamapps/appmanifest_${appid}.acf
  308. update_check.sh
  309. echo ""
  310. echo "Test complete!"
  311. sleep 1
  312. echo ""
  313. echo "4.4 - update - remove appmanifest file"
  314. echo "================================="
  315. echo "Description:"
  316. echo "removing appmanifest file will cause script to repair."
  317. requiredstatus="OFFLINE"
  318. fn_setstatus
  319. fn_printinfonl "removed appmanifest_${appid}.acf."
  320. rm --verbose "${filesdir}/steamapps/appmanifest_${appid}.acf"
  321. update_check.sh
  322. echo ""
  323. echo "Test complete!"
  324. sleep 1
  325. echo ""
  326. echo "4.5 - force-update"
  327. echo "================================="
  328. echo "Description:"
  329. echo "force-update bypassing update check."
  330. requiredstatus="OFFLINE"
  331. fn_setstatus
  332. update_check.sh
  333. echo ""
  334. echo "Test complete!"
  335. sleep 1
  336. echo ""
  337. echo "4.6 - force-update - online"
  338. echo "================================="
  339. echo "Description:"
  340. echo "force-update bypassing update check server while already running."
  341. requiredstatus="ONLINE"
  342. fn_setstatus
  343. update_check.sh
  344. echo ""
  345. echo "Test complete!"
  346. sleep 1
  347. echo ""
  348. echo "4.7 - validate"
  349. echo "================================="
  350. echo "Description:"
  351. echo "validate server files."
  352. requiredstatus="OFFLINE"
  353. fn_setstatus
  354. command_validate.sh
  355. echo ""
  356. echo "Test complete!"
  357. sleep 1
  358. echo ""
  359. echo "4.8 - validate - online"
  360. echo "================================="
  361. echo "Description:"
  362. echo "validate server files while server while already running."
  363. requiredstatus="ONLINE"
  364. fn_setstatus
  365. command_validate.sh
  366. echo ""
  367. echo "Test complete!"
  368. sleep 1
  369. echo ""
  370. echo "5.1 - monitor - online"
  371. echo "================================="
  372. echo "Description:"
  373. echo "run monitor server while already running."
  374. requiredstatus="ONLINE"
  375. fn_setstatus
  376. (command_monitor.sh)
  377. echo ""
  378. echo "Test complete!"
  379. sleep 1
  380. echo ""
  381. echo "5.2 - monitor - offline - no lockfile"
  382. echo "================================="
  383. echo "Description:"
  384. echo "run monitor while server is offline with no lockfile."
  385. requiredstatus="OFFLINE"
  386. fn_setstatus
  387. (command_monitor.sh)
  388. echo ""
  389. echo "Test complete!"
  390. sleep 1
  391. echo ""
  392. echo "5.3 - monitor - offline - with lockfile"
  393. echo "================================="
  394. echo "Description:"
  395. echo "run monitor while server is offline with no lockfile."
  396. requiredstatus="OFFLINE"
  397. fn_setstatus
  398. fn_printinfonl "creating lockfile."
  399. date > "${rootdir}/${lockselfname}"
  400. (command_monitor.sh)
  401. echo ""
  402. echo "Test complete!"
  403. sleep 1
  404. echo ""
  405. echo "5.4 - monitor - gsquery.py failure"
  406. echo "================================="
  407. echo "Description:"
  408. echo "gsquery.py will fail to query port."
  409. requiredstatus="ONLINE"
  410. fn_setstatus
  411. sed -i 's/[0-9]\+/0/' "${servercfgfullpath}"
  412. (command_monitor.sh)
  413. echo ""
  414. fn_printinfonl "Reseting ${servercfg}."
  415. install_config.sh
  416. echo ""
  417. echo "Test complete!"
  418. sleep 1
  419. echo ""
  420. echo "6.0 - details"
  421. echo "================================="
  422. echo "Description:"
  423. echo "display details."
  424. requiredstatus="ONLINE"
  425. fn_setstatus
  426. command_details.sh
  427. echo ""
  428. echo "Test complete!"
  429. sleep 1
  430. echo ""
  431. echo "================================="
  432. echo "Server Tests - Complete!"
  433. echo "Using: ${gamename}"
  434. echo "================================="
  435. echo ""
  436. requiredstatus="OFFLINE"
  437. fn_setstatus
  438. sleep 1
  439. fn_printinfo "Tidying up directories."
  440. sleep 1
  441. rm -rfv ${serverfiles}
  442. echo "END"