87 lines
7.0 KiB
PowerShell
87 lines
7.0 KiB
PowerShell
|
|
#requires -Version 7.2
|
||
|
|
[CmdletBinding()]
|
||
|
|
param(
|
||
|
|
[Parameter(Mandatory)][string]$MigrationPath,
|
||
|
|
[Parameter(Mandatory)][string]$NewDataRoot,
|
||
|
|
[switch]$ConfirmStopped,
|
||
|
|
[switch]$ConfirmSourceStopped,
|
||
|
|
[switch]$ConfirmReplaceUnusedTarget
|
||
|
|
)
|
||
|
|
$ErrorActionPreference='Stop'
|
||
|
|
. (Join-Path $PSScriptRoot 'deployment-common.ps1')
|
||
|
|
$cfg=Read-DeployConfig
|
||
|
|
$lock=Get-DeployLock
|
||
|
|
$token=[guid]::NewGuid().ToString('N')
|
||
|
|
$id='full-sync-'+(Get-Date -Format 'yyyyMMdd-HHmmss')+'-'+$token.Substring(0,8)
|
||
|
|
$switched=$false
|
||
|
|
$maintenance=$false
|
||
|
|
try {
|
||
|
|
if(-not $ConfirmStopped -or -not $ConfirmSourceStopped -or -not $ConfirmReplaceUnusedTarget){throw 'Confirm both sites are physically stopped and the unused target data may be replaced. No target data backup will be created.'}
|
||
|
|
Assert-CleanDeployTree
|
||
|
|
if(Test-Path -LiteralPath (Join-Path $deployPrivate 'full-sync.json')){throw 'Final full synchronization already completed. Future updates must use update.ps1, not another source-data import.'}
|
||
|
|
$MigrationPath=(Resolve-Path -LiteralPath $MigrationPath).Path
|
||
|
|
Run-DeployCommand 'python' @((Join-Path $deployRepo 'scripts/migrate_standalone.py'),'verify','--package',$MigrationPath)
|
||
|
|
$manifest=Get-Content -LiteralPath (Join-Path $MigrationPath 'manifest.json') -Raw | ConvertFrom-Json
|
||
|
|
if(-not $manifest.snapshotId -or -not $manifest.createdUtc -or -not $manifest.sourceCommit){throw 'Use a fresh final export with snapshotId/createdUtc/sourceCommit, not the earlier deployment package.'}
|
||
|
|
$commit=(& git -C $deployRepo rev-parse HEAD).Trim()
|
||
|
|
if($LASTEXITCODE -ne 0 -or $commit -notmatch '^[0-9a-f]{40}$'){throw 'Cannot identify target code commit'}
|
||
|
|
if($manifest.sourceCommit -ne $commit){throw 'Source package and target code differ. Update target to the package sourceCommit first.'}
|
||
|
|
$deployedFile=Join-Path $deployPrivate 'deployed-commit.txt'
|
||
|
|
if(!(Test-Path -LiteralPath $deployedFile) -or (Get-Content -LiteralPath $deployedFile -Raw).Trim() -ne $commit){throw 'Run update.ps1 successfully before the final data cutover'}
|
||
|
|
if(-not [IO.Path]::IsPathFullyQualified($NewDataRoot)){throw 'NewDataRoot must be an absolute new directory'}
|
||
|
|
$NewDataRoot=[IO.Path]::GetFullPath($NewDataRoot).TrimEnd('\','/')
|
||
|
|
$oldRoot=[IO.Path]::GetFullPath($cfg.dataRoot).TrimEnd('\','/')
|
||
|
|
foreach($protected in @($oldRoot,$deployRepo)){
|
||
|
|
if($NewDataRoot.Equals($protected,[StringComparison]::OrdinalIgnoreCase) -or $NewDataRoot.StartsWith($protected+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase) -or $protected.StartsWith($NewDataRoot+[IO.Path]::DirectorySeparatorChar,[StringComparison]::OrdinalIgnoreCase)){throw 'NewDataRoot must not overlap the old data root or repository'}
|
||
|
|
}
|
||
|
|
if(Test-Path -LiteralPath $NewDataRoot){throw 'NewDataRoot must not already exist; old data is never overwritten'}
|
||
|
|
Assert-DeployIdle
|
||
|
|
New-DeployMaintenance $token 'final-full-sync';$maintenance=$true
|
||
|
|
Run-DeployCompose $cfg @('stop','web')
|
||
|
|
Run-DeployCommand 'pwsh' @('-NoProfile','-File',$deployManager,'-Action','Stop','-ConfirmStopped')
|
||
|
|
Assert-DeployIdle
|
||
|
|
# Retain the unused old volumes, without copying/dumping target business data.
|
||
|
|
Run-DeployCompose $cfg @('stop','mysql','redis')
|
||
|
|
$history=Join-Path $deployRepo ".local/standalone-history/$id"
|
||
|
|
New-Item -ItemType Directory -Path (Split-Path -Parent $history) -Force | Out-Null
|
||
|
|
Move-Item -LiteralPath $deployPrivate -Destination $history
|
||
|
|
$switched=$true
|
||
|
|
New-Item -ItemType Directory -Path $deployPrivate,$NewDataRoot -Force | Out-Null
|
||
|
|
New-DeployMaintenance $token 'final-full-sync'
|
||
|
|
$principal=[Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||
|
|
foreach($directory in @($deployPrivate,$NewDataRoot)){Run-DeployCommand 'icacls.exe' @($directory,'/inheritance:r','/grant:r',"${principal}:(OI)(CI)F",'*S-1-5-18:(OI)(CI)F','*S-1-5-32-544:(OI)(CI)F')}
|
||
|
|
foreach($entry in Get-ChildItem -LiteralPath $history -Force){
|
||
|
|
if($entry.Name -notin @('backend-process.json','maintenance.json','full-sync.json')){Copy-Item -LiteralPath $entry.FullName -Destination (Join-Path $deployPrivate $entry.Name) -Recurse}
|
||
|
|
}
|
||
|
|
foreach($name in @('artifacts','uploads','web','backups','logs','certs','tmp')){New-Item -ItemType Directory -Path (Join-Path $NewDataRoot $name) -Force | Out-Null}
|
||
|
|
Copy-Item -Path (Join-Path $oldRoot 'web/*') -Destination (Join-Path $NewDataRoot 'web') -Recurse
|
||
|
|
if(Test-Path -LiteralPath (Join-Path $oldRoot 'certs')){Get-ChildItem -LiteralPath (Join-Path $oldRoot 'certs') -Force | ForEach-Object {Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $NewDataRoot 'certs' $_.Name) -Recurse}}
|
||
|
|
$cfg.dataRoot=$NewDataRoot.Replace('\','/');$cfg.uploadRoot=$cfg.dataRoot+'/uploads';$cfg.instanceId=[guid]::NewGuid().ToString('N')
|
||
|
|
$cfg | Add-Member -NotePropertyName composeProject -NotePropertyValue ('am-freescan-'+$token.Substring(0,12)) -Force
|
||
|
|
$cfg | ConvertTo-Json -Depth 12 | Set-Content -LiteralPath $deployRuntime -Encoding utf8NoBOM
|
||
|
|
$yaml=Join-Path $deployPrivate 'application-private.yaml'
|
||
|
|
if(Test-Path -LiteralPath $yaml){
|
||
|
|
$text=Get-Content -LiteralPath $yaml -Raw
|
||
|
|
$text=$text.Replace($oldRoot.Replace('\','/'),$cfg.dataRoot).Replace($oldRoot,$NewDataRoot)
|
||
|
|
$text | Set-Content -LiteralPath $yaml -Encoding utf8NoBOM
|
||
|
|
}
|
||
|
|
Run-DeployCommand 'pwsh' @('-NoProfile','-File',$deployManager,'-Action','Middleware')
|
||
|
|
Run-DeployCommand 'pwsh' @('-NoProfile','-File',$deployManager,'-Action','Restore','-MigrationPath',$MigrationPath,'-ConfirmStopped')
|
||
|
|
Start-DeployInstance $token
|
||
|
|
Save-DeployRecord $commit 'final-full-sync' $id
|
||
|
|
@{snapshotId=$manifest.snapshotId;sourceCommit=$manifest.sourceCommit;createdUtc=$manifest.createdUtc;importedUtc=[datetime]::UtcNow.ToString('o');manifestSha256=(Get-FileHash -LiteralPath (Join-Path $MigrationPath 'manifest.json')).Hash;oldPrivate=$history;oldDataRoot=$oldRoot;missingHistoricalFiles=@($manifest.missingReferences).Count;targetDataBackupCreated=$false} | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $deployPrivate 'full-sync.json') -Encoding utf8NoBOM
|
||
|
|
Complete-DeployMaintenance $token;$maintenance=$false
|
||
|
|
Write-Output 'Final full sync succeeded. Use only this production instance for business data; future releases use update.ps1.'
|
||
|
|
}
|
||
|
|
catch {
|
||
|
|
if($maintenance -and -not $switched){
|
||
|
|
try{Run-DeployCompose $cfg @('up','-d','--wait','mysql','redis');Start-DeployInstance $token;Complete-DeployMaintenance $token;$maintenance=$false}catch{Write-Warning 'Old instance requires inspection; maintenance retained.'}
|
||
|
|
}
|
||
|
|
if($switched){
|
||
|
|
if(Test-Path -LiteralPath (Join-Path $deployPrivate 'compose.env')){try{Run-DeployCompose $cfg @('stop','web')}catch{Write-Warning 'Check frontend container state.'}}
|
||
|
|
Write-Warning "Cutover incomplete. Old volumes/data remain intact; old private config: $history. Do not start either instance until the target AI diagnoses the failure. Do not delete volumes or re-import over a partial database."
|
||
|
|
}
|
||
|
|
throw
|
||
|
|
}
|
||
|
|
finally{$lock.Dispose()}
|