update_minecraft.sh 5.5 KB

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