diff --git a/ct/alpine-teamspeak-server.sh b/ct/alpine-teamspeak-server.sh index 20464955..104b18f6 100644 --- a/ct/alpine-teamspeak-server.sh +++ b/ct/alpine-teamspeak-server.sh @@ -23,26 +23,32 @@ function update_script() { header_info if [[ ! -d /opt/teamspeak-server ]]; then - msg_error "No ${APP} Installation Found!" + msg_error "No ${APP} installation found!" exit 1 fi - RELEASE=$(curl -fsSL https://teamspeak.com/en/downloads/#server | sed -n '/teamspeak3-server_linux_amd64-/ { s/.*teamspeak3-server_linux_amd64-\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p; q }') + # define custom command to scrape version + local CUSTOM_CMD="curl -fsSL https://teamspeak.com/en/downloads/#server \ + | sed -n '/teamspeak3-server_linux_amd64-/ { s/.*teamspeak3-server_linux_amd64-\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p; q }'" - if [ "${RELEASE}" != "$(cat ~/.teamspeak-server)" ] || [ ! -f ~/.teamspeak-server ]; then - msg_info "Updating ${APP} LXC" + if check_for_update "${APP}" "${CUSTOM_CMD}"; then + local release="$CHECK_UPDATE_RELEASE" + + msg_info "Updating ${APP} LXC to v${release}" $STD apk -U upgrade $STD service teamspeak stop - curl -fsSL "https://files.teamspeak-services.com/releases/server/${RELEASE}/teamspeak3-server_linux_amd64-${RELEASE}.tar.bz2" -o ts3server.tar.bz2 - tar -xf ./ts3server.tar.bz2 + + curl -fsSL "https://files.teamspeak-services.com/releases/server/${release}/teamspeak3-server_linux_amd64-${release}.tar.bz2" -o ts3server.tar.bz2 + tar -xf ts3server.tar.bz2 cp -ru teamspeak3-server_linux_amd64/* /opt/teamspeak-server/ - rm -f ~/ts3server.tar.bz* + + rm -f ts3server.tar.bz2 rm -rf teamspeak3-server_linux_amd64 - echo "${RELEASE}" >~/.teamspeak-server + + echo "${release}" >~/.teamspeak-server + $STD service teamspeak start - msg_ok "Updated Successfully" - else - msg_ok "No update required. ${APP} is already at ${RELEASE}" + msg_ok "Updated ${APP} successfully to v${release}" fi exit 0 diff --git a/misc/tools.func b/misc/tools.func index 5d03c24d..97782ea9 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -1978,3 +1978,50 @@ EOF msg_ok "ClickHouse updated" fi } + +check_for_update() { + local app="$1" # e.g. "wizarr" + local source="$2" # e.g. "org/repo" or custom command + local current_file="$HOME/.${app,,}" + + msg_info "Check for update: $app" + + # DNS check if GitHub + if [[ "$source" != *" "* ]] && [[ "$source" != http* ]]; then + if ! getent hosts api.github.com >/dev/null 2>&1; then + msg_error "Network error: cannot resolve api.github.com" + return 1 + fi + if ! command -v jq &>/dev/null; then + apt-get update -qq &>/dev/null && apt-get install -y jq &>/dev/null || { + msg_error "Failed to install jq" + return 1 + } + fi + fi + + # get release + local release="" + if [[ "$source" == *" "* ]] || [[ "$source" == http* ]]; then + release=$(eval "$source") + else + release=$(curl -fsSL "https://api.github.com/repos/$source/releases/latest" | + jq -r '.tag_name' | sed 's/^v//') + fi + + if [[ -z "$release" ]]; then + msg_error "Unable to determine latest release for $app" + return 1 + fi + + # compare with local version + local current="" + [[ -f "$current_file" ]] && current=$(<"$current_file") + + if [[ "$release" != "$current" ]] || [[ ! -f "$current_file" ]]; then + return 0 # update needed + else + msg_ok "$app is up to date (v$release)" + return 1 # no update + fi +}