update_minecraft.sh 5.2 KB

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