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. 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. requirerestart=1
  35. if [ ! -d "${serverfiles}/logs" ]||[ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; then
  36. fn_print_error "Checking local build: ${remotelocation}"
  37. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  38. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  39. fn_script_log_error "No log files containing version info"
  40. fn_script_log_info "Forcing server restart"
  41. exitbypass=1
  42. command_stop.sh
  43. exitbypass=1
  44. command_start.sh
  45. fn_firstcommand_reset
  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 local build: ${remotelocation}: 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 local build: ${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}" | tail -1)
  66. fi
  67. if [ -z "${localbuild}" ]; then
  68. # Gives time for var to generate.
  69. totalseconds=0
  70. for seconds in {1..120}; do
  71. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  72. if [ -z "${loopignore}" ]; then
  73. loopignore=1
  74. fn_script_log_info "Waiting for local build to generate"
  75. fi
  76. 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)
  77. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; then
  78. break
  79. fi
  80. sleep 1
  81. totalseconds=$((totalseconds + 1))
  82. done
  83. fi
  84. if [ -z "${localbuild}" ]; then
  85. localbuild="0"
  86. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  87. fn_script_log_error "Missing local build info"
  88. fn_script_log_error "Set localbuild to 0"
  89. else
  90. fn_print_ok "Checking local build: ${remotelocation}"
  91. fn_script_log_pass "Checking local build"
  92. fi
  93. }
  94. fn_update_ts3_remotebuild(){
  95. # Gets remote build info.
  96. if [ "${arch}" == "x86_64" ]; then
  97. remotebuild=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86_64.version')
  98. elif [ "${arch}" == "x86" ]; then
  99. remotebuild=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86.version')
  100. fi
  101. if [ "${firstcommandname}" != "INSTALL" ]; then
  102. fn_print_dots "Checking remote build: ${remotelocation}"
  103. # Checks if remotebuild variable has been set.
  104. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  105. fn_print_fail "Checking remote build: ${remotelocation}"
  106. fn_script_log_fatal "Checking remote build"
  107. core_exit.sh
  108. else
  109. fn_print_ok "Checking remote build: ${remotelocation}"
  110. fn_script_log_pass "Checking remote build"
  111. fi
  112. else
  113. # Checks if remotebuild variable has been set.
  114. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  115. fn_print_failure "Unable to get remote build"
  116. fn_script_log_fatal "Unable to get remote build"
  117. core_exit.sh
  118. fi
  119. fi
  120. }
  121. fn_update_ts3_compare(){
  122. # Removes dots so if statement can compare version numbers.
  123. fn_print_dots "Checking for update: ${remotelocation}"
  124. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  125. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  126. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  127. fn_print_ok_nl "Checking for update: ${remotelocation}"
  128. echo -en "\n"
  129. echo -e "Update available"
  130. echo -e "* Local build: ${red}${localbuild}${default}"
  131. echo -e "* Remote build: ${green}${remotebuild}${default}"
  132. fn_script_log_info "Update available"
  133. fn_script_log_info "Local build: ${localbuild}"
  134. fn_script_log_info "Remote build: ${remotebuild}"
  135. fn_script_log_info "${localbuild} > ${remotebuild}"
  136. unset updateonstart
  137. check_status.sh
  138. # If server stopped.
  139. if [ "${status}" == "0" ]; then
  140. exitbypass=1
  141. fn_update_ts3_dl
  142. if [ "${requirerestart}" == "1" ]; then
  143. exitbypass=1
  144. command_start.sh
  145. fn_firstcommand_reset
  146. exitbypass=1
  147. command_stop.sh
  148. fn_firstcommand_reset
  149. fi
  150. # If server started.
  151. else
  152. fn_print_restart_warning
  153. exitbypass=1
  154. command_stop.sh
  155. fn_firstcommand_reset
  156. exitbypass=1
  157. fn_update_ts3_dl
  158. exitbypass=1
  159. command_start.sh
  160. fn_firstcommand_reset
  161. fi
  162. unset exitbypass
  163. date +%s > "${lockdir}/lastupdate.lock"
  164. alert="update"
  165. alert.sh
  166. else
  167. fn_print_ok_nl "Checking for update: ${remotelocation}"
  168. echo -en "\n"
  169. echo -e "No update available"
  170. echo -e "* Local build: ${green}${localbuild}${default}"
  171. echo -e "* Remote build: ${green}${remotebuild}${default}"
  172. echo -en "\n"
  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 [ "${firstcommandname}" == "INSTALL" ]; then
  192. fn_update_ts3_remotebuild
  193. fn_update_ts3_dl
  194. else
  195. fn_print_dots "Checking for update"
  196. fn_print_dots "Checking for update: ${remotelocation}"
  197. fn_script_log_info "Checking for update: ${remotelocation}"
  198. fn_update_ts3_localbuild
  199. fn_update_ts3_remotebuild
  200. fn_update_ts3_compare
  201. fi