Private/Run/Lanes.ps1

# A parallel run (execution.mode = Parallel): the servers are installed at the same time.
#
# Lane the work of one server, in plan order, as in a sequential run. The lanes run side by side, each in
# its own PowerShell runspace of the controller process with its own copy of this module (so the
# remoting sessions and the approved media list of one lane are not shared with another).
# The Vault is one lane, its DR Vault another, the new forest is one lane.
# Signal a wait handle that is set once and stays set:
# Domain the new forest is installed (set from the start when the run creates none)
# Vault the Primary Vault is installed, hardened and verified (set from the start when
# the run installs none). The DR Vault is not waited for.
# PvwaRegistered the first PVWA finished its step 'register'
# Registered:<id> PVWA or CPM <id> finished its step 'register'
# Cert:<id> PVWA <id> has its HTTPS certificate (its first step, 'certificate')
# Done:<id> PVWA <id> is installed and verified
# Fail:<signal> the lane that would have set <signal> failed: who waits for it gives up
# Cancel the lanes are being taken down (set only when the run ends)
# What waits for what (see Docs\PARALLEL.md):
# a joined server Domain, before its join
# a component that trusts
# the PVWA certificate Cert of every PVWA, before it starts (CPM, PSM, HTML5 Gateway, PTA)
# a step that signs in
# to the Vault Vault; every component except the first PVWA also PvwaRegistered;
# everything except PVWA and CPM also Registered of every CPM
# (the vendor's order: PVWA, CPM, then the rest); PTA waits for Done of
# every PVWA
# the DR Vault nothing but the milestones of the Primary Vault (its keys, then
# 'primary-ready'); nothing waits for the DR Vault
# One Vault sign-in at a time is the Vault lock that every such step takes (Engine\Locks.ps1).
# A lane that fails stops only what depends on it. Registered:<id> is an order, not a need: when that server
# fails, those that waited for it go on. Everything else in the list is a need: who waits for it is not
# installed, and says so. The other servers are installed to the end, then the run ends Failed.
# What follows the lanes stays in order in the controller: gateway registration, PTA trust, GPOs.

function Get-PASRunLanes {
    <# The lanes of a configuration, in the order they are started. Each lane:
         Name 'ad', 'vault', 'dr' or the id of the server
         Kind AD, Vault, DR or Server
         Join $true when the server is joined to the domain before its components
         Components ids of the components this lane installs, in plan order (AD and Vault lanes: their own) #>

    param([Parameter(Mandatory)]$Config, [Parameter(Mandatory)][object[]]$PlanItems)
    $lanes = New-Object System.Collections.ArrayList
    $components = @($Config.components | Where-Object { $null -ne $_ })
    $joined = @()
    if ([string]$Config.activeDirectory.mode -ne 'None') {
        $joined = @($Config.activeDirectory.join | ForEach-Object { [string]$_ })
    }
    $forest = @($components | Where-Object { $_.type -eq 'AD' })
    if ($forest.Count) {
        [void]$lanes.Add(@{ Name = 'ad'; Kind = 'AD'; Join = $false; Components = @([string]$forest[0].id) })
    }
    $vaultParts = @($components | Where-Object { $_.type -in @('Vault', 'DRVault') })
    $hasVault = @($vaultParts | Where-Object { $_.type -eq 'Vault' }).Count -gt 0
    # Servers that already have a lane of their own: anything else on them is installed after that lane's work.
    $laneOfHost = @{}
    if ($forest.Count) {
        $laneOfHost[[string]$forest[0].host] = 'ad'
    }
    if ($hasVault) {
        [void]$lanes.Add(@{ Name = 'vault'; Kind = 'Vault'; Join = $false; Components = @($vaultParts | Where-Object { $_.type -eq 'Vault' } | ForEach-Object { [string]$_.id }) })
        foreach ($part in @($vaultParts | Where-Object { $_.type -eq 'Vault' })) {
            $laneOfHost[[string]$part.host] = 'vault'
        }
        $drParts = @($vaultParts | Where-Object { $_.type -eq 'DRVault' })
        if ($drParts.Count) {
            # The DR Vault prepares itself while the Primary is installed and waits only for its milestones.
            [void]$lanes.Add(@{ Name = 'dr'; Kind = 'DR'; Join = $false; Components = @($drParts | ForEach-Object { [string]$_.id }) })
            foreach ($part in $drParts) {
                $laneOfHost[[string]$part.host] = 'dr'
            }
        }
    }
    foreach ($item in @($PlanItems | Where-Object { $_.Type -notin @('Vault', 'DRVault', 'AD') })) {
        $component = @($components | Where-Object { $_.id -eq $item.Id })[0]
        $hostId = [string]$component.host
        if (-not $laneOfHost.ContainsKey($hostId)) {
            $laneOfHost[$hostId] = $hostId
            [void]$lanes.Add(@{ Name = $hostId; Kind = 'Server'; Join = ($joined -contains $hostId); Components = @() })
        }
        $lane = @($lanes | Where-Object { $_.Name -eq $laneOfHost[$hostId] })[0]
        if ($lane.Kind -eq 'Server') {
            $lane.Components = @($lane.Components) + @([string]$component.id)
        } else {
            # A component on the Vault server or on the domain controller: after that lane's own work.
            $lane.After = @(Get-PASValue $lane 'After' @()) + @([string]$component.id)
        }
    }
    # A joined server without a component still has to be joined.
    foreach ($hostId in $joined) {
        if (-not $laneOfHost.ContainsKey($hostId)) {
            $laneOfHost[$hostId] = $hostId
            [void]$lanes.Add(@{ Name = $hostId; Kind = 'Server'; Join = $true; Components = @() })
        }
    }
    return @($lanes.ToArray())
}

