update_minecraft.sh 5.3 KB

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