#!/bin/bash # Bots Infrastructure Deployment Script # This script deploys the complete bots infrastructure using Docker Compose set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration COMPOSE_FILE="docker-compose.yml" ENV_FILE=".env" LOG_DIR="logs" # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to check prerequisites check_prerequisites() { print_status "Checking prerequisites..." if ! command -v docker &> /dev/null; then print_error "Docker is not installed. Please install Docker first." exit 1 fi if ! command -v docker-compose &> /dev/null; then print_error "Docker Compose is not installed. Please install Docker Compose first." exit 1 fi if [ ! -f "$ENV_FILE" ]; then print_error "Environment file $ENV_FILE not found. Please create it from .env.example" exit 1 fi print_status "Prerequisites check passed" } # Function to create necessary directories create_directories() { print_status "Creating necessary directories..." mkdir -p "$LOG_DIR"/telegram-helper-bot mkdir -p "$LOG_DIR"/voice-bot mkdir -p infra/nginx/ssl print_status "Directories created" } # Function to load environment variables load_env() { print_status "Loading environment variables..." if [ -f "$ENV_FILE" ]; then export $(cat "$ENV_FILE" | grep -v '^#' | xargs) print_status "Environment variables loaded" else print_error "Environment file not found" exit 1 fi } # Function to validate environment variables validate_env() { print_status "Validating environment variables..." local required_vars=("BOT_TOKEN_1" "BOT_TOKEN_2" "DB_PASSWORD" "REDIS_PASSWORD") local missing_vars=() for var in "${required_vars[@]}"; do if [ -z "${!var}" ]; then missing_vars+=("$var") fi done if [ ${#missing_vars[@]} -ne 0 ]; then print_error "Missing required environment variables: ${missing_vars[*]}" exit 1 fi print_status "Environment variables validation passed" } # Function to stop existing services stop_services() { print_status "Stopping existing services..." if docker-compose -f "$COMPOSE_FILE" ps -q | grep -q .; then docker-compose -f "$COMPOSE_FILE" down print_status "Existing services stopped" else print_status "No existing services to stop" fi } # Function to deploy services deploy_services() { print_status "Deploying services..." docker-compose -f "$COMPOSE_FILE" up -d --build if [ $? -eq 0 ]; then print_status "Services deployed successfully" else print_error "Failed to deploy services" exit 1 fi } # Function to wait for services to be healthy wait_for_services() { print_status "Waiting for services to be healthy..." local max_attempts=30 local attempt=1 while [ $attempt -le $max_attempts ]; do local unhealthy_services=$(docker-compose -f "$COMPOSE_FILE" ps | grep -c "unhealthy\|starting" || true) if [ "$unhealthy_services" -eq 0 ]; then print_status "All services are healthy" break fi if [ $attempt -eq $max_attempts ]; then print_warning "Some services may not be fully healthy after $max_attempts attempts" break fi print_status "Waiting for services to be healthy... (attempt $attempt/$max_attempts)" sleep 10 ((attempt++)) done } # Function to show service status show_status() { print_status "Service status:" docker-compose -f "$COMPOSE_FILE" ps echo "" print_status "Service logs (last 10 lines):" docker-compose -f "$COMPOSE_FILE" logs --tail=10 } # Function to show access information show_access_info() { echo "" print_status "Access Information:" echo "Grafana Dashboard: http://localhost:3000 (admin/admin)" echo "Prometheus: http://localhost:9090" echo "PostgreSQL: localhost:5432" echo "Redis: localhost:6379" echo "" print_status "Check logs with: docker-compose logs -f [service_name]" } # Main deployment function main() { print_status "Starting bots infrastructure deployment..." check_prerequisites create_directories load_env validate_env stop_services deploy_services wait_for_services show_status show_access_info print_status "Deployment completed successfully!" } # Handle command line arguments case "${1:-}" in "stop") print_status "Stopping services..." docker-compose -f "$COMPOSE_FILE" down print_status "Services stopped" ;; "restart") print_status "Restarting services..." docker-compose -f "$COMPOSE_FILE" restart print_status "Services restarted" ;; "logs") docker-compose -f "$COMPOSE_FILE" logs -f "${2:-}" ;; "status") show_status ;; "help"|"-h"|"--help") echo "Usage: $0 [command]" echo "Commands:" echo " (no args) - Deploy the infrastructure" echo " stop - Stop all services" echo " restart - Restart all services" echo " logs - Show logs (optionally specify service name)" echo " status - Show service status" echo " help - Show this help message" ;; *) main ;; esac