refactor: download repo archive once, copy selected folders to deploy
- Replaces per-file curl downloads with single archive download (tar.gz from Gitea), extracts only common/ + selected type folder. - Interactive prompt uses /dev/tty when stdin is piped (curl|bash). - Falls back to both only when fully headless. - Post-extract sed patch fixes library sourcing path for flat deploy/ until source scripts are updated in the repo.
This commit is contained in:
parent
68fbb25db2
commit
0ee73f5af3
1 changed files with 77 additions and 70 deletions
117
setup.sh
117
setup.sh
|
|
@ -2,18 +2,22 @@
|
|||
# ==============================================================================
|
||||
# Wallarm Deployment Setup Script
|
||||
# ==============================================================================
|
||||
# Downloads Wallarm deployment scripts from the Git repository and makes them
|
||||
# executable.
|
||||
# Downloads Wallarm deployment scripts from the Git repository and places
|
||||
# them in a single deploy/ directory.
|
||||
#
|
||||
# When run interactively (terminal), asks which deployment type to download:
|
||||
# When run interactively (or piped via curl|bash to a terminal), asks which
|
||||
# deployment type to download:
|
||||
# 1. docker - Wallarm filtering node as a Docker container
|
||||
# 2. native - Wallarm filtering node installed directly on the OS (no Docker)
|
||||
# 3. both - All scripts (docker + native)
|
||||
#
|
||||
# When piped (curl ... | bash), downloads BOTH by default for backward
|
||||
# compatibility. Override with the DEPLOYMENT_TYPE env var:
|
||||
# Falls back to downloading BOTH only when fully headless (no /dev/tty).
|
||||
# Override with the DEPLOYMENT_TYPE env var:
|
||||
# DEPLOYMENT_TYPE=native curl -fsSL ".../setup.sh" | bash
|
||||
# DEPLOYMENT_TYPE=docker curl -fsSL ".../setup.sh" | bash
|
||||
#
|
||||
# Downloads the repo archive once, then copies only the needed folders
|
||||
# (common + docker and/or native) into deploy/. No per-file curl calls.
|
||||
# ==============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
|
@ -27,20 +31,12 @@ CYAN='\033[0;36m'
|
|||
BOLD='\033[1m'
|
||||
NC='\033[0m'
|
||||
|
||||
# Git repository URL base
|
||||
GIT_BASE="https://git.sechpoint.app/customer-engineering/wallarm/raw/branch/main/"
|
||||
# Git repository archive URL (Gitea-style)
|
||||
REPO_ARCHIVE="https://git.sechpoint.app/customer-engineering/wallarm/archive/main.tar.gz"
|
||||
|
||||
# Per-deployment-type scripts
|
||||
DOCKER_SCRIPTS=(
|
||||
"wallarm-ct-check.sh"
|
||||
"wallarm-ct-deploy.sh"
|
||||
"wallarm-ct-reconfigure.sh"
|
||||
"wallarm-ct-uninstall.sh"
|
||||
)
|
||||
|
||||
NATIVE_SCRIPTS=(
|
||||
"wallarm-native.sh"
|
||||
)
|
||||
# Temp directory for extracted archive
|
||||
TEMP_DIR=$(mktemp -d /tmp/wallarm-setup.XXXXXX)
|
||||
trap 'rm -rf "$TEMP_DIR"' EXIT
|
||||
|
||||
# Detect download command
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
|
|
@ -53,43 +49,39 @@ else
|
|||
exit 1
|
||||
fi
|
||||
|
||||
download_file() {
|
||||
local url="$1"
|
||||
local dest="$2"
|
||||
local description="$3"
|
||||
# --- Archive download and extraction ---
|
||||
|
||||
echo -e "${YELLOW}Downloading ${description}...${NC}"
|
||||
download_archive() {
|
||||
echo -e "${YELLOW}Downloading deployment scripts from repository...${NC}"
|
||||
if [ "$DOWNLOAD_NAME" = "curl" ]; then
|
||||
curl -fsSL "$url" -o "$dest"
|
||||
curl -fsSL "$REPO_ARCHIVE" | tar -xz --strip-components=1 -C "$TEMP_DIR" 2>/dev/null
|
||||
else
|
||||
wget -q "$url" -O "$dest"
|
||||
wget -qO- "$REPO_ARCHIVE" | tar -xz --strip-components=1 -C "$TEMP_DIR" 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ -s "$dest" ]; then
|
||||
chmod +x "$dest"
|
||||
echo -e "${GREEN} Success: ${description} downloaded to ${dest}${NC}"
|
||||
else
|
||||
rm -f "$dest"
|
||||
echo -e "${RED} Failed to download ${description}. Check network connectivity.${NC}"
|
||||
if [[ ! -d "$TEMP_DIR/common" ]]; then
|
||||
echo -e "${RED}${BOLD}ERROR:${NC} Failed to download or extract repository archive."
|
||||
echo -e "Check network connectivity to: ${REPO_ARCHIVE}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN} Success: archive downloaded and extracted.${NC}"
|
||||
}
|
||||
|
||||
download_deployment_type() {
|
||||
deploy_type() {
|
||||
local type="$1"
|
||||
local scripts
|
||||
|
||||
case "$type" in
|
||||
native) scripts=("${NATIVE_SCRIPTS[@]}") ;;
|
||||
docker) scripts=("${DOCKER_SCRIPTS[@]}") ;;
|
||||
*) err "Unknown deployment type: $type"; exit 1 ;;
|
||||
esac
|
||||
# Copy common library (always needed)
|
||||
cp "$TEMP_DIR/common/"* "deploy/" 2>/dev/null || true
|
||||
|
||||
mkdir -p "deploy"
|
||||
|
||||
for script in "${scripts[@]}"; do
|
||||
download_file "${GIT_BASE}${type}/${script}" "deploy/${script}" "deploy/${script}"
|
||||
done
|
||||
# Copy the selected deployment type's folder
|
||||
if [[ -d "$TEMP_DIR/$type" ]]; then
|
||||
cp "$TEMP_DIR/$type/"* "deploy/" 2>/dev/null || true
|
||||
chmod +x deploy/*.sh 2>/dev/null || true
|
||||
echo -e "${GREEN} Copied: common + ${type} scripts → deploy/${NC}"
|
||||
else
|
||||
echo -e "${RED} Folder '$type' not found in archive.${NC}"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Main
|
||||
|
|
@ -109,15 +101,28 @@ if [[ "${DEPLOYMENT_TYPE:-}" =~ ^(docker|native)$ ]]; then
|
|||
DEPLOY_TYPES+=("$DEPLOYMENT_TYPE")
|
||||
echo -e "${GREEN}Downloading only: $DEPLOYMENT_TYPE (from DEPLOYMENT_TYPE env var)${NC}"
|
||||
|
||||
# Priority 2: interactive terminal — ask the user
|
||||
elif [[ -t 0 ]]; then
|
||||
# Priority 2 + 3: try interactive prompt via terminal or /dev/tty
|
||||
else
|
||||
# Determine where we can read user input from
|
||||
INTERACTIVE=""
|
||||
if [[ -t 0 ]]; then
|
||||
INTERACTIVE="/dev/stdin"
|
||||
elif [[ -c /dev/tty ]]; then
|
||||
INTERACTIVE="/dev/tty"
|
||||
fi
|
||||
|
||||
if [[ -n "$INTERACTIVE" ]]; then
|
||||
echo -e "${CYAN}Which deployment type do you need?${NC}"
|
||||
echo -e " ${YELLOW}1${NC}) Docker only — Wallarm node as a container"
|
||||
echo -e " ${YELLOW}2${NC}) Native only — Wallarm node directly on this OS (no Docker)"
|
||||
echo -e " ${YELLOW}3${NC}) Both — docker + native scripts"
|
||||
echo
|
||||
while true; do
|
||||
read -r -p "$(echo -e "${YELLOW}Enter choice [1/2/3]: ${NC}")" choice
|
||||
read -r -p "$(echo -e "${YELLOW}Enter choice [1/2/3]: ${NC}")" choice <"$INTERACTIVE" 2>/dev/null || {
|
||||
echo -e "${YELLOW}Input unavailable, defaulting to BOTH.${NC}"
|
||||
DEPLOY_TYPES+=("docker" "native")
|
||||
break
|
||||
}
|
||||
case "$choice" in
|
||||
1) DEPLOY_TYPES+=("docker"); break ;;
|
||||
2) DEPLOY_TYPES+=("native"); break ;;
|
||||
|
|
@ -126,26 +131,28 @@ elif [[ -t 0 ]]; then
|
|||
esac
|
||||
done
|
||||
echo
|
||||
|
||||
# Priority 3: piped (non-interactive) — default to both for backward compatibility
|
||||
else
|
||||
else
|
||||
# Fully headless — default to both
|
||||
DEPLOY_TYPES+=("docker" "native")
|
||||
echo -e "${CYAN}Non-interactive run: downloading BOTH deployment types.${NC}"
|
||||
echo -e "${CYAN}No terminal available: downloading BOTH deployment types.${NC}"
|
||||
echo -e "${YELLOW}To download only one type, set DEPLOYMENT_TYPE=docker or DEPLOYMENT_TYPE=native${NC}"
|
||||
fi
|
||||
fi
|
||||
echo
|
||||
|
||||
# Download shared library (required by all deployment scripts)
|
||||
# Download archive once, then copy only the selected folders
|
||||
echo
|
||||
mkdir -p "deploy"
|
||||
download_file "${GIT_BASE}common/wallarm-lib.sh" "deploy/wallarm-lib.sh" "shared library (deploy/wallarm-lib.sh)"
|
||||
download_archive
|
||||
|
||||
# Download the selected deployment type(s)
|
||||
for type in "${DEPLOY_TYPES[@]}"; do
|
||||
download_deployment_type "$type"
|
||||
deploy_type "$type"
|
||||
done
|
||||
|
||||
# Clean exit trap removes temp dir
|
||||
|
||||
# Patch library sourcing path for flat deploy/ structure
|
||||
# (needed until the source scripts in the repo are updated)
|
||||
# (repo scripts still reference ../common/ — fix until they're updated)
|
||||
for script in deploy/*.sh; do
|
||||
[[ "$script" == "deploy/wallarm-lib.sh" ]] && continue
|
||||
sed -i 's|source "\$SCRIPT_DIR/\.\./common/wallarm-lib\.sh"|source "$SCRIPT_DIR/wallarm-lib.sh"|' "$script"
|
||||
|
|
|
|||
Loading…
Reference in a new issue