update_qw.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. # LinuxGSM update_qw.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of QuakeWorld servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_fail() {
  9. fn_print_fail_nl "${1}"
  10. fn_script_log_fail "${1}"
  11. exitcode=1
  12. core_exit.sh
  13. }
  14. fn_update_dl() {
  15. # Verify and stage the complete bundle before changing serverfiles.
  16. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "nochmodx" "norun" "forcedl" "${remotebuildhash}"
  17. local stagedir configfile relativefile
  18. stagedir=$(mktemp -d "${tmpdir}/qw-update.XXXXXXXX") || fn_update_fail "Unable to create QuakeWorld staging directory"
  19. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${stagedir}"
  20. if [ ! -x "${stagedir}/mvdsv" ] || [ ! -f "${stagedir}/ktx/qwprogs.so" ] || [ ! -f "${stagedir}/ktx/server.cfg" ]; then
  21. fn_update_fail "Invalid QuakeWorld bundle layout"
  22. fi
  23. # Keep existing configs, map lists and local scripts, including config symlinks.
  24. # New default configs are still installed. Never remove files from serverfiles.
  25. while IFS= read -r -d '' configfile; do
  26. relativefile="${configfile#"${stagedir}/"}"
  27. case "${relativefile,,}" in
  28. *.cfg | *.conf | *.ini | *.txt | *.sh) ;;
  29. *)
  30. if [ "${serverfiles}/${relativefile}" != "${servercfgfullpath}" ]; then
  31. continue
  32. fi
  33. ;;
  34. esac
  35. if [ -e "${serverfiles}/${relativefile}" ] || [ -L "${serverfiles}/${relativefile}" ]; then
  36. rm -f "${configfile}" || fn_update_fail "Unable to preserve QuakeWorld config: ${relativefile}"
  37. fi
  38. done < <(find "${stagedir}" \( -type f -o -type l \) -print0)
  39. cp -a "${stagedir}/." "${serverfiles}/" || fn_update_fail "Unable to copy QuakeWorld update"
  40. echo "${remotebuild}" > "${serverfiles}/build.txt" || fn_update_fail "Unable to save QuakeWorld build"
  41. fn_clear_tmp
  42. }
  43. fn_update_localbuild() {
  44. # Gets local build info.
  45. fn_print_dots "Checking local build: ${remotelocation}"
  46. # Legacy installations without build.txt are updated once.
  47. localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
  48. if [ -z "${localbuild}" ]; then
  49. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  50. fn_script_log_error "Missing local build info"
  51. fn_script_log_error "Set localbuild to 0"
  52. localbuild="0"
  53. else
  54. fn_print_ok "Checking local build: ${remotelocation}"
  55. fn_script_log_pass "Checking local build"
  56. fi
  57. }
  58. fn_update_remotebuild() {
  59. # Use the dated release, not the mutable release tagged "latest".
  60. apiurl="https://api.github.com/repos/GameServerManagers/qwserver-build/releases/latest"
  61. fn_print_dots "Checking remote build: ${remotelocation}"
  62. remotebuildresponse=$(curl -fsSL --connect-timeout 10 --max-time 60 "${apiurl}") || fn_update_fail "Unable to get QuakeWorld release"
  63. remotebuild=$(echo "${remotebuildresponse}" | jq -er '.tag_name | select(test("^build-[0-9]{8}$"))') || fn_update_fail "Invalid QuakeWorld release tag"
  64. remotebuildfilename="nquake.server.linux.${remotebuild#build-}.full.tar.xz"
  65. remotebuildurl=$(echo "${remotebuildresponse}" | jq -er --arg name "${remotebuildfilename}" '[.assets[] | select(.name == $name)] | select(length == 1) | .[0].browser_download_url | select(type == "string" and startswith("https://"))') || fn_update_fail "Missing QuakeWorld release archive"
  66. local checksumurl checksums
  67. checksumurl=$(echo "${remotebuildresponse}" | jq -er '[.assets[] | select(.name == "SHA256SUMS")] | select(length == 1) | .[0].browser_download_url | select(type == "string" and startswith("https://"))') || fn_update_fail "Missing QuakeWorld checksums"
  68. checksums=$(curl -fsSL --connect-timeout 10 --max-time 60 "${checksumurl}") || fn_update_fail "Unable to get QuakeWorld checksums"
  69. remotebuildhash=$(echo "${checksums}" | awk -v filename="${remotebuildfilename}" 'NF == 2 && $2 == filename {print $1}')
  70. if [[ ! "${remotebuildhash}" =~ ^[a-f0-9]{64}$ ]]; then
  71. fn_update_fail "Missing or invalid QuakeWorld archive checksum"
  72. fi
  73. fn_print_ok "Checking remote build: ${remotelocation}"
  74. fn_script_log_pass "Checking remote build"
  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. fn_update_apply fn_update_dl
  108. unset exitbypass
  109. alert="update"
  110. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  111. alert="check-update"
  112. fi
  113. alert.sh
  114. else
  115. fn_print_ok_nl "Checking for update: ${remotelocation}"
  116. fn_print "\n"
  117. fn_print_nl "${bold}${underline}No update${default} available"
  118. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  119. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  120. if [ -n "${branch}" ]; then
  121. fn_print_nl "* Branch: ${branch}"
  122. fi
  123. fn_print "\n"
  124. fn_script_log_info "No update available"
  125. fn_script_log_info "Local build: ${localbuild}"
  126. fn_script_log_info "Remote build: ${remotebuild}"
  127. if [ -n "${branch}" ]; then
  128. fn_script_log_info "Branch: ${branch}"
  129. fi
  130. if [ -f "${rootdir}/.dev-debug" ]; then
  131. fn_print_nl "Remote build info"
  132. fn_print_nl "* apiurl: ${apiurl}"
  133. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  134. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  135. fn_print_nl "* remotebuild: ${remotebuild}"
  136. fi
  137. fi
  138. }
  139. # The location where the builds are checked and downloaded.
  140. remotelocation="github.com"
  141. if [ "${firstcommandname}" == "INSTALL" ]; then
  142. fn_update_remotebuild
  143. fn_update_dl
  144. else
  145. fn_print_dots "Checking for update"
  146. fn_print_dots "Checking for update: ${remotelocation}"
  147. fn_script_log_info "Checking for update: ${remotelocation}"
  148. fn_update_localbuild
  149. fn_update_remotebuild
  150. fn_update_compare
  151. fi