#!/bin/bash #============================================================================ # FOXHUNT HFT TRADING SYSTEM - PRODUCTION DEPLOYMENT SCRIPT #============================================================================ # Complete production deployment with infrastructure, services, and monitoring # # Usage: # ./deploy.sh [OPTIONS] # # Options: # --infrastructure-only Deploy only infrastructure services # --monitoring-only Deploy only monitoring stack # --services-only Deploy only application services # --skip-build Skip Docker image builds # --validate Validate configuration before deployment # --help Show this help message #============================================================================ set -euo pipefail # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_NAME="foxhunt" DOCKER_COMPOSE_FILE="docker-compose.production.yml" ENV_FILE=".env.production" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Help function show_help() { cat << EOF Foxhunt HFT Trading System - Production Deployment Usage: $0 [OPTIONS] OPTIONS: --infrastructure-only Deploy only infrastructure services (Vault, PostgreSQL, Redis, InfluxDB) --monitoring-only Deploy only monitoring stack (Prometheus, Grafana, AlertManager) --services-only Deploy only application services (Trading, ML, Backtesting, TLI) --skip-build Skip Docker image builds --validate Validate configuration before deployment --help Show this help message EXAMPLES: $0 # Full deployment $0 --infrastructure-only # Deploy only databases and Vault $0 --services-only --skip-build # Deploy services without rebuilding images $0 --validate # Validate configuration only ENVIRONMENT: Copy .env.production to .env.production.local and customize for your environment. EOF } # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." # Check Docker if ! command -v docker &> /dev/null; then log_error "Docker is not installed" exit 1 fi # Check Docker Compose if ! command -v docker-compose &> /dev/null; then log_error "Docker Compose is not installed" exit 1 fi # Check environment file if [[ ! -f "$ENV_FILE" ]]; then log_warning "Environment file $ENV_FILE not found. Using defaults." log_info "Copy $ENV_FILE to $ENV_FILE.local and customize for production" fi # Check if running as root if [[ $EUID -eq 0 ]]; then log_warning "Running as root. Consider using a dedicated user for production." fi log_success "Prerequisites check completed" } # Validate Docker Compose configuration validate_config() { log_info "Validating Docker Compose configuration..." if docker-compose -f "$DOCKER_COMPOSE_FILE" config -q; then log_success "Docker Compose configuration is valid" else log_error "Docker Compose configuration validation failed" exit 1 fi } # Create necessary directories create_directories() { log_info "Creating necessary directories..." local dirs=( "/opt/foxhunt/config" "/opt/foxhunt/data" "/opt/foxhunt/models" "/opt/foxhunt/backtests" "/opt/foxhunt/checkpoints" "/opt/foxhunt/vault/data" "/opt/foxhunt/vault/logs" "/opt/foxhunt/postgres/data" "/opt/foxhunt/redis/data" "/opt/foxhunt/influxdb/data" "/opt/foxhunt/influxdb/config" "/opt/foxhunt/monitoring/prometheus" "/opt/foxhunt/monitoring/grafana" "/opt/foxhunt/monitoring/alertmanager" "/opt/foxhunt/monitoring/loki" "/opt/foxhunt/monitoring/tempo" "/var/log/foxhunt" ) for dir in "${dirs[@]}"; do sudo mkdir -p "$dir" sudo chown -R $(id -u):$(id -g) "$dir" 2>/dev/null || true log_info "Created directory: $dir" done log_success "Directory creation completed" } # Deploy infrastructure services deploy_infrastructure() { log_info "Deploying infrastructure services..." docker-compose -f docker-compose.infrastructure.yml up -d \ vault \ postgresql \ redis \ influxdb log_info "Waiting for infrastructure services to become healthy..." sleep 30 # Wait for services to be healthy local max_attempts=60 local attempt=0 while [[ $attempt -lt $max_attempts ]]; do if docker-compose -f docker-compose.infrastructure.yml ps | grep -q "healthy"; then log_success "Infrastructure services are healthy" return 0 fi ((attempt++)) log_info "Waiting for services to be healthy... ($attempt/$max_attempts)" sleep 5 done log_error "Infrastructure services failed to become healthy" exit 1 } # Deploy monitoring stack deploy_monitoring() { log_info "Deploying monitoring stack..." docker-compose -f docker-compose.monitoring.yml up -d log_info "Waiting for monitoring services to start..." sleep 20 log_success "Monitoring stack deployed" } # Build application images build_images() { log_info "Building Docker images..." # Build trading service docker build -t foxhunt/trading-service:latest -f services/trading_service/Dockerfile . # Build ML training service docker build -t foxhunt/ml-training:latest -f ml/Dockerfile . # Build backtesting service docker build -t foxhunt/backtesting-service:latest -f services/backtesting_service/Dockerfile . # Build TLI docker build -t foxhunt/tli:latest -f tli/Dockerfile . log_success "Docker images built successfully" } # Deploy application services deploy_services() { log_info "Deploying application services..." docker-compose -f "$DOCKER_COMPOSE_FILE" up -d \ trading-service \ ml-training-service \ backtesting-service \ tli log_info "Waiting for application services to start..." sleep 30 log_success "Application services deployed" } # Deploy full stack deploy_full() { log_info "Deploying full Foxhunt HFT Trading System..." docker-compose -f "$DOCKER_COMPOSE_FILE" up -d log_info "Waiting for all services to start..." sleep 60 log_success "Full deployment completed" } # Health check health_check() { log_info "Performing health check..." local services=("trading-service" "ml-training-service" "backtesting-service" "tli") local failed_services=() for service in "${services[@]}"; do if docker-compose -f "$DOCKER_COMPOSE_FILE" ps "$service" | grep -q "Up (healthy)"; then log_success "$service is healthy" else log_warning "$service is not healthy" failed_services+=("$service") fi done if [[ ${#failed_services[@]} -eq 0 ]]; then log_success "All services are healthy" else log_warning "Some services are not healthy: ${failed_services[*]}" log_info "Check service logs: docker-compose -f $DOCKER_COMPOSE_FILE logs " fi } # Display service URLs show_urls() { cat << EOF ${GREEN}============================================================================= FOXHUNT HFT TRADING SYSTEM - DEPLOYMENT COMPLETE =============================================================================${NC} ${BLUE}Service URLs:${NC} • Trading Service: http://localhost:8080 • TLI Interface: http://localhost:8081 • ML Training: http://localhost:8082 • Backtesting: http://localhost:8083 • Grafana: http://localhost:3000 (admin/admin) • Prometheus: http://localhost:9090 • AlertManager: http://localhost:9093 • Vault: http://localhost:8200 • PgAdmin: http://localhost:5050 • Redis Commander: http://localhost:8081 ${BLUE}Database Connections:${NC} • PostgreSQL: localhost:5432 (foxhunt/password from .env) • Redis: localhost:6379 (password from .env) • InfluxDB: localhost:8086 (credentials from .env) ${BLUE}Management Commands:${NC} • View logs: docker-compose -f $DOCKER_COMPOSE_FILE logs -f • Stop services: docker-compose -f $DOCKER_COMPOSE_FILE down • Restart service: docker-compose -f $DOCKER_COMPOSE_FILE restart ${YELLOW}Next Steps:${NC} 1. Configure your broker API credentials in Vault 2. Set up Grafana dashboards 3. Configure AlertManager notifications 4. Run initial system validation ${GREEN}Deployment completed successfully!${NC} EOF } # Main deployment logic main() { local infrastructure_only=false local monitoring_only=false local services_only=false local skip_build=false local validate_only=false # Parse arguments while [[ $# -gt 0 ]]; do case $1 in --infrastructure-only) infrastructure_only=true shift ;; --monitoring-only) monitoring_only=true shift ;; --services-only) services_only=true shift ;; --skip-build) skip_build=true shift ;; --validate) validate_only=true shift ;; --help) show_help exit 0 ;; *) log_error "Unknown option: $1" show_help exit 1 ;; esac done log_info "Starting Foxhunt HFT Trading System deployment..." check_prerequisites create_directories if [[ "$validate_only" == true ]]; then validate_config log_success "Configuration validation completed" exit 0 fi validate_config if [[ "$skip_build" == false ]]; then build_images fi if [[ "$infrastructure_only" == true ]]; then deploy_infrastructure elif [[ "$monitoring_only" == true ]]; then deploy_monitoring elif [[ "$services_only" == true ]]; then deploy_services else deploy_full fi health_check show_urls } # Run main function main "$@"