#requires -Version 5.1
param(
[Parameter(Mandatory)][string]$ServerPath,
[string]$Root,
[string]$Origin = 'http://127.0.0.1:8444'
)
$ErrorActionPreference = 'Stop'
$taskWorkspace = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
if (-not $Root) { $Root = Join-Path $taskWorkspace 'Server/runtime' }
$Root = [IO.Path]::GetFullPath($Root)
$ServerPath = [IO.Path]::GetFullPath($ServerPath)
$taskUri = [Uri]$Origin
if (-not $taskUri.IsLoopback -or $taskUri.Scheme -ne 'http' -or $taskUri.AbsolutePath -ne '/') { throw 'Use the local management API origin.' }
if ([IO.Path]::GetFileName($ServerPath) -ne 'XFEWarframe.Server.exe' -or -not (Test-Path -LiteralPath $ServerPath -PathType Leaf)) { throw 'The updated server executable is missing.' }
$taskSettingsPath = Join-Path $Root 'server.json'
$taskStatePath = Join-Path $Root 'data/state.json'
if (-not (Test-Path -LiteralPath $taskSettingsPath -PathType Leaf) -or -not (Test-Path -LiteralPath $taskStatePath -PathType Leaf)) { throw 'The existing server configuration and save are required.' }
$taskSettings = Get-Content -LiteralPath $taskSettingsPath -Raw | ConvertFrom-Json
$taskHealth = $null
try { $taskHealth = Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/health') -TimeoutSec 3 } catch { }
if ($taskHealth) {
if ($taskHealth.service -ne 'XFEWarframe') { throw 'This port belongs to another service.' }
$taskOwners = @(Get-NetTCPConnection -LocalPort $taskUri.Port -State Listen | Select-Object -ExpandProperty OwningProcess -Unique)
if ($taskOwners.Count -ne 1) { throw 'The local listener process is ambiguous.' }
$taskProcess = Get-Process -Id $taskOwners[0]
if ([IO.Path]::GetFileName($taskProcess.Path) -ne 'XFEWarframe.Server.exe') { throw 'The listener is not the expected server process.' }
Write-Output 'Gracefully stopping the existing local private server...'
Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/control/v1/shutdown') -Method Post -ContentType 'application/json' -Body '{}' -Headers @{ Authorization = 'Bearer ' + $taskSettings.AdminToken } -TimeoutSec 5 | Out-Null
if (-not $taskProcess.WaitForExit(15000)) { throw 'The old server did not finish saving and exit. No files were replaced.' }
}
$taskBackup = Join-Path $taskWorkspace ('artifacts/server-update-backups/' + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $taskBackup | Out-Null
foreach ($taskRelative in @('server.json', 'data/state.json', 'data/state.json.bak')) {
$taskSource = Join-Path $Root $taskRelative
if (Test-Path -LiteralPath $taskSource -PathType Leaf) {
$taskTarget = Join-Path $taskBackup $taskRelative
New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($taskTarget)) -Force | Out-Null
Copy-Item -LiteralPath $taskSource -Destination $taskTarget
}
}
$taskArchive = Join-Path $Root 'data/mission-receipts'
if (Test-Path -LiteralPath $taskArchive -PathType Container) { Copy-Item -LiteralPath $taskArchive -Destination (Join-Path $taskBackup 'data/mission-receipts') -Recurse }
$taskOut = Join-Path $taskBackup 'updated-server.log'
$taskErr = Join-Path $taskBackup 'updated-server.err.log'
$taskNext = Start-Process -FilePath $ServerPath -ArgumentList ('--root="' + $Root + '"') -WorkingDirectory $taskWorkspace -WindowStyle Hidden -RedirectStandardOutput $taskOut -RedirectStandardError $taskErr -PassThru
$taskDeadline = [DateTime]::UtcNow.AddSeconds(30)
while ([DateTime]::UtcNow -lt $taskDeadline) {
if ($taskNext.HasExited) { throw ('The updated server exited. Save backup and error log: ' + $taskBackup) }
try {
$taskHealth = Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/health') -TimeoutSec 2
if ($taskHealth.service -eq 'XFEWarframe' -and $taskHealth.status -eq 'ok') {
Write-Output ('Updated server ready. PID: ' + $taskNext.Id)
Write-Output ('Existing save preserved; backup: ' + $taskBackup)
exit 0
}
} catch { }
Start-Sleep -Milliseconds 250
}
throw ('The updated server did not become ready. Inspect: ' + $taskErr)
#requires -Version 5.1
param(
[Parameter(Mandatory)][string]$ServerPath,
[string]$Root,
[string]$Origin = 'http://127.0.0.1:8444'
)
$ErrorActionPreference = 'Stop'
$taskWorkspace = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
if (-not $Root) { $Root = Join-Path $taskWorkspace 'Server/runtime' }
$Root = [IO.Path]::GetFullPath($Root)
$ServerPath = [IO.Path]::GetFullPath($ServerPath)
$taskUri = [Uri]$Origin
if (-not $taskUri.IsLoopback -or $taskUri.Scheme -ne 'http' -or $taskUri.AbsolutePath -ne '/') { throw 'Use the local management API origin.' }
if ([IO.Path]::GetFileName($ServerPath) -ne 'XFEWarframe.Server.exe' -or -not (Test-Path -LiteralPath $ServerPath -PathType Leaf)) { throw 'The updated server executable is missing.' }
$taskSettingsPath = Join-Path $Root 'server.json'
$taskStatePath = Join-Path $Root 'data/state.json'
if (-not (Test-Path -LiteralPath $taskSettingsPath -PathType Leaf) -or -not (Test-Path -LiteralPath $taskStatePath -PathType Leaf)) { throw 'The existing server configuration and save are required.' }
$taskSettings = Get-Content -LiteralPath $taskSettingsPath -Raw | ConvertFrom-Json
$taskHealth = $null
try { $taskHealth = Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/health') -TimeoutSec 3 } catch { }
if ($taskHealth) {
if ($taskHealth.service -ne 'XFEWarframe') { throw 'This port belongs to another service.' }
$taskOwners = @(Get-NetTCPConnection -LocalPort $taskUri.Port -State Listen | Select-Object -ExpandProperty OwningProcess -Unique)
if ($taskOwners.Count -ne 1) { throw 'The local listener process is ambiguous.' }
$taskProcess = Get-Process -Id $taskOwners[0]
if ([IO.Path]::GetFileName($taskProcess.Path) -ne 'XFEWarframe.Server.exe') { throw 'The listener is not the expected server process.' }
Write-Output 'Gracefully stopping the existing local private server...'
Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/control/v1/shutdown') -Method Post -ContentType 'application/json' -Body '{}' -Headers @{ Authorization = 'Bearer ' + $taskSettings.AdminToken } -TimeoutSec 5 | Out-Null
if (-not $taskProcess.WaitForExit(15000)) { throw 'The old server did not finish saving and exit. No files were replaced.' }
}
$taskBackup = Join-Path $taskWorkspace ('artifacts/server-update-backups/' + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $taskBackup | Out-Null
foreach ($taskRelative in @('server.json', 'data/state.json', 'data/state.json.bak')) {
$taskSource = Join-Path $Root $taskRelative
if (Test-Path -LiteralPath $taskSource -PathType Leaf) {
$taskTarget = Join-Path $taskBackup $taskRelative
New-Item -ItemType Directory -Path ([IO.Path]::GetDirectoryName($taskTarget)) -Force | Out-Null
Copy-Item -LiteralPath $taskSource -Destination $taskTarget
}
}
$taskArchive = Join-Path $Root 'data/mission-receipts'
if (Test-Path -LiteralPath $taskArchive -PathType Container) { Copy-Item -LiteralPath $taskArchive -Destination (Join-Path $taskBackup 'data/mission-receipts') -Recurse }
$taskOut = Join-Path $taskBackup 'updated-server.log'
$taskErr = Join-Path $taskBackup 'updated-server.err.log'
$taskNext = Start-Process -FilePath $ServerPath -ArgumentList ('--root="' + $Root + '"') -WorkingDirectory $taskWorkspace -WindowStyle Hidden -RedirectStandardOutput $taskOut -RedirectStandardError $taskErr -PassThru
$taskDeadline = [DateTime]::UtcNow.AddSeconds(30)
while ([DateTime]::UtcNow -lt $taskDeadline) {
if ($taskNext.HasExited) { throw ('The updated server exited. Save backup and error log: ' + $taskBackup) }
try {
$taskHealth = Invoke-RestMethod -Uri ($Origin.TrimEnd('/') + '/health') -TimeoutSec 2
if ($taskHealth.service -eq 'XFEWarframe' -and $taskHealth.status -eq 'ok') {
Write-Output ('Updated server ready. PID: ' + $taskNext.Id)
Write-Output ('Existing save preserved; backup: ' + $taskBackup)
exit 0
}
} catch { }
Start-Sleep -Milliseconds 250
}
throw ('The updated server did not become ready. Inspect: ' + $taskErr)