Переписан BGP сервер на FRR

This commit is contained in:
2025-07-11 18:06:07 +07:00
parent 3c5740784b
commit e197dae9f7
14 changed files with 1222 additions and 812 deletions
+89 -62
View File
@@ -1,96 +1,123 @@
# PowerShell скрипт для запуска BGP сервера
# Скрипт запуска BGP Server (FRR)
param(
[string]$ContainerName = "bgp-server",
[string]$DataPath = "./data",
[string]$LogsPath = "./logs",
[switch]$Build,
[switch]$Logs,
[switch]$Monitor
[switch]$Force
)
Write-Host "=== BGP Server Management ===" -ForegroundColor Green
Write-Host "=== BGP Server (FRR) Startup Script ===" -ForegroundColor Green
# Проверка Docker
Write-Host "Checking Docker..." -ForegroundColor Yellow
try {
docker --version | Out-Null
Write-Host "✓ Docker is available" -ForegroundColor Green
} catch {
Write-Host "✗ Docker is not available. Please install Docker Desktop." -ForegroundColor Red
exit 1
# Проверка существования контейнера
$containerExists = docker ps -a --filter "name=$ContainerName" --format "table {{.Names}}" | Select-String $ContainerName
if ($containerExists) {
Write-Host "Found existing container: $ContainerName" -ForegroundColor Yellow
if (-not $Force) {
$response = Read-Host "Do you want to stop and remove the existing container? (y/N)"
if ($response -ne "y" -and $response -ne "Y") {
Write-Host "Operation cancelled." -ForegroundColor Red
exit 0
}
}
# Остановка и удаление старого контейнера
Write-Host "Stopping container..." -ForegroundColor Yellow
docker stop $ContainerName
Write-Host "Removing container..." -ForegroundColor Yellow
docker rm $ContainerName
}
# Проверка Docker Compose
Write-Host "Checking Docker Compose..." -ForegroundColor Yellow
try {
docker-compose --version | Out-Null
Write-Host "✓ Docker Compose is available" -ForegroundColor Green
} catch {
Write-Host "✗ Docker Compose is not available." -ForegroundColor Red
exit 1
# Создание директорий если не существуют
if (-not (Test-Path $DataPath)) {
Write-Host "Creating data directory: $DataPath" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $DataPath -Force | Out-Null
}
# Создание необходимых директорий
Write-Host "Creating directories..." -ForegroundColor Yellow
if (!(Test-Path "logs")) {
New-Item -ItemType Directory -Path "logs" | Out-Null
Write-Host "✓ Created logs directory" -ForegroundColor Green
if (-not (Test-Path $LogsPath)) {
Write-Host "Creating logs directory: $LogsPath" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $LogsPath -Force | Out-Null
}
if (!(Test-Path "data")) {
New-Item -ItemType Directory -Path "data" | Out-Null
Write-Host "✓ Created data directory" -ForegroundColor Green
# Создание файла с префиксами если не существует
$prefixesFile = Join-Path $DataPath "prefixes.txt"
if (-not (Test-Path $prefixesFile)) {
Write-Host "Creating default prefixes file..." -ForegroundColor Yellow
@"
# Default prefixes file
# Add your prefixes here, one per line
192.168.1.0/24
10.0.0.0/8
172.16.0.0/12
"@ | Out-File -FilePath $prefixesFile -Encoding UTF8
}
# Сборка образа если требуется
if ($Build) {
Write-Host "Building Docker image..." -ForegroundColor Yellow
docker-compose build
docker build -t bgp-server-frr .
if ($LASTEXITCODE -ne 0) {
Write-Host "✗ Build failed" -ForegroundColor Red
Write-Host "Failed to build image. Exiting." -ForegroundColor Red
exit 1
}
Write-Host "✓ Build completed" -ForegroundColor Green
}
# Остановка существующего контейнера
Write-Host "Stopping existing container..." -ForegroundColor Yellow
docker-compose down 2>$null
Write-Host "✓ Existing container stopped" -ForegroundColor Green
# Запуск контейнера
Write-Host "Starting BGP Server container..." -ForegroundColor Yellow
$currentPath = Get-Location
$dataVolume = "${currentPath}\${DataPath}:/app/data:ro"
$logsVolume = "${currentPath}\${LogsPath}:/app/logs"
# Запуск сервера
Write-Host "Starting BGP server..." -ForegroundColor Yellow
docker-compose up -d
docker run -d `
--name $ContainerName `
--restart unless-stopped `
-p 179:179 `
-v $dataVolume `
-v $logsVolume `
--cap-add NET_ADMIN `
--privileged `
bgp-server-frr
if ($LASTEXITCODE -eq 0) {
Write-Host "✓ BGP server started successfully" -ForegroundColor Green
Write-Host "Container started successfully!" -ForegroundColor Green
# Ожидание запуска
Write-Host "Waiting for server to initialize..." -ForegroundColor Yellow
Write-Host "Waiting for container to start..." -ForegroundColor Yellow
Start-Sleep -Seconds 10
# Проверка статуса
Write-Host "Checking server status..." -ForegroundColor Yellow
$status = docker-compose ps
Write-Host $status
$status = docker ps --filter "name=$ContainerName" --format "table {{.Status}}"
Write-Host "Container status: $status" -ForegroundColor Cyan
if ($Logs) {
Write-Host "Showing logs..." -ForegroundColor Yellow
docker-compose logs -f bgp-server
# Проверка FRR статуса
Write-Host "Checking FRR status..." -ForegroundColor Cyan
docker exec $ContainerName vtysh -c "show version" 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Host "FRR is running successfully!" -ForegroundColor Green
# Показ BGP статуса
Write-Host "BGP Summary:" -ForegroundColor Cyan
docker exec $ContainerName vtysh -c "show ip bgp summary" 2>$null
# Показ логов
Write-Host "Recent logs:" -ForegroundColor Cyan
docker logs --tail=10 $ContainerName
Write-Host "`nBGP Server (FRR) is ready!" -ForegroundColor Green
Write-Host "To view logs: docker logs -f $ContainerName" -ForegroundColor Cyan
Write-Host "To check BGP status: docker exec $ContainerName vtysh -c 'show ip bgp summary'" -ForegroundColor Cyan
Write-Host "To access FRR CLI: docker exec -it $ContainerName vtysh" -ForegroundColor Cyan
} else {
Write-Host "FRR failed to start properly. Check logs:" -ForegroundColor Red
docker logs $ContainerName
}
if ($Monitor) {
Write-Host "Starting monitoring..." -ForegroundColor Yellow
docker exec bgp-server python3 /app/scripts/monitor.py
}
} else {
Write-Host "Failed to start BGP server" -ForegroundColor Red
Write-Host "Check logs with: docker-compose logs bgp-server" -ForegroundColor Yellow
Write-Host "Failed to start container. Check the error above." -ForegroundColor Red
exit 1
}
Write-Host "=== Setup Complete ===" -ForegroundColor Green
Write-Host "Useful commands:" -ForegroundColor Cyan
Write-Host " View logs: docker-compose logs -f bgp-server" -ForegroundColor White
Write-Host " Check status: docker-compose ps" -ForegroundColor White
Write-Host " Stop server: docker-compose down" -ForegroundColor White
Write-Host " Monitor: docker exec bgp-server python3 /app/scripts/monitor.py" -ForegroundColor White
}