update_minecraft.sh 6.6 KB

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