update_ts3.sh 7.4 KB

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