update_minecraft.sh 7.0 KB

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