function Get-PASLaneComponentGate {
    <# What one component waits for and what it announces.
         StartAfter names of the signals it waits for before it starts
         VaultAfter names of the signals its Vault steps wait for
         StepSignals @{ <step id> = names of the signals that step sets }
         DoneSignals names of the signals set when the component is installed and verified
       The registrations follow the vendor's order: the first PVWA, the other PVWAs, every CPM, then the rest. #>

    param([Parameter(Mandatory)]$Config, [Parameter(Mandatory)][hashtable]$Component)
    $id = [string]$Component.id
    $type = [string]$Component.type
    $pvwas = @($Config.components | Where-Object { $_.type -eq 'PVWA' } | ForEach-Object { [string]$_.id })
    $cpms = @($Config.components | Where-Object { $_.type -eq 'CPM' } | ForEach-Object { [string]$_.id })
    $gate = @{ StartAfter = @(); VaultAfter = @('Vault'); StepSignals = @{}; DoneSignals = @() }
    if ([string](Get-PASValue (Get-PASValue $Component 'options' $null) 'pvwaTrustFrom' '')) {
        $gate.StartAfter = @($pvwas | ForEach-Object { 'Cert:' + $_ })
    }
    if ($type -eq 'PVWA') {
        $gate.StepSignals['certificate'] = @('Cert:' + $id)
        $gate.StepSignals['register'] = @('Registered:' + $id)
        $gate.DoneSignals = @(('Cert:' + $id), ('Registered:' + $id), ('Done:' + $id))
        if ($pvwas.Count -and $pvwas[0] -eq $id) {
            $gate.StepSignals['register'] += 'PvwaRegistered'
            $gate.DoneSignals += 'PvwaRegistered'
        } else {
            $gate.VaultAfter += 'PvwaRegistered'
        }
    } elseif ($type -eq 'CPM') {
        # The CPM registration looks at the certificate of every PVWA that is registered in the Vault.
        if ($pvwas.Count) {
            $gate.VaultAfter += 'PvwaRegistered'
        }
        $gate.VaultAfter += @($pvwas | ForEach-Object { 'Registered:' + $_ })
        $gate.StepSignals['register'] = @('Registered:' + $id)
        $gate.DoneSignals = @('Registered:' + $id)
    } elseif ($type -eq 'PTA') {
        # The PTA configuration names the PVWA and signs in through it: the PVWA servers must be finished.
        $gate.VaultAfter += @($pvwas | ForEach-Object { 'Done:' + $_ })
        $gate.VaultAfter += @($cpms | ForEach-Object { 'Registered:' + $_ })
    } else {
        if ($pvwas.Count) {
            $gate.VaultAfter += 'PvwaRegistered'
        }
        $gate.VaultAfter += @($cpms | ForEach-Object { 'Registered:' + $_ })
    }
    return $gate
}

