update_mcb.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/bash
  2. # LinuxGSM update_mcb.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft Bedrock servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "bedrock_server.${remotebuild}.zip" "nochmodx" "norun" "noforce" "nohash"
  10. echo -e "Extracting to ${serverfiles}...\c"
  11. if [ "${firstcommandname}" == "INSTALL" ]; then
  12. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "server.properties" -d "${serverfiles}"
  13. else
  14. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "permissions.json" "server.properties" "allowlist.json" -d "${serverfiles}"
  15. fi
  16. exitcode=$?
  17. if [ "${exitcode}" -ne 0 ]; then
  18. fn_print_fail_eol_nl
  19. fn_script_log_fail "Extracting ${local_filename}"
  20. if [ -f "${lgsmlog}" ]; then
  21. echo -e "${extractcmd}" >> "${lgsmlog}"
  22. fi
  23. echo -e "${extractcmd}"
  24. fn_clear_tmp
  25. core_exit.sh
  26. else
  27. fn_print_ok_eol_nl
  28. if [ -f "${lgsmlog}" ]; then
  29. fn_script_log_pass "Extracting ${local_filename}"
  30. fi
  31. fn_clear_tmp
  32. fi
  33. }
  34. fn_update_localbuild() {
  35. # Gets local build info.
  36. fn_print_dots "Checking local build: ${remotelocation}"
  37. # Uses log file to get local build.
  38. localbuild=$(grep -hoP 'Version:\s*\K[\d.]+' "${consolelogdir}"/* 2> /dev/null | sort -V -r | head -n1)
  39. if [ -z "${localbuild}" ]; then
  40. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  41. fn_script_log_error "Missing local build info"
  42. fn_script_log_error "Set localbuild to 0"
  43. localbuild="0"
  44. else
  45. fn_print_ok "Checking local build: ${remotelocation}"
  46. fn_script_log_pass "Checking local build"
  47. fi
  48. }
  49. fn_update_remotebuild() {
  50. # Gets remote build info.
  51. apiurl="https://net-secondary.web.minecraft-services.net/api/v1.0/download/links"
  52. remotebuildresponse=$(curl -s "${apiurl}" | jq '.result.links[]')
  53. # Latest preview.
  54. if [ "${mcversion}" == "preview" ]; then
  55. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r 'select(.downloadType == "serverBedrockPreviewLinux") | .downloadUrl')
  56. # Latest release.
  57. else
  58. remotebuildurl=$(echo "${remotebuildresponse}" | jq -r 'select(.downloadType == "serverBedrockLinux") | .downloadUrl')
  59. fi
  60. remotebuild=$(echo "${remotebuildurl}" | grep -Eo "[.0-9]+[0-9]")
  61. remotebuildfilename="bedrock-server-${remotebuild}.zip"
  62. if [ "${firstcommandname}" != "INSTALL" ]; then
  63. fn_print_dots "Checking remote build: ${remotelocation}"
  64. # Checks if remotebuild variable has been set.
  65. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  66. fn_print_fail "Checking remote build: ${remotelocation}"
  67. fn_script_log_fail "Checking remote build"
  68. core_exit.sh
  69. else
  70. fn_print_ok "Checking remote build: ${remotelocation}"
  71. fn_script_log_pass "Checking remote build"
  72. fi
  73. else
  74. # Checks if remotebuild variable has been set.
  75. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  76. fn_print_failure "Unable to get remote build"
  77. fn_script_log_fail "Unable to get remote build"
  78. core_exit.sh
  79. fi
  80. fi
  81. }
  82. fn_update_compare() {
  83. fn_print_dots "Checking for update: ${remotelocation}"
  84. # Update has been found or force update.
  85. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  86. # Create update lockfile.
  87. date '+%s' > "${lockdir:?}/update.lock"
  88. fn_print_ok_nl "Checking for update: ${remotelocation}"
  89. fn_print "\n"
  90. fn_print_nl "${bold}${underline}Update${default} available"
  91. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  92. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  93. if [ -n "${branch}" ]; then
  94. fn_print_nl "* Branch: ${branch}"
  95. fi
  96. if [ -f "${rootdir}/.dev-debug" ]; then
  97. fn_print_nl "Remote build info"
  98. fn_print_nl "* apiurl: ${apiurl}"
  99. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  100. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  101. fn_print_nl "* remotebuild: ${remotebuild}"
  102. fi
  103. fn_print "\n"
  104. fn_script_log_info "Update available"
  105. fn_script_log_info "Local build: ${localbuild}"
  106. fn_script_log_info "Remote build: ${remotebuild}"
  107. if [ -n "${branch}" ]; then
  108. fn_script_log_info "Branch: ${branch}"
  109. fi
  110. fn_script_log_info "${localbuild} > ${remotebuild}"
  111. if [ "${commandname}" == "UPDATE" ]; then
  112. date +%s > "${lockdir:?}/last-updated.lock"
  113. unset updateonstart
  114. check_status.sh
  115. # If server stopped.
  116. if [ "${status}" == "0" ]; then
  117. fn_update_dl
  118. if [ "${localbuild}" == "0" ]; then
  119. exitbypass=1
  120. command_start.sh
  121. fn_firstcommand_reset
  122. exitbypass=1
  123. fn_sleep_time_5
  124. command_stop.sh
  125. fn_firstcommand_reset
  126. fi
  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_dl
  135. exitbypass=1
  136. command_start.sh
  137. fn_firstcommand_reset
  138. fi
  139. unset exitbypass
  140. alert="update"
  141. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  142. alert="check-update"
  143. fi
  144. alert.sh
  145. else
  146. fn_print_ok_nl "Checking for update: ${remotelocation}"
  147. fn_print "\n"
  148. fn_print_nl "${bold}${underline}No update${default} available"
  149. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  150. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  151. if [ -n "${branch}" ]; then
  152. fn_print_nl "* Branch: ${branch}"
  153. fi
  154. fn_print "\n"
  155. fn_script_log_info "No update available"
  156. fn_script_log_info "Local build: ${localbuild}"
  157. fn_script_log_info "Remote build: ${remotebuild}"
  158. if [ -n "${branch}" ]; then
  159. fn_script_log_info "Branch: ${branch}"
  160. fi
  161. if [ -f "${rootdir}/.dev-debug" ]; then
  162. fn_print_nl "Remote build info"
  163. fn_print_nl "* apiurl: ${apiurl}"
  164. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  165. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  166. fn_print_nl "* remotebuild: ${remotebuild}"
  167. fi
  168. fi
  169. }
  170. # The location where the builds are checked and downloaded.
  171. remotelocation="minecraft.net"
  172. if [ "${firstcommandname}" == "INSTALL" ]; then
  173. fn_update_remotebuild
  174. fn_update_dl
  175. else
  176. fn_print_dots "Checking for update"
  177. fn_print_dots "Checking for update: ${remotelocation}"
  178. fn_script_log_info "Checking for update: ${remotelocation}"
  179. fn_update_localbuild
  180. fn_update_remotebuild
  181. fn_update_compare
  182. fi