update_minecraft.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. # LGSM 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 ${filesdir}...\c"
  12. fn_script_log "Copying to ${filesdir}"
  13. cp "${tmpdir}/minecraft_server.${availablebuild}.jar" "${filesdir}/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 "${filesdir}/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 "${filesdir}/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=$(curl -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. else
  77. fn_print_ok_nl "Checking for update: mojang.com"
  78. fn_script_log_pass "Checking for update: mojang.com"
  79. sleep 1
  80. fi
  81. }
  82. fn_update_compare(){
  83. # Removes dots so if can compare version numbers
  84. currentbuilddigit=$(echo "${currentbuild}"|tr -cd '[:digit:]')
  85. availablebuilddigit=$(echo "${availablebuild}"|tr -cd '[:digit:]')
  86. if [ "${currentbuilddigit}" -ne "${availablebuilddigit}" ]; then
  87. echo -e "\n"
  88. echo -e "Update available:"
  89. sleep 1
  90. echo -e " Current build: ${red}${currentbuild}${default}"
  91. echo -e " Available build: ${green}${availablebuild}${default}"
  92. echo -e ""
  93. sleep 1
  94. echo ""
  95. echo -en "Applying update.\r"
  96. sleep 1
  97. echo -en "Applying update..\r"
  98. sleep 1
  99. echo -en "Applying update...\r"
  100. sleep 1
  101. echo -en "\n"
  102. fn_script_log "Update available"
  103. fn_script_log "Current build: ${currentbuild}"
  104. fn_script_log "Available build: ${availablebuild}"
  105. fn_script_log "${currentbuild} > ${availablebuild}"
  106. unset updateonstart
  107. check_status.sh
  108. if [ "${status}" == "0" ]; then
  109. fn_update_dl
  110. exitbypass=1
  111. command_start.sh
  112. exitbypass=1
  113. command_stop.sh
  114. else
  115. exitbypass=1
  116. command_stop.sh
  117. fn_update_dl
  118. exitbypass=1
  119. command_start.sh
  120. fi
  121. alert="update"
  122. alert.sh
  123. else
  124. echo -e "\n"
  125. echo -e "No update available:"
  126. echo -e " Current version: ${green}${currentbuild}${default}"
  127. echo -e " Available version: ${green}${availablebuild}${default}"
  128. echo -e ""
  129. fn_print_ok_nl "No update available"
  130. fn_script_log_info "Current build: ${currentbuild}"
  131. fn_script_log_info "Available build: ${availablebuild}"
  132. fi
  133. }
  134. if [ "${installer}" == "1" ]; then
  135. fn_update_availablebuild
  136. fn_update_dl
  137. else
  138. # Checks for server update from mojang.com
  139. fn_print_dots "Checking for update: mojang.com"
  140. fn_script_log_info "Checking for update: mojang.com"
  141. sleep 1
  142. fn_update_currentbuild
  143. fn_update_availablebuild
  144. fn_update_compare
  145. fi