function New-PASLaneSignals {
    # The signals of one run. Those nothing in this run will ever set are set from the start.
    param([Parameter(Mandatory)]$Config)
    $signals = @{}
    foreach ($name in @('Domain', 'Vault', 'PvwaRegistered', 'Cancel')) {
        $signals[$name] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
    }
    $components = @($Config.components | Where-Object { $null -ne $_ })
    if (-not @($components | Where-Object { $_.type -eq 'AD' }).Count) {
        [void]$signals.Domain.Set()
    }
    if (-not @($components | Where-Object { $_.type -eq 'Vault' }).Count) {
        [void]$signals.Vault.Set()
    }
    $pvwas = @($components | Where-Object { $_.type -eq 'PVWA' })
    if (-not $pvwas.Count) {
        [void]$signals.PvwaRegistered.Set()
    }
    foreach ($pvwa in $pvwas) {
        $signals['Registered:' + [string]$pvwa.id] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
        $signals['Cert:' + [string]$pvwa.id] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
        $signals['Done:' + [string]$pvwa.id] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
    }
    foreach ($cpm in @($components | Where-Object { $_.type -eq 'CPM' })) {
        $signals['Registered:' + [string]$cpm.id] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
    }
    # Every signal that is a need has a second handle that says "this will never come".
    foreach ($name in @($signals.Keys | Where-Object { $_ -ne 'Cancel' -and $_ -notlike 'Registered:*' })) {
        $signals['Fail:' + $name] = New-Object System.Threading.ManualResetEvent -ArgumentList $false
    }
    return $signals
}

function Get-PASLaneGate {
    # The gate for the named signals: the handles to wait for, and those that end the wait (the run is being
    # taken down, or the lane that would have set one of the signals failed).
    param([hashtable]$Signals, [string[]]$Names)
    $known = @($Names | Where-Object { $_ -and $Signals.ContainsKey($_) })
    $cancel = @($Signals.Cancel) + @($known | Where-Object { $Signals.ContainsKey('Fail:' + $_) } | ForEach-Object { $Signals['Fail:' + $_] })
    return @{ Wait = @($known | ForEach-Object { $Signals[$_] }); Cancel = $cancel }
}

function Wait-PASLaneSignal {
    # Waits until the named signals are set. Throws PASLaneStopped when one of them will never come.
    param([hashtable]$Signals, [string[]]$Names)
    [void](Wait-PASVaultGate (Get-PASLaneGate $Signals $Names))
}

function Get-PASLaneOwedSignals {
    # The names of the signals a lane sets when its work succeeds.
    param([Parameter(Mandatory)]$Config, [Parameter(Mandatory)][hashtable]$Lane)
    if ($Lane.Kind -eq 'AD') {
        return @('Domain')
    }
    $names = @()
    if ($Lane.Kind -eq 'Vault') {
        $names += 'Vault'
    }
    foreach ($id in (@($Lane.Components) + @(Get-PASValue $Lane 'After' @()))) {
        $component = @($Config.components | Where-Object { $_.id -eq $id })[0]
        if (-not $component -or [string]$component.type -in @('AD', 'Vault', 'DRVault')) {
            continue
        }
        $gate = Get-PASLaneComponentGate -Config $Config -Component $component
        $names += @($gate.DoneSignals)
        foreach ($step in @($gate.StepSignals.Keys)) {
            $names += @($gate.StepSignals[$step])
        }
    }
    return @($names | Where-Object { $_ } | Select-Object -Unique)
}

