update_ts3.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. # LinuxGSM command_ts3.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of Teamspeak 3 servers.
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_update_ts3_dl(){
  8. if [ "${ts3arch}" == "amd64" ]; then
  9. remotebuildurl=$(curl -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86_64.mirrors."teamspeak.com"')
  10. elif [ "${ts3arch}" == "x86" ]; then
  11. remotebuildurl=$(curl -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86.mirrors."teamspeak.com"')
  12. fi
  13. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${remotebuild}.tar.bz2" "" "norun" "noforce" "nomd5"
  14. fn_dl_extract "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${remotebuild}.tar.bz2" "${tmpdir}"
  15. echo -e "copying to ${serverfiles}...\c"
  16. cp -R "${tmpdir}/teamspeak3-server_linux_${ts3arch}/"* "${serverfiles}"
  17. local exitcode=$?
  18. if [ "${exitcode}" == "0" ]; then
  19. fn_print_ok_eol_nl
  20. fn_script_log_pass "Copying to ${serverfiles}"
  21. fn_clear_tmp
  22. else
  23. fn_print_fail_eol_nl
  24. fn_script_log_fatal "Copying to ${serverfiles}"
  25. fn_clear_tmp
  26. core_exit.sh
  27. fi
  28. }
  29. fn_update_ts3_localbuild(){
  30. # Gets local build info.
  31. fn_print_dots "Checking local build: ${remotelocation}"
  32. # Uses log file to gather info.
  33. # Gives time for log file to generate.
  34. if [ ! -d "${serverfiles}/logs" ]||[ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; then
  35. fn_print_error "Checking local build: ${remotelocation}"
  36. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  37. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  38. fn_script_log_error "No log files containing version info"
  39. fn_script_log_info "Forcing server restart"
  40. exitbypass=1
  41. command_stop.sh
  42. exitbypass=1
  43. command_start.sh
  44. totalseconds=0
  45. # Check again, allow time to generate logs.
  46. while [ ! -d "${serverfiles}/logs" ]||[ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; do
  47. sleep 1
  48. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  49. if [ -v "${loopignore}" ]; then
  50. loopignore=1
  51. fn_script_log_info "Waiting for log file to generate"
  52. fi
  53. if [ "${totalseconds}" -gt "120" ]; then
  54. localbuild="0"
  55. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  56. fn_script_log_error "Missing log file"
  57. fn_script_log_error "Set localbuild to 0"
  58. fi
  59. totalseconds=$((totalseconds + 1))
  60. done
  61. fi
  62. if [ -z "${localbuild}" ]; then
  63. localbuild=$(cat "$(find ./* -name "ts3server*_0.log" 2> /dev/null | sort | tail -1)" | grep -Eo "TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | grep -Eo "((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | tail -1)
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. # Gives time for var to generate.
  67. totalseconds=0
  68. for seconds in {1..120}; do
  69. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  70. if [ -z "${loopignore}" ]; then
  71. loopignore=1
  72. fn_script_log_info "Waiting for local build to generate"
  73. fi
  74. localbuild=$(cat "$(find ./* -name "ts3server*_0.log" 2> /dev/null | sort | tail -1)" | grep -Eo "TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | grep -Eo "((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}" | tail -1)
  75. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  76. break
  77. fi
  78. sleep 1
  79. totalseconds=$((totalseconds + 1))
  80. done
  81. fi
  82. if [ -z "${localbuild}" ]; then
  83. localbuild="0"
  84. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  85. fn_script_log_error "Missing local build info"
  86. fn_script_log_error "Set localbuild to 0"
  87. else
  88. fn_print_ok "Checking local build: ${remotelocation}"
  89. fn_script_log_pass "Checking local build"
  90. fi
  91. }
  92. fn_update_ts3_remotebuild(){
  93. # Gets remote build info.
  94. if [ "${arch}" == "x86_64" ]; then
  95. remotebuild=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86_64.version')
  96. elif [ "${arch}" == "x86" ]; then
  97. remotebuild=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86.version')
  98. fi
  99. if [ "${installer}" != "1" ]; then
  100. fn_print_dots "Checking remote build: ${remotelocation}"
  101. # Checks if remotebuild variable has been set.
  102. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  103. fn_print_fail "Checking remote build: ${remotelocation}"
  104. fn_script_log_fatal "Checking remote build"
  105. core_exit.sh
  106. else
  107. fn_print_ok "Checking remote build: ${remotelocation}"
  108. fn_script_log_pass "Checking remote build"
  109. fi
  110. else
  111. # Checks if remotebuild variable has been set.
  112. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  113. fn_print_failure "Unable to get remote build"
  114. fn_script_log_fatal "Unable to get remote build"
  115. core_exit.sh
  116. fi
  117. fi
  118. }
  119. fn_update_ts3_compare(){
  120. # Removes dots so if statement can compare version numbers.
  121. fn_print_dots "Checking for update: ${remotelocation}"
  122. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  123. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  124. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  125. fn_print_ok_nl "Checking for update: ${remotelocation}"
  126. echo -en "\n"
  127. echo -e "Update available"
  128. echo -e "* Local build: ${red}${localbuild}${default}"
  129. echo -e "* Remote build: ${green}${remotebuild}${default}"
  130. fn_script_log_info "Update available"
  131. fn_script_log_info "Local build: ${localbuild}"
  132. fn_script_log_info "Remote build: ${remotebuild}"
  133. fn_script_log_info "${localbuild} > ${remotebuild}"
  134. unset updateonstart
  135. check_status.sh
  136. # If server stopped.
  137. if [ "${status}" == "0" ]; then
  138. exitbypass=1
  139. fn_update_ts3_dl
  140. exitbypass=1
  141. command_start.sh
  142. exitbypass=1
  143. command_stop.sh
  144. # If server started.
  145. else
  146. fn_stop_warning
  147. exitbypass=1
  148. command_stop.sh
  149. exitbypass=1
  150. fn_update_ts3_dl
  151. exitbypass=1
  152. command_start.sh
  153. fi
  154. date +%s > "${lockdir}/lastupdate.lock"
  155. alert="update"
  156. alert.sh
  157. else
  158. fn_print_ok_nl "Checking for update: ${remotelocation}"
  159. echo -en "\n"
  160. echo -e "No update available"
  161. echo -e "* Local build: ${green}${localbuild}${default}"
  162. echo -e "* Remote build: ${green}${remotebuild}${default}"
  163. echo -en "\n"
  164. fn_script_log_info "No update available"
  165. fn_script_log_info "Local build: ${localbuild}"
  166. fn_script_log_info "Remote build: ${remotebuild}"
  167. fi
  168. }
  169. # Game server architecture.
  170. info_distro.sh
  171. if [ "${arch}" == "x86_64" ]; then
  172. ts3arch="amd64"
  173. elif [ "${arch}" == "i386" ]||[ "${arch}" == "i686" ]; then
  174. ts3arch="x86"
  175. else
  176. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  177. fn_script_log_fatal "Unknown or unsupported architecture: ${arch}"
  178. core_exit.sh
  179. fi
  180. fn_stop_warning(){
  181. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  182. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  183. totalseconds=3
  184. for seconds in {3..1}; do
  185. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  186. totalseconds=$((totalseconds - 1))
  187. sleep 1
  188. if [ "${seconds}" == "0" ]; then
  189. break
  190. fi
  191. done
  192. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  193. }
  194. # The location where the builds are checked and downloaded.
  195. remotelocation="teamspeak.com"
  196. if [ "${installer}" == "1" ]; then
  197. fn_update_ts3_remotebuild
  198. fn_update_ts3_dl
  199. else
  200. fn_print_dots "Checking for update"
  201. fn_print_dots "Checking for update: ${remotelocation}"
  202. fn_script_log_info "Checking for update: ${remotelocation}"
  203. fn_update_ts3_localbuild
  204. fn_update_ts3_remotebuild
  205. fn_update_ts3_compare
  206. fi
  207. if [ "${commandname}" != "INSTALL" ]; then
  208. core_exit.sh
  209. fi