update_luanti.sh 6.3 KB

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