am-freescan/deploy/windows11/deployment-common.ps1

64 lines
4.5 KiB
PowerShell
Raw Normal View History

# Shared implementation for Update and the one-time full-data cutover. No actions on import.
$deployRepo = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../..'))
$deployPrivate = Join-Path $deployRepo '.local/standalone'
$deployRuntime = Join-Path $deployPrivate 'runtime.json'
$deployManager = Join-Path $PSScriptRoot 'manage.ps1'
$deployPython = Join-Path $deployRepo 'scripts/bootstrap_standalone.py'
$deployCompose = Join-Path $PSScriptRoot 'compose.yaml'
function Run-DeployCommand([string]$Program, [string[]]$Arguments) {
& $Program @Arguments
if ($LASTEXITCODE -ne 0) { throw "$Program failed ($LASTEXITCODE); deployment stopped" }
}
function Read-DeployConfig {
if (-not (Test-Path -LiteralPath $deployRuntime)) { throw 'Existing standalone instance not found. Follow README first; do not run Init over an existing deployment.' }
$value = Get-Content -LiteralPath $deployRuntime -Raw | ConvertFrom-Json
if ($value.mysql.host -ne '127.0.0.1' -or $value.mysql.port -ne 3306 -or $value.mysql.database -ne 'am-freescan' -or $value.mysql.user -ne 'freescan' -or $value.redis.host -ne '127.0.0.1' -or $value.redis.port -ne 6379 -or $value.redis.database -ne 15) { throw 'This script only operates on the existing localhost standalone instance' }
return $value
}
function Run-DeployCompose($Config, [string[]]$Arguments) {
$project = if ($Config.composeProject) { $Config.composeProject } else { 'am-freescan-standalone' }
if ($project -notmatch '^[a-z0-9][a-z0-9_-]*$') { throw 'Invalid Compose project' }
Run-DeployCommand 'docker' (@('compose','-p',$project,'--env-file',(Join-Path $deployPrivate 'compose.env'),'-f',$deployCompose) + $Arguments)
}
function Get-DeployLock {
$path = Join-Path $deployRepo '.local/deployment.lock'
try { return [IO.File]::Open($path,[IO.FileMode]::OpenOrCreate,[IO.FileAccess]::ReadWrite,[IO.FileShare]::None) }
catch { throw 'Another deployment is running or the deployment lock is inaccessible; do not run concurrent updates' }
}
function Assert-CleanDeployTree {
$changes = & git -C $deployRepo status --porcelain
if ($LASTEXITCODE -ne 0 -or $changes) { throw 'Repository has local changes or Git failed; preserve changes and resolve them first. No reset/stash is performed.' }
$branch = & git -C $deployRepo branch --show-current
if ($LASTEXITCODE -ne 0 -or $branch -ne 'main') { throw 'Deployment requires the main branch' }
}
function Assert-DeployIdle {
Run-DeployCommand 'python' @($deployPython,'--config',$deployRuntime,'--check-idle')
}
function New-DeployMaintenance([string]$Token, [string]$Kind) {
$file = Join-Path $deployPrivate 'maintenance.json'
if (Test-Path -LiteralPath $file) { throw 'An unfinished maintenance operation exists; inspect it before continuing' }
@{token=$Token;kind=$Kind;createdUtc=[datetime]::UtcNow.ToString('o')} | ConvertTo-Json | Set-Content -LiteralPath $file -Encoding utf8NoBOM
}
function Complete-DeployMaintenance([string]$Token) {
$file = Join-Path $deployPrivate 'maintenance.json'
if (Test-Path -LiteralPath $file) {
$marker = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json
if ($marker.token -ne $Token) { throw 'Maintenance identity changed; refusing to clear it' }
Remove-Item -LiteralPath $file
}
}
function Start-DeployInstance([string]$Token) {
Run-DeployCommand 'pwsh' @('-NoProfile','-File',$deployManager,'-Action','Start','-MaintenanceToken',$Token)
$reply = Invoke-RestMethod 'http://127.0.0.1:5180/admin-api/system/tenant/simple-list' -TimeoutSec 15
if ($reply.code -ne 0 -or @($reply.data | Where-Object { $_.id -eq 1 }).Count -ne 1) { throw 'Frontend proxy health check failed' }
$html = Invoke-WebRequest 'http://127.0.0.1:5180/' -TimeoutSec 15
if ($html.StatusCode -ne 200 -or $html.Content -notmatch '<html') { throw 'Frontend index health check failed' }
}
function Save-DeployRecord([string]$Commit, [string]$Kind, [string]$Id) {
$jar = Join-Path $deployPrivate 'backend/yudao-server.jar'
$Commit | Set-Content -LiteralPath (Join-Path $deployPrivate 'deployed-commit.txt') -Encoding utf8NoBOM
(Get-FileHash -LiteralPath $jar -Algorithm SHA256).Hash | Set-Content -LiteralPath (Join-Path $deployPrivate 'backend-sha256.txt') -Encoding utf8NoBOM
@{commit=$Commit;kind=$Kind;releaseId=$Id;healthyUtc=[datetime]::UtcNow.ToString('o');jarSha256=(Get-FileHash -LiteralPath $jar).Hash} | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $deployPrivate 'deployment.json') -Encoding utf8NoBOM
}