4
0

update_pmc.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash
  2. # LinuxGSM update_pmc.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of PaperMC and Waterfall servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. # Download and extract files to serverfiles.
  10. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "force" "${remotebuildhash}"
  11. cp -f "${tmpdir}/${remotebuildfilename}" "${serverfiles}/${executable#./}"
  12. echo "${remotebuild}" > "${serverfiles}/build.txt"
  13. fn_clear_tmp
  14. }
  15. fn_update_localbuild() {
  16. # Gets local build info.
  17. fn_print_dots "Checking local build: ${remotelocation}"
  18. # Uses build file to get local build.
  19. localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
  20. if [ -z "${localbuild}" ]; then
  21. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  22. fn_script_log_error "Missing local build info"
  23. fn_script_log_error "Set localbuild to 0"
  24. localbuild="0"
  25. else
  26. fn_print_ok "Checking local build: ${remotelocation}"
  27. fn_script_log_pass "Checking local build"
  28. fi
  29. }
  30. fn_update_remotebuild() {
  31. jq_versions_all='.versions | to_entries | map(.value[])'
  32. # Gets remote build info.
  33. apiurl="https://fill.papermc.io/v3/projects"
  34. # Get list of projects.
  35. remotebuildresponse=$(curl -s "${apiurl}")
  36. # Get list of Minecraft versions for project.
  37. remotebuildresponseproject=$(curl -s "${apiurl}/${paperproject}")
  38. # Get latest Minecraft: Java Edition version or user specified version.
  39. if [ "${mcversion}" == "latest" ]; then
  40. remotebuildmcversion=$(echo "${remotebuildresponseproject}" | jq -r "$jq_versions_all | first")
  41. else
  42. # Checks if user specified version exists.
  43. remotebuildmcversion=$(echo "${remotebuildresponseproject}" | jq -r -e --arg mcversion "${mcversion}" "$jq_versions_all | .[] | select(. == \$mcversion)")
  44. if [ -z "${remotebuildmcversion}" ]; then
  45. # user passed version does not exist
  46. fn_print_error_nl "Version ${mcversion} not available from ${remotelocation}"
  47. fn_script_log_error "Version ${mcversion} not available from ${remotelocation}"
  48. core_exit.sh
  49. fi
  50. fi
  51. # Get list of paper builds for specific Minecraft: Java Edition version.
  52. remotebuildresponsemcversion=$(curl -s "${apiurl}/${paperproject}/versions/${remotebuildmcversion}")
  53. # Get latest paper build for specific Minecraft: Java Edition version.
  54. remotebuildpaperversion=$(echo "${remotebuildresponsemcversion}" | jq -r '.builds[0]')
  55. # Get various info about the paper build.
  56. remotebuildresponseversion=$(curl -s "${apiurl}/${paperproject}/versions/${remotebuildmcversion}/builds/${remotebuildpaperversion}")
  57. remotebuildfilename=$(echo "${remotebuildresponseversion}" | jq -r '.downloads["server:default"].name')
  58. remotebuildhash=$(echo "${remotebuildresponseversion}" | jq -r '.downloads["server:default"].checksums.sha256')
  59. remotebuildurl=$(echo "${remotebuildresponseversion}" | jq -r '.downloads["server:default"].url')
  60. # Combines Minecraft: Java Edition version and paper build. e.g 1.16.5-456
  61. remotebuild="${remotebuildmcversion}-${remotebuildpaperversion}"
  62. if [ "${firstcommandname}" != "INSTALL" ]; then
  63. fn_print_dots "Checking remote build: ${remotelocation}"
  64. # Checks if remotebuild variable has been set.
  65. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  66. fn_print_fail "Checking remote build: ${remotelocation}"
  67. fn_script_log_fail "Checking remote build"
  68. core_exit.sh
  69. else
  70. fn_print_ok "Checking remote build: ${remotelocation}"
  71. fn_script_log_pass "Checking remote build"
  72. fi
  73. else
  74. # Checks if remotebuild variable has been set.
  75. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  76. fn_print_failure "Unable to get remote build"
  77. fn_script_log_fail "Unable to get remote build"
  78. core_exit.sh
  79. fi
  80. fi
  81. }
  82. fn_update_compare() {
  83. fn_print_dots "Checking for update: ${remotelocation}"
  84. # Update has been found or force update.
  85. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  86. # Create update lockfile.
  87. date '+%s' > "${lockdir:?}/update.lock"
  88. fn_print_ok_nl "Checking for update: ${remotelocation}"
  89. fn_print "\n"
  90. fn_print_nl "${bold}${underline}Update${default} available"
  91. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  92. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  93. if [ -n "${branch}" ]; then
  94. fn_print_nl "* Branch: ${branch}"
  95. fi
  96. if [ -f "${rootdir}/.dev-debug" ]; then
  97. fn_print_nl "Remote build info"
  98. fn_print_nl "* apiurl: ${apiurl}"
  99. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  100. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  101. fn_print_nl "* remotebuild: ${remotebuild}"
  102. fi
  103. fn_print "\n"
  104. fn_script_log_info "Update available"
  105. fn_script_log_info "Local build: ${localbuild}"
  106. fn_script_log_info "Remote build: ${remotebuild}"
  107. if [ -n "${branch}" ]; then
  108. fn_script_log_info "Branch: ${branch}"
  109. fi
  110. fn_script_log_info "${localbuild} > ${remotebuild}"
  111. if [ "${commandname}" == "UPDATE" ]; then
  112. date +%s > "${lockdir:?}/last-updated.lock"
  113. unset updateonstart
  114. check_status.sh
  115. # If server stopped.
  116. if [ "${status}" == "0" ]; then
  117. fn_update_dl
  118. if [ "${localbuild}" == "0" ]; then
  119. exitbypass=1
  120. command_start.sh
  121. fn_firstcommand_reset
  122. exitbypass=1
  123. fn_sleep_time_5
  124. command_stop.sh
  125. fn_firstcommand_reset
  126. fi
  127. # If server started.
  128. else
  129. fn_print_restart_warning
  130. exitbypass=1
  131. command_stop.sh
  132. fn_firstcommand_reset
  133. exitbypass=1
  134. fn_update_dl
  135. exitbypass=1
  136. command_start.sh
  137. fn_firstcommand_reset
  138. fi
  139. unset exitbypass
  140. alert="update"
  141. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  142. alert="check-update"
  143. fi
  144. alert.sh
  145. else
  146. fn_print_ok_nl "Checking for update: ${remotelocation}"
  147. fn_print "\n"
  148. fn_print_nl "${bold}${underline}No update${default} available"
  149. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  150. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  151. if [ -n "${branch}" ]; then
  152. fn_print_nl "* Branch: ${branch}"
  153. fi
  154. fn_print "\n"
  155. fn_script_log_info "No update available"
  156. fn_script_log_info "Local build: ${localbuild}"
  157. fn_script_log_info "Remote build: ${remotebuild}"
  158. if [ -n "${branch}" ]; then
  159. fn_script_log_info "Branch: ${branch}"
  160. fi
  161. if [ -f "${rootdir}/.dev-debug" ]; then
  162. fn_print_nl "Remote build info"
  163. fn_print_nl "* apiurl: ${apiurl}"
  164. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  165. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  166. fn_print_nl "* remotebuild: ${remotebuild}"
  167. fi
  168. fi
  169. }
  170. # The location where the builds are checked and downloaded.
  171. remotelocation="papermc.io"
  172. if [ "${shortname}" == "pmc" ]; then
  173. paperproject="paper"
  174. elif [ "${shortname}" == "vpmc" ]; then
  175. paperproject="velocity"
  176. elif [ "${shortname}" == "wmc" ]; then
  177. paperproject="waterfall"
  178. fi
  179. if [ "${firstcommandname}" == "INSTALL" ]; then
  180. fn_update_remotebuild
  181. fn_update_dl
  182. else
  183. fn_print_dots "Checking for update"
  184. fn_print_dots "Checking for update: ${remotelocation}"
  185. fn_script_log_info "Checking for update: ${remotelocation}"
  186. fn_update_localbuild
  187. fn_update_remotebuild
  188. fn_update_compare
  189. fi