Private/Invoke-KadenOperatorModelApplyCore.ps1
|
function Invoke-KadenOperatorModelApplyCore { <# .SYNOPSIS Apply operator model choices onto managed agent frontmatter. .DESCRIPTION Reads .kaden.model.json, writes model frontmatter only for Kaden-managed agents on declared stacks, and records the outcome. A missing managed agent is a skip. A managed agent that cannot be tuned is a partway failure when another file was tuned. The receipt names each tuned file, the value applied, and the operator input it came from. Never writes the operator file. Does not throw for a missing or unreadable operator file. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$ChildRoot ) $operatorRelative = '.kaden.model.json' $operatorPath = Join-Path $ChildRoot $operatorRelative $before = Get-KadenOperatorFileFingerprint -LiteralPath $operatorPath $parsed = Read-KadenOperatorModelFile -LiteralPath $operatorPath $tunedFiles = New-Object System.Collections.Generic.List[object] $notTuned = New-Object System.Collections.Generic.List[object] $changed = $false $status = [string]$parsed.Status $reason = [string]$parsed.Reason $stackReports = @($parsed.Reports) if ($status -eq 'ready') { $payload = Get-KadenInstalledManagedManifest if (-not $payload.Valid) { $status = 'failed' $reason = [string]$payload.Message } else { $byStack = @{} foreach ($report in @($parsed.Reports)) { $byStack[[string]$report.Name] = $report } $destinationsByStack = Get-KadenOperatorModelDestinationsByStack -Manifest $payload.Manifest foreach ($report in @($parsed.Reports)) { if ($report.Disposition -ne 'tuned') { continue } $destinations = @() $stackName = [string]$report.Name if ($destinationsByStack.ContainsKey($stackName)) { $destinations = @($destinationsByStack[$stackName].ToArray()) } $written = Invoke-KadenOperatorModelStackWrite -ChildRoot $ChildRoot -Report $report -Destinations $destinations if ($written.Changed) { $changed = $true } foreach ($file in @($written.Tuned)) { $tunedFiles.Add($file) } foreach ($missed in @($written.Missed)) { $notTuned.Add($missed) } $byStack[$stackName] = $written.Report } $rebuilt = New-Object System.Collections.Generic.List[object] foreach ($report in @($parsed.Reports)) { $rebuilt.Add($byStack[[string]$report.Name]) } $stackReports = @($rebuilt.ToArray()) $final = Get-KadenOperatorModelApplyFinalStatus -WriteFailures @($notTuned | Where-Object { $_.Kind -eq 'not-written' }).Count -TunedCount $tunedFiles.Count -NotTunedCount $notTuned.Count -Changed $changed $status = [string]$final.Status $reason = [string]$final.Reason } } $after = Get-KadenOperatorFileFingerprint -LiteralPath $operatorPath $outcome = New-KadenOperatorModelApplyOutcome -Status $status -Reason $reason -Path $ChildRoot -OperatorFile $operatorRelative -OperatorFileBytesUnchanged (Test-KadenOperatorFileBytesUnchanged -Before $before -After $after) -Changed $changed -Stacks $stackReports -TunedFiles @($tunedFiles.ToArray()) -NotTuned @($notTuned.ToArray()) try { Write-KadenOperatorModelOutcome -ChildRoot $ChildRoot -Outcome $outcome } catch { $outcome.Reason = "$($outcome.Reason); outcome file was not recorded: $($_.Exception.Message)" } return $outcome } |