Refactor supervisor to use environment variables and simplify binary

- Created scripts/generate_secret.sh to generate supervisor secrets
- Added .env.example with all configuration options
- Created scripts/environment.sh to load env vars from .env
- Updated scripts/run.sh to use env vars and pass as flags
- Simplified supervisor binary:
  - Removed bootstrap-admin-key and config file support
  - Made admin-secret required
  - Minimal output (only URLs)
  - Clean startup with no verbose logging
- Updated .gitignore for .env and log files
This commit is contained in:
Timur Gordon
2025-11-07 00:08:32 +01:00
parent 8a02fffcca
commit 609af6ec15
7 changed files with 179 additions and 158 deletions

View File

@@ -1,64 +1,134 @@
#!/bin/bash
set -e
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
PROJECT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
# Build first
"$SCRIPT_DIR/build.sh"
# Load environment variables
source "$SCRIPT_DIR/environment.sh"
# Configuration
REDIS_URL="${REDIS_URL:-redis://localhost:6379}"
# Spinner function
spinner() {
local pid=$1
local delay=0.1
local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
while ps -p $pid > /dev/null 2>&1; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
echo "Starting Hero Supervisor"
echo ""
# Build
printf "🔨 Building... "
if "$SCRIPT_DIR/build.sh" > /tmp/supervisor-run-build.log 2>&1 & spinner $!; wait $!; then
echo "✅"
else
echo "❌"
echo " Error: Build failed. Check /tmp/supervisor-run-build.log"
exit 1
fi
# Validate required environment variables
if [ -z "$ADMIN_SECRETS" ]; then
echo "❌ Error: ADMIN_SECRETS not set in .env"
echo " Generate a secret with: ./scripts/generate_secret.sh"
exit 1
fi
# Set defaults
REDIS_URL="${REDIS_URL:-redis://127.0.0.1:6379}"
PORT="${PORT:-3030}"
BIND_ADDRESS="${BIND_ADDRESS:-127.0.0.1}"
BOOTSTRAP_ADMIN_KEY="${BOOTSTRAP_ADMIN_KEY:-admin}"
ADMIN_UI_PORT="${ADMIN_UI_PORT:-8080}"
LOG_LEVEL="${LOG_LEVEL:-info}"
LOG_LEVEL="${LOG_LEVEL:-error}"
# Cleanup function
cleanup() {
echo "Shutting down..."
echo ""
printf "🛑 Stopping... "
kill $(jobs -p) 2>/dev/null || true
echo "✅"
exit 0
}
trap cleanup SIGINT SIGTERM
echo "Starting Hero Supervisor..."
# Start supervisor
printf "📡 Supervisor... "
cd "$PROJECT_DIR"
# Start supervisor in background
RUST_LOG="$LOG_LEVEL" RUST_LOG_STYLE=never \
target/release/supervisor \
--bootstrap-admin-key "$BOOTSTRAP_ADMIN_KEY" \
--redis-url "$REDIS_URL" \
--port "$PORT" \
--bind-address "$BIND_ADDRESS" &
# Build command with flags
SUPERVISOR_CMD="target/release/supervisor --redis-url $REDIS_URL --port $PORT --bind-address $BIND_ADDRESS"
SUPERVISOR_PID=$!
# Add admin secrets
IFS=',' read -ra SECRETS <<< "$ADMIN_SECRETS"
for secret in "${SECRETS[@]}"; do
SUPERVISOR_CMD="$SUPERVISOR_CMD --admin-secret $secret"
done
# Wait for supervisor to start
sleep 2
# Check if supervisor is running
if ! ps -p $SUPERVISOR_PID > /dev/null 2>&1; then
echo "Failed to start supervisor"
exit 1
# Add user secrets if provided
if [ ! -z "$USER_SECRETS" ]; then
IFS=',' read -ra SECRETS <<< "$USER_SECRETS"
for secret in "${SECRETS[@]}"; do
SUPERVISOR_CMD="$SUPERVISOR_CMD --user-secret $secret"
done
fi
# Add register secrets if provided
if [ ! -z "$REGISTER_SECRETS" ]; then
IFS=',' read -ra SECRETS <<< "$REGISTER_SECRETS"
for secret in "${SECRETS[@]}"; do
SUPERVISOR_CMD="$SUPERVISOR_CMD --register-secret $secret"
done
fi
# Add mycelium URL if provided
if [ ! -z "$MYCELIUM_URL" ]; then
SUPERVISOR_CMD="$SUPERVISOR_CMD --mycelium-url $MYCELIUM_URL"
fi
RUST_LOG="$LOG_LEVEL" RUST_LOG_STYLE=never $SUPERVISOR_CMD > /tmp/supervisor-run.log 2>&1 &
SUPERVISOR_PID=$!
sleep 2
if ! ps -p $SUPERVISOR_PID > /dev/null 2>&1; then
echo "❌"
echo " Error: Supervisor failed to start. Check /tmp/supervisor-run.log"
exit 1
fi
echo "✅"
# Start admin UI
echo "Starting Admin UI on port $ADMIN_UI_PORT..."
printf "🎨 Admin UI... "
cd "$PROJECT_DIR/ui"
trunk serve --port "$ADMIN_UI_PORT" &
ADMIN_UI_PID=$!
if ! command -v trunk &> /dev/null; then
echo "⚠️ (trunk not installed)"
else
trunk serve --port "$ADMIN_UI_PORT" > /tmp/supervisor-ui.log 2>&1 &
ADMIN_UI_PID=$!
sleep 1
if ps -p $ADMIN_UI_PID > /dev/null 2>&1; then
echo "✅"
else
echo "❌"
fi
fi
echo ""
echo "✅ Hero Supervisor system started"
echo " 📡 Supervisor API: http://$BIND_ADDRESS:$PORT"
echo " 🎨 Admin UI: http://127.0.0.1:$ADMIN_UI_PORT"
echo "📡 http://$BIND_ADDRESS:$PORT"
if [ ! -z "$ADMIN_UI_PID" ] && ps -p $ADMIN_UI_PID > /dev/null 2>&1; then
echo "🎨 http://127.0.0.1:$ADMIN_UI_PORT"
fi
echo ""
echo "Press Ctrl+C to stop all services"
echo "Press Ctrl+C to stop"
# Wait for both processes
# Wait for processes
wait