Switch-EdgeRelease.ps1 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #Requires -Version 7.0
  2. #Requires -RunAsAdministrator
  3. [CmdletBinding()]
  4. param(
  5. [Parameter(Mandatory)][string] $InstallRoot,
  6. [Parameter(Mandatory)][string] $DataDirectory,
  7. [Parameter(Mandatory)][string] $Config,
  8. [Parameter(Mandatory)][string] $Manifest,
  9. [Parameter(Mandatory)][string] $Signature,
  10. [Parameter(Mandatory)][string] $Archive,
  11. [ValidateRange(4, 90)][int] $HealthSeconds = 20
  12. )
  13. $ErrorActionPreference = 'Stop'
  14. $name = 'AiDopDataEdge'
  15. $root = (Resolve-Path -LiteralPath $InstallRoot).Path
  16. $publisher = Join-Path $root 'publisher.pem'
  17. $cfg = (Resolve-Path -LiteralPath $Config).Path
  18. $health = Join-Path (Resolve-Path -LiteralPath $DataDirectory).Path 'health'
  19. $service = Get-CimInstance -ClassName Win32_Service -Filter "Name='AiDopDataEdge'"
  20. if (!$service -or $service.PathName -notmatch '^"([^"]+)" --run "[^"]+"$') {
  21. throw 'Expected the service created by Install-EdgeService.'
  22. }
  23. $previousCommand = $service.PathName
  24. $previousExecutable = $Matches[1]
  25. $stage = & $previousExecutable --stage-release $root $publisher $Manifest $Signature $Archive
  26. if ($LASTEXITCODE -ne 0) { throw 'Release verification failed.' }
  27. $next = (Resolve-Path -LiteralPath (Join-Path $stage 'Admin.NET.DataPlatform.Edge.exe')).Path
  28. $releaseRoot = Join-Path $root 'releases'
  29. if (!$next.StartsWith($releaseRoot + [System.IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase)) {
  30. throw 'Staged release is outside the installation root.'
  31. }
  32. function Set-ServiceBinary([string] $Binary) {
  33. & sc.exe config $name 'binPath=' $Binary | Out-Null
  34. if ($LASTEXITCODE -ne 0) { throw 'Service binary configuration failed.' }
  35. }
  36. Stop-Service -Name $name
  37. try {
  38. $cutover = [DateTime]::UtcNow
  39. Set-ServiceBinary ('"{0}" --run "{1}"' -f $next, $cfg)
  40. Start-Service -Name $name
  41. $deadline = [DateTime]::UtcNow.AddSeconds($HealthSeconds)
  42. do {
  43. if ((Get-Service -Name $name).Status -eq 'Running' -and
  44. (Test-Path -LiteralPath $health) -and (Get-Item -LiteralPath $health).LastWriteTimeUtc -gt $cutover) {
  45. Write-Output ('Activated ' + $next)
  46. return
  47. }
  48. Start-Sleep -Milliseconds 500
  49. } while ([DateTime]::UtcNow -lt $deadline)
  50. throw 'New service version did not report health.'
  51. }
  52. catch {
  53. Stop-Service -Name $name -ErrorAction SilentlyContinue
  54. Set-ServiceBinary $previousCommand
  55. Start-Service -Name $name
  56. throw
  57. }