@woai
πͺ Add Windows deployment scripts and documentation - PowerShell and Batch scripts for Windows Server deployment, Complete Windows Services setup with NSSM, Firewall configuration, .env template creation, Detailed troubleshooting guide in DEPLOYMENT_WINDOWS.md
6bf65c9
| # π YouTube Metadata Extractor - Windows Deployment Script | |
| # This script deploys the project on a Windows Server | |
| Write-Host "π Starting deployment of YouTube Metadata Extractor on Windows..." -ForegroundColor Green | |
| # Configuration | |
| $ProjectDir = "$env:USERPROFILE\YouTube" | |
| $RepoUrl = "https://huggingface.co/spaces/dzianisBY/YouTube_Creator_MetaData" | |
| $PythonEnv = "$ProjectDir\.venv" | |
| # Function to print colored output | |
| function Write-Status { | |
| param($Message) | |
| Write-Host "β $Message" -ForegroundColor Green | |
| } | |
| function Write-Warning { | |
| param($Message) | |
| Write-Host "β οΈ $Message" -ForegroundColor Yellow | |
| } | |
| function Write-Error { | |
| param($Message) | |
| Write-Host "β $Message" -ForegroundColor Red | |
| } | |
| # Check if running as Administrator | |
| if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
| Write-Error "This script must be run as Administrator" | |
| Write-Host "Please right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow | |
| exit 1 | |
| } | |
| # Install Chocolatey if not present | |
| if (!(Get-Command choco -ErrorAction SilentlyContinue)) { | |
| Write-Status "Installing Chocolatey package manager..." | |
| Set-ExecutionPolicy Bypass -Scope Process -Force | |
| [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 | |
| iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) | |
| refreshenv | |
| } | |
| # Install required software | |
| Write-Status "Installing system dependencies..." | |
| choco install python git -y | |
| refreshenv | |
| # Create project directory | |
| Write-Status "Setting up project directory..." | |
| if (!(Test-Path $ProjectDir)) { | |
| New-Item -ItemType Directory -Path $ProjectDir -Force | |
| } | |
| Set-Location $ProjectDir | |
| # Clone or update repository | |
| if (Test-Path ".git") { | |
| Write-Status "Updating existing repository..." | |
| git pull origin main | |
| } else { | |
| Write-Status "Cloning repository..." | |
| git clone $RepoUrl . | |
| } | |
| # Create virtual environment | |
| Write-Status "Setting up Python virtual environment..." | |
| python -m venv $PythonEnv | |
| & "$PythonEnv\Scripts\Activate.ps1" | |
| # Install Python dependencies | |
| Write-Status "Installing Python dependencies..." | |
| python -m 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 (!(Test-Path ".env")) { | |
| Write-Warning "Creating .env template file..." | |
| @" | |
| # 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 | |
| "@ | Out-File -FilePath ".env" -Encoding UTF8 | |
| Write-Warning "Please edit .env file with your actual API keys!" | |
| Write-Warning "File location: $ProjectDir\.env" | |
| } | |
| # Create Windows Service scripts | |
| Write-Status "Creating Windows Service scripts..." | |
| # API Server service script | |
| @" | |
| # Start API Server | |
| Set-Location '$ProjectDir' | |
| & '$PythonEnv\Scripts\Activate.ps1' | |
| python main.py | |
| "@ | Out-File -FilePath "start-api.ps1" -Encoding UTF8 | |
| # Telegram Bot service script | |
| @" | |
| # Start Telegram Bot | |
| Set-Location '$ProjectDir' | |
| & '$PythonEnv\Scripts\Activate.ps1' | |
| python run_telegram_bot.py | |
| "@ | Out-File -FilePath "start-bot.ps1" -Encoding UTF8 | |
| # Install NSSM (Non-Sucking Service Manager) for Windows Services | |
| Write-Status "Installing NSSM for Windows Services..." | |
| choco install nssm -y | |
| # Create Windows Services | |
| Write-Status "Creating Windows Services..." | |
| # Remove existing services if they exist | |
| try { | |
| nssm remove "YouTube-API" confirm | |
| nssm remove "YouTube-Bot" confirm | |
| } catch { | |
| # Services don't exist, that's ok | |
| } | |
| # Create API Server service | |
| nssm install "YouTube-API" powershell.exe | |
| nssm set "YouTube-API" AppParameters "-ExecutionPolicy Bypass -File `"$ProjectDir\start-api.ps1`"" | |
| nssm set "YouTube-API" DisplayName "YouTube MCP API Server" | |
| nssm set "YouTube-API" Description "YouTube Metadata Extractor API Server" | |
| nssm set "YouTube-API" Start SERVICE_AUTO_START | |
| # Create Telegram Bot service | |
| nssm install "YouTube-Bot" powershell.exe | |
| nssm set "YouTube-Bot" AppParameters "-ExecutionPolicy Bypass -File `"$ProjectDir\start-bot.ps1`"" | |
| nssm set "YouTube-Bot" DisplayName "YouTube Telegram Bot" | |
| nssm set "YouTube-Bot" Description "YouTube Metadata Extractor Telegram Bot" | |
| nssm set "YouTube-Bot" Start SERVICE_AUTO_START | |
| # Configure service dependencies (Bot depends on API) | |
| nssm set "YouTube-Bot" DependOnService "YouTube-API" | |
| # Start services | |
| Write-Status "Starting Windows Services..." | |
| Start-Service "YouTube-API" | |
| Start-Sleep 5 | |
| Start-Service "YouTube-Bot" | |
| # Check service status | |
| Write-Status "Checking service status..." | |
| Start-Sleep 5 | |
| $apiStatus = Get-Service "YouTube-API" -ErrorAction SilentlyContinue | |
| $botStatus = Get-Service "YouTube-Bot" -ErrorAction SilentlyContinue | |
| if ($apiStatus.Status -eq "Running") { | |
| Write-Status "YouTube API Server is running" | |
| } else { | |
| Write-Error "YouTube API Server failed to start" | |
| Get-Service "YouTube-API" | Format-Table | |
| } | |
| if ($botStatus.Status -eq "Running") { | |
| Write-Status "YouTube Telegram Bot is running" | |
| } else { | |
| Write-Error "YouTube Telegram Bot failed to start" | |
| Get-Service "YouTube-Bot" | Format-Table | |
| } | |
| # Configure Windows Firewall | |
| Write-Status "Configuring Windows Firewall..." | |
| try { | |
| New-NetFirewallRule -DisplayName "YouTube API Server" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow | |
| Write-Status "Firewall rule added for port 8080" | |
| } catch { | |
| Write-Warning "Could not add firewall rule. You may need to do this manually." | |
| } | |
| Write-Status "Deployment completed!" | |
| Write-Host "" | |
| Write-Host "π Next steps:" -ForegroundColor Cyan | |
| Write-Host "1. Edit $ProjectDir\.env with your API keys" | |
| Write-Host "2. Restart services: Restart-Service YouTube-API, YouTube-Bot" | |
| Write-Host "3. Check logs in Event Viewer or service logs" | |
| Write-Host "" | |
| Write-Host "π Service URLs:" -ForegroundColor Cyan | |
| Write-Host "- API Server: http://localhost:8080" | |
| Write-Host "- Health Check: http://localhost:8080/health" | |
| Write-Host "" | |
| Write-Host "π± Service Management:" -ForegroundColor Cyan | |
| Write-Host "- Start: Start-Service YouTube-API, YouTube-Bot" | |
| Write-Host "- Stop: Stop-Service YouTube-API, YouTube-Bot" | |
| Write-Host "- Restart: Restart-Service YouTube-API, YouTube-Bot" | |
| Write-Host "- Status: Get-Service YouTube-API, YouTube-Bot" | |
| Write-Host "- Remove: nssm remove YouTube-API confirm; nssm remove YouTube-Bot confirm" |