Files
bgp-server/scripts/update-container.ps1

92 lines
3.2 KiB
PowerShell

# Скрипт для безопасного обновления BGP Server контейнера
param(
[string]$ContainerName = "bgp-server",
[string]$ImageName = "git.shts.su/infra/bgp-server:latest",
[string]$DataPath = "./data",
[string]$LogsPath = "./logs",
[switch]$Force
)
Write-Host "=== BGP Server Container Update Script ===" -ForegroundColor Green
# Проверка существования контейнера
$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 update the container? (y/N)"
if ($response -ne "y" -and $response -ne "Y") {
Write-Host "Update cancelled." -ForegroundColor Red
exit 0
}
}
# Остановка и удаление старого контейнера
Write-Host "Stopping container..." -ForegroundColor Yellow
docker stop $ContainerName
Write-Host "Removing container..." -ForegroundColor Yellow
docker rm $ContainerName
} else {
Write-Host "No existing container found. Creating new one..." -ForegroundColor Green
}
# Создание директорий если не существуют
if (-not (Test-Path $DataPath)) {
Write-Host "Creating data directory: $DataPath" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $DataPath -Force | Out-Null
}
if (-not (Test-Path $LogsPath)) {
Write-Host "Creating logs directory: $LogsPath" -ForegroundColor Yellow
New-Item -ItemType Directory -Path $LogsPath -Force | Out-Null
}
# Загрузка нового образа
Write-Host "Pulling latest image..." -ForegroundColor Yellow
docker pull $ImageName
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to pull image. Exiting." -ForegroundColor Red
exit 1
}
# Запуск нового контейнера
Write-Host "Starting new container..." -ForegroundColor Yellow
$currentPath = Get-Location
$dataVolume = "${currentPath}\${DataPath}:/app/data:ro"
$logsVolume = "${currentPath}\${LogsPath}:/app/logs"
docker run -d `
--name $ContainerName `
--restart unless-stopped `
-p 179:179 `
-v $dataVolume `
-v $logsVolume `
$ImageName
if ($LASTEXITCODE -eq 0) {
Write-Host "Container started successfully!" -ForegroundColor Green
# Ожидание запуска
Write-Host "Waiting for container to start..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
# Проверка статуса
$status = docker ps --filter "name=$ContainerName" --format "table {{.Status}}"
Write-Host "Container status: $status" -ForegroundColor Cyan
# Показ логов
Write-Host "Recent logs:" -ForegroundColor Cyan
docker logs --tail=10 $ContainerName
Write-Host "`nContainer update completed successfully!" -ForegroundColor Green
Write-Host "To view logs: docker logs -f $ContainerName" -ForegroundColor Cyan
Write-Host "To check status: docker ps" -ForegroundColor Cyan
} else {
Write-Host "Failed to start container. Check the error above." -ForegroundColor Red
exit 1
}