function Set-PASLaneFailed {
    # A lane ended with an error: what it still owed will never come. A signal that is only an order
    # (Registered:<id>) is set, so that who waited for its turn goes on; a signal that is a need gets its
    # Fail handle set, so that who waits for it gives up. Returns the names of the needs that failed.
    param([hashtable]$Signals, [string[]]$Owed)
    $failed = @()
    foreach ($name in @($Owed)) {
        if (-not $Signals.ContainsKey($name) -or $Signals[$name].WaitOne(0)) {
            continue
        }
        if ($Signals.ContainsKey('Fail:' + $name)) {
            [void]$Signals['Fail:' + $name].Set()
            $failed += $name
        } else {
            [void]$Signals[$name].Set()
        }
    }
    return $failed
}

function Test-PASLaneFinished {
    <# $true when everything of a lane was installed and verified by this run already (a continued run): every
       engine target of the lane has a journal that says Succeeded. Such a lane is not run again: its servers
       are left alone, and what it owes the others counts as done. Lanes that join a domain or install the
       forest are always run (their steps check themselves). #>

    param([Parameter(Mandatory)][hashtable]$Lane, [Parameter(Mandatory)]$Config, [Parameter(Mandatory)][string]$Directory)
    if ($Lane.Kind -eq 'AD' -or $Lane.Join) {
        return $false
    }
    $withDR = [bool]@($Config.components | Where-Object { $_.type -eq 'DRVault' }).Count
    $targets = @()
    if ($Lane.Kind -eq 'Vault') {
        $targets += 'primary'
        if ($withDR) {
            $targets += 'primary_final'
        }
    } elseif ($Lane.Kind -eq 'DR') {
        $targets += @('dr', 'dr_final')
    } else {
        $targets += @($Lane.Components)
    }
    $targets += @(Get-PASValue $Lane 'After' @())
    if (-not $targets.Count) {
        return $false
    }
    foreach ($target in $targets) {
        $statePath = Join-Path (Join-Path $Directory ([string]$target)) 'state.json'
        if (-not (Test-Path -LiteralPath $statePath -PathType Leaf)) {
            return $false
        }
        $state = $null
        try {
            $state = Get-Content -LiteralPath $statePath -Raw -Encoding UTF8 | ConvertFrom-Json
        } catch {
            return $false
        }
        if ([string]$state.status -ne 'Succeeded') {
            return $false
        }
    }
    return $true
}

