update_mdt.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/bin/bash
  2. # LinuxGSM update_mdt.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Mindustry 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. echo "${remotebuild}" > "${executabledir}/build.txt"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. local mode="${1:-}"
  16. if [ "${mode}" != "silent" ]; then
  17. fn_print_dots "Checking local build: ${remotelocation}"
  18. fi
  19. localbuild=$(head -n 1 "${executabledir}/build.txt" 2> /dev/null)
  20. localbuild="${localbuild:-0}"
  21. if [ "${mode}" != "silent" ]; then
  22. fn_print_ok "Checking local build: ${remotelocation}"
  23. fi
  24. }
  25. fn_update_remotebuild() {
  26. apiurl="https://api.github.com/repos/Anuken/Mindustry/releases/latest"
  27. remotebuildresponse=$(curl -fsSL --retry 3 "${apiurl}")
  28. remotebuild=$(echo "${remotebuildresponse}" | jq -r '.tag_name // empty')
  29. remotebuildfilename="server-release.jar"
  30. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r --arg name "${remotebuildfilename}" '.assets[]? | select(.name == $name) | .browser_download_url')
  31. remotebuildhash=$(echo "${remotebuildresponse}" | jq -r --arg name "${remotebuildfilename}" '.assets[]? | select(.name == $name) | .digest // empty' | sed 's/^sha256://')
  32. if [ -z "${remotebuild}" ] || [ -z "${remotebuildurl}" ] || [[ ! "${remotebuildhash}" =~ ^[0-9a-f]{64}$ ]]; then
  33. fn_print_fail_nl "Unable to get a Mindustry release, download URL or SHA256 checksum"
  34. fn_script_log_fail "Unable to get a Mindustry release, download URL or SHA256 checksum"
  35. core_exit.sh
  36. fi
  37. }
  38. fn_update_compare() {
  39. fn_print_dots "Checking for update: ${remotelocation}"
  40. # Update has been found or force update.
  41. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  42. # Create update lockfile.
  43. date '+%s' > "${lockdir:?}/update.lock"
  44. fn_print_ok_nl "Checking for update: ${remotelocation}"
  45. fn_print "\n"
  46. fn_print_nl "${bold}${underline}Update${default} available"
  47. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  48. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  49. if [ -n "${branch}" ]; then
  50. fn_print_nl "* Branch: ${branch}"
  51. fi
  52. if [ -f "${rootdir}/.dev-debug" ]; then
  53. fn_print_nl "Remote build info"
  54. fn_print_nl "* apiurl: ${apiurl}"
  55. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  56. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  57. fn_print_nl "* remotebuild: ${remotebuild}"
  58. fi
  59. fn_print "\n"
  60. fn_script_log_info "Update available"
  61. fn_script_log_info "Local build: ${localbuild}"
  62. fn_script_log_info "Remote build: ${remotebuild}"
  63. if [ -n "${branch}" ]; then
  64. fn_script_log_info "Branch: ${branch}"
  65. fi
  66. fn_script_log_info "${localbuild} > ${remotebuild}"
  67. if [ "${commandname}" == "UPDATE" ]; then
  68. # Keep track of the build before updating for alert purposes.
  69. previousbuild="${localbuild}"
  70. unset updateonstart
  71. fn_update_apply fn_update_dl
  72. # Refresh local build value after update (silent) so alerts show new version.
  73. fn_update_localbuild silent
  74. # Verify the update applied correctly unless forced update to same version.
  75. if [ "${localbuild}" != "${remotebuild}" ]; then
  76. # If forced update and version unchanged treat as acceptable; otherwise failure.
  77. if [ "${forceupdate}" != "1" ] || [ "${previousbuild}" = "${localbuild}" ]; then
  78. fn_script_log_error "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  79. fn_print_fail_nl "Update verification failed: expected ${remotebuild}, got ${localbuild}"
  80. updatefailureexpected="${remotebuild}"
  81. updatefailuregot="${localbuild}"
  82. alert="update-failed"
  83. alert.sh
  84. core_exit.sh
  85. fi
  86. else
  87. fn_script_log_pass "Update verification success: ${previousbuild} -> ${localbuild}"
  88. fi
  89. unset exitbypass
  90. alert="update"
  91. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  92. alert="check-update"
  93. fi
  94. alert.sh
  95. else
  96. fn_print_ok_nl "Checking for update: ${remotelocation}"
  97. fn_print "\n"
  98. fn_print_nl "${bold}${underline}No update${default} available"
  99. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  100. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  101. if [ -n "${branch}" ]; then
  102. fn_print_nl "* Branch: ${branch}"
  103. fi
  104. fn_print "\n"
  105. fn_script_log_info "No update available"
  106. fn_script_log_info "Local build: ${localbuild}"
  107. fn_script_log_info "Remote build: ${remotebuild}"
  108. if [ -n "${branch}" ]; then
  109. fn_script_log_info "Branch: ${branch}"
  110. fi
  111. if [ -f "${rootdir}/.dev-debug" ]; then
  112. fn_print_nl "Remote build info"
  113. fn_print_nl "* apiurl: ${apiurl}"
  114. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  115. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  116. fn_print_nl "* remotebuild: ${remotebuild}"
  117. fi
  118. fi
  119. }
  120. # The location where the builds are checked and downloaded.
  121. remotelocation="github.com/Anuken/Mindustry"
  122. if [ "${firstcommandname}" == "INSTALL" ]; then
  123. fn_update_remotebuild
  124. fn_update_dl
  125. else
  126. fn_print_dots "Checking for update"
  127. fn_print_dots "Checking for update: ${remotelocation}"
  128. fn_script_log_info "Checking for update: ${remotelocation}"
  129. fn_update_localbuild
  130. fn_update_remotebuild
  131. fn_update_compare
  132. fi