update_papermc.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. # LinuxGSM update_papermc.sh function
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of PaperMC and Waterfall servers.
  7. commandname="UPDATE"
  8. commandaction="Update"
  9. function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_update_papermc_dl() {
  11. # get build info
  12. builddata=$(curl -s "https://${remotelocation}/api/v2/projects/${paperproject}/versions/${paperversion}/builds/${remotebuild}" | jq '.downloads')
  13. buildname=$(echo -e "${builddata}" | jq -r '.application.name')
  14. buildsha256=$(echo -e "${builddata}" | jq -r '.application.sha256')
  15. fn_fetch_file "https://${remotelocation}/api/v2/projects/${paperproject}/versions/${paperversion}/builds/${remotebuild}/downloads/${buildname}" "" "" "" "${tmpdir}" "${buildname}" "nochmodx" "norun" "force" "${buildsha256}"
  16. echo -e "copying to ${serverfiles}...\c"
  17. cp -f "${tmpdir}/${buildname}" "${serverfiles}/${executable#./}"
  18. local exitcode=$?
  19. if [ "${exitcode}" == "0" ]; then
  20. fn_print_ok_eol_nl
  21. fn_script_log_pass "Copying to ${serverfiles}"
  22. chmod u+x "${serverfiles}/${executable#./}"
  23. echo "${remotebuild}" > "${localversionfile}"
  24. fn_clear_tmp
  25. else
  26. fn_print_fail_eol_nl
  27. fn_script_log_fatal "Copying to ${serverfiles}"
  28. core_exit.sh
  29. fi
  30. }
  31. fn_update_papermc_localbuild() {
  32. # Gets local build info.
  33. fn_print_dots "Checking for update: ${remotelocation}: checking local build"
  34. sleep 0.5
  35. if [ ! -f "${localversionfile}" ]; then
  36. fn_print_error_nl "Checking for update: ${remotelocation}: checking local build: no local build files"
  37. fn_script_log_error "No local build file found"
  38. else
  39. localbuild=$(head -n 1 "${localversionfile}")
  40. fi
  41. if [ -z "${localbuild}" ]; then
  42. localbuild="0"
  43. fn_print_error "Checking for update: ${remotelocation}: waiting for local build: missing local build info"
  44. fn_script_log_error "Missing local build info, Set localbuild to 0"
  45. else
  46. fn_print_ok "Checking for update: ${remotelocation}: checking local build"
  47. fn_script_log_pass "Checking local build"
  48. fi
  49. sleep 0.5
  50. }
  51. fn_update_papermc_remotebuild() {
  52. # Gets remote build info.
  53. remotebuild=$(curl -s "https://${remotelocation}/api/v2/projects/${paperproject}/versions/${paperversion}" | jq -r '.builds[-1]')
  54. # Checks if remotebuild variable has been set.
  55. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  56. fn_print_failure "Unable to get remote build"
  57. fn_script_log_fatal "Unable to get remote build"
  58. core_exit.sh
  59. else
  60. fn_print_ok "Got build for version ${paperversion}"
  61. fn_script_log "Got build for version ${paperversion}"
  62. fi
  63. }
  64. fn_update_papermc_compare() {
  65. fn_print_dots "Checking for update: ${remotelocation}"
  66. sleep 0.5
  67. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  68. fn_print_ok_nl "Checking for update: ${remotelocation}"
  69. echo -en "\n"
  70. echo -e "Update available for version ${paperversion}"
  71. echo -e "* Local build: ${red}${localbuild}${default}"
  72. echo -e "* Remote build: ${green}${remotebuild}${default}"
  73. fn_script_log_info "Update available for version ${paperversion}"
  74. fn_script_log_info "Local build: ${localbuild}"
  75. fn_script_log_info "Remote build: ${remotebuild}"
  76. fn_script_log_info "${localbuild} > ${remotebuild}"
  77. echo -en "\n"
  78. echo -en "applying update.\r"
  79. echo -en "\n"
  80. unset updateonstart
  81. check_status.sh
  82. # If server stopped.
  83. if [ "${status}" == "0" ]; then
  84. fn_update_papermc_dl
  85. # If server started.
  86. else
  87. exitbypass=1
  88. command_stop.sh
  89. exitbypass=1
  90. fn_update_papermc_dl
  91. exitbypass=1
  92. command_start.sh
  93. fi
  94. alert="update"
  95. alert.sh
  96. else
  97. fn_print_ok_nl "Checking for update: ${remotelocation}"
  98. echo -en "\n"
  99. echo -e "No update available for version ${paperversion}"
  100. echo -e "* Local build: ${green}${localbuild}${default}"
  101. echo -e "* Remote build: ${green}${remotebuild}${default}"
  102. fn_script_log_info "No update available"
  103. fn_script_log_info "Local build: ${localbuild}"
  104. fn_script_log_info "Remote build: ${remotebuild}"
  105. fi
  106. }
  107. # The location where the builds are checked and downloaded.
  108. remotelocation="papermc.io"
  109. if [ "${shortname}" == "pmc" ]; then
  110. paperproject="paper"
  111. elif [ "${shortname}" == "vpmc" ]; then
  112. paperproject="velocity"
  113. elif [ "${shortname}" == "wmc" ]; then
  114. paperproject="waterfall"
  115. fi
  116. localversionfile="${datadir}/${paperproject}-version"
  117. # check if datadir was created, if not create it
  118. if [ ! -d "${datadir}" ]; then
  119. mkdir -p "${datadir}"
  120. fi
  121. # check version if the user did set one and check it
  122. if [ "${mcversion}" == "latest" ]; then
  123. paperversion=$(curl -s "https://${remotelocation}/api/v2/projects/${paperproject}" | jq -r '.versions[-1]')
  124. else
  125. # check if version there for the download from the api
  126. paperversion=$(curl -s "https://${remotelocation}/api/v2/projects/${paperproject}" | jq -r -e --arg mcversion "${mcversion}" '.versions[]|select(. == $mcversion)')
  127. if [ -z "${paperversion}" ]; then
  128. # user passed version does not exist
  129. fn_print_error_nl "Version ${mcversion} not available from ${remotelocation}"
  130. fn_script_log_error "Version ${mcversion} not available from ${remotelocation}"
  131. core_exit.sh
  132. fi
  133. fi
  134. if [ "${firstcommandname}" == "INSTALL" ]; then
  135. fn_update_papermc_remotebuild
  136. fn_update_papermc_dl
  137. else
  138. fn_print_dots "Checking for update: ${remotelocation}"
  139. fn_script_log_info "Checking for update: ${remotelocation}"
  140. sleep 0.5
  141. fn_update_papermc_localbuild
  142. fn_update_papermc_remotebuild
  143. fn_update_papermc_compare
  144. fi