update_xnt.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if [ "${firstcommandname}" != "INSTALL" ]; then
  54. fn_print_dots "Checking remote build: ${remotelocation}"
  55. # Checks if remotebuild variable has been set.
  56. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  57. fn_print_fail "Checking remote build: ${remotelocation}"
  58. fn_script_log_fail "Checking remote build"
  59. core_exit.sh
  60. else
  61. fn_print_ok "Checking remote build: ${remotelocation}"
  62. fn_script_log_pass "Checking remote build"
  63. fi
  64. else
  65. # Checks if remotebuild variable has been set.
  66. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  67. fn_print_failure "Unable to get remote build"
  68. fn_script_log_fail "Unable to get remote build"
  69. core_exit.sh
  70. fi
  71. fi
  72. }
  73. fn_update_compare() {
  74. fn_print_dots "Checking for update: ${remotelocation}"
  75. # Update has been found or force update.
  76. if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
  77. # Create update lockfile.
  78. date '+%s' > "${lockdir:?}/update.lock"
  79. fn_print_ok_nl "Checking for update: ${remotelocation}"
  80. fn_print "\n"
  81. fn_print_nl "${bold}${underline}Update${default} available"
  82. fn_print_nl "* Local build: ${red}${localbuild}${default}"
  83. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  84. if [ -n "${branch}" ]; then
  85. fn_print_nl "* Branch: ${branch}"
  86. fi
  87. if [ -f "${rootdir}/.dev-debug" ]; then
  88. fn_print_nl "Remote build info"
  89. fn_print_nl "* apiurl: ${apiurl}"
  90. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  91. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  92. fn_print_nl "* remotebuild: ${remotebuild}"
  93. fi
  94. fn_print "\n"
  95. fn_script_log_info "Update available"
  96. fn_script_log_info "Local build: ${localbuild}"
  97. fn_script_log_info "Remote build: ${remotebuild}"
  98. if [ -n "${branch}" ]; then
  99. fn_script_log_info "Branch: ${branch}"
  100. fi
  101. fn_script_log_info "${localbuild} > ${remotebuild}"
  102. if [ "${commandname}" == "UPDATE" ]; then
  103. date +%s > "${lockdir:?}/last-updated.lock"
  104. unset updateonstart
  105. check_status.sh
  106. # If server stopped.
  107. if [ "${status}" == "0" ]; then
  108. fn_update_dl
  109. if [ "${localbuild}" == "0" ]; then
  110. exitbypass=1
  111. command_start.sh
  112. fn_firstcommand_reset
  113. exitbypass=1
  114. fn_sleep_time_5
  115. command_stop.sh
  116. fn_firstcommand_reset
  117. fi
  118. # If server started.
  119. else
  120. fn_print_restart_warning
  121. exitbypass=1
  122. command_stop.sh
  123. fn_firstcommand_reset
  124. exitbypass=1
  125. fn_update_dl
  126. exitbypass=1
  127. command_start.sh
  128. fn_firstcommand_reset
  129. fi
  130. unset exitbypass
  131. alert="update"
  132. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  133. alert="check-update"
  134. fi
  135. alert.sh
  136. else
  137. fn_print_ok_nl "Checking for update: ${remotelocation}"
  138. fn_print "\n"
  139. fn_print_nl "${bold}${underline}No update${default} available"
  140. fn_print_nl "* Local build: ${green}${localbuild}${default}"
  141. fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
  142. if [ -n "${branch}" ]; then
  143. fn_print_nl "* Branch: ${branch}"
  144. fi
  145. fn_print "\n"
  146. fn_script_log_info "No update available"
  147. fn_script_log_info "Local build: ${localbuild}"
  148. fn_script_log_info "Remote build: ${remotebuild}"
  149. if [ -n "${branch}" ]; then
  150. fn_script_log_info "Branch: ${branch}"
  151. fi
  152. if [ -f "${rootdir}/.dev-debug" ]; then
  153. fn_print_nl "Remote build info"
  154. fn_print_nl "* apiurl: ${apiurl}"
  155. fn_print_nl "* remotebuildfilename: ${remotebuildfilename}"
  156. fn_print_nl "* remotebuildurl: ${remotebuildurl}"
  157. fn_print_nl "* remotebuild: ${remotebuild}"
  158. fi
  159. fi
  160. }
  161. # The location where the builds are checked and downloaded.
  162. remotelocation="github.com"
  163. if [ "${firstcommandname}" == "INSTALL" ]; then
  164. fn_update_remotebuild
  165. fn_update_dl
  166. else
  167. fn_print_dots "Checking for update"
  168. fn_print_dots "Checking for update: ${remotelocation}"
  169. fn_script_log_info "Checking for update: ${remotelocation}"
  170. fn_update_localbuild
  171. fn_update_remotebuild
  172. fn_update_compare
  173. fi