update_ts3.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. ts3latestdata=$(curl -s "https://www.${remotelocation}/versions/server.json" | jq '.linux')
  10. if [ "${ts3arch}" == "amd64" ]; then
  11. remotebuildurl=$(echo -e "${ts3latestdata}" | jq -r '.x86_64.mirrors."teamspeak.com"')
  12. remotehash=$(echo -e "${ts3latestdata}" | jq -r '.x86_64.checksum')
  13. elif [ "${ts3arch}" == "x86" ]; then
  14. remotebuildurl=$(echo -e "${ts3latestdata}" | jq -r '.x86.mirrors."teamspeak.com"')
  15. remotehash=$(echo -e "${ts3latestdata}" | jq -r '.x86.checksum')
  16. fi
  17. remotefile=$(basename "${remotebuildurl}")
  18. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotefile}" "" "norun" "noforce" "${remotehash}"
  19. fn_dl_extract "${tmpdir}" "${remotefile}" "${tmpdir}"
  20. echo -e "copying to ${serverfiles}...\c"
  21. cp -R "${tmpdir}/teamspeak3-server_linux_${ts3arch}/"* "${serverfiles}"
  22. local exitcode=$?
  23. if [ "${exitcode}" == "0" ]; then
  24. fn_print_ok_eol_nl
  25. fn_script_log_pass "Copying to ${serverfiles}"
  26. fn_clear_tmp
  27. else
  28. fn_print_fail_eol_nl
  29. fn_script_log_fatal "Copying to ${serverfiles}"
  30. fn_clear_tmp
  31. core_exit.sh
  32. fi
  33. }
  34. fn_update_ts3_localbuild() {
  35. # Gets local build info.
  36. fn_print_dots "Checking local build: ${remotelocation}"
  37. # Uses log file to gather info.
  38. # Gives time for log file to generate.
  39. requirerestart=1
  40. if [ ! -d "${serverfiles}/logs" ] || [ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; then
  41. fn_print_error "Checking local build: ${remotelocation}"
  42. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  43. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  44. fn_script_log_error "No log files containing version info"
  45. fn_script_log_info "Forcing server restart"
  46. exitbypass=1
  47. command_stop.sh
  48. exitbypass=1
  49. command_start.sh
  50. fn_firstcommand_reset
  51. totalseconds=0
  52. # Check again, allow time to generate logs.
  53. while [ ! -d "${serverfiles}/logs" ] || [ -z "$(find "${serverfiles}/logs/"* -name 'ts3server*_0.log' 2> /dev/null)" ]; do
  54. sleep 1
  55. fn_print_info "Checking local build: ${remotelocation}: waiting for log file: ${totalseconds}"
  56. if [ -v "${loopignore}" ]; then
  57. loopignore=1
  58. fn_script_log_info "Waiting for log file to generate"
  59. fi
  60. if [ "${totalseconds}" -gt "120" ]; then
  61. localbuild="0"
  62. fn_print_error "Checking local build: ${remotelocation}: waiting for log file: missing log file"
  63. fn_script_log_error "Missing log file"
  64. fn_script_log_error "Set localbuild to 0"
  65. fi
  66. totalseconds=$((totalseconds + 1))
  67. done
  68. fi
  69. if [ -z "${localbuild}" ]; then
  70. 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)
  71. fi
  72. if [ -z "${localbuild}" ]; then
  73. # Gives time for var to generate.
  74. totalseconds=0
  75. for seconds in {1..120}; do
  76. fn_print_info "Checking local build: ${remotelocation}: waiting for local build: ${totalseconds}"
  77. if [ -z "${loopignore}" ]; then
  78. loopignore=1
  79. fn_script_log_info "Waiting for local build to generate"
  80. fi
  81. 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)
  82. if [ "${localbuild}" ] || [ "${seconds}" == "120" ]; then
  83. break
  84. fi
  85. sleep 1
  86. totalseconds=$((totalseconds + 1))
  87. done
  88. fi
  89. if [ -z "${localbuild}" ]; then
  90. localbuild="0"
  91. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  92. fn_script_log_error "Missing local build info"
  93. fn_script_log_error "Set localbuild to 0"
  94. else
  95. fn_print_ok "Checking local build: ${remotelocation}"
  96. fn_script_log_pass "Checking local build"
  97. fi
  98. }
  99. fn_update_ts3_remotebuild() {
  100. # Gets remote build info.
  101. ts3latestdata=$(curl -s "https://www.${remotelocation}/versions/server.json" | jq '.linux')
  102. if [ "${ts3arch}" == "amd64" ]; then
  103. remotebuild=$(echo -e "${ts3latestdata}" | jq -r '.x86_64.version')
  104. elif [ "${ts3arch}" == "x86" ]; then
  105. remotebuild=$(echo -e "${ts3latestdata}" | jq -r '.x86.version')
  106. fi
  107. if [ "${firstcommandname}" != "INSTALL" ]; then
  108. fn_print_dots "Checking remote build: ${remotelocation}"
  109. # Checks if remotebuild variable has been set.
  110. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  111. fn_print_fail "Checking remote build: ${remotelocation}"
  112. fn_script_log_fatal "Checking remote build"
  113. core_exit.sh
  114. else
  115. fn_print_ok "Checking remote build: ${remotelocation}"
  116. fn_script_log_pass "Checking remote build"
  117. fi
  118. else
  119. # Checks if remotebuild variable has been set.
  120. if [ -z "${remotebuild}" ] || [ "${remotebuild}" == "null" ]; then
  121. fn_print_failure "Unable to get remote build"
  122. fn_script_log_fatal "Unable to get remote build"
  123. core_exit.sh
  124. fi
  125. fi
  126. }
  127. fn_update_ts3_compare() {
  128. # Removes dots so if statement can compare version numbers.
  129. fn_print_dots "Checking for update: ${remotelocation}"
  130. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  131. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  132. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ] || [ "${forceupdate}" == "1" ]; then
  133. fn_print_ok_nl "Checking for update: ${remotelocation}"
  134. echo -en "\n"
  135. echo -e "Update available"
  136. echo -e "* Local build: ${red}${localbuild}${default}"
  137. echo -e "* Remote build: ${green}${remotebuild}${default}"
  138. fn_script_log_info "Update available"
  139. fn_script_log_info "Local build: ${localbuild}"
  140. fn_script_log_info "Remote build: ${remotebuild}"
  141. fn_script_log_info "${localbuild} > ${remotebuild}"
  142. if [ "${commandname}" == "UPDATE" ]; then
  143. unset updateonstart
  144. check_status.sh
  145. # If server stopped.
  146. if [ "${status}" == "0" ]; then
  147. exitbypass=1
  148. fn_update_ts3_dl
  149. if [ "${requirerestart}" == "1" ]; then
  150. exitbypass=1
  151. command_start.sh
  152. fn_firstcommand_reset
  153. exitbypass=1
  154. command_stop.sh
  155. fn_firstcommand_reset
  156. fi
  157. # If server started.
  158. else
  159. fn_print_restart_warning
  160. exitbypass=1
  161. command_stop.sh
  162. fn_firstcommand_reset
  163. exitbypass=1
  164. fn_update_ts3_dl
  165. exitbypass=1
  166. command_start.sh
  167. fn_firstcommand_reset
  168. fi
  169. unset exitbypass
  170. date +%s > "${lockdir}/lastupdate.lock"
  171. alert="update"
  172. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  173. alert="check-update"
  174. fi
  175. alert.sh
  176. else
  177. fn_print_ok_nl "Checking for update: ${remotelocation}"
  178. echo -en "\n"
  179. echo -e "No update available"
  180. echo -e "* Local build: ${green}${localbuild}${default}"
  181. echo -e "* Remote build: ${green}${remotebuild}${default}"
  182. echo -en "\n"
  183. fn_script_log_info "No update available"
  184. fn_script_log_info "Local build: ${localbuild}"
  185. fn_script_log_info "Remote build: ${remotebuild}"
  186. fi
  187. }
  188. # Game server architecture.
  189. info_distro.sh
  190. if [ "${arch}" == "x86_64" ]; then
  191. ts3arch="amd64"
  192. elif [ "${arch}" == "i386" ] || [ "${arch}" == "i686" ]; then
  193. ts3arch="x86"
  194. else
  195. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  196. fn_script_log_fatal "Unknown or unsupported architecture: ${arch}"
  197. core_exit.sh
  198. fi
  199. # The location where the builds are checked and downloaded.
  200. remotelocation="teamspeak.com"
  201. if [ "${firstcommandname}" == "INSTALL" ]; then
  202. fn_update_ts3_remotebuild
  203. fn_update_ts3_dl
  204. else
  205. fn_print_dots "Checking for update"
  206. fn_print_dots "Checking for update: ${remotelocation}"
  207. fn_script_log_info "Checking for update: ${remotelocation}"
  208. fn_update_ts3_localbuild
  209. fn_update_ts3_remotebuild
  210. fn_update_ts3_compare
  211. fi