update_ts3.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. # LinuxGSM command_ts3.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Teamspeak 3 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}" "teamspeak3-server_linux_${ts3arch}"
  12. fn_clear_tmp
  13. }
  14. fn_update_localbuild() {
  15. # Gets local build info.
  16. fn_print_dots "Checking local build: ${remotelocation}"
  17. # Uses log file to get local build.
  18. localbuild=$(grep -Eo "TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" "$(find "${serverfiles}"/* -name "ts3server*_0.log" 2> /dev/null | sort | tail -1)" | grep -Eo "((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | tail -1)
  19. if [ -z "${localbuild}" ]; then
  20. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  21. fn_script_log_error "Missing local build info"
  22. fn_script_log_error "Set localbuild to 0"
  23. localbuild="0"
  24. else
  25. fn_print_ok "Checking local build: ${remotelocation}"
  26. fn_script_log_pass "Checking local build"
  27. fi
  28. }
  29. fn_update_remotebuild() {
  30. # Get remote build info.
  31. apiurl="https://www.teamspeak.com/versions/server.json"
  32. remotebuildresponse=$(curl -s "${apiurl}")
  33. if [ "${ts3arch}" == "amd64" ]; then
  34. remotebuildurl=$(echo -e "${remotebuildresponse}" | jq -r '.linux.x86_64.mirrors."teamspeak.com"')
  35. remotebuildhash=$(echo -e "${remotebuildresponse}" | jq -r '.linux.x86_64.checksum')
  36. elif [ "${ts3arch}" == "x86" ]; then
  37. remotebuildurl=$(echo -e "${remotebuildresponse}" | jq -r '.linux.x86.mirrors."teamspeak.com"')
  38. remotebuildhash=$(echo -e "${remotebuildresponse}" | jq -r '.linux.x86.checksum')
  39. fi
  40. remotebuildfilename=$(basename "${remotebuildurl}")
  41. remotebuildversion=$(echo -e "${remotebuildresponse}" | jq -r '.linux.x86_64.version')
  42. if [ "${firstcommandname}" != "INSTALL" ]; then
  43. fn_print_dots "Checking remote build: ${remotelocation}"
  44. # Checks if remotebuildversion variable has been set.
  45. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  46. fn_print_fail "Checking remote build: ${remotelocation}"
  47. fn_script_log_fail "Checking remote build"
  48. core_exit.sh
  49. else
  50. fn_print_ok "Checking remote build: ${remotelocation}"
  51. fn_script_log_pass "Checking remote build"
  52. fi
  53. else
  54. # Checks if remotebuild variable has been set.
  55. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  56. fn_print_failure "Unable to get remote build"
  57. fn_script_log_fail "Unable to get remote build"
  58. core_exit.sh
  59. fi
  60. fi
  61. }
  62. fn_update_compare() {
  63. fn_print_dots "Checking for update: ${remotelocation}"
  64. # Update has been found or force update.
  65. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  66. # Create update lockfile.
  67. date '+%s' > "${lockdir:?}/update.lock"
  68. fn_print_ok_nl "Checking for update: ${remotelocation}"
  69. echo -en "\n"
  70. echo -e "Update available"
  71. echo -e "* Local build: ${red}${localbuild}${default}"
  72. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  73. if [ -n "${branch}" ]; then
  74. echo -e "* Branch: ${branch}"
  75. fi
  76. if [ -f "${rootdir}/.dev-debug" ]; then
  77. echo -e "Remote build info"
  78. echo -e "* apiurl: ${apiurl}"
  79. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  80. echo -e "* remotebuildurl: ${remotebuildurl}"
  81. echo -e "* remotebuildversion: ${remotebuildversion}"
  82. fi
  83. echo -en "\n"
  84. fn_script_log_info "Update available"
  85. fn_script_log_info "Local build: ${localbuild}"
  86. fn_script_log_info "Remote build: ${remotebuildversion}"
  87. if [ -n "${branch}" ]; then
  88. fn_script_log_info "Branch: ${branch}"
  89. fi
  90. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  91. if [ "${commandname}" == "UPDATE" ]; then
  92. date +%s > "${lockdir}/last-updated.lock"
  93. unset updateonstart
  94. check_status.sh
  95. # If server stopped.
  96. if [ "${status}" == "0" ]; then
  97. fn_update_dl
  98. if [ "${localbuild}" == "0" ]; then
  99. exitbypass=1
  100. command_start.sh
  101. fn_firstcommand_reset
  102. exitbypass=1
  103. fn_sleep_time_5
  104. command_stop.sh
  105. fn_firstcommand_reset
  106. fi
  107. # If server started.
  108. else
  109. fn_print_restart_warning
  110. exitbypass=1
  111. command_stop.sh
  112. fn_firstcommand_reset
  113. exitbypass=1
  114. fn_update_dl
  115. exitbypass=1
  116. command_start.sh
  117. fn_firstcommand_reset
  118. fi
  119. unset exitbypass
  120. alert="update"
  121. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  122. alert="check-update"
  123. fi
  124. alert.sh
  125. else
  126. fn_print_ok_nl "Checking for update: ${remotelocation}"
  127. echo -en "\n"
  128. echo -e "No update available"
  129. echo -e "* Local build: ${green}${localbuild}${default}"
  130. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  131. if [ -n "${branch}" ]; then
  132. echo -e "* Branch: ${branch}"
  133. fi
  134. echo -en "\n"
  135. fn_script_log_info "No update available"
  136. fn_script_log_info "Local build: ${localbuild}"
  137. fn_script_log_info "Remote build: ${remotebuildversion}"
  138. if [ -n "${branch}" ]; then
  139. fn_script_log_info "Branch: ${branch}"
  140. fi
  141. if [ -f "${rootdir}/.dev-debug" ]; then
  142. echo -e "Remote build info"
  143. echo -e "* apiurl: ${apiurl}"
  144. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  145. echo -e "* remotebuildurl: ${remotebuildurl}"
  146. echo -e "* remotebuildversion: ${remotebuildversion}"
  147. fi
  148. fi
  149. }
  150. # Game server architecture.
  151. info_distro.sh
  152. if [ "${arch}" == "x86_64" ]; then
  153. ts3arch="amd64"
  154. elif [ "${arch}" == "i386" ] || [ "${arch}" == "i686" ]; then
  155. ts3arch="x86"
  156. else
  157. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  158. fn_script_log_fail "Unknown or unsupported architecture: ${arch}"
  159. core_exit.sh
  160. fi
  161. # The location where the builds are checked and downloaded.
  162. remotelocation="teamspeak.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