#!/bin/bash # 🚀 YouTube Metadata Extractor - Deployment Script # This script deploys the project on a remote Ubuntu server echo "🚀 Starting deployment of YouTube Metadata Extractor..." # Configuration PROJECT_DIR="$HOME/YouTube" REPO_URL="https://huggingface.co/spaces/dzianisBY/YouTube_Creator_MetaData" PYTHON_ENV="$PROJECT_DIR/.venv" # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } # Check if running on Ubuntu/Debian if ! command -v apt &> /dev/null; then print_error "This script is designed for Ubuntu/Debian systems" exit 1 fi # Update system packages print_status "Updating system packages..." sudo apt update && sudo apt upgrade -y # Install required system packages print_status "Installing system dependencies..." sudo apt install -y python3 python3-pip python3-venv git curl nginx # Create project directory print_status "Setting up project directory..." mkdir -p "$PROJECT_DIR" cd "$PROJECT_DIR" # Clone or update repository if [ -d ".git" ]; then print_status "Updating existing repository..." git pull origin main else print_status "Cloning repository..." git clone "$REPO_URL" . fi # Create virtual environment print_status "Setting up Python virtual environment..." python3 -m venv "$PYTHON_ENV" source "$PYTHON_ENV/bin/activate" # Install Python dependencies print_status "Installing Python dependencies..." pip install --upgrade pip pip install -r requirements.txt pip install -r telegram_requirements.txt # Create .env file template if it doesn't exist if [ ! -f ".env" ]; then print_warning "Creating .env template file..." cat > .env << EOF # YouTube API Configuration YOUTUBE_API_KEY=your_youtube_api_key_here # Telegram Bot Configuration TELEGRAM_TOKEN=your_telegram_bot_token_here # MCP Server Configuration MCP_BASE_URL=https://youtube-bot.tuttech.net/api/mcp # Gemini AI Configuration GEMINI_API_KEY=your_gemini_api_key_here # Server Configuration HOST=0.0.0.0 PORT=8080 EOF print_warning "Please edit .env file with your actual API keys!" print_warning "File location: $PROJECT_DIR/.env" fi # Create systemd service files print_status "Creating systemd service files..." # API Server service sudo tee /etc/systemd/system/youtube-api.service > /dev/null << EOF [Unit] Description=YouTube MCP API Server After=network.target [Service] Type=simple User=$USER WorkingDirectory=$PROJECT_DIR Environment=PATH=$PYTHON_ENV/bin ExecStart=$PYTHON_ENV/bin/python main.py Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF # Telegram Bot service sudo tee /etc/systemd/system/youtube-bot.service > /dev/null << EOF [Unit] Description=YouTube Telegram Bot After=network.target youtube-api.service [Service] Type=simple User=$USER WorkingDirectory=$PROJECT_DIR Environment=PATH=$PYTHON_ENV/bin ExecStart=$PYTHON_ENV/bin/python run_telegram_bot.py Restart=always RestartSec=10 [Install] WantedBy=multi-user.target EOF # Reload systemd and enable services print_status "Enabling systemd services..." sudo systemctl daemon-reload sudo systemctl enable youtube-api.service sudo systemctl enable youtube-bot.service # Configure Nginx (optional) print_status "Configuring Nginx..." sudo tee /etc/nginx/sites-available/youtube-api > /dev/null << EOF server { listen 80; server_name localhost; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } } EOF # Enable Nginx site sudo ln -sf /etc/nginx/sites-available/youtube-api /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl reload nginx # Start services print_status "Starting services..." sudo systemctl start youtube-api.service sudo systemctl start youtube-bot.service # Check service status print_status "Checking service status..." sleep 5 if systemctl is-active --quiet youtube-api.service; then print_status "YouTube API Server is running" else print_error "YouTube API Server failed to start" sudo systemctl status youtube-api.service fi if systemctl is-active --quiet youtube-bot.service; then print_status "YouTube Telegram Bot is running" else print_error "YouTube Telegram Bot failed to start" sudo systemctl status youtube-bot.service fi print_status "Deployment completed!" echo "" echo "📋 Next steps:" echo "1. Edit $PROJECT_DIR/.env with your API keys" echo "2. Restart services: sudo systemctl restart youtube-api youtube-bot" echo "3. Check logs: sudo journalctl -f -u youtube-api -u youtube-bot" echo "" echo "🔗 Service URLs:" echo "- API Server: http://localhost:8080" echo "- Health Check: http://localhost:8080/health" echo "" echo "📱 Service Management:" echo "- Start: sudo systemctl start youtube-api youtube-bot" echo "- Stop: sudo systemctl stop youtube-api youtube-bot" echo "- Restart: sudo systemctl restart youtube-api youtube-bot" echo "- Status: sudo systemctl status youtube-api youtube-bot" echo "- Logs: sudo journalctl -f -u youtube-api -u youtube-bot"