update_minecraft.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. localbuild=$(grep -i version "${consolelogdir}"/* | tail -1 | sed 's/.*[Vv]ersion //' | sed 's/\r//g' 2>/dev/null)
  35. if [ -z "${localbuild}" ]; 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. fn_firstcommand_reset
  44. exitbypass=1
  45. command_start.sh
  46. fn_firstcommand_reset
  47. totalseconds=0
  48. localbuild=$(grep -i version "${consolelogdir}"/* | tail -1 | sed 's/.*[Vv]ersion //' | sed 's/\r//g' 2>/dev/null)
  49. while [ -z "${localbuild}" ]; do
  50. sleep 1
  51. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  52. if [ -v "${loopignore}" ]; then
  53. loopignore=1
  54. fn_script_log_info "Waiting for log file to generate"
  55. fi
  56. localbuild=$(grep -i version "${consolelogdir}"/* | tail -1 | sed 's/.*[Vv]ersion //' | sed 's/\r//g' 2>/dev/null)
  57. if [ "${totalseconds}" -gt "120" ]; then
  58. localbuild="0"
  59. fn_print_error "Checking local build: ${remotelocation}: waiting for log file"
  60. fn_script_log_error "Local build did not generate"
  61. fn_script_log_error "Required log file may be missing"
  62. fn_script_log_error "Local build set to 0"
  63. fi
  64. totalseconds=$((totalseconds + 1))
  65. done
  66. fi
  67. if [ "${localbuild}" != "0" ]; then
  68. fn_print_ok "Checking local build: ${remotelocation}"
  69. fn_script_log_pass "Checking local build"
  70. fi
  71. }
  72. fn_update_minecraft_remotebuild(){
  73. # Gets remote build info.
  74. if [ "${branch}" == "release" ]; then
  75. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  76. else
  77. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.versions[0].id')
  78. fi
  79. if [ "${firstcommandname}" != "INSTALL" ]; then
  80. fn_print_dots "Checking remote build: ${remotelocation}"
  81. # Checks if remotebuild variable has been set.
  82. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  83. fn_print_fail "Checking remote build: ${remotelocation}"
  84. fn_script_log_fatal "Checking remote build"
  85. core_exit.sh
  86. else
  87. fn_print_ok "Checking remote build: ${remotelocation}"
  88. fn_script_log_pass "Checking remote build"
  89. fi
  90. else
  91. # Checks if remotebuild variable has been set.
  92. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  93. fn_print_failure "Unable to get remote build"
  94. fn_script_log_fatal "Unable to get remote build"
  95. core_exit.sh
  96. fi
  97. fi
  98. }
  99. fn_update_minecraft_compare(){
  100. # Removes dots so if statement can compare version numbers.
  101. fn_print_dots "Checking for update: ${remotelocation}"
  102. if [ "${localbuild}" != "${remotebuild}" ]||[ "${forceupdate}" == "1" ]; then
  103. fn_print_ok_nl "Checking for update: ${remotelocation}"
  104. echo -en "\n"
  105. echo -e "Update available"
  106. echo -e "* Local build: ${red}${localbuild}${default}"
  107. echo -e "* Remote build: ${green}${remotebuild}${default}"
  108. echo -en "\n"
  109. if [ -n "${branch}" ]; then
  110. echo -e "* Branch: ${branch}"
  111. fi
  112. fn_script_log_info "Update available"
  113. fn_script_log_info "Local build: ${localbuild}"
  114. fn_script_log_info "Remote build: ${remotebuild}"
  115. fn_script_log_info "${localbuild} > ${remotebuild}"
  116. unset updateonstart
  117. check_status.sh
  118. # If server stopped.
  119. if [ "${status}" == "0" ]; then
  120. exitbypass=1
  121. fn_update_minecraft_dl
  122. exitbypass=1
  123. command_start.sh
  124. exitbypass=1
  125. command_stop.sh
  126. fn_firstcommand_reset
  127. # If server started.
  128. else
  129. fn_print_restart_warning
  130. exitbypass=1
  131. command_stop.sh
  132. fn_firstcommand_reset
  133. exitbypass=1
  134. fn_update_minecraft_dl
  135. exitbypass=1
  136. command_start.sh
  137. fn_firstcommand_reset
  138. fi
  139. unset exitbypass
  140. date +%s > "${lockdir}/lastupdate.lock"
  141. alert="update"
  142. alert.sh
  143. else
  144. fn_print_ok_nl "Checking for update: ${remotelocation}"
  145. echo -en "\n"
  146. echo -e "No update available"
  147. echo -e "* Local build: ${green}${localbuild}${default}"
  148. echo -e "* Remote build: ${green}${remotebuild}${default}"
  149. if [ -n "${branch}" ]; then
  150. echo -e "* Branch: ${branch}"
  151. fi
  152. echo -en "\n"
  153. fn_script_log_info "No update available"
  154. fn_script_log_info "Local build: ${localbuild}"
  155. fn_script_log_info "Remote build: ${remotebuild}"
  156. if [ -n "${branch}" ]; then
  157. fn_script_log_info "Branch: ${branch}"
  158. fi
  159. fi
  160. }
  161. # The location where the builds are checked and downloaded.
  162. remotelocation="mojang.com"
  163. if [ "${firstcommandname}" == "INSTALL" ]; then
  164. fn_update_minecraft_remotebuild
  165. fn_update_minecraft_dl
  166. else
  167. fn_print_dots "Checking for update"
  168. fn_print_dots "Checking for update: ${remotelocation}"
  169. fn_script_log_info "Checking for update: ${remotelocation}"
  170. fn_update_minecraft_localbuild
  171. fn_update_minecraft_remotebuild
  172. fn_update_minecraft_compare
  173. fi