4
0

update_mc.sh 6.3 KB

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