update_ts3.sh 7.3 KB

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