update_vints.sh 6.1 KB

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