update_minecraft.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft: Java Edition servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to serverfiles.
  10. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "noforce" "nohash"
  11. cp -f "${tmpdir}/${remotebuildfilename}" "${serverfiles}/${executable#./}"
  12. }
  13. fn_update_localbuild() {
  14. # Gets local build info.
  15. fn_print_dots "Checking local build: ${remotelocation}"
  16. # Uses executable to get local build.
  17. if [ -d "${executabledir}" ]; then
  18. cd "${executabledir}" || exit
  19. localbuild=$(unzip -p "minecraft_server.jar" version.json | jq -r '.id')
  20. fi
  21. if [ -z "${localbuild}" ]; then
  22. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  23. fn_script_log_error "Missing local build info"
  24. fn_script_log_error "Set localbuild to 0"
  25. localbuild="0"
  26. else
  27. fn_print_ok "Checking local build: ${remotelocation}"
  28. fn_script_log_pass "Checking local build"
  29. fi
  30. }
  31. fn_update_remotebuild() {
  32. # Get remote build info.
  33. apiurl="https://launchermeta.mojang.com/mc/game/version_manifest.json"
  34. remotebuildresponse=$(curl -s "${apiurl}")
  35. # Latest release.
  36. if [ "${branch}" == "release" ] && [ "${mcversion}" == "latest" ]; then
  37. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r '.latest.release')
  38. # Latest snapshot.
  39. elif [ "${branch}" == "snapshot" ] && [ "${mcversion}" == "latest" ]; then
  40. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r '.latest.snapshot')
  41. # Specific release/snapshot.
  42. else
  43. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r --arg branch "${branch}" --arg mcversion "${mcversion}" '.versions | .[] | select(.type==$branch and .id==$mcversion) | .id')
  44. fi
  45. remotebuildfilename="minecraft_server.${remotebuildversion}.jar"
  46. # Generate link to version manifest json.
  47. remotebuildmanifest=$(echo "${remotebuildresponse}" | jq -r --arg branch "${branch}" --arg mcversion "${remotebuildversion}" '.versions | .[] | select(.type==$branch and .id==$mcversion) | .url')
  48. # Generate link to server.jar
  49. remotebuildurl=$(curl -s "${remotebuildmanifest}" | jq -r '.downloads.server.url')
  50. if [ "${firstcommandname}" != "INSTALL" ]; then
  51. fn_print_dots "Checking remote build: ${remotelocation}"
  52. # Checks if remotebuildversion variable has been set.
  53. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  54. fn_print_fail "Checking remote build: ${remotelocation}"
  55. fn_script_log_fatal "Checking remote build"
  56. core_exit.sh
  57. else
  58. fn_print_ok "Checking remote build: ${remotelocation}"
  59. fn_script_log_pass "Checking remote build"
  60. fi
  61. else
  62. # Checks if remotebuild variable has been set.
  63. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  64. fn_print_failure "Unable to get remote build"
  65. fn_script_log_fatal "Unable to get remote build"
  66. core_exit.sh
  67. fi
  68. fi
  69. }
  70. fn_update_compare() {
  71. fn_print_dots "Checking for update: ${remotelocation}"
  72. # Update has been found or force update.
  73. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  74. # Create update lockfile.
  75. date '+%s' > "${lockdir:?}/update.lock"
  76. fn_print_ok_nl "Checking for update: ${remotelocation}"
  77. echo -en "\n"
  78. echo -e "Update available"
  79. echo -e "* Local build: ${red}${localbuild}${default}"
  80. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  81. if [ -n "${branch}" ]; then
  82. echo -e "* Branch: ${branch}"
  83. fi
  84. if [ -f "${rootdir}/.dev-debug" ]; then
  85. echo -e "Remote build info"
  86. echo -e "* apiurl: ${apiurl}"
  87. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  88. echo -e "* remotebuildurl: ${remotebuildurl}"
  89. echo -e "* remotebuildversion: ${remotebuildversion}"
  90. fi
  91. echo -en "\n"
  92. fn_script_log_info "Update available"
  93. fn_script_log_info "Local build: ${localbuild}"
  94. fn_script_log_info "Remote build: ${remotebuildversion}"
  95. if [ -n "${branch}" ]; then
  96. fn_script_log_info "Branch: ${branch}"
  97. fi
  98. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  99. if [ "${commandname}" == "UPDATE" ]; then
  100. unset updateonstart
  101. check_status.sh
  102. # If server stopped.
  103. if [ "${status}" == "0" ]; then
  104. fn_update_dl
  105. if [ "${localbuild}" == "0" ]; then
  106. exitbypass=1
  107. command_start.sh
  108. fn_firstcommand_reset
  109. exitbypass=1
  110. sleep 5
  111. command_stop.sh
  112. fn_firstcommand_reset
  113. fi
  114. # If server started.
  115. else
  116. fn_print_restart_warning
  117. exitbypass=1
  118. command_stop.sh
  119. fn_firstcommand_reset
  120. exitbypass=1
  121. fn_update_dl
  122. exitbypass=1
  123. command_start.sh
  124. fn_firstcommand_reset
  125. fi
  126. unset exitbypass
  127. date +%s > "${lockdir}/last-updated.lock"
  128. alert="update"
  129. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  130. alert="check-update"
  131. fi
  132. alert.sh
  133. else
  134. fn_print_ok_nl "Checking for update: ${remotelocation}"
  135. echo -en "\n"
  136. echo -e "No update available"
  137. echo -e "* Local build: ${green}${localbuild}${default}"
  138. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  139. if [ -n "${branch}" ]; then
  140. echo -e "* Branch: ${branch}"
  141. fi
  142. echo -en "\n"
  143. fn_script_log_info "No update available"
  144. fn_script_log_info "Local build: ${localbuild}"
  145. fn_script_log_info "Remote build: ${remotebuildversion}"
  146. if [ -n "${branch}" ]; then
  147. fn_script_log_info "Branch: ${branch}"
  148. fi
  149. if [ -f "${rootdir}/.dev-debug" ]; then
  150. echo -e "Remote build info"
  151. echo -e "* apiurl: ${apiurl}"
  152. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  153. echo -e "* remotebuildurl: ${remotebuildurl}"
  154. echo -e "* remotebuildversion: ${remotebuildversion}"
  155. fi
  156. fi
  157. }
  158. # The location where the builds are checked and downloaded.
  159. remotelocation="mojang.com"
  160. if [ "${firstcommandname}" == "INSTALL" ]; then
  161. fn_update_remotebuild
  162. fn_update_dl
  163. else
  164. fn_print_dots "Checking for update"
  165. fn_print_dots "Checking for update: ${remotelocation}"
  166. fn_script_log_info "Checking for update: ${remotelocation}"
  167. fn_update_localbuild
  168. fn_update_remotebuild
  169. fn_update_compare
  170. fi