Update tools.func

This commit is contained in:
Tobias 2025-05-13 20:52:05 +02:00 committed by GitHub
parent d6887de9b1
commit 48db4482b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -518,13 +518,11 @@ fetch_and_deploy_gh_release() {
local api_response tag http_code local api_response tag http_code
local current_version="" local current_version=""
local curl_timeout="--connect-timeout 10 --max-time 30" local curl_timeout="--connect-timeout 10 --max-time 30"
# Check if the app directory exists and if there's a version file # Check if the app directory exists and if there's a version file
if [[ -f "/opt/${app}_version.txt" ]]; then if [[ -f "/opt/${app}_version.txt" ]]; then
current_version=$(cat "/opt/${app}_version.txt") current_version=$(cat "/opt/${app}_version.txt")
$STD msg_info "Current version: $current_version" $STD msg_info "Current version: $current_version"
fi fi
# ensure that jq is installed # ensure that jq is installed
if ! command -v jq &>/dev/null; then if ! command -v jq &>/dev/null; then
$STD msg_info "Installing jq..." $STD msg_info "Installing jq..."
@ -534,71 +532,57 @@ fetch_and_deploy_gh_release() {
return 1 return 1
} }
fi fi
[[ -n "${GITHUB_TOKEN:-}" ]] && header=(-H "Authorization: token $GITHUB_TOKEN") [[ -n "${GITHUB_TOKEN:-}" ]] && header=(-H "Authorization: token $GITHUB_TOKEN")
until [[ $attempt -ge $max_attempts ]]; do until [[ $attempt -ge $max_attempts ]]; do
((attempt++)) || true ((attempt++)) || true
$STD msg_info "[$attempt/$max_attempts] Fetching GitHub release for $repo...\n" $STD msg_info "[$attempt/$max_attempts] Fetching GitHub release for $repo...\n"
api_response=$(curl $curl_timeout -fsSL -w "%{http_code}" -o /tmp/gh_resp.json "${header[@]}" "$api_url") api_response=$(curl $curl_timeout -fsSL -w "%{http_code}" -o /tmp/gh_resp.json "${header[@]}" "$api_url")
http_code="${api_response:(-3)}" http_code="${api_response:(-3)}"
if [[ "$http_code" == "404" ]]; then if [[ "$http_code" == "404" ]]; then
msg_error "Repository $repo has no Release candidate (404)" msg_error "Repository $repo has no Release candidate (404)"
return 1 return 1
fi fi
if [[ "$http_code" != "200" ]]; then if [[ "$http_code" != "200" ]]; then
$STD msg_info "Request failed with HTTP $http_code, retrying...\n" $STD msg_info "Request failed with HTTP $http_code, retrying...\n"
sleep $((attempt * 2)) sleep $((attempt * 2))
continue continue
fi fi
api_response=$(</tmp/gh_resp.json) api_response=$(</tmp/gh_resp.json)
if echo "$api_response" | grep -q "API rate limit exceeded"; then if echo "$api_response" | grep -q "API rate limit exceeded"; then
msg_error "GitHub API rate limit exceeded." msg_error "GitHub API rate limit exceeded."
return 1 return 1
fi fi
if echo "$api_response" | jq -e '.message == "Not Found"' &>/dev/null; then if echo "$api_response" | jq -e '.message == "Not Found"' &>/dev/null; then
msg_error "Repository not found: $repo" msg_error "Repository not found: $repo"
return 1 return 1
fi fi
tag=$(echo "$api_response" | jq -r '.tag_name // .name // empty') tag=$(echo "$api_response" | jq -r '.tag_name // .name // empty')
[[ "$tag" =~ ^v[0-9] ]] && tag="${tag:1}"
version="${tag#v}" version="${tag#v}"
if [[ -z "$tag" ]]; then if [[ -z "$tag" ]]; then
$STD msg_info "Empty tag received, retrying...\n" $STD msg_info "Empty tag received, retrying...\n"
sleep $((attempt * 2)) sleep $((attempt * 2))
continue continue
fi fi
$STD msg_ok "Found release: $tag for $repo" $STD msg_ok "Found release: $tag for $repo"
break break
done done
if [[ -z "$tag" ]]; then if [[ -z "$tag" ]]; then
msg_error "Failed to fetch release for $repo after $max_attempts attempts." msg_error "Failed to fetch release for $repo after $max_attempts attempts."
exit 1 exit 1
fi fi
# Version comparison (if we already have this version, skip) # Version comparison (if we already have this version, skip)
if [[ "$current_version" == "$version" ]]; then if [[ "$current_version" == "$tag" ]]; then
$STD msg_info "Already running the latest version ($version). Skipping update." $STD msg_info "Already running the latest version ($tag). Skipping update."
return 0 return 0
fi fi
local base_url="https://github.com/$repo/releases/download/v$tag" local base_url="https://github.com/$repo/releases/download/v$tag"
local tmpdir local tmpdir
tmpdir=$(mktemp -d) || return 1 tmpdir=$(mktemp -d) || return 1
# Extract list of assets from the Release API # Extract list of assets from the Release API
local assets urls local assets urls
assets=$(echo "$api_response" | jq -r '.assets[].browser_download_url') || true assets=$(echo "$api_response" | jq -r '.assets[].browser_download_url') || true
# Detect current architecture # Detect current architecture
local arch local arch
if command -v dpkg &>/dev/null; then if command -v dpkg &>/dev/null; then
@ -615,7 +599,6 @@ fetch_and_deploy_gh_release() {
arch="unknown" arch="unknown"
fi fi
$STD msg_info "Detected system architecture: $arch" $STD msg_info "Detected system architecture: $arch"
# Try to find a matching asset for our architecture # Try to find a matching asset for our architecture
local url="" local url=""
for u in $assets; do for u in $assets; do
@ -625,7 +608,6 @@ fetch_and_deploy_gh_release() {
break break
fi fi
done done
# Fallback to other architectures if our specific one isn't found # Fallback to other architectures if our specific one isn't found
if [[ -z "$url" ]]; then if [[ -z "$url" ]]; then
for u in $assets; do for u in $assets; do
@ -636,7 +618,6 @@ fetch_and_deploy_gh_release() {
fi fi
done done
fi fi
# Fallback to any tar.gz # Fallback to any tar.gz
if [[ -z "$url" ]]; then if [[ -z "$url" ]]; then
for u in $assets; do for u in $assets; do
@ -647,24 +628,26 @@ fetch_and_deploy_gh_release() {
fi fi
done done
fi fi
# Final fallback to GitHub source tarball # Final fallback to GitHub source tarball
if [[ -z "$url" ]]; then if [[ -z "$url" ]]; then
url="https://github.com/$repo/archive/refs/tags/$version.tar.gz" # Use tarball_url directly from API response instead of constructing our own URL
$STD msg_info "Trying GitHub source tarball fallback: $url" url=$(echo "$api_response" | jq -r '.tarball_url // empty')
# If tarball_url is empty for some reason, fall back to a constructed URL as before
if [[ -z "$url" ]]; then
url="https://github.com/$repo/archive/refs/tags/v$version.tar.gz"
fi
$STD msg_info "Using GitHub source tarball: $url"
fi fi
local filename="${url##*/}" local filename="${url##*/}"
$STD msg_info "Downloading $url" $STD msg_info "Downloading $url"
if ! curl $curl_timeout -fsSL -o "$tmpdir/$filename" "$url"; then if ! curl $curl_timeout -fsSL -o "$tmpdir/$filename" "$url"; then
msg_error "Failed to download release asset from $url" msg_error "Failed to download release asset from $url"
rm -rf "$tmpdir" rm -rf "$tmpdir"
return 1 return 1
fi fi
mkdir -p "/opt/$app" mkdir -p "/opt/$app"
tar -xzf "$tmpdir/$filename" -C "$tmpdir" tar -xzf "$tmpdir/$filename" -C "$tmpdir"
local content_root local content_root
content_root=$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d) content_root=$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d)
@ -673,7 +656,6 @@ fetch_and_deploy_gh_release() {
else else
cp -r "$tmpdir"/* "/opt/$app/" cp -r "$tmpdir"/* "/opt/$app/"
fi fi
echo "$version" >"/opt/${app}_version.txt" echo "$version" >"/opt/${app}_version.txt"
$STD msg_ok "Deployed $app v$version to /opt/$app" $STD msg_ok "Deployed $app v$version to /opt/$app"
rm -rf "$tmpdir" rm -rf "$tmpdir"