update_ts3.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/bash
  2. # LinuxGSM command_ts3.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Teamspeak 3 servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_ts3_dl(){
  9. if [ "${ts3arch}" == "amd64" ]; then
  10. remotebuildurl=$(curl -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86_64.mirrors."teamspeak.com"')
  11. elif [ "${ts3arch}" == "x86" ]; then
  12. remotebuildurl=$(curl -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86.mirrors."teamspeak.com"')
  13. fi
  14. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${remotebuild}.tar.bz2" "" "norun" "noforce" "nomd5"
  15. fn_dl_extract "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${remotebuild}.tar.bz2" "${tmpdir}"
  16. echo -e "copying to ${serverfiles}...\c"
  17. cp -R "${tmpdir}/teamspeak3-server_linux_${ts3arch}/"* "${serverfiles}"
  18. local exitcode=$?
  19. if [ "${exitcode}" == "0" ]; then
  20. fn_print_ok_eol_nl
  21. fn_script_log_pass "Copying to ${serverfiles}"
  22. fn_clear_tmp
  23. else
  24. fn_print_fail_eol_nl
  25. fn_script_log_fatal "Copying to ${serverfiles}"
  26. fn_clear_tmp
  27. core_exit.sh
  28. fi
  29. }
  30. fn_update_ts3_localbuild(){
  31. # Gets local build info.
  32. fn_print_dots "Checking local build: ${remotelocation}"
  33. # Uses log file to gather info.
  34. # Gives time for log file to generate.
  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. exitbypass=1
  143. command_start.sh
  144. exitbypass=1
  145. command_stop.sh
  146. fn_firstcommand_reset
  147. # If server started.
  148. else
  149. fn_print_restart_warning
  150. exitbypass=1
  151. command_stop.sh
  152. fn_firstcommand_reset
  153. exitbypass=1
  154. fn_update_ts3_dl
  155. exitbypass=1
  156. command_start.sh
  157. fn_firstcommand_reset
  158. fi
  159. unset exitbypass
  160. date +%s > "${lockdir}/lastupdate.lock"
  161. alert="update"
  162. alert.sh
  163. else
  164. fn_print_ok_nl "Checking for update: ${remotelocation}"
  165. echo -en "\n"
  166. echo -e "No update available"
  167. echo -e "* Local build: ${green}${localbuild}${default}"
  168. echo -e "* Remote build: ${green}${remotebuild}${default}"
  169. echo -en "\n"
  170. fn_script_log_info "No update available"
  171. fn_script_log_info "Local build: ${localbuild}"
  172. fn_script_log_info "Remote build: ${remotebuild}"
  173. fi
  174. }
  175. # Game server architecture.
  176. info_distro.sh
  177. if [ "${arch}" == "x86_64" ]; then
  178. ts3arch="amd64"
  179. elif [ "${arch}" == "i386" ]||[ "${arch}" == "i686" ]; then
  180. ts3arch="x86"
  181. else
  182. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  183. fn_script_log_fatal "Unknown or unsupported architecture: ${arch}"
  184. core_exit.sh
  185. fi
  186. # The location where the builds are checked and downloaded.
  187. remotelocation="teamspeak.com"
  188. if [ "${firstcommandname}" == "INSTALL" ]; then
  189. fn_update_ts3_remotebuild
  190. fn_update_ts3_dl
  191. else
  192. fn_print_dots "Checking for update"
  193. fn_print_dots "Checking for update: ${remotelocation}"
  194. fn_script_log_info "Checking for update: ${remotelocation}"
  195. fn_update_ts3_localbuild
  196. fn_update_ts3_remotebuild
  197. fn_update_ts3_compare
  198. fi