function Invoke-PASLane {
    <# The work of one lane. Runs inside the lane's own runspace (see Invoke-PASRunLanes), with its own copy of
       the run context: its own configuration object, credential and secret tables.
       Returns @{ Lane; Error; Target; Component; Stopped }: Error is '' when the lane finished. #>

    param([Parameter(Mandatory)][hashtable]$Lane, [Parameter(Mandatory)][hashtable]$Shared)
    $result = @{ Lane = [string]$Lane.Name; Error = ''; Target = ''; Component = ''; Stopped = $false }
    $run = $null
    try {
        $signals = $Shared.Signals
        $config = Read-PASConfiguration -Path (Join-Path $Shared.Directory 'config.json')
        $script:PASFrozenMedia = @{}
        foreach ($path in @($Shared.FrozenMedia.Keys)) {
            $script:PASFrozenMedia[[string]$path] = [string]$Shared.FrozenMedia[$path]
        }
        $run = @{
            RunId = $Shared.RunId
            DataRoot = $Shared.DataRoot
            Directory = $Shared.Directory
            Config = $config
            Credentials = $Shared.Credentials.Clone()
            Secrets = $Shared.Secrets.Clone()
            Record = @{ currentComponent = '' }
            RecordPath = ''
            Target = ''
            SshReady = @{}
            RetryFailedPreflight = [bool]$Shared.RetryFailedPreflight
            Retry = $Shared.Retry
            Lane = [string]$Lane.Name
            Current = $Shared.Current
            VaultGate = $null
            StepSignals = @{}
        }
        if ($Lane.Kind -eq 'AD') {
            Invoke-PASActiveDirectoryForest -Run $run
            [void]$signals.Domain.Set()
        }
        if ($Lane.Kind -eq 'Vault') {
            Write-PASRunEvent $run.Directory 'VaultSequence' 'Starting the installation of the Primary Vault'
            # The installation of the Primary holds the Vault lock; the steps of the other servers that sign in
            # to the Vault wait for the signal below and then for this lock.
            $vaultLock = Enter-PASVaultLock -Address ([string]$config.vault.address) -TimeoutSeconds 14400 -Cancel $signals.Cancel
            try {
                Invoke-PASVaultSequence -Run $run -Part Primary
            } finally {
                Exit-PASVaultLock $vaultLock
            }
            foreach ($id in @($Lane.Components)) {
                $Shared.Installed[[string]$id] = $true
            }
            # From here the other servers register, one at a time.
            [void]$signals.Vault.Set()
            # With a DR Vault: the test safe of the replication test, one short sign-in in that same queue.
            # With it the Primary Vault is finished; the DR Vault looks for the safe when it replicates.
            $run.VaultGate = @{ Wait = @(); Cancel = @($signals.Cancel) }
            Invoke-PASVaultSequence -Run $run -Part PrimaryTest
            $run.VaultGate = $null
        }
        if ($Lane.Kind -eq 'DR') {
            Write-PASRunEvent $run.Directory 'VaultSequence' 'Starting the installation of the DR Vault (next to the Primary Vault)' ([string]$Lane.Components[0])
            # Its own steps wait for the milestones of the Primary; the last one waits until the test safe the
            # Primary created has arrived here.
            $run.VaultGate = @{ Wait = @(); Cancel = @($signals.Cancel, $signals['Fail:Vault']) }
            Invoke-PASVaultSequence -Run $run -Part DR
            $run.VaultGate = $null
            foreach ($id in @($Lane.Components)) {
                $Shared.Installed[[string]$id] = $true
            }
        }
        if ($Lane.Join) {
            Wait-PASLaneSignal $signals @('Domain')
            Invoke-PASDomainJoins -Run $run -OnlyHost ([string]$Lane.Name)
        }
        foreach ($id in (@($Lane.Components) + @(Get-PASValue $Lane 'After' @()))) {
            $component = @($config.components | Where-Object { $_.id -eq $id })[0]
            if ([string]$component.type -in @('AD', 'Vault', 'DRVault')) {
                continue
            }
            if ($signals.Cancel.WaitOne(0)) {
                throw 'PASLaneStopped: the run is being stopped. This component was not started.'
            }
            $gate = Get-PASLaneComponentGate -Config $config -Component $component
            if ($gate.StartAfter.Count) {
                $waitsFor = @($gate.StartAfter | Where-Object { $signals.ContainsKey($_) -and -not $signals[$_].WaitOne(0) })
                if ($waitsFor.Count) {
                    $run.Current[[string]$Lane.Name] = [string]$id
                    Write-PASRunEvent $run.Directory 'ComponentWaiting' 'Waiting for the PVWA certificate before this component starts' ([string]$id)
                }
                Wait-PASLaneSignal $signals $gate.StartAfter
            }
            $run.VaultGate = Get-PASLaneGate $signals $gate.VaultAfter
            $run.StepSignals = @{}
            foreach ($step in @($gate.StepSignals.Keys)) {
                $run.StepSignals[$step] = @($gate.StepSignals[$step] | ForEach-Object { $signals[$_] })
            }
            Invoke-PASComponent -Run $run -Component $component
            $Shared.Installed[[string]$id] = $true
            foreach ($name in @($gate.DoneSignals)) {
                [void]$signals[$name].Set()
            }
        }
    } catch {
        $result.Error = [string]$_.Exception.Message
        $result.Stopped = ($result.Error -match 'PASLaneStopped:')
        if ($run) {
            $result.Target = [string]$run.Target
            $result.Component = [string]$run.Record.currentComponent
        }
    } finally {
        $Shared.Current.Remove([string]$Lane.Name)
        $script:PASFrozenMedia = $null
    }
    return $result
}

