From 403f08fd637de5a769abd88faf843c18293d2726 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 10 Apr 2025 16:22:12 +0200 Subject: [PATCH] gh function into helper.func --- misc/helper.func | 172 ++++++++++++++++++++++++++++++++++++++++++++++ misc/install.func | 171 --------------------------------------------- 2 files changed, 172 insertions(+), 171 deletions(-) diff --git a/misc/helper.func b/misc/helper.func index d1fb221..1205c68 100644 --- a/misc/helper.func +++ b/misc/helper.func @@ -206,3 +206,175 @@ function install_mysql() { msg_ok "Installed MySQL $MYSQL_VERSION" fi } + +fetch_and_deploy_gh_release() { + local repo="$1" + local app=$(echo ${APPLICATION,,} | tr -d ' ') + local api_url="https://api.github.com/repos/$repo/releases/latest" + local header=() + local attempt=0 + local max_attempts=3 + local api_response tag http_code + local current_version="" + local curl_timeout="--connect-timeout 10 --max-time 30" + + # Check if the app directory exists and if there's a version file + if [[ -f "/opt/${app}_version.txt" ]]; then + current_version=$(cat "/opt/${app}_version.txt") + $STD msg_info "Current version: $current_version" + fi + + # ensure that jq is installed + if ! command -v jq &>/dev/null; then + $STD msg_info "Installing jq..." + apt-get update -qq &>/dev/null + apt-get install -y jq &>/dev/null || { + msg_error "Failed to install jq" + return 1 + } + fi + + [[ -n "${GITHUB_TOKEN:-}" ]] && header=(-H "Authorization: token $GITHUB_TOKEN") + + until [[ $attempt -ge $max_attempts ]]; do + ((attempt++)) || true + $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") + http_code="${api_response:(-3)}" + + if [[ "$http_code" == "404" ]]; then + msg_error "Repository $repo has no Release candidate (404)" + return 1 + fi + + if [[ "$http_code" != "200" ]]; then + $STD msg_info "Request failed with HTTP $http_code, retrying...\n" + sleep $((attempt * 2)) + continue + fi + + api_response=$(/dev/null; then + msg_error "Repository not found: $repo" + return 1 + fi + + tag=$(echo "$api_response" | jq -r '.tag_name // .name // empty') + [[ "$tag" =~ ^v[0-9] ]] && tag="${tag:1}" + + if [[ -z "$tag" ]]; then + $STD msg_info "Empty tag received, retrying...\n" + sleep $((attempt * 2)) + continue + fi + + $STD msg_ok "Found release: $tag for $repo" + break + done + + if [[ -z "$tag" ]]; then + msg_error "Failed to fetch release for $repo after $max_attempts attempts." + exit 1 + fi + + # Version comparison (if we already have this version, skip) + if [[ "$current_version" == "$tag" ]]; then + $STD msg_info "Already running the latest version ($tag). Skipping update." + return 0 + fi + + local version="$tag" + local base_url="https://github.com/$repo/releases/download/v$tag" + local tmpdir + tmpdir=$(mktemp -d) || return 1 + + # Extract list of assets from the Release API + local assets urls + assets=$(echo "$api_response" | jq -r '.assets[].browser_download_url') || true + + # Detect current architecture + local arch + if command -v dpkg &>/dev/null; then + arch=$(dpkg --print-architecture) + elif command -v uname &>/dev/null; then + case "$(uname -m)" in + x86_64) arch="amd64" ;; + aarch64) arch="arm64" ;; + armv7l) arch="armv7" ;; + armv6l) arch="armv6" ;; + *) arch="unknown" ;; + esac + else + arch="unknown" + fi + $STD msg_info "Detected system architecture: $arch" + + # Try to find a matching asset for our architecture + local url="" + for u in $assets; do + if [[ "$u" =~ $arch.*\.tar\.gz$ ]]; then + url="$u" + $STD msg_info "Found matching architecture asset: $url" + break + fi + done + + # Fallback to other architectures if our specific one isn't found + if [[ -z "$url" ]]; then + for u in $assets; do + if [[ "$u" =~ (x86_64|amd64|arm64|armv7|armv6).*\.tar\.gz$ ]]; then + url="$u" + $STD msg_info "Architecture-specific asset not found, using: $url" + break + fi + done + fi + + # Fallback to any tar.gz + if [[ -z "$url" ]]; then + for u in $assets; do + if [[ "$u" =~ \.tar\.gz$ ]]; then + url="$u" + $STD msg_info "Using generic tarball: $url" + break + fi + done + fi + + # Final fallback to GitHub source tarball + if [[ -z "$url" ]]; then + url="https://github.com/$repo/archive/refs/tags/$version.tar.gz" + $STD msg_info "Trying GitHub source tarball fallback: $url" + fi + + local filename="${url##*/}" + $STD msg_info "Downloading $url" + + if ! curl $curl_timeout -fsSL -o "$tmpdir/$filename" "$url"; then + msg_error "Failed to download release asset from $url" + rm -rf "$tmpdir" + return 1 + fi + + mkdir -p "/opt/$app" + + tar -xzf "$tmpdir/$filename" -C "$tmpdir" + local content_root + content_root=$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d) + if [[ $(echo "$content_root" | wc -l) -eq 1 ]]; then + cp -r "$content_root"/* "/opt/$app/" + else + cp -r "$tmpdir"/* "/opt/$app/" + fi + + echo "$version" >"/opt/${app}_version.txt" + $STD msg_ok "Deployed $app v$version to /opt/$app" + rm -rf "$tmpdir" +} diff --git a/misc/install.func b/misc/install.func index d5c7b1d..5d1bae4 100644 --- a/misc/install.func +++ b/misc/install.func @@ -276,174 +276,3 @@ EOF fi } -fetch_and_deploy_gh_release() { - local repo="$1" - local app=$(echo ${APPLICATION,,} | tr -d ' ') - local api_url="https://api.github.com/repos/$repo/releases/latest" - local header=() - local attempt=0 - local max_attempts=3 - local api_response tag http_code - local current_version="" - local curl_timeout="--connect-timeout 10 --max-time 30" - - # Check if the app directory exists and if there's a version file - if [[ -f "/opt/${app}_version.txt" ]]; then - current_version=$(cat "/opt/${app}_version.txt") - $STD msg_info "Current version: $current_version" - fi - - # ensure that jq is installed - if ! command -v jq &>/dev/null; then - $STD msg_info "Installing jq..." - apt-get update -qq &>/dev/null - apt-get install -y jq &>/dev/null || { - msg_error "Failed to install jq" - return 1 - } - fi - - [[ -n "${GITHUB_TOKEN:-}" ]] && header=(-H "Authorization: token $GITHUB_TOKEN") - - until [[ $attempt -ge $max_attempts ]]; do - ((attempt++)) || true - $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") - http_code="${api_response:(-3)}" - - if [[ "$http_code" == "404" ]]; then - msg_error "Repository $repo has no Release candidate (404)" - return 1 - fi - - if [[ "$http_code" != "200" ]]; then - $STD msg_info "Request failed with HTTP $http_code, retrying...\n" - sleep $((attempt * 2)) - continue - fi - - api_response=$(/dev/null; then - msg_error "Repository not found: $repo" - return 1 - fi - - tag=$(echo "$api_response" | jq -r '.tag_name // .name // empty') - [[ "$tag" =~ ^v[0-9] ]] && tag="${tag:1}" - - if [[ -z "$tag" ]]; then - $STD msg_info "Empty tag received, retrying...\n" - sleep $((attempt * 2)) - continue - fi - - $STD msg_ok "Found release: $tag for $repo" - break - done - - if [[ -z "$tag" ]]; then - msg_error "Failed to fetch release for $repo after $max_attempts attempts." - exit 1 - fi - - # Version comparison (if we already have this version, skip) - if [[ "$current_version" == "$tag" ]]; then - $STD msg_info "Already running the latest version ($tag). Skipping update." - return 0 - fi - - local version="$tag" - local base_url="https://github.com/$repo/releases/download/v$tag" - local tmpdir - tmpdir=$(mktemp -d) || return 1 - - # Extract list of assets from the Release API - local assets urls - assets=$(echo "$api_response" | jq -r '.assets[].browser_download_url') || true - - # Detect current architecture - local arch - if command -v dpkg &>/dev/null; then - arch=$(dpkg --print-architecture) - elif command -v uname &>/dev/null; then - case "$(uname -m)" in - x86_64) arch="amd64" ;; - aarch64) arch="arm64" ;; - armv7l) arch="armv7" ;; - armv6l) arch="armv6" ;; - *) arch="unknown" ;; - esac - else - arch="unknown" - fi - $STD msg_info "Detected system architecture: $arch" - - # Try to find a matching asset for our architecture - local url="" - for u in $assets; do - if [[ "$u" =~ $arch.*\.tar\.gz$ ]]; then - url="$u" - $STD msg_info "Found matching architecture asset: $url" - break - fi - done - - # Fallback to other architectures if our specific one isn't found - if [[ -z "$url" ]]; then - for u in $assets; do - if [[ "$u" =~ (x86_64|amd64|arm64|armv7|armv6).*\.tar\.gz$ ]]; then - url="$u" - $STD msg_info "Architecture-specific asset not found, using: $url" - break - fi - done - fi - - # Fallback to any tar.gz - if [[ -z "$url" ]]; then - for u in $assets; do - if [[ "$u" =~ \.tar\.gz$ ]]; then - url="$u" - $STD msg_info "Using generic tarball: $url" - break - fi - done - fi - - # Final fallback to GitHub source tarball - if [[ -z "$url" ]]; then - url="https://github.com/$repo/archive/refs/tags/$version.tar.gz" - $STD msg_info "Trying GitHub source tarball fallback: $url" - fi - - local filename="${url##*/}" - $STD msg_info "Downloading $url" - - if ! curl $curl_timeout -fsSL -o "$tmpdir/$filename" "$url"; then - msg_error "Failed to download release asset from $url" - rm -rf "$tmpdir" - return 1 - fi - - mkdir -p "/opt/$app" - - tar -xzf "$tmpdir/$filename" -C "$tmpdir" - local content_root - content_root=$(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d) - if [[ $(echo "$content_root" | wc -l) -eq 1 ]]; then - cp -r "$content_root"/* "/opt/$app/" - else - cp -r "$tmpdir"/* "/opt/$app/" - fi - - echo "$version" >"/opt/${app}_version.txt" - $STD msg_ok "Deployed $app v$version to /opt/$app" - rm -rf "$tmpdir" -}