update_ts3.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. requirerestart=1
  36. if [ ! -d "${serverfiles}/logs" ]||[ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; then
  37. fn_print_error "Checking local build: ${remotelocation}"
  38. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  39. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  40. fn_script_log_error "No log files containing version info"
  41. fn_script_log_info "Forcing server restart"
  42. exitbypass=1
  43. command_stop.sh
  44. exitbypass=1
  45. command_start.sh
  46. fn_firstcommand_reset
  47. totalseconds=0
  48. # Check again, allow time to generate logs.
  49. while [ ! -d "${serverfiles}/logs" ]||[ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; do
  50. sleep 1
  51. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  52. if [ -v "${loopignore}" ]; then
  53. loopignore=1
  54. fn_script_log_info "Waiting for log file to generate"
  55. fi
  56. if [ "${totalseconds}" -gt "120" ]; then
  57. localbuild="0"
  58. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  59. fn_script_log_error "Missing log file"
  60. fn_script_log_error "Set localbuild to 0"
  61. fi
  62. totalseconds=$((totalseconds + 1))
  63. done
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. 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)
  67. fi
  68. if [ -z "${localbuild}" ]; then
  69. # Gives time for var to generate.
  70. totalseconds=0
  71. for seconds in {1..120}; do
  72. fn_print_info "Checking local build: ${remotelocation}: 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}" | tail -1)
  78. if [ "${localbuild}" ]||[ "${seconds}" == "120" ]; 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 local build: ${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 local build: ${remotelocation}"
  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=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86_64.version')
  99. elif [ "${arch}" == "x86" ]; then
  100. remotebuild=$(curl -s "https://www.teamspeak.com/versions/server.json" | jq -r '.linux.x86.version')
  101. fi
  102. if [ "${firstcommandname}" != "INSTALL" ]; then
  103. fn_print_dots "Checking remote build: ${remotelocation}"
  104. # Checks if remotebuild variable has been set.
  105. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  106. fn_print_fail "Checking remote build: ${remotelocation}"
  107. fn_script_log_fatal "Checking remote build"
  108. core_exit.sh
  109. else
  110. fn_print_ok "Checking remote build: ${remotelocation}"
  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 -e "${localbuild}" | tr -cd '[:digit:]')
  126. remotebuilddigit=$(echo -e "${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. unset updateonstart
  138. check_status.sh
  139. # If server stopped.
  140. if [ "${status}" == "0" ]; then
  141. exitbypass=1
  142. fn_update_ts3_dl
  143. if [ "${requirerestart}" == "1" ]; then
  144. exitbypass=1
  145. command_start.sh
  146. fn_firstcommand_reset
  147. exitbypass=1
  148. command_stop.sh
  149. fn_firstcommand_reset
  150. fi
  151. # If server started.
  152. else
  153. fn_print_restart_warning
  154. exitbypass=1
  155. command_stop.sh
  156. fn_firstcommand_reset
  157. exitbypass=1
  158. fn_update_ts3_dl
  159. exitbypass=1
  160. command_start.sh
  161. fn_firstcommand_reset
  162. fi
  163. unset exitbypass
  164. date +%s > "${lockdir}/lastupdate.lock"
  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. echo -en "\n"
  174. fn_script_log_info "No update available"
  175. fn_script_log_info "Local build: ${localbuild}"
  176. fn_script_log_info "Remote build: ${remotebuild}"
  177. fi
  178. }
  179. # Game server architecture.
  180. info_distro.sh
  181. if [ "${arch}" == "x86_64" ]; then
  182. ts3arch="amd64"
  183. elif [ "${arch}" == "i386" ]||[ "${arch}" == "i686" ]; then
  184. ts3arch="x86"
  185. else
  186. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  187. fn_script_log_fatal "Unknown or unsupported architecture: ${arch}"
  188. core_exit.sh
  189. fi
  190. # The location where the builds are checked and downloaded.
  191. remotelocation="teamspeak.com"
  192. if [ "${firstcommandname}" == "INSTALL" ]; then
  193. fn_update_ts3_remotebuild
  194. fn_update_ts3_dl
  195. else
  196. fn_print_dots "Checking for update"
  197. fn_print_dots "Checking for update: ${remotelocation}"
  198. fn_script_log_info "Checking for update: ${remotelocation}"
  199. fn_update_ts3_localbuild
  200. fn_update_ts3_remotebuild
  201. fn_update_ts3_compare
  202. fi