#!/bin/bash # Discord Voice Translator v2 - Deployment Script # Microservices Architecture with GPU-accelerated transcription set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration COMPOSE_FILE="docker-compose.yml" ENV_FILE=".env" SERVICES=("recorder" "audio-processor" "whisper-service" "translator" "transcriber" "dashboard" "postgres" "redis") GPU_SERVICES=("whisper-service") # Function to print colored output print_status() { 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" } # Function to check if command exists command_exists() { command -v "$1" >/dev/null 2>&1 } # Function to check system requirements check_requirements() { print_status "Checking system requirements..." # Check Docker if ! command_exists docker; then print_error "Docker is not installed. Please install Docker first." exit 1 fi # Check Docker Compose if ! command_exists docker-compose && ! docker compose version >/dev/null 2>&1; then print_error "Docker Compose is not installed. Please install Docker Compose first." exit 1 fi # Check NVIDIA runtime for GPU services if docker info | grep -q "nvidia"; then print_success "NVIDIA Docker runtime detected" else print_warning "NVIDIA Docker runtime not detected. GPU acceleration may not work." fi # Check if .env file exists if [[ ! -f "$ENV_FILE" ]]; then print_warning ".env file not found. Using .env.example as template." if [[ -f ".env.example" ]]; then cp .env.example .env print_status "Created .env file from .env.example. Please configure it before running." else print_error ".env.example file not found. Please create environment configuration." exit 1 fi fi print_success "System requirements check completed" } # Function to create necessary directories create_directories() { print_status "Creating data directories..." directories=( "data/audio/raw" "data/audio/processed" "data/database" "data/redis" "data/logs" "data/models" ) for dir in "${directories[@]}"; do if [[ ! -d "$dir" ]]; then mkdir -p "$dir" print_status "Created directory: $dir" fi done # Set proper permissions chmod -R 755 data/ print_success "Data directories created" } # Function to pull required Docker images pull_images() { print_status "Pulling Docker images..." # Pull other base images docker pull postgres:15-alpine docker pull redis:7-alpine docker pull nginx:alpine # Pull optional admin images docker pull dpage/pgadmin4:latest docker pull rediscommander/redis-commander:latest print_success "Docker images pulled" } # Function to build services build_services() { print_status "Building microservices..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi # Build all services $COMPOSE_CMD build --no-cache print_success "Services built successfully" } # Function to start services start_services() { print_status "Starting services..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi # Start core services first print_status "Starting data services (postgres, redis)..." $COMPOSE_CMD up -d postgres redis # Wait for database to be ready print_status "Waiting for database to be ready..." sleep 10 # Start processing services print_status "Starting processing services..." $COMPOSE_CMD up -d audio-processor whisper-service translator # Wait for processing services to be ready print_status "Waiting for processing services to be ready..." sleep 5 # Start recorder and transcriber print_status "Starting Discord bot services..." $COMPOSE_CMD up -d recorder transcriber # Start dashboard print_status "Starting dashboard..." $COMPOSE_CMD up -d dashboard print_success "All services started successfully" } # Function to show service status show_status() { print_status "Service Status:" echo "====================" # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi $COMPOSE_CMD ps echo "" print_status "Service URLs:" echo "====================" echo "Dashboard: http://localhost:3010" echo "Whisper Service: http://localhost:8001" echo "Translator Service: http://localhost:8002" echo "Database: localhost:5434" echo "Redis: localhost:6379" echo "" print_status "Optional Admin Interfaces (use --profile admin):" echo "PGAdmin: http://localhost:8085" echo "Redis Commander: http://localhost:8086" } # Function to display help show_help() { echo "Discord Voice Translator v2 - Deployment Script" echo "Usage: $0 [OPTION]" echo "" echo "Options:" echo " start Start all services (default)" echo " stop Stop all services" echo " restart Restart all services" echo " build Build all services" echo " pull Pull Docker images" echo " status Show service status" echo " logs Show logs for all services" echo " logs [service] Show logs for specific service" echo " clean Stop and remove all containers and volumes" echo " admin Start with admin interfaces (PGAdmin, Redis Commander)" echo " proxy Start with nginx proxy" echo " help Show this help message" echo "" echo "Examples:" echo " $0 # Start all services" echo " $0 start # Start all services" echo " $0 admin # Start with admin interfaces" echo " $0 logs whisper-service # Show logs for whisper service" echo " $0 clean # Clean up everything" } # Function to show logs show_logs() { # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi if [[ -n "$1" ]]; then print_status "Showing logs for service: $1" $COMPOSE_CMD logs -f "$1" else print_status "Showing logs for all services" $COMPOSE_CMD logs -f fi } # Function to stop services stop_services() { print_status "Stopping all services..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi $COMPOSE_CMD down print_success "All services stopped" } # Function to clean up clean_up() { print_warning "This will remove all containers, networks, and volumes. Are you sure? (y/N)" read -r response if [[ "$response" =~ ^[Yy]$ ]]; then print_status "Cleaning up containers, networks, and volumes..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi $COMPOSE_CMD down -v --remove-orphans # Remove all related images docker image prune -f print_success "Cleanup completed" else print_status "Cleanup cancelled" fi } # Function to restart services restart_services() { print_status "Restarting all services..." stop_services sleep 2 start_services } # Main script logic main() { # Parse command line arguments case "${1:-start}" in "start") check_requirements create_directories pull_images build_services start_services show_status ;; "stop") stop_services ;; "restart") restart_services show_status ;; "build") check_requirements build_services ;; "pull") pull_images ;; "status") show_status ;; "logs") show_logs "$2" ;; "clean") clean_up ;; "admin") check_requirements create_directories pull_images build_services print_status "Starting with admin interfaces..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi $COMPOSE_CMD --profile admin up -d show_status ;; "proxy") check_requirements create_directories pull_images build_services print_status "Starting with nginx proxy..." # Use docker-compose or docker compose based on availability if command_exists docker-compose; then COMPOSE_CMD="docker-compose" else COMPOSE_CMD="docker compose" fi $COMPOSE_CMD --profile proxy up -d show_status ;; "help"|"-h"|"--help") show_help ;; *) print_error "Unknown command: $1" show_help exit 1 ;; esac } # Run main function main "$@"