#!/bin/bash # Project Management Script for ML Experiment Manager # Provides unified interface for managing all components set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' print_info() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_header() { echo -e "${PURPLE}$1${NC}" } print_app() { echo -e "${CYAN}$1${NC}" } show_status() { print_header "ML Experiment Manager Status" echo "==================================" echo "" # Check Go apps print_app "Go Applications:" local go_apps=("api-server" "worker" "tui" "data_manager" "user_manager") for app in "${go_apps[@]}"; do if [[ -f "bin/$app" ]]; then echo " ✅ $app: Built" else echo " ❌ $app: Not built" fi done echo "" # Check Zig CLI print_app "Zig CLI:" if [[ -f "cli/zig-out/bin/ml" ]]; then echo " ✅ CLI: Built" else echo " ❌ CLI: Not built" fi echo "" # Check services print_app "Services:" if command -v redis-cli &> /dev/null; then if redis-cli ping | grep -q "PONG"; then echo " ✅ Redis: Running" else echo " ⚠️ Redis: Not running" fi else echo " ❌ Redis: Not installed" fi if command -v docker &> /dev/null; then echo " ✅ Docker: Available" else echo " ❌ Docker: Not installed" fi echo "" # Check configuration print_app "Configuration:" if [[ -f "configs/config-local.yaml" ]]; then echo " ✅ Security config: Found" else echo " ⚠️ Security config: Not found" fi if [[ -f ".env.dev" ]]; then echo " ✅ Environment: Found" else echo " ⚠️ Environment: Not found" fi if [[ -f "ssl/cert.pem" && -f "ssl/key.pem" ]]; then echo " ✅ SSL certificates: Found" else echo " ⚠️ SSL certificates: Not found" fi echo "" } build_all() { print_header "Building All Components" echo "=============================" echo "" print_info "Building Go applications..." make build if command -v zig &> /dev/null; then print_info "Building Zig CLI..." make cli-build else print_warning "Zig not found, skipping CLI build" fi print_success "Build completed!" } test_all() { print_header "Running All Tests" echo "====================" echo "" print_info "Running main test suite..." make test print_info "Running comprehensive tests..." make test-all print_success "All tests completed!" } start_services() { print_header "Starting Services" echo "===================" echo "" # Start Redis if available if command -v redis-server &> /dev/null; then if ! pgrep -f "redis-server" > /dev/null; then print_info "Starting Redis..." redis-server --daemonize yes --port 6379 print_success "Redis started" else print_info "Redis already running" fi fi # Start API server if built if [[ -f "bin/api-server" ]]; then print_info "Starting API server..." if [[ -f "configs/config-local.yaml" ]]; then ./bin/api-server --config configs/config-local.yaml & else print_warning "No config found, using defaults" ./bin/api-server & fi print_success "API server started (PID: $!)" else print_error "API server not built. Run 'make build' first." fi print_success "Services started!" } check_health() { print_header "API Health Check" echo "==================" echo "" print_info "Checking if API port is open..." # First check if port 9101 is open if ! nc -z localhost 9101 2>/dev/null; then print_error "API port 9101 not open - is it running?" print_info "Start with: ./tools/manage.sh start" return 1 fi print_info "Port 9101 is open, checking API health endpoint..." # Try the health endpoint response=$(curl -k -s --max-time 3 -H 'X-API-Key: password' -H 'X-Forwarded-For: 127.0.0.1' https://localhost:9101/health 2>/dev/null) if [[ "$response" == "OK" ]]; then print_success "API is healthy: $response" elif [[ "$response" == *"IP not whitelisted"* ]]; then print_warning "API running but IP not whitelisted (expected behavior)" print_info "Try: curl -k -H 'X-API-Key: password' -H 'X-Forwarded-For: 127.0.0.1' https://localhost:9101/health" else print_error "Unexpected response: $response" fi } stop_services() { print_header "Stopping Services" echo "==================" echo "" # Stop API server if pgrep -f "api-server" > /dev/null; then print_info "Stopping API server..." pkill -f "api-server" print_success "API server stopped" fi # Stop Redis if command -v redis-cli &> /dev/null; then print_info "Stopping Redis..." redis-cli shutdown 2>/dev/null || true print_success "Redis stopped" fi print_success "All services stopped!" } run_security() { print_header "Security Management" echo "====================" echo "" case "${1:-check}" in "check") print_info "Running security checks..." make security-check ;; "monitor") print_info "Starting security monitoring..." make security-monitor ;; "deploy") print_info "Deploying with security..." make security-deploy ;; "audit") print_info "Running security audit..." make security-audit ;; *) echo "Usage: $0 security {check|monitor|deploy|audit}" exit 1 ;; esac } run_development() { print_header "Development Environment" echo "=========================" echo "" case "${1:-setup}" in "setup") print_info "Setting up development environment..." ./scripts/auto_setup.sh ;; "quick") print_info "Running quick start..." ./scripts/quick_start.sh ;; "deps") print_info "Installing dependencies..." make install-deps ;; *) echo "Usage: $0 dev {setup|quick|deps}" exit 1 ;; esac } show_logs() { print_header "Application Logs" echo "==================" echo "" # Show application logs if [[ -f "logs/fetch_ml.log" ]]; then print_app "Application Log:" tail -20 logs/fetch_ml.log echo "" fi if [[ -f "logs/audit.log" ]]; then print_app "Security Log:" tail -20 logs/audit.log echo "" fi # Show Docker logs if running if command -v docker &> /dev/null; then local containers=$(docker ps --format "table {{.Names}}" | grep "ml-experiment" || true) if [[ -n "$containers" ]]; then print_app "Docker Logs:" docker logs --tail=20 $(echo "$containers" | tail -1) 2>/dev/null || true fi fi } cleanup() { print_header "Cleanup Project" echo "================" echo "" print_info "Cleaning project artifacts..." make clean-all print_info "Stopping services..." stop_services print_success "Cleanup completed!" } show_help() { print_header "Project Management Script" echo "===========================" echo "" echo "Usage: ./tools/manage.sh {status|build|test|start|stop|health|security|dev|logs|cleanup|help}" echo "" echo "Commands:" echo " status - Show project status" echo " build - Build all components" echo " test - Run all tests" echo " start - Start all services" echo " stop - Stop all services" echo " health - Check API health endpoint" echo " security - Security management (check|monitor|deploy|audit)" echo " dev - Development environment (setup|quick|deps)" echo " logs - Show application logs" echo " cleanup - Clean project artifacts and stop services" echo " help - Show this help" echo "" echo "Examples:" echo " $0 status # Show current status" echo " $0 health # Check API health" echo " $0 build && $0 test # Build and test everything" echo " $0 start # Start all services" echo " $0 security monitor # Start security monitoring" echo " $0 dev setup # Setup development environment" echo "" echo "Quick Start:" echo " $0 dev setup && $0 start && $0 status" } # Main function main() { case "${1:-help}" in "status") show_status ;; "build") build_all ;; "test") test_all ;; "start") start_services ;; "stop") stop_services ;; "health") check_health ;; "security") run_security "${2:-check}" ;; "dev") run_development "${2:-setup}" ;; "logs") show_logs ;; "cleanup") cleanup ;; "help"|"-h"|"--help") show_help ;; *) print_error "Unknown command: $1" echo "Use '$0 help' for usage information" exit 1 ;; esac } # Run main function main "$@"