update_minecraft.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. # Generate link to version manifest json.
  9. remotebuildlink=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r --arg branch ${branch} --arg mcversion ${remotebuild} '.versions | .[] | select(.type==$branch and .id==$mcversion) | .url')
  10. # Generate link to server.jar
  11. remotebuildurl=$(curl -s "${remotebuildlink}" | jq -r '.downloads.server.url')
  12. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "minecraft_server.${remotebuild}.jar" "" "norun" "noforce" "nomd5"
  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. fn_clear_tmp
  25. core_exit.sh
  26. fi
  27. }
  28. fn_update_minecraft_localbuild(){
  29. # Gets local build info.
  30. fn_print_dots "Checking local build: ${remotelocation}"
  31. # Uses executable to find local build.
  32. cd "${executabledir}" || exit
  33. if [ -f "minecraft_server.jar" ]; then
  34. localbuild=$(unzip -p "minecraft_server.jar" version.json | jq -r '.id')
  35. fn_print_ok "Checking local build: ${remotelocation}"
  36. fn_script_log_pass "Checking local build"
  37. else
  38. localbuild="0"
  39. fn_print_error "Checking local build: ${remotelocation}"
  40. fn_script_log_error "Checking local build"
  41. fi
  42. }
  43. fn_update_minecraft_remotebuild(){
  44. # Gets remote build info.
  45. # Latest release.
  46. if [ "${branch}" == "release" ] && [ "${mcversion}" == "latest" ]; then
  47. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.release')
  48. # Latest snapshot.
  49. elif [ "${branch}" == "snapshot" ] && [ "${mcversion}" == "latest" ]; then
  50. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r '.latest.snapshot')
  51. # Specific release/snapshot.
  52. else
  53. remotebuild=$(curl -s "https://launchermeta.${remotelocation}/mc/game/version_manifest.json" | jq -r --arg branch ${branch} --arg mcversion ${mcversion} '.versions | .[] | select(.type==$branch and .id==$mcversion) | .id')
  54. fi
  55. if [ "${firstcommandname}" != "INSTALL" ]; then
  56. fn_print_dots "Checking remote build: ${remotelocation}"
  57. # Checks if remotebuild variable has been set.
  58. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  59. fn_print_fail "Checking remote build: ${remotelocation}"
  60. fn_script_log_fatal "Checking remote build"
  61. core_exit.sh
  62. else
  63. fn_print_ok "Checking remote build: ${remotelocation}"
  64. fn_script_log_pass "Checking remote build"
  65. fi
  66. else
  67. # Checks if remotebuild variable has been set.
  68. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  69. fn_print_failure "Unable to get remote build"
  70. fn_script_log_fatal "Unable to get remote build"
  71. core_exit.sh
  72. fi
  73. fi
  74. }
  75. fn_update_minecraft_compare(){
  76. # Removes dots so if statement can compare version numbers.
  77. fn_print_dots "Checking for update: ${remotelocation}"
  78. if [ "${localbuild}" != "${remotebuild}" ]||[ "${forceupdate}" == "1" ]; then
  79. fn_print_ok_nl "Checking for update: ${remotelocation}"
  80. echo -en "\n"
  81. echo -e "Update available"
  82. echo -e "* Local build: ${red}${localbuild}${default}"
  83. echo -e "* Remote build: ${green}${remotebuild}${default}"
  84. echo -en "\n"
  85. if [ -n "${branch}" ]; then
  86. echo -e "* Branch: ${branch}"
  87. fi
  88. fn_script_log_info "Update available"
  89. fn_script_log_info "Local build: ${localbuild}"
  90. fn_script_log_info "Remote build: ${remotebuild}"
  91. fn_script_log_info "${localbuild} > ${remotebuild}"
  92. unset updateonstart
  93. check_status.sh
  94. # If server stopped.
  95. if [ "${status}" == "0" ]; then
  96. exitbypass=1
  97. fn_update_minecraft_dl
  98. exitbypass=1
  99. command_start.sh
  100. exitbypass=1
  101. command_stop.sh
  102. fn_firstcommand_reset
  103. # If server started.
  104. else
  105. fn_print_restart_warning
  106. exitbypass=1
  107. command_stop.sh
  108. fn_firstcommand_reset
  109. exitbypass=1
  110. fn_update_minecraft_dl
  111. exitbypass=1
  112. command_start.sh
  113. fn_firstcommand_reset
  114. fi
  115. unset exitbypass
  116. date +%s > "${lockdir}/lastupdate.lock"
  117. alert="update"
  118. alert.sh
  119. else
  120. fn_print_ok_nl "Checking for update: ${remotelocation}"
  121. echo -en "\n"
  122. echo -e "No update available"
  123. echo -e "* Local build: ${green}${localbuild}${default}"
  124. echo -e "* Remote build: ${green}${remotebuild}${default}"
  125. if [ -n "${branch}" ]; then
  126. echo -e "* Branch: ${branch}"
  127. fi
  128. echo -en "\n"
  129. fn_script_log_info "No update available"
  130. fn_script_log_info "Local build: ${localbuild}"
  131. fn_script_log_info "Remote build: ${remotebuild}"
  132. if [ -n "${branch}" ]; then
  133. fn_script_log_info "Branch: ${branch}"
  134. fi
  135. fi
  136. }
  137. # The location where the builds are checked and downloaded.
  138. remotelocation="mojang.com"
  139. if [ "${firstcommandname}" == "INSTALL" ]; then
  140. fn_update_minecraft_remotebuild
  141. fn_update_minecraft_dl
  142. else
  143. fn_print_dots "Checking for update"
  144. fn_print_dots "Checking for update: ${remotelocation}"
  145. fn_script_log_info "Checking for update: ${remotelocation}"
  146. fn_update_minecraft_localbuild
  147. fn_update_minecraft_remotebuild
  148. fn_update_minecraft_compare
  149. fi