add fetch_and_deploy_archive

This commit is contained in:
tremor021 2026-01-22 22:20:06 +01:00
parent 7bf43f007e
commit 7d5123bdd6

View File

@ -5922,3 +5922,116 @@ EOF
msg_ok "Docker setup completed" 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
}