update_minecraft.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. functionselfname="$(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. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  73. fn_print_ok_nl "Checking for update: ${remotelocation}"
  74. echo -en "\n"
  75. echo -e "Update available"
  76. echo -e "* Local build: ${red}${localbuild}${default}"
  77. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  78. if [ -n "${branch}" ]; then
  79. echo -e "* Branch: ${branch}"
  80. fi
  81. if [ -f "${rootdir}/.dev-debug" ]; then
  82. echo -e "Remote build info"
  83. echo -e "* apiurl: ${apiurl}"
  84. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  85. echo -e "* remotebuildurl: ${remotebuildurl}"
  86. echo -e "* remotebuildversion: ${remotebuildversion}"
  87. fi
  88. echo -en "\n"
  89. fn_script_log_info "Update available"
  90. fn_script_log_info "Local build: ${localbuild}"
  91. fn_script_log_info "Remote build: ${remotebuildversion}"
  92. if [ -n "${branch}" ]; then
  93. fn_script_log_info "Branch: ${branch}"
  94. fi
  95. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  96. if [ "${commandname}" == "UPDATE" ]; then
  97. unset updateonstart
  98. check_status.sh
  99. # If server stopped.
  100. if [ "${status}" == "0" ]; then
  101. fn_update_dl
  102. if [ "${localbuild}" == "0" ]; then
  103. exitbypass=1
  104. command_start.sh
  105. fn_firstcommand_reset
  106. exitbypass=1
  107. sleep 5
  108. command_stop.sh
  109. fn_firstcommand_reset
  110. fi
  111. # If server started.
  112. else
  113. fn_print_restart_warning
  114. exitbypass=1
  115. command_stop.sh
  116. fn_firstcommand_reset
  117. exitbypass=1
  118. fn_update_dl
  119. exitbypass=1
  120. command_start.sh
  121. fn_firstcommand_reset
  122. fi
  123. unset exitbypass
  124. date +%s > "${lockdir}/lastupdate.lock"
  125. alert="update"
  126. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  127. alert="check-update"
  128. fi
  129. alert.sh
  130. else
  131. fn_print_ok_nl "Checking for update: ${remotelocation}"
  132. echo -en "\n"
  133. echo -e "No update available"
  134. echo -e "* Local build: ${green}${localbuild}${default}"
  135. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  136. if [ -n "${branch}" ]; then
  137. echo -e "* Branch: ${branch}"
  138. fi
  139. echo -en "\n"
  140. fn_script_log_info "No update available"
  141. fn_script_log_info "Local build: ${localbuild}"
  142. fn_script_log_info "Remote build: ${remotebuildversion}"
  143. if [ -n "${branch}" ]; then
  144. fn_script_log_info "Branch: ${branch}"
  145. fi
  146. if [ -f "${rootdir}/.dev-debug" ]; then
  147. echo -e "Remote build info"
  148. echo -e "* apiurl: ${apiurl}"
  149. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  150. echo -e "* remotebuildurl: ${remotebuildurl}"
  151. echo -e "* remotebuildversion: ${remotebuildversion}"
  152. fi
  153. fi
  154. }
  155. # The location where the builds are checked and downloaded.
  156. remotelocation="mojang.com"
  157. if [ "${firstcommandname}" == "INSTALL" ]; then
  158. fn_update_remotebuild
  159. fn_update_dl
  160. else
  161. fn_print_dots "Checking for update"
  162. fn_print_dots "Checking for update: ${remotelocation}"
  163. fn_script_log_info "Checking for update: ${remotelocation}"
  164. fn_update_localbuild
  165. fn_update_remotebuild
  166. fn_update_compare
  167. fi