update_minecraft.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. exitbypass=1
  44. command_start.sh
  45. totalseconds=0
  46. # Check again, allow time to generate logs.
  47. while [ ! -f "${serverfiles}/logs/latest.log" ]; do
  48. sleep 1
  49. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  50. if [ -v "${loopignore}" ]; then
  51. loopignore=1
  52. fn_script_log_info "Waiting for log file to generate"
  53. fi
  54. if [ "${totalseconds}" -gt "120" ]; then
  55. localbuild="0"
  56. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  57. fn_script_log_error "Missing log file"
  58. fn_script_log_error "Set localbuild to 0"
  59. fi
  60. totalseconds=$((totalseconds + 1))
  61. done
  62. fi
  63. if [ -z "${localbuild}" ]; then
  64. 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])')
  65. fi
  66. if [ -z "${localbuild}" ]; then
  67. # Gives time for var to generate.
  68. totalseconds=0
  69. for seconds in {1..120}; do
  70. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  71. if [ -z "${loopignore}" ]; then
  72. loopignore=1
  73. fn_script_log_info "Waiting for local build to generate"
  74. fi
  75. 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])')
  76. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  77. break
  78. fi
  79. sleep 1
  80. totalseconds=$((totalseconds + 1))
  81. done
  82. fi
  83. if [ -z "${localbuild}" ]; then
  84. localbuild="0"
  85. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  86. fn_script_log_error "Missing local build info"
  87. fn_script_log_error "Set localbuild to 0"
  88. else
  89. fn_print_ok "Checking local build: ${remotelocation}"
  90. fn_script_log_pass "Checking local build"
  91. fi
  92. }
  93. fn_update_minecraft_remotebuild(){
  94. # Gets remote build info.
  95. if [ "${branch}" == "release" ]; then
  96. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  97. else
  98. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.versions[0].id')
  99. fi
  100. if [ "${installer}" != "1" ]; then
  101. fn_print_dots "Checking remote build: ${remotelocation}"
  102. # Checks if remotebuild variable has been set.
  103. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  104. fn_print_fail "Checking remote build: ${remotelocation}"
  105. fn_script_log_fatal "Checking remote build"
  106. core_exit.sh
  107. else
  108. fn_print_ok "Checking remote build: ${remotelocation}"
  109. fn_script_log_pass "Checking remote build"
  110. fi
  111. else
  112. # Checks if remotebuild variable has been set.
  113. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  114. fn_print_failure "Unable to get remote build"
  115. fn_script_log_fatal "Unable to get remote build"
  116. core_exit.sh
  117. fi
  118. fi
  119. }
  120. fn_update_minecraft_compare(){
  121. # Removes dots so if statement can compare version numbers.
  122. fn_print_dots "Checking for update: ${remotelocation}"
  123. if [ "${localbuild}" != "${remotebuild}" ]||[ "${forceupdate}" == "1" ]; then
  124. fn_print_ok_nl "Checking for update: ${remotelocation}"
  125. echo -en "\n"
  126. echo -e "Update available"
  127. echo -e "* Local build: ${red}${localbuild}${default}"
  128. echo -e "* Remote build: ${green}${remotebuild}${default}"
  129. echo -en "\n"
  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. echo -en "\n"
  170. fn_script_log_info "No update available"
  171. fn_script_log_info "Local build: ${localbuild}"
  172. fn_script_log_info "Remote build: ${remotebuild}"
  173. if [ -n "${branch}" ]; then
  174. fn_script_log_info "Branch: ${branch}"
  175. fi
  176. fi
  177. }
  178. fn_stop_warning(){
  179. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  180. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  181. totalseconds=3
  182. for seconds in {3..1}; do
  183. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  184. totalseconds=$((totalseconds - 1))
  185. sleep 1
  186. if [ "${seconds}" == "0" ]; then
  187. break
  188. fi
  189. done
  190. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  191. }
  192. # The location where the builds are checked and downloaded.
  193. remotelocation="mojang.com"
  194. if [ "${installer}" == "1" ]; then
  195. fn_update_minecraft_remotebuild
  196. fn_update_minecraft_dl
  197. else
  198. fn_print_dots "Checking for update"
  199. fn_print_dots "Checking for update: ${remotelocation}"
  200. fn_script_log_info "Checking for update: ${remotelocation}"
  201. fn_update_minecraft_localbuild
  202. fn_update_minecraft_remotebuild
  203. fn_update_minecraft_compare
  204. fi
  205. if [ "${commandname}" != "INSTALL" ]; then
  206. core_exit.sh
  207. fi