Files
ad-infr-control/setuper/setup.sh
2025-12-02 14:13:03 +03:00

338 lines
9.2 KiB
Bash

#!/bin/bash
# Setuper script for A/D Infrastructure
# Installs and configures: Packmate, moded_distructive_farm, Firegex
# Don't exit on error - we want to continue even if API registration fails
set +e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICES_DIR="${SERVICES_DIR:-$SCRIPT_DIR/../services}"
CONTROLLER_API="${CONTROLLER_API:-http://localhost:8001}"
SECRET_TOKEN="${SECRET_TOKEN:-change-me-in-production}"
echo "=== A/D Infrastructure Setuper ==="
echo "Services directory: $SERVICES_DIR"
echo ""
# Create services directory
mkdir -p "$SERVICES_DIR"
# Function to call controller API
call_api() {
local endpoint="$1"
local method="${2:-GET}"
local data="${3:-}"
if [ "$method" = "POST" ]; then
curl -s -X POST "$CONTROLLER_API$endpoint" \
-H "Authorization: Bearer $SECRET_TOKEN" \
-H "Content-Type: application/json" \
-d "$data"
else
curl -s "$CONTROLLER_API$endpoint" \
-H "Authorization: Bearer $SECRET_TOKEN"
fi
}
# Function to setup Packmate
setup_packmate() {
echo "=== Setting up Packmate ==="
local packmate_dir="$SERVICES_DIR/packmate"
if [ -d "$packmate_dir" ]; then
echo "Packmate directory already exists, updating..."
cd "$packmate_dir"
git pull
git submodule update --init --recursive
else
echo "Cloning Packmate with submodules..."
git clone --recursive https://gitlab.com/packmate/Packmate.git "$packmate_dir"
cd "$packmate_dir"
fi
# Create necessary directories
mkdir -p pcaps rsa_keys Packmate_stuff
# Create .env file
cat > .env <<'ENVEOF'
BUILD_TAG=latest
PACKMATE_DB_PASSWORD=${PACKMATE_DB_PASSWORD:-K604YnL3G1hp2RDkCZNjGpxbyNpNHTRb}
NET_INTERFACE=${NET_INTERFACE:-eth0}
PACKMATE_LOCAL_IP=${PACKMATE_LOCAL_IP:-10.60.0.1}
WEB_LOGIN=${WEB_LOGIN:-admin}
WEB_PASSWORD=${WEB_PASSWORD:-admin123}
ENVEOF
# Create PostgreSQL config
cat > Packmate_stuff/postgresql.conf <<'PGEOF'
port = 65001
max_connections = 100
shared_buffers = 128MB
PGEOF
# Create update script
cat > Packmate_stuff/update_db_config.sh <<'SHEOF'
#!/bin/bash
cp /tmp/postgresql.conf /var/lib/postgresql/data/postgresql.conf
SHEOF
chmod +x Packmate_stuff/update_db_config.sh
# Create docker-compose.yml
cat > docker-compose.yml <<'DCEOF'
version: '3.8'
services:
packmate:
environment:
DB_PASSWORD: ${PACKMATE_DB_PASSWORD:-K604YnL3G1hp2RDkCZNjGpxbyNpNHTRb}
INTERFACE: ${NET_INTERFACE:-}
LOCAL_IP: ${PACKMATE_LOCAL_IP}
MODE: LIVE
WEB_LOGIN: ${WEB_LOGIN:-admin}
WEB_PASSWORD: ${WEB_PASSWORD:-admin123}
OLD_STREAMS_CLEANUP_ENABLED: true
OLD_STREAMS_CLEANUP_INTERVAL: 5
OLD_STREAMS_CLEANUP_THRESHOLD: 240
env_file:
- .env
container_name: packmate-app
network_mode: "host"
image: registry.gitlab.com/packmate/packmate:${BUILD_TAG:-latest}
volumes:
- "./pcaps/:/app/pcaps/:ro"
- "./rsa_keys/:/app/rsa_keys/:ro"
depends_on:
db:
condition: service_healthy
db:
container_name: packmate-db
environment:
POSTGRES_USER: packmate
POSTGRES_PASSWORD: ${PACKMATE_DB_PASSWORD:-K604YnL3G1hp2RDkCZNjGpxbyNpNHTRb}
POSTGRES_DB: packmate
network_mode: "host"
image: postgres:15.2
volumes:
- "./Packmate_stuff/postgresql.conf:/tmp/postgresql.conf:ro"
- "./Packmate_stuff/update_db_config.sh:/docker-entrypoint-initdb.d/_update_db_config.sh:ro"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U packmate -p 65001" ]
interval: 2s
timeout: 5s
retries: 15
DCEOF
echo "Packmate setup complete!"
# Register with controller
echo "Registering Packmate with controller..."
if call_api "/services" "POST" "{\"name\": \"packmate\", \"path\": \"$packmate_dir\", \"git_url\": \"https://gitlab.com/packmate/Packmate.git\"}" 2>/dev/null; then
echo "✓ Packmate registered with controller"
else
echo "⚠ Warning: Could not register with controller (is it running?)"
echo " You can register manually later via the web dashboard"
fi
cd "$SCRIPT_DIR"
}
# Function to setup moded_distructive_farm
setup_farm() {
echo ""
echo "=== Setting up moded_distructive_farm ==="
local farm_dir="$SERVICES_DIR/moded_distructive_farm"
if [ -d "$farm_dir" ]; then
echo "Farm directory already exists, updating..."
cd "$farm_dir"
git pull
else
echo "Cloning moded_distructive_farm..."
git clone https://github.com/ilyastar9999/moded_distructive_farm.git "$farm_dir"
cd "$farm_dir"
fi
# Create .env file
cat > .env <<'ENVEOF'
# Database configuration
DB_PORT=5432
DB_HOST=postgres
DB_USER=farm
DB_PASS=${FARM_DB_PASS:-farmpassword123}
DB_NAME=farm
# Scoreboard configuration
BOARD_URL=${BOARD_URL:-http://10.60.0.1}
TEAM_TOKEN=${TEAM_TOKEN:-your-team-token}
# Web interface
WEB_PASSWORD=${FARM_WEB_PASSWORD:-farmadmin}
# Game configuration
NUM_TEAMS=${NUM_TEAMS:-10}
IP_TEAM_BASE=${IP_TEAM_BASE:-10.60.}
# API Token
API_TOKEN=${FARM_API_TOKEN:-farm-api-token-123}
ENVEOF
# Create docker-compose.yml
cat > docker-compose.yml <<'DCEOF'
version: '3.8'
services:
farm:
image: ghcr.io/ilyastar9999/moded_distructive_farm:latest
depends_on:
postgres:
condition: service_healthy
environment:
- DB_PORT=${DB_PORT}
- DB_HOST=${DB_HOST}
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- DB_NAME=${DB_NAME}
- BOARD_URL=${BOARD_URL}
- TEAM_TOKEN=${TEAM_TOKEN}
- WEB_PASSWORD=${WEB_PASSWORD}
- NUM_TEAMS=${NUM_TEAMS}
- IP_TEAM_BASE=${IP_TEAM_BASE}
- API_TOKEN=${API_TOKEN}
env_file:
- .env
container_name: farm-app
restart: always
ports:
- "3333:8000"
postgres:
image: postgres:18
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
- POSTGRES_DB=${DB_NAME}
healthcheck:
test: pg_isready -U ${DB_USER} -d ${DB_NAME}
interval: 10s
timeout: 3s
retries: 3
volumes:
- farm-db:/var/lib/postgresql/data
volumes:
farm-db:
DCEOF
echo "moded_distructive_farm setup complete!"
# Register with controller
echo "Registering farm with controller..."
if call_api "/services" "POST" "{\"name\": \"farm\", \"path\": \"$farm_dir\", \"git_url\": \"https://github.com/ilyastar9999/moded_distructive_farm.git\"}" 2>/dev/null; then
echo "✓ Farm registered with controller"
else
echo "⚠ Warning: Could not register with controller (is it running?)"
echo " You can register manually later via the web dashboard"
fi
cd "$SCRIPT_DIR"
}
# Function to setup Firegex
setup_firegex() {
echo ""
echo "=== Setting up Firegex ==="
local firegex_dir="$SERVICES_DIR/firegex"
if [ -d "$firegex_dir" ]; then
echo "Firegex directory already exists, updating..."
cd "$firegex_dir"
git pull
else
echo "Cloning Firegex..."
git clone https://github.com/Pwnzer0tt1/firegex.git "$firegex_dir"
cd "$firegex_dir"
fi
# Create .env file
cat > .env <<'ENVEOF'
# Firegex configuration
TEAM_TOKEN=${TEAM_TOKEN:-your-team-token}
SCOREBOARD_URL=${BOARD_URL:-http://10.60.0.1}
FIREGEX_PORT=${FIREGEX_PORT:-5000}
ENVEOF
# Create docker-compose.yml if not exists
if [ ! -f "docker-compose.yml" ]; then
cat > docker-compose.yml <<'DCEOF'
version: '3.8'
services:
firegex:
build: .
env_file:
- .env
environment:
- TEAM_TOKEN=${TEAM_TOKEN}
- SCOREBOARD_URL=${SCOREBOARD_URL}
container_name: firegex-app
restart: always
ports:
- "${FIREGEX_PORT:-5000}:5000"
DCEOF
fi
echo "Firegex setup complete!"
# Register with controller
echo "Registering Firegex with controller..."
if call_api "/services" "POST" "{\"name\": \"firegex\", \"path\": \"$firegex_dir\", \"git_url\": \"https://github.com/Pwnzer0tt1/firegex.git\"}" 2>/dev/null; then
echo "✓ Firegex registered with controller"
else
echo "⚠ Warning: Could not register with controller (is it running?)"
echo " You can register manually later via the web dashboard"
fi
cd "$SCRIPT_DIR"
}
# Main setup flow
main() {
echo "Starting setup process..."
echo ""
# Read configuration
read -p "Setup Packmate? (y/n): " setup_pm
read -p "Setup moded_distructive_farm? (y/n): " setup_fm
read -p "Setup Firegex? (y/n): " setup_fg
echo ""
if [ "$setup_pm" = "y" ]; then
setup_packmate
fi
if [ "$setup_fm" = "y" ]; then
setup_farm
fi
if [ "$setup_fg" = "y" ]; then
setup_firegex
fi
echo ""
echo "=== Setup Complete! ==="
echo "Services have been configured in: $SERVICES_DIR"
echo ""
echo "Next steps:"
echo " 1. Start the infrastructure: docker-compose up -d"
echo " 2. Access web dashboard: http://localhost:8000"
echo " 3. Register services via the dashboard if auto-registration failed"
echo ""
}
# Run main if executed directly
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
main
fi