From 7d5123bdd688fba4ebabc98e1d69b491a7adb472 Mon Sep 17 00:00:00 2001 From: tremor021 Date: Thu, 22 Jan 2026 22:20:06 +0100 Subject: [PATCH] add fetch_and_deploy_archive --- misc/tools.func | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/misc/tools.func b/misc/tools.func index 260fb9a03..a86ff9fc6 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -5922,3 +5922,116 @@ EOF msg_ok "Docker setup completed" } + +# ------------------------------------------------------------------------------ +# Fetch and deploy archive from URL +# Downloads an archive (zip or tar.gz) from a URL and extracts it to a directory +# +# Usage: fetch_and_deploy_archive "url" "directory" +# url - URL to the archive (zip or tar.gz) +# directory - Destination path where the archive will be extracted +# +# Examples: +# fetch_and_deploy_archive "https://example.com/app.tar.gz" "/opt/myapp" +# fetch_and_deploy_archive "https://example.com/app.zip" "/opt/myapp" +# ------------------------------------------------------------------------------ +function fetch_and_deploy_archive() { + local url="$1" + local directory="$2" + + if [[ -z "$url" ]]; then + msg_error "URL parameter is required" + return 1 + fi + + if [[ -z "$directory" ]]; then + msg_error "Directory parameter is required" + return 1 + fi + + local filename="${url##*/}" + + local archive_type="zip" + if [[ "$filename" == *.tar.gz || "$filename" == *.tgz ]]; then + archive_type="tar" + fi + + msg_info "Downloading archive from $url" + + local tmpdir + tmpdir=$(mktemp -d) || { + msg_error "Failed to create temporary directory" + return 1 + } + + curl -fsSL -o "$tmpdir/$filename" "$url" || { + msg_error "Download failed: $url" + rm -rf "$tmpdir" + return 1 + } + + msg_info "Extracting archive to $directory" + + mkdir -p "$directory" + + if [[ "${CLEAN_INSTALL:-0}" == "1" ]]; then + rm -rf "${directory:?}/"* + fi + + local unpack_tmp + unpack_tmp=$(mktemp -d) + + if [[ "$archive_type" == "zip" ]]; then + ensure_dependencies unzip + unzip -q "$tmpdir/$filename" -d "$unpack_tmp" || { + msg_error "Failed to extract ZIP archive" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + } + elif [[ "$archive_type" == "tar" ]]; then + tar --no-same-owner -xf "$tmpdir/$filename" -C "$unpack_tmp" || { + msg_error "Failed to extract TAR archive" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + } + fi + + local top_entries + top_entries=$(find "$unpack_tmp" -mindepth 1 -maxdepth 1) + + if [[ "$(echo "$top_entries" | wc -l)" -eq 1 && -d "$top_entries" ]]; then + local inner_dir="$top_entries" + shopt -s dotglob nullglob + if compgen -G "$inner_dir/*" >/dev/null; then + cp -r "$inner_dir"/* "$directory/" || { + msg_error "Failed to copy contents from $inner_dir to $directory" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + } + else + msg_error "Inner directory is empty: $inner_dir" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + fi + shopt -u dotglob nullglob + else + shopt -s dotglob nullglob + if compgen -G "$unpack_tmp/*" >/dev/null; then + cp -r "$unpack_tmp"/* "$directory/" || { + msg_error "Failed to copy contents to $directory" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + } + else + msg_error "Unpacked archive is empty" + rm -rf "$tmpdir" "$unpack_tmp" + return 1 + fi + shopt -u dotglob nullglob + fi + + rm -rf "$tmpdir" "$unpack_tmp" + msg_ok "Successfully deployed archive to $directory" + return 0 +} +