update_bmp.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. # LinuxGSM update_bmp.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of BeamMP servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "force" "${remotebuildhash}"
  10. mv -f "${tmpdir}/${remotebuildfilename}" "${executabledir}/${executable#./}"
  11. fn_clear_tmp
  12. }
  13. fn_update_localbuild() {
  14. local mode="${1:-}"
  15. if [ "${mode}" != "silent" ]; then
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. fi
  18. localbuild=$("${executabledir}/${executable#./}" --version 2> /dev/null | awk '/^BeamMP-Server / {print $2; exit}')
  19. localbuild="${localbuild:-0}"
  20. if [ "${mode}" != "silent" ]; then
  21. fn_print_ok "Checking local build: ${remotelocation}"
  22. fi
  23. }
  24. fn_update_remotebuild() {
  25. info_distro.sh
  26. case "$(uname -m)" in
  27. x86_64) bmparch="x86_64" ;;
  28. aarch64) bmparch="arm64" ;;
  29. *)
  30. fn_print_fail_nl "Unsupported BeamMP architecture: $(uname -m)"
  31. fn_script_log_fail "Unsupported BeamMP architecture: $(uname -m)"
  32. core_exit.sh
  33. ;;
  34. esac
  35. apiurl="https://api.github.com/repos/BeamMP/BeamMP-Server/releases/latest"
  36. remotebuildresponse=$(curl -fsSL --retry 3 "${apiurl}")
  37. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.tag_name // empty')
  38. bmpdistroid="${distroid,,}"
  39. bmpdistroversion=""
  40. bmpmatchversion="${distroversion}"
  41. case "${bmpdistroid}" in
  42. rhel | rocky | almalinux | centos)
  43. if [ "${distroversion%%.*}" -ge 10 ] 2> /dev/null; then
  44. bmpdistroid="ubuntu"
  45. bmpmatchversion="24.04"
  46. fi
  47. ;;
  48. esac
  49. while read -r bmpcandidate; do
  50. if [ "$(printf '%s\n%s\n' "${bmpcandidate}" "${bmpmatchversion}" | sort -V | tail -n 1)" == "${bmpmatchversion}" ]; then
  51. bmpdistroversion="${bmpcandidate}"
  52. fi
  53. done < <(echo "${remotebuildresponse}" | jq -r --arg id "${bmpdistroid}" --arg arch "${bmparch}" '.assets[]?.name | capture("^BeamMP-Server\\." + $id + "\\.(?<v>[0-9.]+)\\." + $arch + "$")? | .v' | sort -V)
  54. remotebuildfilename="BeamMP-Server.${bmpdistroid}.${bmpdistroversion}.${bmparch}"
  55. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r --arg name "${remotebuildfilename}" '.assets[]? | select(.name == $name) | .browser_download_url')
  56. remotebuildhash=$(echo "${remotebuildresponse}" | jq -r --arg name "${remotebuildfilename}" '.assets[]? | select(.name == $name) | .digest // empty' | sed 's/^sha256://')
  57. if [ -z "${remotebuild}" ] || [ -z "${remotebuildurl}" ] || [[ ! "${remotebuildhash}" =~ ^[0-9a-f]{64}$ ]]; then
  58. fn_print_fail_nl "Unable to get a BeamMP release, download URL or SHA256 checksum for ${distroid} ${distroversion} (${bmparch})"
  59. fn_script_log_fail "Unable to get a BeamMP release, download URL or SHA256 checksum for ${distroid} ${distroversion} (${bmparch})"
  60. core_exit.sh
  61. fi
  62. }
  63. fn_update_compare() {
  64. fn_print_dots "Checking for update: ${remotelocation}"
  65. # Update has been found or force update.
  66. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  67. # Create update lockfile.
  68. date '+%s' > "${lockdir:?}/update.lock"
  69. fn_print_ok_nl "Checking for update: ${remotelocation}"
  70. fn_print "\n"
  71. fn_print_nl "${bold}${underline}Update${default} available"
  72. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  73. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  74. if [ -n "${branch}" ]; then
  75. fn_print_nl "* Branch: ${branch}"
  76. fi
  77. if [ -f "${rootdir}/.dev-debug" ]; then
  78. fn_print_nl "Remote build info"
  79. fn_print_nl "* apiurl: ${apiurl}"
  80. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  81. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  82. fn_print_nl "* remotebuild: ${remotebuild}"
  83. fi
  84. fn_print "\n"
  85. fn_script_log_info "Update available"
  86. fn_script_log_info "Local build: ${localbuild}"
  87. fn_script_log_info "Remote build: ${remotebuild}"
  88. if [ -n "${branch}" ]; then
  89. fn_script_log_info "Branch: ${branch}"
  90. fi
  91. fn_script_log_info "${localbuild} > ${remotebuild}"
  92. if [ "${commandname}" == "UPDATE" ]; then
  93. # Keep track of the build before updating for alert purposes.
  94. previousbuild="${localbuild}"
  95. unset updateonstart
  96. fn_update_apply fn_update_dl
  97. # Refresh local build value after update (silent) so alerts show new version.
  98. fn_update_localbuild silent
  99. # Verify the update applied correctly unless forced update to same version.
  100. if [ "${localbuild}" != "${remotebuild}" ]; then
  101. # If forced update and version unchanged treat as acceptable; otherwise failure.
  102. if [ "${forceupdate}" != "1" ] || [ "${previousbuild}" = "${localbuild}" ]; then
  103. fn_script_log_error "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  104. fn_print_fail_nl "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  105. updatefailureexpected="${remotebuild}"
  106. updatefailuregot="${localbuild}"
  107. alert="update-failed"
  108. alert.sh
  109. core_exit.sh
  110. fi
  111. else
  112. fn_script_log_pass "Update verification success: ${previousbuild} -> ${localbuild}"
  113. fi
  114. unset exitbypass
  115. alert="update"
  116. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  117. alert="check-update"
  118. fi
  119. alert.sh
  120. else
  121. fn_print_ok_nl "Checking for update: ${remotelocation}"
  122. fn_print "\n"
  123. fn_print_nl "${bold}${underline}No update${default} available"
  124. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  125. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  126. if [ -n "${branch}" ]; then
  127. fn_print_nl "* Branch: ${branch}"
  128. fi
  129. fn_print "\n"
  130. fn_script_log_info "No update available"
  131. fn_script_log_info "Local build: ${localbuild}"
  132. fn_script_log_info "Remote build: ${remotebuild}"
  133. if [ -n "${branch}" ]; then
  134. fn_script_log_info "Branch: ${branch}"
  135. fi
  136. if [ -f "${rootdir}/.dev-debug" ]; then
  137. fn_print_nl "Remote build info"
  138. fn_print_nl "* apiurl: ${apiurl}"
  139. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  140. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  141. fn_print_nl "* remotebuild: ${remotebuild}"
  142. fi
  143. fi
  144. }
  145. # The location where the builds are checked and downloaded.
  146. remotelocation="github.com/BeamMP/BeamMP-Server"
  147. if [ "${firstcommandname}" == "INSTALL" ]; then
  148. fn_update_remotebuild
  149. fn_update_dl
  150. else
  151. fn_print_dots "Checking for update"
  152. fn_print_dots "Checking for update: ${remotelocation}"
  153. fn_script_log_info "Checking for update: ${remotelocation}"
  154. fn_update_localbuild
  155. fn_update_remotebuild
  156. fn_update_compare
  157. fi