update_minecraft.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. totalseconds=0
  71. for seconds in {1..120}; do
  72. fn_print_info "Checking for update: ${remotelocation}: checking local build: waiting for local build: ${totalseconds}"
  73. if [ -z "${loopignore}" ]; then
  74. loopignore=1
  75. fn_script_log_info "Waiting for local build to generate"
  76. fi
  77. 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}")
  78. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  79. break
  80. fi
  81. sleep 1
  82. totalseconds=$((totalseconds + 1))
  83. done
  84. fi
  85. if [ -z "${localbuild}" ]; then
  86. localbuild="0"
  87. fn_print_error "Checking for update: ${remotelocation}: waiting for local build: missing local build info"
  88. fn_script_log_error "Missing local build info"
  89. fn_script_log_error "Set localbuild to 0"
  90. else
  91. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  92. fn_script_log_pass "Checking local build"
  93. fi
  94. sleep 0.5
  95. }
  96. fn_update_minecraft_remotebuild(){
  97. # Gets remote build info.
  98. remotebuild=$(${curlpath} -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  99. if [ "${installer}" != "1" ]; then
  100. fn_print_dots "Checking for update: ${remotelocation}: checking remote build"
  101. sleep 0.5
  102. # Checks if remotebuild variable has been set.
  103. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  104. fn_print_fail "Checking for update: ${remotelocation}: checking remote build"
  105. fn_script_log_fatal "Checking remote build"
  106. core_exit.sh
  107. else
  108. fn_print_ok "Checking for update: ${remotelocation}: checking remote build"
  109. fn_script_log_pass "Checking remote build"
  110. sleep 0.5
  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. sleep 0.5
  125. localbuilddigit=$(echo "${localbuild}" | tr -cd '[:digit:]')
  126. remotebuilddigit=$(echo "${remotebuild}" | tr -cd '[:digit:]')
  127. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  128. fn_print_ok_nl "Checking for update: ${remotelocation}"
  129. sleep 0.5
  130. echo -en "\n"
  131. echo -e "Update available"
  132. echo -e "* Local build: ${red}${localbuild}${default}"
  133. echo -e "* Remote build: ${green}${remotebuild}${default}"
  134. fn_script_log_info "Update available"
  135. fn_script_log_info "Local build: ${localbuild}"
  136. fn_script_log_info "Remote build: ${remotebuild}"
  137. fn_script_log_info "${localbuild} > ${remotebuild}"
  138. sleep 0.5
  139. echo -en "\n"
  140. echo -en "applying update.\r"
  141. sleep 1
  142. echo -en "applying update..\r"
  143. sleep 1
  144. echo -en "applying update...\r"
  145. sleep 1
  146. echo -en "\n"
  147. unset updateonstart
  148. check_status.sh
  149. # If server stopped.
  150. if [ "${status}" == "0" ]; then
  151. exitbypass=1
  152. fn_update_minecraft_dl
  153. exitbypass=1
  154. command_start.sh
  155. exitbypass=1
  156. command_stop.sh
  157. # If server started.
  158. else
  159. exitbypass=1
  160. command_stop.sh
  161. exitbypass=1
  162. fn_update_minecraft_dl
  163. exitbypass=1
  164. command_start.sh
  165. fi
  166. alert="update"
  167. alert.sh
  168. else
  169. fn_print_ok_nl "Checking for update: ${remotelocation}"
  170. sleep 0.5
  171. echo -en "\n"
  172. echo -e "No update available"
  173. echo -e "* Local build: ${green}${localbuild}${default}"
  174. echo -e "* Remote build: ${green}${remotebuild}${default}"
  175. fn_script_log_info "No update available"
  176. fn_script_log_info "Local build: ${localbuild}"
  177. fn_script_log_info "Remote build: ${remotebuild}"
  178. fi
  179. }
  180. # The location where the builds are checked and downloaded.
  181. remotelocation="mojang.com"
  182. if [ "${installer}" == "1" ]; then
  183. fn_update_minecraft_remotebuild
  184. fn_update_minecraft_dl
  185. else
  186. fn_print_dots "Checking for update: ${remotelocation}"
  187. fn_script_log_info "Checking for update: ${remotelocation}"
  188. sleep 0.5
  189. fn_update_minecraft_localbuild
  190. fn_update_minecraft_remotebuild
  191. fn_update_minecraft_compare
  192. fi