update_vints.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/bash
  2. # LinuxGSM update_vints.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Vintage Story 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}" "nochmodx" "norun" "force" "${remotebuildhash}"
  11. fn_dl_extract "${tmpdir}" "${remotebuildfilename}" "${serverfiles}"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Uses executable to get local build.
  18. if [ -d "${executabledir}" ]; then
  19. cd "${executabledir}" || exit
  20. # shellcheck disable=SC2086 # Intentional: preexecutable may be empty; unquoted to drop it without producing an empty arg.
  21. localbuild="$(${preexecutable} ${executable} --version 2> /dev/null | sed '/^[[:space:]]*$/d')"
  22. fi
  23. if [ -z "${localbuild}" ]; then
  24. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  25. fn_script_log_error "Missing local build info"
  26. fn_script_log_error "Set localbuild to 0"
  27. localbuild="0"
  28. else
  29. fn_print_ok "Checking local build: ${remotelocation}"
  30. fn_script_log_pass "Checking local build"
  31. fi
  32. }
  33. fn_update_remotebuild() {
  34. # Gets remote build info.
  35. apiurl="http://api.vintagestory.at/stable-unstable.json"
  36. remotebuildresponse=$(curl -s "${apiurl}")
  37. if [ "${branch}" == "stable" ]; then
  38. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r '[ to_entries[] ] | .[].key' | grep -Ev "\-rc|\-pre" | sort -r -V | head -1)
  39. elif [ "${branch}" == "unstable" ]; then
  40. remotebuildversion=$(echo "${remotebuildresponse}" | jq -r '[ to_entries[] ] | .[].key' | grep -E "\-rc|\-pre" | sort -r -V | head -1)
  41. else
  42. remotebuildversion="${branch}"
  43. fi
  44. remotebuildfilename=$(echo "${remotebuildresponse}" | jq --arg remotebuild "${remotebuild}" -r '.[$remotebuild].linuxserver.filename')
  45. remotebuildurl=$(echo "${remotebuildresponse}" | jq --arg remotebuild "${remotebuild}" -r '.[$remotebuild].linuxserver.urls.cdn')
  46. remotebuildhash=$(echo "${remotebuildresponse}" | jq --arg remotebuild "${remotebuild}" -r '.[$remotebuild].linuxserver.md5')
  47. if [ "${firstcommandname}" != "INSTALL" ]; then
  48. fn_print_dots "Checking remote build: ${remotelocation}"
  49. # Checks if remotebuild variable has been set.
  50. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  51. fn_print_fail "Checking remote build: ${remotelocation}"
  52. fn_script_log_fail "Checking remote build"
  53. core_exit.sh
  54. else
  55. fn_print_ok "Checking remote build: ${remotelocation}"
  56. fn_script_log_pass "Checking remote build"
  57. fi
  58. else
  59. # Checks if remotebuild variable has been set.
  60. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  61. fn_print_failure "Unable to get remote build"
  62. fn_script_log_fail "Unable to get remote build"
  63. core_exit.sh
  64. fi
  65. fi
  66. }
  67. fn_update_compare() {
  68. fn_print_dots "Checking for update: ${remotelocation}"
  69. # Update has been found or force update.
  70. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  71. # Create update lockfile.
  72. date '+%s' > "${lockdir:?}/update.lock"
  73. fn_print_ok_nl "Checking for update: ${remotelocation}"
  74. fn_print "\n"
  75. fn_print_nl "${bold}${underline}Update${default} available"
  76. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  77. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  78. if [ -n "${branch}" ]; then
  79. fn_print_nl "* Branch: ${branch}"
  80. fi
  81. if [ -f "${rootdir}/.dev-debug" ]; then
  82. fn_print_nl "Remote build info"
  83. fn_print_nl "* apiurl: ${apiurl}"
  84. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  85. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  86. fn_print_nl "* remotebuild: ${remotebuild}"
  87. fi
  88. fn_print "\n"
  89. fn_script_log_info "Update available"
  90. fn_script_log_info "Local build: ${localbuild}"
  91. fn_script_log_info "Remote build: ${remotebuild}"
  92. if [ -n "${branch}" ]; then
  93. fn_script_log_info "Branch: ${branch}"
  94. fi
  95. fn_script_log_info "${localbuild} > ${remotebuild}"
  96. if [ "${commandname}" == "UPDATE" ]; then
  97. date +%s > "${lockdir:?}/last-updated.lock"
  98. unset updateonstart
  99. check_status.sh
  100. # If server stopped.
  101. if [ "${status}" == "0" ]; then
  102. fn_update_dl
  103. if [ "${localbuild}" == "0" ]; then
  104. exitbypass=1
  105. command_start.sh
  106. fn_firstcommand_reset
  107. exitbypass=1
  108. fn_sleep_time_5
  109. command_stop.sh
  110. fn_firstcommand_reset
  111. fi
  112. # If server started.
  113. else
  114. fn_print_restart_warning
  115. exitbypass=1
  116. command_stop.sh
  117. fn_firstcommand_reset
  118. exitbypass=1
  119. fn_update_dl
  120. exitbypass=1
  121. command_start.sh
  122. fn_firstcommand_reset
  123. fi
  124. unset exitbypass
  125. alert="update"
  126. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  127. alert="check-update"
  128. fi
  129. alert.sh
  130. else
  131. fn_print_ok_nl "Checking for update: ${remotelocation}"
  132. fn_print "\n"
  133. fn_print_nl "${bold}${underline}No update${default} available"
  134. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  135. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  136. if [ -n "${branch}" ]; then
  137. fn_print_nl "* Branch: ${branch}"
  138. fi
  139. fn_print "\n"
  140. fn_script_log_info "No update available"
  141. fn_script_log_info "Local build: ${localbuild}"
  142. fn_script_log_info "Remote build: ${remotebuild}"
  143. if [ -n "${branch}" ]; then
  144. fn_script_log_info "Branch: ${branch}"
  145. fi
  146. if [ -f "${rootdir}/.dev-debug" ]; then
  147. fn_print_nl "Remote build info"
  148. fn_print_nl "* apiurl: ${apiurl}"
  149. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  150. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  151. fn_print_nl "* remotebuild: ${remotebuild}"
  152. fi
  153. fi
  154. }
  155. # The location where the builds are checked and downloaded.
  156. remotelocation="vintagestory.at"
  157. if [ "${firstcommandname}" == "INSTALL" ]; then
  158. fn_update_remotebuild
  159. fn_update_dl
  160. else
  161. fn_print_dots "Checking for update"
  162. fn_print_dots "Checking for update: ${remotelocation}"
  163. fn_script_log_info "Checking for update: ${remotelocation}"
  164. fn_update_localbuild
  165. fn_update_remotebuild
  166. fn_update_compare
  167. fi