update_jk3.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # LinuxGSM update_jk3.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Jedi Academy OpenJK servers.
  7. # shellcheck disable=SC2034 # Used by the module loader.
  8. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_dl() {
  10. # Download and extract files to GameData.
  11. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "forcedl" "${remotebuildhash:-nohash}"
  12. # OpenJK archives already have the executable and base/ at their root.
  13. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${systemdir}"
  14. if [ ! -x "${executabledir}/openjkded.x86_64" ] || [ ! -f "${systemdir}/base/jampgamex86_64.so" ]; then
  15. fn_print_fail_nl "OpenJK server files are missing after extraction"
  16. fn_script_log_fail "OpenJK server files are missing after extraction"
  17. # shellcheck disable=SC2034 # Used by core_exit.sh.
  18. exitcode=1
  19. core_exit.sh
  20. fi
  21. if ! echo "${remotebuild}" > "${systemdir}/openjk-build.txt"; then
  22. fn_print_fail_nl "Unable to save the OpenJK build ID"
  23. fn_script_log_fail "Unable to save the OpenJK build ID"
  24. # shellcheck disable=SC2034 # Used by core_exit.sh.
  25. exitcode=1
  26. core_exit.sh
  27. fi
  28. fn_clear_tmp
  29. }
  30. fn_update_localbuild() {
  31. # Gets local build info.
  32. fn_print_dots "Checking local build: ${remotelocation}"
  33. # Uses build file to get local build.
  34. localbuild=$(head -n 1 "${systemdir}/openjk-build.txt" 2> /dev/null)
  35. if [ -z "${localbuild}" ]; then
  36. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  37. fn_script_log_error "Missing local build info"
  38. fn_script_log_error "Set localbuild to 0"
  39. localbuild="0"
  40. else
  41. fn_print_ok "Checking local build: ${remotelocation}"
  42. fn_script_log_pass "Checking local build"
  43. fi
  44. }
  45. fn_update_remotebuild() {
  46. # Gets remote build info.
  47. apiurl="https://api.github.com/repos/JACoders/OpenJK/releases/latest"
  48. remotebuildfilename="OpenJK-linux-x86_64.tar.gz"
  49. remotebuildresponse=$(curl --connect-timeout 10 --max-time 30 --retry 2 --fail --silent --show-error --location "${apiurl}") || remotebuildresponse=""
  50. # Select exactly one dedicated Linux x86_64 asset.
  51. remotebuildasset=$(echo "${remotebuildresponse}" | jq -er --arg filename "${remotebuildfilename}" '[.assets[]? | select(.name == $filename)] | select(length == 1) | .[0] | select((.id | type) == "number" and (.browser_download_url | type) == "string")') || remotebuildasset=""
  52. remotebuildurl=$(echo "${remotebuildasset}" | jq -r '.browser_download_url')
  53. # The rolling latest tag is reused; replacing its asset changes the asset ID.
  54. remotebuild=$(echo "${remotebuildasset}" | jq -r '.id')
  55. remotebuildhash=$(echo "${remotebuildasset}" | jq -r '.digest // empty' | sed -n 's/^sha256://p')
  56. if [ "${firstcommandname}" != "INSTALL" ]; then
  57. fn_print_dots "Checking remote build: ${remotelocation}"
  58. fi
  59. # Checks if the remote build and download URL have been set.
  60. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ] || [ -z "${remotebuildurl}" ] || [ "${remotebuildurl}" == "null" ]; then
  61. if [ "${firstcommandname}" != "INSTALL" ]; then
  62. fn_print_fail "Checking remote build: ${remotelocation}"
  63. fn_script_log_fail "Checking remote build"
  64. else
  65. fn_print_fail_nl "Unable to get remote build"
  66. fn_script_log_fail "Unable to get remote build"
  67. fi
  68. # shellcheck disable=SC2034 # Used by core_exit.sh.
  69. exitcode=1
  70. core_exit.sh
  71. elif [ "${firstcommandname}" != "INSTALL" ]; then
  72. fn_print_ok "Checking remote build: ${remotelocation}"
  73. fn_script_log_pass "Checking remote build"
  74. fi
  75. }
  76. fn_update_compare() {
  77. fn_print_dots "Checking for update: ${remotelocation}"
  78. # Update has been found or force update.
  79. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  80. # Create update lockfile.
  81. date '+%s' > "${lockdir:?}/update.lock"
  82. fn_print_ok_nl "Checking for update: ${remotelocation}"
  83. fn_print "\n"
  84. fn_print_nl "${bold}${underline}Update${default} available"
  85. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  86. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  87. if [ -n "${branch}" ]; then
  88. fn_print_nl "* Branch: ${branch}"
  89. fi
  90. if [ -f "${rootdir}/.dev-debug" ]; then
  91. fn_print_nl "Remote build info"
  92. fn_print_nl "* apiurl: ${apiurl}"
  93. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  94. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  95. fn_print_nl "* remotebuild: ${remotebuild}"
  96. fi
  97. fn_print "\n"
  98. fn_script_log_info "Update available"
  99. fn_script_log_info "Local build: ${localbuild}"
  100. fn_script_log_info "Remote build: ${remotebuild}"
  101. if [ -n "${branch}" ]; then
  102. fn_script_log_info "Branch: ${branch}"
  103. fi
  104. fn_script_log_info "${localbuild} > ${remotebuild}"
  105. if [ "${commandname}" == "UPDATE" ]; then
  106. unset updateonstart
  107. check_status.sh
  108. # If server stopped.
  109. if [ "${status}" == "0" ]; then
  110. fn_update_dl
  111. # If server started.
  112. else
  113. fn_print_restart_warning
  114. exitbypass=1
  115. command_stop.sh
  116. fn_firstcommand_reset
  117. fn_update_dl
  118. exitbypass=1
  119. command_start.sh
  120. fn_firstcommand_reset
  121. fi
  122. unset exitbypass
  123. date +%s > "${lockdir:?}/last-updated.lock"
  124. alert="update"
  125. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  126. alert="check-update"
  127. fi
  128. alert.sh
  129. else
  130. fn_print_ok_nl "Checking for update: ${remotelocation}"
  131. fn_print "\n"
  132. fn_print_nl "${bold}${underline}No update${default} available"
  133. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  134. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  135. if [ -n "${branch}" ]; then
  136. fn_print_nl "* Branch: ${branch}"
  137. fi
  138. fn_print "\n"
  139. fn_script_log_info "No update available"
  140. fn_script_log_info "Local build: ${localbuild}"
  141. fn_script_log_info "Remote build: ${remotebuild}"
  142. if [ -n "${branch}" ]; then
  143. fn_script_log_info "Branch: ${branch}"
  144. fi
  145. if [ -f "${rootdir}/.dev-debug" ]; then
  146. fn_print_nl "Remote build info"
  147. fn_print_nl "* apiurl: ${apiurl}"
  148. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  149. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  150. fn_print_nl "* remotebuild: ${remotebuild}"
  151. fi
  152. fi
  153. }
  154. # The location where the builds are checked and downloaded.
  155. remotelocation="github.com"
  156. if [ "${firstcommandname}" == "INSTALL" ]; then
  157. fn_update_remotebuild
  158. fn_update_dl
  159. else
  160. fn_print_dots "Checking for update"
  161. fn_print_dots "Checking for update: ${remotelocation}"
  162. fn_script_log_info "Checking for update: ${remotelocation}"
  163. fn_update_localbuild
  164. fn_update_remotebuild
  165. fn_update_compare
  166. fi