update_minecraft.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. core_exit.sh
  27. fi
  28. }
  29. fn_update_minecraft_localbuild(){
  30. # Gets local build info.
  31. fn_print_dots "Checking local build: ${remotelocation}"
  32. # Uses log file to gather info.
  33. # Gives time for log file to generate.
  34. if [ ! -f "${serverfiles}/logs/latest.log" ]; then
  35. fn_print_error "Checking local build: ${remotelocation}"
  36. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  37. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  38. fn_script_log_error "No log files containing version info"
  39. fn_script_log_info "Forcing server restart"
  40. exitbypass=1
  41. command_stop.sh
  42. exitbypass=1
  43. command_start.sh
  44. totalseconds=0
  45. # Check again, allow time to generate logs.
  46. while [ ! -f "${serverfiles}/logs/latest.log" ]; do
  47. sleep 1
  48. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  49. if [ -v "${loopignore}" ]; then
  50. loopignore=1
  51. fn_script_log_info "Waiting for log file to generate"
  52. fi
  53. if [ "${totalseconds}" -gt "120" ]; then
  54. localbuild="0"
  55. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  56. fn_script_log_error "Missing log file"
  57. fn_script_log_error "Set localbuild to 0"
  58. fi
  59. totalseconds=$((totalseconds + 1))
  60. done
  61. fi
  62. if [ -z "${localbuild}" ]; then
  63. 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])')
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. # Gives time for var to generate.
  67. totalseconds=0
  68. for seconds in {1..120}; do
  69. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  70. if [ -z "${loopignore}" ]; then
  71. loopignore=1
  72. fn_script_log_info "Waiting for local build to generate"
  73. fi
  74. 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])')
  75. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  76. break
  77. fi
  78. sleep 1
  79. totalseconds=$((totalseconds + 1))
  80. done
  81. fi
  82. if [ -z "${localbuild}" ]; then
  83. localbuild="0"
  84. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  85. fn_script_log_error "Missing local build info"
  86. fn_script_log_error "Set localbuild to 0"
  87. else
  88. fn_print_ok "Checking local build: ${remotelocation}"
  89. fn_script_log_pass "Checking local build"
  90. fi
  91. }
  92. fn_update_minecraft_remotebuild(){
  93. # Gets remote build info.
  94. if [ "${branch}" == "release" ]; then
  95. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  96. else
  97. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.versions[0].id')
  98. fi
  99. if [ "${installer}" != "1" ]; then
  100. fn_print_dots "Checking remote build: ${remotelocation}"
  101. # Checks if remotebuild variable has been set.
  102. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  103. fn_print_fail "Checking remote build: ${remotelocation}"
  104. fn_script_log_fatal "Checking remote build"
  105. core_exit.sh
  106. else
  107. fn_print_ok "Checking remote build: ${remotelocation}"
  108. fn_script_log_pass "Checking remote build"
  109. fi
  110. else
  111. # Checks if remotebuild variable has been set.
  112. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  113. fn_print_failure "Unable to get remote build"
  114. fn_script_log_fatal "Unable to get remote build"
  115. core_exit.sh
  116. fi
  117. fi
  118. }
  119. fn_update_minecraft_compare(){
  120. # Removes dots so if statement can compare version numbers.
  121. fn_print_dots "Checking for update: ${remotelocation}"
  122. if [ "${localbuild}" != "${remotebuild}" ]||[ "${forceupdate}" == "1" ]; then
  123. fn_print_ok_nl "Checking for update: ${remotelocation}"
  124. echo -en "\n"
  125. echo -e "Update available"
  126. echo -e "* Local build: ${red}${localbuild}${default}"
  127. echo -e "* Remote build: ${green}${remotebuild}${default}"
  128. if [ -n "${branch}" ]; then
  129. echo -e "* Branch: ${branch}"
  130. fi
  131. fn_script_log_info "Update available"
  132. fn_script_log_info "Local build: ${localbuild}"
  133. fn_script_log_info "Remote build: ${remotebuild}"
  134. fn_script_log_info "${localbuild} > ${remotebuild}"
  135. unset updateonstart
  136. check_status.sh
  137. # If server stopped.
  138. if [ "${status}" == "0" ]; then
  139. exitbypass=1
  140. fn_update_minecraft_dl
  141. exitbypass=1
  142. command_start.sh
  143. exitbypass=1
  144. command_stop.sh
  145. # If server started.
  146. else
  147. fn_stop_warning
  148. exitbypass=1
  149. command_stop.sh
  150. exitbypass=1
  151. fn_update_minecraft_dl
  152. exitbypass=1
  153. command_start.sh
  154. fi
  155. date +%s > "${lockdir}/lastupdate.lock"
  156. alert="update"
  157. alert.sh
  158. else
  159. fn_print_ok_nl "Checking for update: ${remotelocation}"
  160. echo -en "\n"
  161. echo -e "No update available"
  162. echo -e "* Local build: ${green}${localbuild}${default}"
  163. echo -e "* Remote build: ${green}${remotebuild}${default}"
  164. if [ -n "${branch}" ]; then
  165. echo -e "* Branch: ${branch}"
  166. fi
  167. fn_script_log_info "No update available"
  168. fn_script_log_info "Local build: ${localbuild}"
  169. fn_script_log_info "Remote build: ${remotebuild}"
  170. if [ -n "${branch}" ]; then
  171. fn_script_log_info "Branch: ${branch}"
  172. fi
  173. fi
  174. }
  175. fn_stop_warning(){
  176. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  177. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  178. totalseconds=3
  179. for seconds in {3..1}; do
  180. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  181. totalseconds=$((totalseconds - 1))
  182. sleep 1
  183. if [ "${seconds}" == "0" ]; then
  184. break
  185. fi
  186. done
  187. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  188. }
  189. # The location where the builds are checked and downloaded.
  190. remotelocation="mojang.com"
  191. if [ "${installer}" == "1" ]; then
  192. fn_update_minecraft_remotebuild
  193. fn_update_minecraft_dl
  194. else
  195. fn_print_dots "Checking for update: ${remotelocation}"
  196. fn_script_log_info "Checking for update: ${remotelocation}"
  197. fn_update_minecraft_localbuild
  198. fn_update_minecraft_remotebuild
  199. fn_update_minecraft_compare
  200. fi