function Invoke-PASRunLanes {
    <# Starts every lane of the run in its own runspace and waits for all of them. While they work, run.json
       names the components that are being installed. When a lane fails, only what depends on it gives up
       (Set-PASLaneFailed); every other server is installed to the end.
       $Run.Installed then names the components that are installed.
       Throws the error of the lane that failed first; $Run.Target is then the engine target it failed in. #>

    param([Parameter(Mandatory)][hashtable]$Run, [Parameter(Mandatory)][object[]]$PlanItems)
    $lanes = @(Get-PASRunLanes -Config $Run.Config -PlanItems $PlanItems)
    if (-not $lanes.Count) {
        return
    }
    # Shared by the lanes: created once here so that no lane has to add to a table another lane reads.
    Add-PASDomainCredentialAlias -Config $Run.Config -Credentials $Run.Credentials
    foreach ($component in @($Run.Config.components | Where-Object { $_.type -eq 'PTA' })) {
        $root = Get-PASHostCredential $Run.Config $Run.Credentials ([string]$component.host) ([string]$component.id)
        if ($root -and $root.Password.Length) {
            $Run.Secrets['PTARoot-' + [string]$component.id] = $root.Password
        }
    }
    # The inventory file of the Vault workflows is shared by the Vault lane and the DR lane: written once, here.
    if (@($Run.Config.components | Where-Object { $_.type -eq 'Vault' }).Count) {
        Invoke-PASVaultSequence -Run $Run -Part Prepare
    }
    $frozen = @{}
    if ($script:PASFrozenMedia) {
        foreach ($path in @($script:PASFrozenMedia.Keys)) {
            $frozen[$path] = $script:PASFrozenMedia[$path]
        }
    }
    $shared = @{
        RunId = $Run.RunId
        DataRoot = $Run.DataRoot
        Directory = $Run.Directory
        Credentials = $Run.Credentials
        Secrets = $Run.Secrets
        RetryFailedPreflight = $Run.RetryFailedPreflight
        Retry = $Run.Retry
        FrozenMedia = $frozen
        Signals = (New-PASLaneSignals -Config $Run.Config)
        Current = [hashtable]::Synchronized(@{})
        Installed = [hashtable]::Synchronized(@{})
    }
    $manifest = Join-Path (Get-PASModuleRoot) 'PASeasyInstall.psd1'
    # A continued run: what is installed and verified already is left alone. Only the rest is started.
    $finished = @($lanes | Where-Object { Test-PASLaneFinished -Lane $_ -Config $Run.Config -Directory $Run.Directory })
    foreach ($lane in $finished) {
        foreach ($name in @(Get-PASLaneOwedSignals -Config $Run.Config -Lane $lane)) {
            if ($shared.Signals.ContainsKey($name)) {
                [void]$shared.Signals[$name].Set()
            }
        }
        foreach ($id in (@($lane.Components) + @(Get-PASValue $lane 'After' @()))) {
            $shared.Installed[[string]$id] = $true
        }
    }
    $lanes = @($lanes | Where-Object { $finished -notcontains $_ })
    if ($finished.Count) {
        Write-PASRunEvent $Run.Directory 'LanesSkipped' ('Already installed and verified in this run, left as they are: ' + (($finished | ForEach-Object { $_.Name }) -join ', '))
    }
    if (-not $lanes.Count) {
        $Run.Installed = @($shared.Installed.Keys | ForEach-Object { [string]$_ })
        foreach ($signal in @($shared.Signals.Values)) {
            $signal.Dispose()
        }
        return
    }
    Write-PASRunEvent $Run.Directory 'LanesStarted' ('Installing ' + $lanes.Count + ' server lane(s) at the same time: ' + (($lanes | ForEach-Object { $_.Name }) -join ', '))
    $workers = @()
    try {
        foreach ($lane in $lanes) {
            $runspace = [runspacefactory]::CreateRunspace()
            $runspace.ThreadOptions = 'ReuseThread'
            $runspace.Open()
            $shell = [powershell]::Create()
            $shell.Runspace = $runspace
            [void]$shell.AddScript({
                    param([string]$Manifest, [hashtable]$Lane, [hashtable]$Shared)
                    $ErrorActionPreference = 'Stop'
                    Import-Module $Manifest -ErrorAction Stop
                    & (Get-Module -Name PASeasyInstall) {
                        param($Lane, $Shared)
                        Invoke-PASLane -Lane $Lane -Shared $Shared
                    } $Lane $Shared
                }).AddArgument($manifest).AddArgument($lane).AddArgument($shared)
            $workers += @{ Lane = $lane; Shell = $shell; Runspace = $runspace; Handle = $shell.BeginInvoke(); Result = $null }
        }
        $shown = ''
        $firstFailure = $null
        while (@($workers | Where-Object { $null -eq $_.Result }).Count) {
            foreach ($worker in @($workers | Where-Object { $null -eq $_.Result -and $_.Handle.IsCompleted })) {
                $worker.Result = Complete-PASLaneWorker $worker
                if ($worker.Result.Error) {
                    if (-not $worker.Result.Stopped -and -not $firstFailure) {
                        $firstFailure = $worker.Result
                    }
                    $owed = @(Get-PASLaneOwedSignals -Config $Run.Config -Lane $worker.Lane)
                    $needs = @(Set-PASLaneFailed -Signals $shared.Signals -Owed $owed)
                    $what = "Server lane '" + $worker.Result.Lane + "' stopped with an error."
                    if ($worker.Result.Stopped) {
                        $what = "Server lane '" + $worker.Result.Lane + "' is not installed, because a server it depends on failed."
                    }
                    if ($needs.Count) {
                        $what += ' What depends on it is not installed. Every other server is installed to the end.'
                    } else {
                        $what += ' Nothing else depends on it: every other server is installed to the end.'
                    }
                    Write-PASRunEvent $Run.Directory 'LaneFailed' $what ([string]$worker.Result.Component) '' 'Warning'
                }
            }
            $current = (@($shared.Current.Values | Sort-Object) -join ', ')
            if ($current -ne $shown) {
                $shown = $current
                $Run.Record.currentComponent = $current
                Write-PASJsonAtomic $Run.RecordPath $Run.Record
            }
            Start-Sleep -Milliseconds 500
        }
        if (-not $firstFailure) {
            $firstFailure = @($workers | Where-Object { $_.Result.Error } | ForEach-Object { $_.Result })[0]
        }
        $Run.Installed = @($shared.Installed.Keys | ForEach-Object { [string]$_ })
        if ($firstFailure) {
            $Run.Target = [string]$firstFailure.Target
            $Run.Record.currentComponent = [string]$firstFailure.Component
            $others = @($workers | Where-Object { $_.Result.Error -and -not $_.Result.Stopped -and $_.Result.Lane -ne $firstFailure.Lane } | ForEach-Object { [string]$_.Result.Lane })
            $skipped = @($workers | Where-Object { $_.Result.Error -and $_.Result.Stopped } | ForEach-Object { [string]$_.Result.Lane })
            $message = [string]$firstFailure.Error
            if ($others.Count) {
                $message += ' Also failed: ' + ($others -join ', ') + '.'
            }
            if ($skipped.Count) {
                $message += ' Not installed because of it: ' + ($skipped -join ', ') + '.'
            }
            throw $message
        }
        $Run.Target = ''
        $Run.Record.currentComponent = ''
    } finally {
        [void]$shared.Signals.Cancel.Set()
        foreach ($worker in $workers) {
            try {
                if (-not $worker.Handle.IsCompleted) {
                    $worker.Shell.Stop()
                }
            } catch {
                Write-Verbose 'lane stop failed'
            }
            $worker.Shell.Dispose()
            $worker.Runspace.Dispose()
        }
        foreach ($signal in @($shared.Signals.Values)) {
            $signal.Dispose()
        }
    }
}

function Complete-PASLaneWorker {
    # The result of a lane whose runspace has ended.
    param([hashtable]$Worker)
    $name = [string]$Worker.Lane.Name
    try {
        $output = @($Worker.Shell.EndInvoke($Worker.Handle))
        $result = @($output | Where-Object { $_ -is [hashtable] -and $_.ContainsKey('Lane') }) | Select-Object -Last 1
        if ($result) {
            return $result
        }
        $problem = 'The lane ended without a result.'
        if ($Worker.Shell.Streams.Error.Count) {
            $problem = [string]$Worker.Shell.Streams.Error[0]
        }
        return @{ Lane = $name; Error = $problem; Target = ''; Component = ''; Stopped = $false }
    } catch {
        return @{ Lane = $name; Error = [string]$_.Exception.Message; Target = ''; Component = ''; Stopped = $false }
    }
}