update_ts3.sh 7.5 KB

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