update_minecraft.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 local build: ${remotelocation}"
  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 local build: ${remotelocation}"
  38. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  39. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  40. fn_script_log_error "No log files containing version info"
  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 local build: ${remotelocation}: 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 local build: ${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 local build: ${remotelocation}: 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 local build: ${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 local build: ${remotelocation}"
  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 remote build: ${remotelocation}"
  103. # Checks if remotebuild variable has been set.
  104. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  105. fn_print_fail "Checking remote build: ${remotelocation}"
  106. fn_script_log_fatal "Checking remote build"
  107. core_exit.sh
  108. else
  109. fn_print_ok "Checking remote build: ${remotelocation}"
  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 -en "\n"
  127. echo -e "Update available"
  128. echo -e "* Local build: ${red}${localbuild}${default}"
  129. echo -e "* Remote build: ${green}${remotebuild}${default}"
  130. if [ -n "${branch}" ]; then
  131. echo -e "* Branch: ${branch}"
  132. fi
  133. fn_script_log_info "Update available"
  134. fn_script_log_info "Local build: ${localbuild}"
  135. fn_script_log_info "Remote build: ${remotebuild}"
  136. fn_script_log_info "${localbuild} > ${remotebuild}"
  137. unset updateonstart
  138. check_status.sh
  139. # If server stopped.
  140. if [ "${status}" == "0" ]; then
  141. exitbypass=1
  142. fn_update_minecraft_dl
  143. exitbypass=1
  144. command_start.sh
  145. exitbypass=1
  146. command_stop.sh
  147. # If server started.
  148. else
  149. fn_stop_warning
  150. exitbypass=1
  151. command_stop.sh
  152. exitbypass=1
  153. fn_update_minecraft_dl
  154. exitbypass=1
  155. command_start.sh
  156. fi
  157. date +%s > "${lockdir}/lastupdate.lock"
  158. alert="update"
  159. alert.sh
  160. else
  161. fn_print_ok_nl "Checking for update: ${remotelocation}"
  162. echo -en "\n"
  163. echo -e "No update available"
  164. echo -e "* Local build: ${green}${localbuild}${default}"
  165. echo -e "* Remote build: ${green}${remotebuild}${default}"
  166. if [ -n "${branch}" ]; then
  167. echo -e "* Branch: ${branch}"
  168. fi
  169. fn_script_log_info "No update available"
  170. fn_script_log_info "Local build: ${localbuild}"
  171. fn_script_log_info "Remote build: ${remotebuild}"
  172. if [ -n "${branch}" ]; then
  173. fn_script_log_info "Branch: ${branch}"
  174. fi
  175. fi
  176. }
  177. fn_stop_warning(){
  178. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  179. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  180. totalseconds=3
  181. for seconds in {3..1}; do
  182. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  183. totalseconds=$((totalseconds - 1))
  184. sleep 1
  185. if [ "${seconds}" == "0" ]; then
  186. break
  187. fi
  188. done
  189. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  190. }
  191. # The location where the builds are checked and downloaded.
  192. remotelocation="mojang.com"
  193. if [ "${installer}" == "1" ]; then
  194. fn_update_minecraft_remotebuild
  195. fn_update_minecraft_dl
  196. else
  197. fn_print_dots "Checking for update: ${remotelocation}"
  198. fn_script_log_info "Checking for update: ${remotelocation}"
  199. fn_update_minecraft_localbuild
  200. fn_update_minecraft_remotebuild
  201. fn_update_minecraft_compare
  202. fi