initializare
This commit is contained in:
275
deployment/windows/scripts/install_service.ps1
Normal file
275
deployment/windows/scripts/install_service.ps1
Normal file
@@ -0,0 +1,275 @@
|
||||
#Requires -RunAsAdministrator
|
||||
<#
|
||||
.SYNOPSIS
|
||||
BTGO Telegram Bot - Service Installation Script
|
||||
.DESCRIPTION
|
||||
Instalează bot-ul Telegram ca serviciu Windows folosind NSSM
|
||||
.NOTES
|
||||
Rulare: Right-click → "Run with PowerShell" (ca Administrator)
|
||||
#>
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# ============================================================================
|
||||
# FUNCTIONS
|
||||
# ============================================================================
|
||||
|
||||
function Write-ColorOutput {
|
||||
param(
|
||||
[string]$Message,
|
||||
[string]$Prefix = ""
|
||||
)
|
||||
$color = switch ($Prefix) {
|
||||
"[OK]" { "Green" }
|
||||
"[INFO]" { "Cyan" }
|
||||
"[EROARE]" { "Red" }
|
||||
"[AVERTIZARE]" { "Yellow" }
|
||||
default { "White" }
|
||||
}
|
||||
if ($Prefix) {
|
||||
Write-Host "$Prefix " -ForegroundColor $color -NoNewline
|
||||
Write-Host $Message
|
||||
} else {
|
||||
Write-Host $Message
|
||||
}
|
||||
}
|
||||
|
||||
function Write-Separator {
|
||||
param([string]$Title = "")
|
||||
Write-Host ""
|
||||
Write-Host ("=" * 80) -ForegroundColor Cyan
|
||||
if ($Title) {
|
||||
Write-Host $Title -ForegroundColor Yellow
|
||||
Write-Host ("=" * 80) -ForegroundColor Cyan
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# CONFIGURATION
|
||||
# ============================================================================
|
||||
|
||||
$ServiceName = "BTGOTelegramBot"
|
||||
$ServiceDisplayName = "BTGO Telegram Trigger Bot"
|
||||
$ServiceDescription = "Bot Telegram pentru declansarea BTGO Scraper prin comanda /scrape"
|
||||
|
||||
$ScriptDir = Split-Path -Parent $PSCommandPath
|
||||
$ProjectDir = Resolve-Path (Join-Path $ScriptDir "..\..\..") | Select-Object -ExpandProperty Path
|
||||
$BotScript = Join-Path $ProjectDir "telegram_trigger_bot.py"
|
||||
$EnvFile = Join-Path $ProjectDir ".env"
|
||||
$LogDir = Join-Path $ProjectDir "logs"
|
||||
$ToolsDir = Join-Path $ProjectDir "deployment\windows\tools"
|
||||
$NssmPath = Join-Path $ToolsDir "nssm.exe"
|
||||
|
||||
Write-Separator "BTGO TELEGRAM BOT - INSTALARE SERVICIU"
|
||||
Write-ColorOutput "Director proiect: $ProjectDir" -Prefix "[INFO]"
|
||||
|
||||
# ============================================================================
|
||||
# VERIFICARI PRE-INSTALARE
|
||||
# ============================================================================
|
||||
|
||||
Write-Separator "VERIFICARI PRE-INSTALARE"
|
||||
|
||||
# Verifică Python
|
||||
try {
|
||||
$pythonPath = (Get-Command python).Source
|
||||
Write-ColorOutput "Python: $pythonPath" -Prefix "[OK]"
|
||||
} catch {
|
||||
Write-ColorOutput "Python nu a fost gasit in PATH!" -Prefix "[EROARE]"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verifică script bot
|
||||
if (Test-Path $BotScript) {
|
||||
Write-ColorOutput "Script bot: $BotScript" -Prefix "[OK]"
|
||||
} else {
|
||||
Write-ColorOutput "Script bot nu a fost gasit: $BotScript" -Prefix "[EROARE]"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verifică .env
|
||||
if (Test-Path $EnvFile) {
|
||||
Write-ColorOutput "Fisier .env: $EnvFile" -Prefix "[OK]"
|
||||
} else {
|
||||
Write-ColorOutput "Fisier .env nu a fost gasit: $EnvFile" -Prefix "[EROARE]"
|
||||
Write-Host "Copiaza .env.example la .env si configureaza-l"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verifică variabile critice din .env
|
||||
$envContent = Get-Content $EnvFile -Raw
|
||||
if ($envContent -match "TELEGRAM_BOT_TOKEN=.+") {
|
||||
Write-ColorOutput "TELEGRAM_BOT_TOKEN configurat in .env" -Prefix "[OK]"
|
||||
} else {
|
||||
Write-ColorOutput "TELEGRAM_BOT_TOKEN nu este setat in .env!" -Prefix "[EROARE]"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Creează director logs
|
||||
if (-not (Test-Path $LogDir)) {
|
||||
New-Item -ItemType Directory -Path $LogDir -Force | Out-Null
|
||||
Write-ColorOutput "Director logs creat: $LogDir" -Prefix "[INFO]"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# DOWNLOAD NSSM (daca nu exista)
|
||||
# ============================================================================
|
||||
|
||||
if (-not (Test-Path $NssmPath)) {
|
||||
Write-Separator "DOWNLOAD NSSM"
|
||||
Write-ColorOutput "NSSM nu a fost gasit. Descarcam..." -Prefix "[INFO]"
|
||||
|
||||
if (-not (Test-Path $ToolsDir)) {
|
||||
New-Item -ItemType Directory -Path $ToolsDir -Force | Out-Null
|
||||
}
|
||||
|
||||
try {
|
||||
$nssmUrl = "https://nssm.cc/release/nssm-2.24.zip"
|
||||
$nssmZip = Join-Path $env:TEMP "nssm.zip"
|
||||
$nssmExtract = Join-Path $env:TEMP "nssm"
|
||||
|
||||
Write-ColorOutput "Descarcare NSSM..." -Prefix "[INFO]"
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
Invoke-WebRequest -Uri $nssmUrl -OutFile $nssmZip -UseBasicParsing
|
||||
|
||||
Write-ColorOutput "Extragere NSSM..." -Prefix "[INFO]"
|
||||
Expand-Archive -Path $nssmZip -DestinationPath $nssmExtract -Force
|
||||
|
||||
# Copiază executabilul
|
||||
$nssmExe = Join-Path $nssmExtract "nssm-2.24\win64\nssm.exe"
|
||||
Copy-Item $nssmExe $NssmPath
|
||||
|
||||
# Curăță
|
||||
Remove-Item $nssmZip -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item $nssmExtract -Recurse -Force -ErrorAction SilentlyContinue
|
||||
|
||||
Write-ColorOutput "NSSM descarcat: $NssmPath" -Prefix "[OK]"
|
||||
} catch {
|
||||
Write-ColorOutput "Descarcarea NSSM a esuat!" -Prefix "[EROARE]"
|
||||
Write-Host "Descarca manual de la: https://nssm.cc/download"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# INSTALARE SERVICIU
|
||||
# ============================================================================
|
||||
|
||||
Write-Separator "INSTALARE SERVICIU WINDOWS"
|
||||
|
||||
# Verifică dacă serviciul există deja
|
||||
$existingService = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
|
||||
if ($existingService) {
|
||||
Write-ColorOutput "Serviciul $ServiceName exista deja!" -Prefix "[AVERTIZARE]"
|
||||
Write-ColorOutput "Ruland dezinstalare mai intai..." -Prefix "[INFO]"
|
||||
& "$ScriptDir\uninstall_service.ps1"
|
||||
Start-Sleep -Seconds 3
|
||||
}
|
||||
|
||||
Write-ColorOutput "Instalare serviciu: $ServiceName" -Prefix "[INFO]"
|
||||
|
||||
# Instalează serviciul
|
||||
& $NssmPath install $ServiceName $pythonPath $BotScript
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-ColorOutput "Instalarea serviciului a esuat!" -Prefix "[EROARE]"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# CONFIGURARE SERVICIU
|
||||
# ============================================================================
|
||||
|
||||
Write-ColorOutput "Configurare serviciu..." -Prefix "[INFO]"
|
||||
|
||||
# Display name si descriere
|
||||
& $NssmPath set $ServiceName DisplayName $ServiceDisplayName
|
||||
& $NssmPath set $ServiceName Description $ServiceDescription
|
||||
|
||||
# Verifică și configurează Playwright browsers global (dacă nu e deja setat)
|
||||
$globalBrowserPath = [System.Environment]::GetEnvironmentVariable("PLAYWRIGHT_BROWSERS_PATH", [System.EnvironmentVariableTarget]::Machine)
|
||||
|
||||
if (-not $globalBrowserPath) {
|
||||
Write-ColorOutput "PLAYWRIGHT_BROWSERS_PATH nu este setat!" -Prefix "[AVERTIZARE]"
|
||||
Write-Host "Ruleaza 'deploy.ps1' pentru instalare completa cu browsere globale"
|
||||
} else {
|
||||
Write-ColorOutput "Playwright browsere: $globalBrowserPath" -Prefix "[OK]"
|
||||
}
|
||||
|
||||
# Working directory
|
||||
& $NssmPath set $ServiceName AppDirectory $ProjectDir
|
||||
|
||||
# Stdout/Stderr logging
|
||||
$stdoutLog = Join-Path $LogDir "telegram_bot_stdout.log"
|
||||
$stderrLog = Join-Path $LogDir "telegram_bot_stderr.log"
|
||||
& $NssmPath set $ServiceName AppStdout $stdoutLog
|
||||
& $NssmPath set $ServiceName AppStderr $stderrLog
|
||||
|
||||
# Rotatie logs (10 MB max)
|
||||
& $NssmPath set $ServiceName AppStdoutCreationDisposition 4
|
||||
& $NssmPath set $ServiceName AppStderrCreationDisposition 4
|
||||
& $NssmPath set $ServiceName AppRotateFiles 1
|
||||
& $NssmPath set $ServiceName AppRotateOnline 1
|
||||
& $NssmPath set $ServiceName AppRotateBytes 10485760
|
||||
|
||||
# Restart policy (restart la crash)
|
||||
& $NssmPath set $ServiceName AppExit Default Restart
|
||||
& $NssmPath set $ServiceName AppRestartDelay 5000
|
||||
|
||||
# Start mode (Automatic Delayed Start)
|
||||
& $NssmPath set $ServiceName Start SERVICE_DELAYED_AUTO_START
|
||||
|
||||
Write-ColorOutput "Serviciu configurat cu succes!" -Prefix "[OK]"
|
||||
|
||||
# ============================================================================
|
||||
# PORNIRE SERVICIU
|
||||
# ============================================================================
|
||||
|
||||
Write-Separator "PORNIRE SERVICIU"
|
||||
|
||||
Write-ColorOutput "Pornire serviciu: $ServiceName" -Prefix "[INFO]"
|
||||
Start-Service -Name $ServiceName
|
||||
|
||||
if ((Get-Service -Name $ServiceName).Status -ne "Running") {
|
||||
Write-ColorOutput "Pornirea serviciului a esuat!" -Prefix "[EROARE]"
|
||||
Write-Host "Verifica logurile in: $LogDir"
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# ============================================================================
|
||||
# VERIFICARE STATUS
|
||||
# ============================================================================
|
||||
|
||||
$service = Get-Service -Name $ServiceName
|
||||
|
||||
if ($service.Status -eq "Running") {
|
||||
Write-Separator "[SUCCES] SERVICIU INSTALAT SI PORNIT!"
|
||||
Write-Host ""
|
||||
Write-Host "Serviciu: $ServiceName" -ForegroundColor Green
|
||||
Write-Host "Display: $ServiceDisplayName" -ForegroundColor Green
|
||||
Write-Host "Script: $BotScript" -ForegroundColor Green
|
||||
Write-Host "Logs: $LogDir" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
Write-Host "Comenzi utile:" -ForegroundColor Yellow
|
||||
Write-Host " - Opreste: Stop-Service $ServiceName"
|
||||
Write-Host " - Porneste: Start-Service $ServiceName"
|
||||
Write-Host " - Restart: Restart-Service $ServiceName"
|
||||
Write-Host " - Status: Get-Service $ServiceName"
|
||||
Write-Host " - Dezinstaleaza: .\uninstall_service.ps1"
|
||||
Write-Host ""
|
||||
Write-Separator
|
||||
} else {
|
||||
Write-ColorOutput "Serviciul este instalat dar nu ruleaza!" -Prefix "[AVERTIZARE]"
|
||||
Write-Host "Verifica logurile: $LogDir"
|
||||
Get-Service -Name $ServiceName | Format-List
|
||||
}
|
||||
|
||||
Read-Host "`nApasa Enter pentru a inchide"
|
||||
Reference in New Issue
Block a user