update_xnt.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. # LinuxGSM command_xnt.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Xontic 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}" "Xonotic"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. check_status.sh
  18. # Send version command to Xonotic server.
  19. if [ "${status}" != "0" ]; then
  20. tmux -L "${socketname}" send-keys -t "${sessionname}" "version" C-m > /dev/null 2>&1
  21. fn_sleep_time_1
  22. else
  23. exitbypass=1
  24. command_start.sh
  25. fn_firstcommand_reset
  26. exitbypass=1
  27. fn_sleep_time_5
  28. tmux -L "${socketname}" send-keys -t "${sessionname}" "version" C-m > /dev/null 2>&1
  29. command_stop.sh
  30. fn_firstcommand_reset
  31. fi
  32. # Uses log file to get local build.
  33. localbuild=$(grep "SVQC version: xonotic-v" "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*SVQC version: \(xonotic-v[0-9.]*\).*/\1/' | tr -d '\000-\011\013-\037')
  34. if [ -z "${localbuild}" ]; then
  35. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  36. fn_script_log_error "Missing local build info"
  37. fn_script_log_error "Set localbuild to 0"
  38. localbuild="0"
  39. else
  40. fn_print_ok "Checking local build: ${remotelocation}"
  41. fn_script_log_pass "Checking local build"
  42. fi
  43. }
  44. fn_update_remotebuild() {
  45. # Gets remote build info.
  46. apiurl="https://api.github.com/repos/xonotic/xonotic/tags"
  47. remotebuildresponse=$(curl -s "${apiurl}")
  48. remotebuildtag=$(echo "${remotebuildresponse}" | jq -r '.[0].name')
  49. remotebuildfilename=$(echo "${remotebuildtag}" | tr -d 'v')
  50. remotebuildfilename="${remotebuildfilename}.zip"
  51. remotebuildurl="https://dl.xonotic.org/${remotebuildfilename}"
  52. remotebuild="${remotebuildtag}"
  53. remotebuildhash=$(curl -s "https://dl.xonotic.org/${remotebuildfilename%.zip}.sha512" | grep "${remotebuildfilename}$" | grep -oE '[a-f0-9]{128}')
  54. if [ "${firstcommandname}" != "INSTALL" ]; then
  55. fn_print_dots "Checking remote build: ${remotelocation}"
  56. # Checks if remotebuild variable has been set.
  57. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  58. fn_print_fail "Checking remote build: ${remotelocation}"
  59. fn_script_log_fail "Checking remote build"
  60. core_exit.sh
  61. else
  62. fn_print_ok "Checking remote build: ${remotelocation}"
  63. fn_script_log_pass "Checking remote build"
  64. fi
  65. else
  66. # Checks if remotebuild variable has been set.
  67. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  68. fn_print_failure "Unable to get remote build"
  69. fn_script_log_fail "Unable to get remote build"
  70. core_exit.sh
  71. fi
  72. fi
  73. }
  74. fn_update_compare() {
  75. fn_print_dots "Checking for update: ${remotelocation}"
  76. # Update has been found or force update.
  77. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  78. # Create update lockfile.
  79. date '+%s' > "${lockdir:?}/update.lock"
  80. fn_print_ok_nl "Checking for update: ${remotelocation}"
  81. fn_print "\n"
  82. fn_print_nl "${bold}${underline}Update${default} available"
  83. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  84. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  85. if [ -n "${branch}" ]; then
  86. fn_print_nl "* Branch: ${branch}"
  87. fi
  88. if [ -f "${rootdir}/.dev-debug" ]; then
  89. fn_print_nl "Remote build info"
  90. fn_print_nl "* apiurl: ${apiurl}"
  91. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  92. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  93. fn_print_nl "* remotebuild: ${remotebuild}"
  94. fi
  95. fn_print "\n"
  96. fn_script_log_info "Update available"
  97. fn_script_log_info "Local build: ${localbuild}"
  98. fn_script_log_info "Remote build: ${remotebuild}"
  99. if [ -n "${branch}" ]; then
  100. fn_script_log_info "Branch: ${branch}"
  101. fi
  102. fn_script_log_info "${localbuild} > ${remotebuild}"
  103. if [ "${commandname}" == "UPDATE" ]; then
  104. date +%s > "${lockdir:?}/last-updated.lock"
  105. unset updateonstart
  106. check_status.sh
  107. # If server stopped.
  108. if [ "${status}" == "0" ]; then
  109. fn_update_dl
  110. if [ "${localbuild}" == "0" ]; then
  111. exitbypass=1
  112. command_start.sh
  113. fn_firstcommand_reset
  114. exitbypass=1
  115. fn_sleep_time_5
  116. command_stop.sh
  117. fn_firstcommand_reset
  118. fi
  119. # If server started.
  120. else
  121. fn_print_restart_warning
  122. exitbypass=1
  123. command_stop.sh
  124. fn_firstcommand_reset
  125. exitbypass=1
  126. fn_update_dl
  127. exitbypass=1
  128. command_start.sh
  129. fn_firstcommand_reset
  130. fi
  131. unset exitbypass
  132. alert="update"
  133. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  134. alert="check-update"
  135. fi
  136. alert.sh
  137. else
  138. fn_print_ok_nl "Checking for update: ${remotelocation}"
  139. fn_print "\n"
  140. fn_print_nl "${bold}${underline}No update${default} available"
  141. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  142. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  143. if [ -n "${branch}" ]; then
  144. fn_print_nl "* Branch: ${branch}"
  145. fi
  146. fn_print "\n"
  147. fn_script_log_info "No update available"
  148. fn_script_log_info "Local build: ${localbuild}"
  149. fn_script_log_info "Remote build: ${remotebuild}"
  150. if [ -n "${branch}" ]; then
  151. fn_script_log_info "Branch: ${branch}"
  152. fi
  153. if [ -f "${rootdir}/.dev-debug" ]; then
  154. fn_print_nl "Remote build info"
  155. fn_print_nl "* apiurl: ${apiurl}"
  156. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  157. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  158. fn_print_nl "* remotebuild: ${remotebuild}"
  159. fi
  160. fi
  161. }
  162. # The location where the builds are checked and downloaded.
  163. remotelocation="github.com"
  164. if [ "${firstcommandname}" == "INSTALL" ]; then
  165. fn_update_remotebuild
  166. fn_update_dl
  167. else
  168. fn_print_dots "Checking for update"
  169. fn_print_dots "Checking for update: ${remotelocation}"
  170. fn_script_log_info "Checking for update: ${remotelocation}"
  171. fn_update_localbuild
  172. fn_update_remotebuild
  173. fn_update_compare
  174. fi