#!/bin/bash # New Setuper script for A/D Infrastructure # Downloads Packmate, moded_distructive_farm, Firegex OUTSIDE SERVICES_DIR, starts them, then starts all game services from SERVICES_DIR, and registers only Packmate and Firegex with controller. set +e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" ROOT_DIR="$SCRIPT_DIR/.." # Read .env for SERVICES_DIR, CONTROLLER_API, SECRET_TOKEN ENV_FILE="$ROOT_DIR/.env" if [ ! -f "$ENV_FILE" ]; then echo ".env file not found in $ROOT_DIR. Exiting." exit 1 fi SERVICES_DIR=$(grep '^SERVICES_DIR=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) CONTROLLER_API=$(grep '^CONTROLLER_API=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) SECRET_TOKEN=$(grep '^SECRET_TOKEN=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) # Defaults if [ -z "$CONTROLLER_API" ]; then CONTROLLER_API="http://localhost:8001" fi if [ -z "$SECRET_TOKEN" ]; then SECRET_TOKEN="change-me-in-production" fi echo "=== A/D Infrastructure Setuper (NEW) ===" echo "Game services directory: $SERVICES_DIR" echo "" # 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 and start Packmate setup_packmate() { echo "=== Setting up Packmate ===" local packmate_dir="$ROOT_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 mkdir -p pcaps rsa_keys Packmate_stuff # Read config from parent .env BOARD_URL=$(grep '^BOARD_URL=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) PACKMATE_LOCAL_IP=$(grep '^PACKMATE_LOCAL_IP=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) NET_INTERFACE=$(grep '^NET_INTERFACE=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) PACKMATE_WEB_LOGIN=$(grep '^PACKMATE_WEB_LOGIN=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) PACKMATE_WEB_PASSWORD=$(grep '^PACKMATE_WEB_PASSWORD=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) # Defaults if not set if [ -z "$PACKMATE_LOCAL_IP" ]; then PACKMATE_LOCAL_IP="10.60.1.2" fi if [ -z "$NET_INTERFACE" ]; then NET_INTERFACE="eth0" fi if [ -z "$PACKMATE_WEB_LOGIN" ]; then PACKMATE_WEB_LOGIN="admin" fi if [ -z "$PACKMATE_WEB_PASSWORD" ]; then PACKMATE_WEB_PASSWORD="admin123" fi # .env and config generation (minimal) cat > .env < Packmate_stuff/postgresql.conf <<'PGEOF' port = 65001 max_connections = 100 shared_buffers = 128MB PGEOF 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 # docker-compose.yml (minimal) cat > docker-compose.yml <<'DCEOF' services: packmate: env_file: - .env container_name: packmate-app network_mode: "host" image: registry.gitlab.com/packmate/packmate:latest volumes: - "./pcaps/:/app/pcaps/:ro" - "./rsa_keys/:/app/rsa_keys/:ro" depends_on: db: condition: service_healthy db: container_name: packmate-db env_file: - .env 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 "Starting Packmate containers..." docker compose up -d --no-build echo "Waiting for Packmate to be ready..." sleep 5 cd "$SCRIPT_DIR" } # Function to setup and start moded_distructive_farm setup_farm() { echo "=== Setting up moded_distructive_farm ===" local farm_dir="$ROOT_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 # Read config from parent .env BOARD_URL=$(grep '^BOARD_URL=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) TEAM_TOKEN=$(grep '^TEAM_TOKEN=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) NUM_TEAMS=$(grep '^NUM_TEAMS=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) IP_TEAM_BASE=$(grep '^IP_TEAM_BASE=' "$ENV_FILE" | cut -d'=' -f2- | tr -d '"' | xargs) cat > .env <' | sed 's/->//' | grep -Ev '^(3333|4444|65000|65001|5432)$' | tr '\n' ',' | sed 's/,$//') echo "Exposed ports for $service_name: $service_ports" # Register service with controller and send ports, except for Firegex, farm, and packmate if [ "$service_name" != "firegex" ] && [ "$service_name" != "farm" ] && [ "$service_name" != "packmate" ]; then echo "Registering $service_name with controller..." ports_json="[$service_ports]" ports_json=$(echo "$ports_json" | sed 's/,/,/g') call_api "/services" "POST" "{\"name\": \"$service_name\", \"path\": \"$service_rel_path\", \"ports\": $ports_json}" else echo "Skipping controller registration for $service_name." fi # Add service to Packmate if Packmate is running if [ -n "$service_ports" ]; then echo "Adding $service_name to Packmate..." # Try to add each exposed host port to Packmate echo "$service_ports" | tr ',' '\n' | while read port; do if [ -n "$port" ]; then # Primary attempt (with trailing slash) status=$(curl -s -k -o /dev/null -w "%{http_code}" -X POST "${PACKMATE_URL}/api/service/" \ -H "Accept: application/json" \ -H "Authorization: Basic $PACKMATE_AUTH" \ -H "Content-Type: application/json" \ ${PACKMATE_COOKIE_HEADER:+-H "$PACKMATE_COOKIE_HEADER"} \ -d "{\"name\": \"$service_name\", \"port\": $port}" ) if [ "$status" -ge 200 ] && [ "$status" -lt 300 ]; then echo "Packmate: registered $service_name port $port (status $status)" else # Fallback attempt without trailing slash status2=$(curl -s -k -o /dev/null -w "%{http_code}" -X POST "${PACKMATE_URL}/api/service" \ -H "Accept: application/json" \ -H "Authorization: Basic $PACKMATE_AUTH" \ -H "Content-Type: application/json" \ ${PACKMATE_COOKIE_HEADER:+-H "$PACKMATE_COOKIE_HEADER"} \ -d "{\"name\": \"$service_name\", \"port\": $port}" ) echo "Packmate: register $service_name port $port -> status $status (fallback $status2)" fi fi done fi service_count=$((service_count + 1)) fi done echo "Started and registered $service_count game service(s)" cd "$SCRIPT_DIR" } # Start infrastructure services (controller, web, telegram bot, scoreboard injector) start_infrastructure_services() { echo "Starting infrastructure services..." docker compose -f "$ROOT_DIR/docker-compose.yaml" up -d --build echo "Infrastructure services started." } # Main setup flow main() { echo "Starting setup process..." echo "" 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" != "n" ]; then setup_packmate fi if [ "$setup_fm" != "n" ]; then setup_farm fi if [ "$setup_fg" != "n" ]; then setup_firegex fi start_infrastructure_services start_game_services echo "" echo "=== Setup Complete! ===" echo "Game services have been started from: $SERVICES_DIR" echo "" echo "Next steps:" echo " 1. Access web dashboard: http://localhost:8000" echo " 2. Register services via the dashboard if auto-registration failed" echo "" } main