update_minecraft.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. sleep 0.5
  31. # Uses log file to gather info.
  32. # Gives time for log file to generate.
  33. if [ ! -f "${serverfiles}/logs/latest.log" ]; then
  34. fn_print_error "Checking for update: ${remotelocation}: checking local build"
  35. sleep 0.5
  36. fn_print_error_nl "Checking for update: ${remotelocation}: checking local build: no log files"
  37. fn_script_log_error "No log file found"
  38. sleep 0.5
  39. fn_print_info_nl "Checking for update: ${remotelocation}: checking local build: forcing server restart"
  40. fn_script_log_info "Forcing server restart"
  41. sleep 0.5
  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. sleep 0.5
  61. fi
  62. totalseconds=$((totalseconds + 1))
  63. done
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. localbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  67. fi
  68. if [ -z "${localbuild}" ]; then
  69. # Gives time for var to generate.
  70. end=$((SECONDS+120))
  71. totalseconds=0
  72. while [ "${SECONDS}" -lt "${end}" ]; do
  73. fn_print_info "Checking for update: ${remotelocation}: checking local build: waiting for local build: ${totalseconds}"
  74. if [ -z "${loopignore}" ]; then
  75. loopignore=1
  76. fn_script_log_info "Waiting for local build to generate"
  77. fi
  78. localbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}') grep -Eo "TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | grep -Eo "((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}")
  79. if [ "${localbuild}" ]; then
  80. break
  81. fi
  82. sleep 1
  83. totalseconds=$((totalseconds + 1))
  84. done
  85. fi
  86. if [ -z "${localbuild}" ]; then
  87. localbuild="0"
  88. fn_print_error "Checking for update: ${remotelocation}: waiting for local build: missing local build info"
  89. fn_script_log_error "Missing local build info"
  90. fn_script_log_error "Set localbuild to 0"
  91. else
  92. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  93. fn_script_log_pass "Checking local build"
  94. fi
  95. sleep 0.5
  96. }
  97. fn_update_minecraft_remotebuild(){
  98. # Gets remote build info.
  99. remotebuild=$(${curlpath} -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  100. if [ "${installer}" != "1" ]; then
  101. fn_print_dots "Checking for update: ${remotelocation}: checking remote build"
  102. sleep 0.5
  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. sleep 0.5
  112. fi
  113. else
  114. # Checks if remotebuild variable has been set.
  115. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  116. fn_print_failure "Unable to get remote build"
  117. fn_script_log_fatal "Unable to get remote build"
  118. core_exit.sh
  119. fi
  120. fi
  121. }
  122. fn_update_minecraft_compare(){
  123. # Removes dots so if statement can compare version numbers.
  124. fn_print_dots "Checking for update: ${remotelocation}"
  125. sleep 0.5
  126. localbuilddigit=$(echo "${localbuild}" | tr -cd '[:digit:]')
  127. remotebuilddigit=$(echo "${remotebuild}" | tr -cd '[:digit:]')
  128. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  129. fn_print_ok_nl "Checking for update: ${remotelocation}"
  130. sleep 0.5
  131. echo -en "\n"
  132. echo -e "Update available"
  133. echo -e "* Local build: ${red}${localbuild}${default}"
  134. echo -e "* Remote build: ${green}${remotebuild}${default}"
  135. fn_script_log_info "Update available"
  136. fn_script_log_info "Local build: ${localbuild}"
  137. fn_script_log_info "Remote build: ${remotebuild}"
  138. fn_script_log_info "${localbuild} > ${remotebuild}"
  139. sleep 0.5
  140. echo -en "\n"
  141. echo -en "applying update.\r"
  142. sleep 1
  143. echo -en "applying update..\r"
  144. sleep 1
  145. echo -en "applying update...\r"
  146. sleep 1
  147. echo -en "\n"
  148. unset updateonstart
  149. check_status.sh
  150. # If server stopped.
  151. if [ "${status}" == "0" ]; then
  152. exitbypass=1
  153. fn_update_minecraft_dl
  154. exitbypass=1
  155. command_start.sh
  156. exitbypass=1
  157. command_stop.sh
  158. # If server started.
  159. else
  160. exitbypass=1
  161. command_stop.sh
  162. exitbypass=1
  163. fn_update_minecraft_dl
  164. exitbypass=1
  165. command_start.sh
  166. fi
  167. alert="update"
  168. alert.sh
  169. else
  170. fn_print_ok_nl "Checking for update: ${remotelocation}"
  171. sleep 0.5
  172. echo -en "\n"
  173. echo -e "No update available"
  174. echo -e "* Local build: ${green}${localbuild}${default}"
  175. echo -e "* Remote build: ${green}${remotebuild}${default}"
  176. fn_script_log_info "No update available"
  177. fn_script_log_info "Local build: ${localbuild}"
  178. fn_script_log_info "Remote build: ${remotebuild}"
  179. fi
  180. }
  181. # The location where the builds are checked and downloaded.
  182. remotelocation="mojang.com"
  183. if [ "${installer}" == "1" ]; then
  184. fn_update_minecraft_remotebuild
  185. fn_update_minecraft_dl
  186. else
  187. fn_print_dots "Checking for update: ${remotelocation}"
  188. fn_script_log_info "Checking for update: ${remotelocation}"
  189. sleep 0.5
  190. fn_update_minecraft_localbuild
  191. fn_update_minecraft_remotebuild
  192. fn_update_minecraft_compare
  193. fi