#!/bin/bash # Setup script for Mana Image Generation as a launchd service on Mac Mini # Run this on the Mac Mini server to install and start the image generation service set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" SERVICE_DIR="$REPO_DIR/services/mana-image-gen" PLIST_NAME="com.manacore.image-gen" PLIST_PATH="$HOME/Library/LaunchAgents/$PLIST_NAME.plist" # flux2.c paths (in home directory, no sudo required) FLUX_BINARY="$HOME/flux2/flux" FLUX_MODEL_DIR="$HOME/flux2/model" echo "==========================================" echo "Mana Image Generation - Mac Mini Setup" echo "==========================================" echo "" echo "Service directory: $SERVICE_DIR" echo "Plist path: $PLIST_PATH" echo "Flux binary: $FLUX_BINARY" echo "Flux model: $FLUX_MODEL_DIR" echo "" # Verify service directory exists if [[ ! -d "$SERVICE_DIR" ]]; then echo "Error: Service directory not found: $SERVICE_DIR" exit 1 fi # Run main setup if venv doesn't exist or flux2.c not installed if [[ ! -d "$SERVICE_DIR/.venv" ]] || [[ ! -x "$FLUX_BINARY" ]]; then echo "Running setup (installs flux2.c + Python environment)..." echo "" "$SERVICE_DIR/setup.sh" echo "" fi # Verify flux2.c is available if [[ ! -x "$FLUX_BINARY" ]]; then echo "Error: flux2.c not found at $FLUX_BINARY" echo "Please run setup.sh first to install flux2.c" exit 1 fi if [[ ! -d "$FLUX_MODEL_DIR" ]]; then echo "Error: Model not found at $FLUX_MODEL_DIR" echo "Please download the FLUX.2 klein 4B model" exit 1 fi # Create LaunchAgents directory if needed mkdir -p "$HOME/Library/LaunchAgents" # Unload existing service if running if launchctl list | grep -q "$PLIST_NAME"; then echo "Stopping existing service..." launchctl unload "$PLIST_PATH" 2>/dev/null || true fi # Create plist file echo "Creating launchd plist..." cat > "$PLIST_PATH" << EOF Label $PLIST_NAME ProgramArguments $SERVICE_DIR/.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 3025 WorkingDirectory $SERVICE_DIR EnvironmentVariables PATH /opt/homebrew/bin:$SERVICE_DIR/.venv/bin:/usr/local/bin:/usr/bin:/bin PORT 3025 FLUX_BINARY $FLUX_BINARY FLUX_MODEL_DIR $FLUX_MODEL_DIR DEFAULT_STEPS 4 DEFAULT_WIDTH 1024 DEFAULT_HEIGHT 1024 GENERATION_TIMEOUT 120 CORS_ORIGINS https://mana.how,https://picture.mana.how,https://chat.mana.how,http://localhost:5173 RunAtLoad KeepAlive SuccessfulExit Crashed ThrottleInterval 10 StandardOutPath /tmp/manacore-image-gen.log StandardErrorPath /tmp/manacore-image-gen.error.log EOF echo "Plist created: $PLIST_PATH" # Load service echo "" echo "Loading service..." launchctl load "$PLIST_PATH" # Wait for startup echo "Waiting for service to start..." sleep 3 # Check if running if launchctl list | grep -q "$PLIST_NAME"; then echo "Service loaded successfully!" else echo "Warning: Service may not have loaded correctly." echo "Check logs: tail -f /tmp/manacore-image-gen.log" fi # Health check echo "" echo "Running health check..." sleep 2 if curl -s http://localhost:3025/health | grep -q "healthy\|degraded"; then echo "Health check passed!" echo "" curl -s http://localhost:3025/health | python3 -m json.tool else echo "Health check failed. Service may still be starting." echo "Try again in a few seconds: curl http://localhost:3025/health" fi echo "" echo "==========================================" echo "Setup Complete!" echo "==========================================" echo "" echo "Service management commands:" echo "" echo " # View logs" echo " tail -f /tmp/manacore-image-gen.log" echo "" echo " # Stop service" echo " launchctl unload $PLIST_PATH" echo "" echo " # Start service" echo " launchctl load $PLIST_PATH" echo "" echo " # Restart service" echo " launchctl unload $PLIST_PATH && launchctl load $PLIST_PATH" echo "" echo " # Check status" echo " launchctl list | grep $PLIST_NAME" echo "" echo "Test endpoints:" echo "" echo " # Health check" echo " curl http://localhost:3025/health" echo "" echo " # Model info" echo " curl http://localhost:3025/models" echo "" echo " # Generate image" echo " curl -X POST http://localhost:3025/generate \\" echo " -H 'Content-Type: application/json' \\" echo " -d '{\"prompt\": \"A cat in space\"}'" echo ""