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. local commandname="UPDATE"
  7. local commandaction="Update"
  8. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_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.${availablebuild}.jar"
  13. echo -e "copying to ${serverfiles}...\c"
  14. fn_script_log "Copying to ${serverfiles}"
  15. cp "${tmpdir}/minecraft_server.${availablebuild}.jar" "${serverfiles}/minecraft_server.jar"
  16. local exitcode=$?
  17. chmod u+x "${serverfiles}/minecraft_server.jar"
  18. if [ ${exitcode} -eq 0 ]; then
  19. fn_print_ok_eol_nl
  20. else
  21. fn_print_fail_eol_nl
  22. fi
  23. }
  24. fn_update_currentbuild(){
  25. # Gets current build info
  26. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  27. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  28. fn_print_error "Checking for update: mojang.com"
  29. sleep 0.5
  30. fn_print_error_nl "Checking for update: mojang.com: No logs with server version found"
  31. fn_script_log_error "Checking for update: mojang.com: No logs with server version found"
  32. sleep 0.5
  33. fn_print_info_nl "Checking for update: mojang.com: Forcing server restart"
  34. fn_script_log_info "Checking for update: mojang.com: Forcing server restart"
  35. sleep 0.5
  36. exitbypass=1
  37. command_stop.sh
  38. exitbypass=1
  39. command_start.sh
  40. sleep 0.5
  41. # Check again and exit on failure.
  42. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  43. fn_print_fail_nl "Checking for update: mojang.com: Still No logs with server version found"
  44. fn_script_log_fatal "Checking for update: mojang.com: Still No logs with server version found"
  45. core_exit.sh
  46. fi
  47. fi
  48. # Get current build from logs
  49. currentbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  50. if [ -z "${currentbuild}" ]; then
  51. fn_print_error_nl "Checking for update: mojang.com: Current build version not found"
  52. fn_script_log_error "Checking for update: mojang.com: Current build version not found"
  53. sleep 0.5
  54. fn_print_info_nl "Checking for update: mojang.com: Forcing server restart"
  55. fn_script_log_info "Checking for update: mojang.com: Forcing server restart"
  56. exitbypass=1
  57. command_stop.sh
  58. exitbypass=1
  59. command_start.sh
  60. currentbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  61. if [ -z "${currentbuild}" ]; then
  62. fn_print_fail_nl "Checking for update: mojang.com: Current build version still not found"
  63. fn_script_log_fatal "Checking for update: mojang.com: Current build version still not found"
  64. core_exit.sh
  65. fi
  66. fi
  67. }
  68. fn_update_availablebuild(){
  69. # Gets latest build info.
  70. availablebuild=$(${curlpath} -s "https://launchermeta.mojang.com/mc/game/version_manifest.json" | jq -r '.latest.release')
  71. # Checks if availablebuild variable has been set
  72. if [ -z "${availablebuild}" ]; then
  73. fn_print_fail "Checking for update: mojang.com"
  74. sleep 0.5
  75. fn_print_fail "Checking for update: mojang.com: Not returning version info"
  76. fn_script_log_fatal "Failure! Checking for update: mojang.com: Not returning version info"
  77. core_exit.sh
  78. elif [ "${installer}" == "1" ]; then
  79. :
  80. else
  81. fn_print_ok_nl "Checking for update: mojang.com"
  82. fn_script_log_pass "Checking for update: mojang.com"
  83. sleep 0.5
  84. fi
  85. }
  86. fn_update_compare(){
  87. # Removes dots so if can compare version numbers
  88. currentbuilddigit=$(echo "${currentbuild}" | tr -cd '[:digit:]')
  89. availablebuilddigit=$(echo "${availablebuild}" | tr -cd '[:digit:]')
  90. if [ "${currentbuilddigit}" -ne "${availablebuilddigit}" ]; then
  91. echo -e "\n"
  92. echo -e "Update available:"
  93. sleep 0.5
  94. echo -e " Current build: ${red}${currentbuild}${default}"
  95. echo -e " Available build: ${green}${availablebuild}${default}"
  96. echo -e ""
  97. sleep 0.5
  98. echo ""
  99. echo -en "Applying update.\r"
  100. sleep 1
  101. echo -en "Applying update..\r"
  102. sleep 1
  103. echo -en "Applying update...\r"
  104. sleep 1
  105. echo -en "\n"
  106. fn_script_log "Update available"
  107. fn_script_log "Current build: ${currentbuild}"
  108. fn_script_log "Available build: ${availablebuild}"
  109. fn_script_log "${currentbuild} > ${availablebuild}"
  110. unset updateonstart
  111. check_status.sh
  112. if [ "${status}" == "0" ]; then
  113. fn_update_dl
  114. exitbypass=1
  115. command_start.sh
  116. exitbypass=1
  117. command_stop.sh
  118. else
  119. exitbypass=1
  120. command_stop.sh
  121. fn_update_dl
  122. exitbypass=1
  123. command_start.sh
  124. fi
  125. alert="update"
  126. alert.sh
  127. else
  128. echo -e "\n"
  129. echo -e "No update available:"
  130. echo -e " Current version: ${green}${currentbuild}${default}"
  131. echo -e " Available version: ${green}${availablebuild}${default}"
  132. echo -e ""
  133. fn_print_ok_nl "No update available"
  134. fn_script_log_info "Current build: ${currentbuild}"
  135. fn_script_log_info "Available build: ${availablebuild}"
  136. fi
  137. }
  138. if [ "${installer}" == "1" ]; then
  139. fn_update_availablebuild
  140. fn_update_dl
  141. else
  142. # Checks for server update from mojang.com
  143. fn_print_dots "Checking for update: mojang.com"
  144. fn_script_log_info "Checking for update: mojang.com"
  145. sleep 0.5
  146. fn_update_currentbuild
  147. fn_update_availablebuild
  148. fn_update_compare
  149. fi