#!/bin/bash # Telegram Marketing Agent System - Startup Script # This script helps with initial setup and deployment set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Functions print_success() { echo -e "${GREEN}✓ $1${NC}" } print_error() { echo -e "${RED}✗ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠ $1${NC}" } print_info() { echo -e "ℹ $1" } # Check prerequisites check_prerequisites() { echo "Checking prerequisites..." # Check Docker if command -v docker &> /dev/null; then print_success "Docker is installed" else print_error "Docker is not installed. Please install Docker first." exit 1 fi # Check Docker Compose if command -v docker-compose &> /dev/null; then print_success "Docker Compose is installed" else print_error "Docker Compose is not installed. Please install Docker Compose first." exit 1 fi # Check Node.js if command -v node &> /dev/null; then print_success "Node.js is installed" else print_warning "Node.js is not installed. It's required for local development." fi } # Setup environment setup_environment() { echo "" echo "Setting up environment..." if [ ! -f .env ]; then if [ -f .env.example ]; then cp .env.example .env print_success "Created .env file from .env.example" print_warning "Please edit .env file and add your API keys" # Prompt for API keys echo "" read -p "Would you like to configure API keys now? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then configure_api_keys fi else print_error ".env.example file not found" exit 1 fi else print_success ".env file already exists" fi } # Configure API keys configure_api_keys() { echo "" echo "Configuring API keys..." # Anthropic API Key read -p "Enter your Anthropic API key: " anthropic_key if [ ! -z "$anthropic_key" ]; then sed -i.bak "s/ANTHROPIC_API_KEY=.*/ANTHROPIC_API_KEY=$anthropic_key/" .env print_success "Anthropic API key configured" fi # OpenAI API Key read -p "Enter your OpenAI API key: " openai_key if [ ! -z "$openai_key" ]; then sed -i.bak "s/OPENAI_API_KEY=.*/OPENAI_API_KEY=$openai_key/" .env print_success "OpenAI API key configured" fi # Telegram API credentials read -p "Enter your Telegram API ID: " telegram_api_id if [ ! -z "$telegram_api_id" ]; then sed -i.bak "s/TELEGRAM_API_ID=.*/TELEGRAM_API_ID=$telegram_api_id/" .env print_success "Telegram API ID configured" fi read -p "Enter your Telegram API Hash: " telegram_api_hash if [ ! -z "$telegram_api_hash" ]; then sed -i.bak "s/TELEGRAM_API_HASH=.*/TELEGRAM_API_HASH=$telegram_api_hash/" .env print_success "Telegram API Hash configured" fi # Generate secure keys echo "" print_info "Generating secure keys..." jwt_secret=$(openssl rand -base64 32) sed -i.bak "s/JWT_SECRET=.*/JWT_SECRET=$jwt_secret/" .env print_success "JWT secret generated" encryption_key=$(openssl rand -hex 32) sed -i.bak "s/ENCRYPTION_KEY=.*/ENCRYPTION_KEY=$encryption_key/" .env print_success "Encryption key generated" # Clean up backup files rm -f .env.bak } # Build Docker images build_images() { echo "" echo "Building Docker images..." docker-compose build if [ $? -eq 0 ]; then print_success "Docker images built successfully" else print_error "Failed to build Docker images" exit 1 fi } # Start infrastructure services start_infrastructure() { echo "" echo "Starting infrastructure services..." docker-compose up -d postgres mongodb redis rabbitmq elasticsearch # Wait for services to be ready print_info "Waiting for services to be ready..." sleep 30 # Check service health docker-compose ps print_success "Infrastructure services started" } # Initialize databases initialize_databases() { echo "" echo "Initializing databases..." # MongoDB indexes print_info "Creating MongoDB indexes..." docker exec -it marketing_mongodb mongosh marketing_agent --eval ' db.tasks.createIndex({ taskId: 1 }, { unique: true }); db.campaigns.createIndex({ campaignId: 1 }, { unique: true }); db.sessions.createIndex({ sessionId: 1 }, { unique: true }); db.sessions.createIndex({ updatedAt: 1 }, { expireAfterSeconds: 2592000 }); db.messages.createIndex({ messageId: 1 }, { unique: true }); db.messages.createIndex({ chatId: 1, timestamp: -1 }); db.events.createIndex({ eventId: 1 }, { unique: true }); db.events.createIndex({ timestamp: -1 }); db.experiments.createIndex({ experimentId: 1 }, { unique: true }); db.variants.createIndex({ experimentId: 1 }); db.assignments.createIndex({ experimentId: 1, userId: 1 }); print("Indexes created successfully"); ' print_success "Database initialization completed" } # Start all services start_all_services() { echo "" echo "Starting all services..." docker-compose up -d # Wait for services to be ready print_info "Waiting for all services to be ready..." sleep 45 # Check service health docker-compose ps # Check API Gateway health if curl -s http://localhost:3000/health > /dev/null; then print_success "API Gateway is healthy" else print_warning "API Gateway health check failed" fi } # Create admin user create_admin_user() { echo "" read -p "Would you like to create an admin user? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then read -p "Enter admin username: " admin_username read -s -p "Enter admin password: " admin_password echo "" read -p "Enter admin email: " admin_email # Create admin user via API response=$(curl -s -X POST http://localhost:3000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d "{ \"username\": \"$admin_username\", \"password\": \"$admin_password\", \"email\": \"$admin_email\" }") if [[ $response == *"success"* ]]; then print_success "Admin user created successfully" else print_error "Failed to create admin user" echo "Response: $response" fi fi } # Show service URLs show_service_urls() { echo "" echo "===================================" echo "Service URLs:" echo "===================================" echo "API Gateway: http://localhost:3000" echo "API Documentation: http://localhost:3000/api-docs" echo "RabbitMQ Management: http://localhost:15672 (admin/admin)" echo "Grafana: http://localhost:3001 (admin/admin)" echo "Prometheus: http://localhost:9090" echo "===================================" } # Main menu show_menu() { echo "" echo "Telegram Marketing Agent System - Startup Script" echo "================================================" echo "1) Full setup (recommended for first time)" echo "2) Start all services" echo "3) Stop all services" echo "4) View logs" echo "5) Check service health" echo "6) Initialize databases only" echo "7) Create admin user" echo "8) Configure API keys" echo "9) Exit" echo "" read -p "Select an option: " choice case $choice in 1) check_prerequisites setup_environment build_images start_infrastructure initialize_databases start_all_services create_admin_user show_service_urls ;; 2) docker-compose up -d show_service_urls ;; 3) docker-compose down print_success "All services stopped" ;; 4) docker-compose logs -f ;; 5) docker-compose ps echo "" curl -s http://localhost:3000/health/services | jq '.' || echo "API Gateway not accessible" ;; 6) initialize_databases ;; 7) create_admin_user ;; 8) configure_api_keys ;; 9) exit 0 ;; *) print_error "Invalid option" show_menu ;; esac } # Run the script if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then echo "Usage: ./startup.sh [option]" echo "" echo "Options:" echo " --full Run full setup" echo " --start Start all services" echo " --stop Stop all services" echo " --logs View logs" echo " --health Check service health" echo "" exit 0 elif [ "$1" == "--full" ]; then check_prerequisites setup_environment build_images start_infrastructure initialize_databases start_all_services create_admin_user show_service_urls elif [ "$1" == "--start" ]; then docker-compose up -d show_service_urls elif [ "$1" == "--stop" ]; then docker-compose down print_success "All services stopped" elif [ "$1" == "--logs" ]; then docker-compose logs -f elif [ "$1" == "--health" ]; then docker-compose ps echo "" curl -s http://localhost:3000/health/services | jq '.' || echo "API Gateway not accessible" else show_menu fi