update_minecraft.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of Minecraft servers.
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_update_minecraft_dl(){
  8. if [ "${branch}" == "release" ]; then
  9. latestmcreleaselink=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release as $latest | .versions[] | select(.id == $latest) | .url')
  10. else
  11. latestmcreleaselink=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.versions[0].url')
  12. fi
  13. latestmcbuildurl=$(curl -s "${latestmcreleaselink}" | jq -r '.downloads.server.url')
  14. fn_fetch_file "${latestmcbuildurl}" "" "" "" "${tmpdir}" "minecraft_server.${remotebuild}.jar" "" "norun" "noforce" "nomd5"
  15. echo -e "copying to ${serverfiles}...\c"
  16. cp "${tmpdir}/minecraft_server.${remotebuild}.jar" "${serverfiles}/minecraft_server.jar"
  17. local exitcode=$?
  18. if [ "${exitcode}" == "0" ]; then
  19. fn_print_ok_eol_nl
  20. fn_script_log_pass "Copying to ${serverfiles}"
  21. chmod u+x "${serverfiles}/minecraft_server.jar"
  22. fn_clear_tmp
  23. else
  24. fn_print_fail_eol_nl
  25. fn_script_log_fatal "Copying to ${serverfiles}"
  26. fn_clear_tmp
  27. core_exit.sh
  28. fi
  29. }
  30. fn_update_minecraft_localbuild(){
  31. # Gets local build info.
  32. fn_print_dots "Checking local build: ${remotelocation}"
  33. # Uses log file to gather info.
  34. # Gives time for log file to generate.
  35. if [ ! -f "${serverfiles}/logs/latest.log" ]; then
  36. fn_print_error "Checking local build: ${remotelocation}"
  37. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  38. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  39. fn_script_log_error "No log files containing version info"
  40. fn_script_log_info "Forcing server restart"
  41. exitbypass=1
  42. command_stop.sh
  43. fn_firstcommand_reset
  44. exitbypass=1
  45. command_start.sh
  46. fn_firstcommand_reset
  47. totalseconds=0
  48. # Check again, allow time to generate logs.
  49. while [ ! -f "${serverfiles}/logs/latest.log" ]; do
  50. sleep 1
  51. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  52. if [ -v "${loopignore}" ]; then
  53. loopignore=1
  54. fn_script_log_info "Waiting for log file to generate"
  55. fi
  56. if [ "${totalseconds}" -gt "120" ]; then
  57. localbuild="0"
  58. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  59. fn_script_log_error "Missing log file"
  60. fn_script_log_error "Set localbuild to 0"
  61. fi
  62. totalseconds=$((totalseconds + 1))
  63. done
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. localbuild=$(grep version "${serverfiles}/logs/latest.log" | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}(-pre[0-9]+)?|([0-9]+w[0-9]+[a-z])')
  67. fi
  68. if [ -z "${localbuild}" ]; then
  69. # Gives time for var to generate.
  70. totalseconds=0
  71. for seconds in {1..120}; do
  72. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  73. if [ -z "${loopignore}" ]; then
  74. loopignore=1
  75. fn_script_log_info "Waiting for local build to generate"
  76. fi
  77. localbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}(-pre[0-9]+)?|([0-9]+w[0-9]+[a-z])')
  78. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  79. break
  80. fi
  81. sleep 1
  82. totalseconds=$((totalseconds + 1))
  83. done
  84. fi
  85. if [ -z "${localbuild}" ]; then
  86. localbuild="0"
  87. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  88. fn_script_log_error "Missing local build info"
  89. fn_script_log_error "Set localbuild to 0"
  90. else
  91. fn_print_ok "Checking local build: ${remotelocation}"
  92. fn_script_log_pass "Checking local build"
  93. fi
  94. }
  95. fn_update_minecraft_remotebuild(){
  96. # Gets remote build info.
  97. if [ "${branch}" == "release" ]; then
  98. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  99. else
  100. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.versions[0].id')
  101. fi
  102. if [ "${installer}" != "1" ]; then
  103. fn_print_dots "Checking remote build: ${remotelocation}"
  104. # Checks if remotebuild variable has been set.
  105. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  106. fn_print_fail "Checking remote build: ${remotelocation}"
  107. fn_script_log_fatal "Checking remote build"
  108. core_exit.sh
  109. else
  110. fn_print_ok "Checking remote build: ${remotelocation}"
  111. fn_script_log_pass "Checking remote build"
  112. fi
  113. else
  114. # Checks if remotebuild variable has been set.
  115. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  116. fn_print_failure "Unable to get remote build"
  117. fn_script_log_fatal "Unable to get remote build"
  118. core_exit.sh
  119. fi
  120. fi
  121. }
  122. fn_update_minecraft_compare(){
  123. # Removes dots so if statement can compare version numbers.
  124. fn_print_dots "Checking for update: ${remotelocation}"
  125. if [ "${localbuild}" != "${remotebuild}" ]||[ "${forceupdate}" == "1" ]; then
  126. fn_print_ok_nl "Checking for update: ${remotelocation}"
  127. echo -en "\n"
  128. echo -e "Update available"
  129. echo -e "* Local build: ${red}${localbuild}${default}"
  130. echo -e "* Remote build: ${green}${remotebuild}${default}"
  131. echo -en "\n"
  132. if [ -n "${branch}" ]; then
  133. echo -e "* Branch: ${branch}"
  134. fi
  135. fn_script_log_info "Update available"
  136. fn_script_log_info "Local build: ${localbuild}"
  137. fn_script_log_info "Remote build: ${remotebuild}"
  138. fn_script_log_info "${localbuild} > ${remotebuild}"
  139. unset updateonstart
  140. check_status.sh
  141. # If server stopped.
  142. if [ "${status}" == "0" ]; then
  143. exitbypass=1
  144. fn_update_minecraft_dl
  145. exitbypass=1
  146. command_start.sh
  147. exitbypass=1
  148. command_stop.sh
  149. fn_firstcommand_reset
  150. # If server started.
  151. else
  152. fn_print_restart_warning
  153. exitbypass=1
  154. command_stop.sh
  155. fn_firstcommand_reset
  156. exitbypass=1
  157. fn_update_minecraft_dl
  158. exitbypass=1
  159. command_start.sh
  160. fn_firstcommand_reset
  161. fi
  162. date +%s > "${lockdir}/lastupdate.lock"
  163. alert="update"
  164. alert.sh
  165. else
  166. fn_print_ok_nl "Checking for update: ${remotelocation}"
  167. echo -en "\n"
  168. echo -e "No update available"
  169. echo -e "* Local build: ${green}${localbuild}${default}"
  170. echo -e "* Remote build: ${green}${remotebuild}${default}"
  171. if [ -n "${branch}" ]; then
  172. echo -e "* Branch: ${branch}"
  173. fi
  174. echo -en "\n"
  175. fn_script_log_info "No update available"
  176. fn_script_log_info "Local build: ${localbuild}"
  177. fn_script_log_info "Remote build: ${remotebuild}"
  178. if [ -n "${branch}" ]; then
  179. fn_script_log_info "Branch: ${branch}"
  180. fi
  181. fi
  182. }
  183. # The location where the builds are checked and downloaded.
  184. remotelocation="mojang.com"
  185. if [ "${installer}" == "1" ]; then
  186. fn_update_minecraft_remotebuild
  187. fn_update_minecraft_dl
  188. else
  189. fn_print_dots "Checking for update"
  190. fn_print_dots "Checking for update: ${remotelocation}"
  191. fn_script_log_info "Checking for update: ${remotelocation}"
  192. fn_update_minecraft_localbuild
  193. fn_update_minecraft_remotebuild
  194. fn_update_minecraft_compare
  195. fi