From 267d36f8b59efd3e0c4407e2e77482dd2392fcd6 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Fri, 14 Jul 2023 22:56:58 +0200 Subject: [PATCH 1/8] GH latest version fetcher --- sources/functions/github | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sources/functions/github diff --git a/sources/functions/github b/sources/functions/github new file mode 100644 index 000000000..1ce03b927 --- /dev/null +++ b/sources/functions/github @@ -0,0 +1,33 @@ +#!/bin/bash + +function _get_gh_latest_release() { + # Uses JQ to parse the JSON response from the GitHub API and return the "browser_download_url" of either latest or a specific tag + # Usage: _get_gh_latest_release "user/repo" ("filter") ("tag") + # Example: _get_gh_latest_release "autobrr/autborr" + # Example: _get_gh_latest_release "autobrr/autborr" "linux-amd64" + # Example: _get_gh_latest_release "autobrr/autborr" "linux-amd64" "v0.1.0" + repo="$1" + filter="$2" + tag="$3" + if [[ -n "$tag" ]]; then + # There is an inconsistency between the API in `tag` and `tags` FYI + response=$(curl -s "https://api.github.com/repos/$repo/releases/tags/$tag") || { + echo_error "Failed to query GitHub for version $tag of $repo" + return 1 + } + else + response=$(curl -s "https://api.github.com/repos/$repo/releases/latest") || { + echo_error "Failed to query GitHub for latest version of $repo" + return 1 + } + fi + + if [[ -z "$filter" ]]; then + echo "$response" | jq -r ".assets[] | .browser_download_url" + return $? + else + echo "$response" | jq -r --arg str "$filter" -c '.assets[] | select(.name | contains($str)) | .browser_download_url' + return $? + fi +} +export -f _get_gh_latest_release From 3f9b0f685efe1a6115c2abaa4339c99fc59cc2e6 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:07:03 +0200 Subject: [PATCH 2/8] Add function to filter the GH api for download URLs --- sources/functions/utils | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sources/functions/utils b/sources/functions/utils index 72c0c3263..58359a135 100644 --- a/sources/functions/utils +++ b/sources/functions/utils @@ -168,3 +168,34 @@ function github_latest_version() { repo=$1 curl -fsSLI -o /dev/null -w %{url_effective} https://github.com/${repo}/releases/latest | grep -o '[^/]*$' } + +function github_release_url() { + # Argument expects the author/repo format, defaults to all DL links of the latest release + # Usage: github_latest_version "user/repo" ["name_filter"] ["tag"] + # Example: github_latest_version "autobrr/autborr" + # Example: github_latest_version "autobrr/autborr" "linux-amd64" + # Example: github_latest_version "autobrr/autborr" "linux-amd64" "v0.1.0" + repo="$1" + filter="$2" + tag="$3" + if [[ -n "$tag" ]]; then + # There is an inconsistency between the API in `tag` and `tags` FYI + response=$(curl -s "https://api.github.com/repos/$repo/releases/tags/$tag") || { + echo_error "Failed to query GitHub for version $tag of $repo" + return 1 + } + else + response=$(curl -s "https://api.github.com/repos/$repo/releases/latest") || { + echo_error "Failed to query GitHub for latest version of $repo" + return 1 + } + fi + + if [[ -z "$filter" ]]; then + echo "$response" | jq -r ".assets[] | .browser_download_url" + return $? + else + echo "$response" | jq -r --arg str "$filter" -c '.assets[] | select(.name | contains($str)) | .browser_download_url' + return $? + fi +} From 0d3e751022e738f030546e68bb1e5b2bfccbc190 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:25:36 +0200 Subject: [PATCH 3/8] switch tag retrieval to the API method --- sources/functions/utils | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/functions/utils b/sources/functions/utils index 58359a135..764d875a3 100644 --- a/sources/functions/utils +++ b/sources/functions/utils @@ -166,7 +166,7 @@ function github_latest_version() { # Argument expects the author/repo format # e.g. swizzin/swizzin repo=$1 - curl -fsSLI -o /dev/null -w %{url_effective} https://github.com/${repo}/releases/latest | grep -o '[^/]*$' + curl -s https://api.github.com/repos/$repo/releases/latest | jq -r '.tag_name' } function github_release_url() { From 2521971a37f8303d98d7ea574dace82f801ebaad Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:48:44 +0200 Subject: [PATCH 4/8] add release tag export --- sources/functions/utils | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/sources/functions/utils b/sources/functions/utils index 764d875a3..637cf1318 100644 --- a/sources/functions/utils +++ b/sources/functions/utils @@ -171,10 +171,11 @@ function github_latest_version() { function github_release_url() { # Argument expects the author/repo format, defaults to all DL links of the latest release - # Usage: github_latest_version "user/repo" ["name_filter"] ["tag"] - # Example: github_latest_version "autobrr/autborr" - # Example: github_latest_version "autobrr/autborr" "linux-amd64" - # Example: github_latest_version "autobrr/autborr" "linux-amd64" "v0.1.0" + # Usage: github_release_url "user/repo" ["name_filter"] ["tag"] + # Example: github_release_url "MediaBrowser/Emby.Releases" + # Example: github_release_url "MediaBrowser/Emby.Releases" "$(_os_arch).deb" + # Example: github_release_url "MediaBrowser/Emby.Releases" "$(_os_arch).deb" "v0.1.0" + # also exports a variable with the name of the repo and the version, e.g. `autobrr_version` repo="$1" filter="$2" tag="$3" @@ -184,13 +185,19 @@ function github_release_url() { echo_error "Failed to query GitHub for version $tag of $repo" return 1 } + release_tag="$tag" else response=$(curl -s "https://api.github.com/repos/$repo/releases/latest") || { echo_error "Failed to query GitHub for latest version of $repo" return 1 } + release_tag=$(echo "$response" | jq -r '.tag_name') fi + # e.g. MediaBrowser/Emby.Releases -> Emby.Releases -> Emby + project_name=$(echo $repo | cut -d'/' -f2 | grep -oe '^\w*') + export ${project_name}_version="$release_tag" + if [[ -z "$filter" ]]; then echo "$response" | jq -r ".assets[] | .browser_download_url" return $? From c0e3bc2c81de623b637485e80e8df6210d47b38e Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:50:30 +0200 Subject: [PATCH 5/8] rename variable to make it more clear --- sources/functions/utils | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/functions/utils b/sources/functions/utils index 637cf1318..03635e6d4 100644 --- a/sources/functions/utils +++ b/sources/functions/utils @@ -175,14 +175,14 @@ function github_release_url() { # Example: github_release_url "MediaBrowser/Emby.Releases" # Example: github_release_url "MediaBrowser/Emby.Releases" "$(_os_arch).deb" # Example: github_release_url "MediaBrowser/Emby.Releases" "$(_os_arch).deb" "v0.1.0" - # also exports a variable with the name of the repo and the version, e.g. `autobrr_version` + # also exports a variable with the name of the repo and the tag, e.g. `github_Emby_tag` repo="$1" filter="$2" tag="$3" if [[ -n "$tag" ]]; then # There is an inconsistency between the API in `tag` and `tags` FYI response=$(curl -s "https://api.github.com/repos/$repo/releases/tags/$tag") || { - echo_error "Failed to query GitHub for version $tag of $repo" + echo_error "Failed to query GitHub for tag $tag of $repo" return 1 } release_tag="$tag" @@ -196,7 +196,7 @@ function github_release_url() { # e.g. MediaBrowser/Emby.Releases -> Emby.Releases -> Emby project_name=$(echo $repo | cut -d'/' -f2 | grep -oe '^\w*') - export ${project_name}_version="$release_tag" + export github_${project_name}_tag="$release_tag" if [[ -z "$filter" ]]; then echo "$response" | jq -r ".assets[] | .browser_download_url" From 96242bcd680ca58e208ae2de2a4dbf6636907bb8 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Sat, 15 Jul 2023 00:02:18 +0200 Subject: [PATCH 6/8] Replace old usage --- scripts/install/emby.sh | 4 ++-- scripts/install/jackett.sh | 4 ++-- scripts/install/mango.sh | 5 ++--- scripts/install/sabnzbd.sh | 8 ++------ scripts/upgrade/emby.sh | 6 +++--- scripts/upgrade/sabnzbd.sh | 16 ++++++---------- 6 files changed, 17 insertions(+), 26 deletions(-) diff --git a/scripts/install/emby.sh b/scripts/install/emby.sh index a4a7e077e..629c3ca24 100755 --- a/scripts/install/emby.sh +++ b/scripts/install/emby.sh @@ -35,8 +35,8 @@ fi username=$(cut -d: -f1 < /root/.master.info) echo_progress_start "Downloading emby installer" -current=$(github_latest_version MediaBrowser/Emby.Releases) -wget -O /tmp/emby.dpkg https://github.com/MediaBrowser/Emby.Releases/releases/download/${current}/emby-server-deb_${current}_$(_os_arch).deb >> $log 2>&1 || { +dl_url=$(github_release_url MediaBrowser/Emby.Releases "$(_os_arch).deb") +wget -O /tmp/emby.dpkg "$dl_url" >> $log 2>&1 || { echo_error "Failed to download" exit 1 } diff --git a/scripts/install/jackett.sh b/scripts/install/jackett.sh index 96d3d8ea5..bff8e9415 100755 --- a/scripts/install/jackett.sh +++ b/scripts/install/jackett.sh @@ -35,12 +35,12 @@ case "$(_os_arch)" in ;; esac -version=$(github_latest_version "Jackett/Jackett") +dl_url=$(github_release_url "Jackett/Jackett" "Linux${arch}") password=$(cut -d: -f2 < /root/.master.info) echo_progress_start "Downloading and extracting jackett" cd /home/$username -wget "https://github.com/Jackett/Jackett/releases/download/${version}/Jackett.Binaries.Linux${arch}.tar.gz" >> "$log" 2>&1 +wget "$dl_url" >> "$log" 2>&1 tar -xvzf Jackett.Binaries.*.tar.gz > /dev/null 2>&1 rm -f Jackett.Binaries.*.tar.gz chown ${username}:${username} -R Jackett diff --git a/scripts/install/mango.sh b/scripts/install/mango.sh index b9e8bbf08..786504e9e 100755 --- a/scripts/install/mango.sh +++ b/scripts/install/mango.sh @@ -8,8 +8,6 @@ mangousr="mango" # Downloading the latest binary function _install_mango() { - echo_progress_start "Downloading binary" - mango_latest=$(github_latest_version getmango/Mango) case "$(_os_arch)" in "arm32" | "arm64") @@ -17,7 +15,7 @@ function _install_mango() { exit 1 ;; "amd64") - dlurl="https://github.com/getmango/Mango/releases/download/${mango_latest}/mango" + dlurl=$(github_release_url getmango/Mango | grep -e 'mango$') ;; *) echo_error "Unsupported arch?" @@ -28,6 +26,7 @@ function _install_mango() { mkdir -p "$mangodir" mkdir -p "$mangodir"/library + echo_progress_start "Downloading binary" wget "${dlurl}" -O $mangodir/mango >> "$log" 2>&1 || { echo_error "Failed to download binary" exit 1 diff --git a/scripts/install/sabnzbd.sh b/scripts/install/sabnzbd.sh index 9bbde660d..e521dc3ff 100755 --- a/scripts/install/sabnzbd.sh +++ b/scripts/install/sabnzbd.sh @@ -15,16 +15,12 @@ user=$(_get_master_username) password="$(_get_user_password ${user})" -latestversion=$(github_latest_version sabnzbd/sabnzbd) || { - echo_error "Failed to query GitHub for latest sabnzbd version" - exit 1 -} -latest="https://github.com/sabnzbd/sabnzbd/archive/refs/tags/${latestversion}.tar.gz" +latest="$(github_release_url sabnzbd/sabnzbd "src.tar.gz")" systempy3_ver=$(get_candidate_version python3) #Version 4.0 is going to raise the min python version to 3.8 so we have to differentiate whether or not to build a pyenv -if dpkg --compare-versions ${systempy3_ver} lt 3.8.0 && dpkg --compare-versions ${latestversion} ge 3.5.0; then +if dpkg --compare-versions ${systempy3_ver} lt 3.8.0 && dpkg --compare-versions ${github_sabnzbd_version} ge 3.5.0; then LIST='par2 p7zip-full libffi-dev libssl-dev libglib2.0-dev libdbus-1-dev' PYENV=True else diff --git a/scripts/upgrade/emby.sh b/scripts/upgrade/emby.sh index b7550a1bb..89eaba444 100755 --- a/scripts/upgrade/emby.sh +++ b/scripts/upgrade/emby.sh @@ -2,12 +2,12 @@ # Simple tool to grab the latest release of emby . /etc/swizzin/sources/functions/utils -latest=$(github_latest_version MediaBrowser/Emby.Releases) +dl_url=$(github_release_url MediaBrowser/Emby.Releases "$(_os_arch).deb") current=$(dpkg-query -f='${Version}' --show emby-server) -if dpkg --compare-versions ${latest} gt ${current}; then +if dpkg --compare-versions ${github_Emby_tag} gt ${current}; then echo_info "Upgrading Emby" - wget -O /tmp/emby.dpkg https://github.com/MediaBrowser/Emby.Releases/releases/download/${latest}/emby-server-deb_${latest}_$(_os_arch).deb >> $log 2>&1 || { + wget -O /tmp/emby.dpkg "$dl_url" >> $log 2>&1 || { echo_error "Emby failed to download" exit 1 } diff --git a/scripts/upgrade/sabnzbd.sh b/scripts/upgrade/sabnzbd.sh index 93a2ac2da..e47a22d59 100755 --- a/scripts/upgrade/sabnzbd.sh +++ b/scripts/upgrade/sabnzbd.sh @@ -10,15 +10,11 @@ fi . /etc/swizzin/sources/functions/pyenv localversion=$(/opt/.venv/sabnzbd/bin/python /opt/sabnzbd/SABnzbd.py --version | grep -m1 SABnzbd | cut -d- -f2) #latest=$(curl -s https://sabnzbd.org/downloads | grep -m1 Linux | grep download-link-src | grep -oP "href=\"\K[^\"]+") -latestversion=$(github_latest_version sabnzbd/sabnzbd) || { - echo_error "Failed to query GitHub for latest sabnzbd version" - exit 1 -} -latest="https://github.com/sabnzbd/sabnzbd/archive/refs/tags/${latestversion}.tar.gz" +latest=$(github_release_url sabnzbd/sabnzbd "src.tar.gz") pyvenv_version=$(/opt/.venv/sabnzbd/bin/python --version | awk '{print $2}') -if dpkg --compare-versions ${pyvenv_version} lt 3.8.0 && dpkg --compare-versions ${latestversion} ge 3.5.0; then +if dpkg --compare-versions ${pyvenv_version} lt 3.8.0 && dpkg --compare-versions ${github_sabnzbd_version} ge 3.5.0; then LIST='par2 p7zip-full libffi-dev libssl-dev libglib2.0-dev libdbus-1-dev' PYENV_REBUILD=True elif [[ -f /opt/.venv/sabnzbd/bin/python2 ]]; then @@ -29,7 +25,7 @@ else fi apt_install $LIST -if dpkg --compare-versions ${localversion:-0.0} lt ${latestversion}; then +if dpkg --compare-versions ${localversion:-0.0} lt ${github_sabnzbd_version}; then if [[ $PYENV_REBUILD == True ]]; then echo_progress_start "Upgrading SABnzbd python virtual environment" rm -rf /opt/.venv/sabnzbd @@ -79,14 +75,14 @@ if dpkg --compare-versions ${localversion:-0.0} lt ${latestversion}; then rm /tmp/sabnzbd.tar.gz echo_progress_start "Checking pip requirements" - if [[ $latestversion =~ ^3\.0\.[1-2] ]]; then + if [[ $github_sabnzbd_version =~ ^3\.0\.[1-2] ]]; then sed -i "s/feedparser.*/feedparser<6.0.0/g" /opt/sabnzbd/requirements.txt fi sudo -u ${user} bash -c "/opt/.venv/sabnzbd/bin/pip install -r /opt/sabnzbd/requirements.txt" >> "$log" 2>&1 echo_progress_done systemctl try-restart sabnzbd - echo_info "SABnzbd has been upgraded to version ${latestversion}!" + echo_info "SABnzbd has been upgraded to version ${github_sabnzbd_version}!" else - echo_info "Nothing to do! Current version (${localversion}) matches the remote version (${latestversion})" + echo_info "Nothing to do! Current version (${localversion}) matches the remote version (${github_sabnzbd_version})" exit 0 fi From 08f5556555bac6db71bdb689cfc8abe33735ae00 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Sat, 15 Jul 2023 12:10:35 +0200 Subject: [PATCH 7/8] don't need this twice --- sources/functions/github | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 sources/functions/github diff --git a/sources/functions/github b/sources/functions/github deleted file mode 100644 index 1ce03b927..000000000 --- a/sources/functions/github +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -function _get_gh_latest_release() { - # Uses JQ to parse the JSON response from the GitHub API and return the "browser_download_url" of either latest or a specific tag - # Usage: _get_gh_latest_release "user/repo" ("filter") ("tag") - # Example: _get_gh_latest_release "autobrr/autborr" - # Example: _get_gh_latest_release "autobrr/autborr" "linux-amd64" - # Example: _get_gh_latest_release "autobrr/autborr" "linux-amd64" "v0.1.0" - repo="$1" - filter="$2" - tag="$3" - if [[ -n "$tag" ]]; then - # There is an inconsistency between the API in `tag` and `tags` FYI - response=$(curl -s "https://api.github.com/repos/$repo/releases/tags/$tag") || { - echo_error "Failed to query GitHub for version $tag of $repo" - return 1 - } - else - response=$(curl -s "https://api.github.com/repos/$repo/releases/latest") || { - echo_error "Failed to query GitHub for latest version of $repo" - return 1 - } - fi - - if [[ -z "$filter" ]]; then - echo "$response" | jq -r ".assets[] | .browser_download_url" - return $? - else - echo "$response" | jq -r --arg str "$filter" -c '.assets[] | select(.name | contains($str)) | .browser_download_url' - return $? - fi -} -export -f _get_gh_latest_release From 2d32414e8a0f065d85fbc9c3dd8771b08ac769a3 Mon Sep 17 00:00:00 2001 From: flying-sausages <23618693+flying-sausages@users.noreply.github.com> Date: Sat, 15 Jul 2023 13:01:14 +0200 Subject: [PATCH 8/8] re-write without using API --- sources/functions/utils | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/sources/functions/utils b/sources/functions/utils index 03635e6d4..84ffc5b28 100644 --- a/sources/functions/utils +++ b/sources/functions/utils @@ -166,7 +166,7 @@ function github_latest_version() { # Argument expects the author/repo format # e.g. swizzin/swizzin repo=$1 - curl -s https://api.github.com/repos/$repo/releases/latest | jq -r '.tag_name' + curl -fsSLI -o /dev/null -w %{url_effective} https://github.com/${repo}/releases/latest | grep -o '[^/]*$' } function github_release_url() { @@ -178,31 +178,31 @@ function github_release_url() { # also exports a variable with the name of the repo and the tag, e.g. `github_Emby_tag` repo="$1" filter="$2" - tag="$3" - if [[ -n "$tag" ]]; then - # There is an inconsistency between the API in `tag` and `tags` FYI - response=$(curl -s "https://api.github.com/repos/$repo/releases/tags/$tag") || { - echo_error "Failed to query GitHub for tag $tag of $repo" - return 1 - } - release_tag="$tag" - else - response=$(curl -s "https://api.github.com/repos/$repo/releases/latest") || { + + if [[ -z "$3" ]]; then + tag=$(github_latest_version "${repo}") || { echo_error "Failed to query GitHub for latest version of $repo" return 1 } - release_tag=$(echo "$response" | jq -r '.tag_name') + else + tag="$3" fi - # e.g. MediaBrowser/Emby.Releases -> Emby.Releases -> Emby - project_name=$(echo $repo | cut -d'/' -f2 | grep -oe '^\w*') - export github_${project_name}_tag="$release_tag" + link_payload=$(curl -fsL https://github.com/${repo}/releases/expanded_assets/$tag) || { + echo_error "Failed to query GitHub for tag ${tag:='latest'} of $repo" + return 1 + } + + links=$(echo "${link_payload}" | grep -oP '(?<=href=")[^"]+' | sed -e 's|^|https://github.com|') - if [[ -z "$filter" ]]; then - echo "$response" | jq -r ".assets[] | .browser_download_url" - return $? + if [[ -n "$filter" ]]; then + echo "$links" | grep "$filter" else - echo "$response" | jq -r --arg str "$filter" -c '.assets[] | select(.name | contains($str)) | .browser_download_url' - return $? + echo "$links" fi + + # e.g. MediaBrowser/Emby.Releases -> Emby.Releases -> Emby + project_name=$(echo "$repo" | cut -d'/' -f2 | grep -oe '^\w*') + export github_${project_name}_tag="$tag" + }