207 lines
8.1 KiB
Go
207 lines
8.1 KiB
Go
|
|
package notarius
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"path/filepath"
|
||
|
|
"sort"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ReadinessRequest selects the same invocation as execution, with isolated
|
||
|
|
// report paths and the lanes Narratio requires from the selected pipeline.
|
||
|
|
type ReadinessRequest struct {
|
||
|
|
RunRequest
|
||
|
|
RequiredLanes []string
|
||
|
|
InputDescription string
|
||
|
|
InspectionPath string
|
||
|
|
InspectionLogPath string
|
||
|
|
}
|
||
|
|
|
||
|
|
type ReadinessCheck struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
Scope json.RawMessage `json:"scope"`
|
||
|
|
Result string `json:"result"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
Code string `json:"code,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type ReadinessResult struct {
|
||
|
|
Pipeline string
|
||
|
|
Lanes []string
|
||
|
|
InspectionComplete bool
|
||
|
|
AssessmentComplete bool
|
||
|
|
Ready bool
|
||
|
|
Checks []ReadinessCheck
|
||
|
|
DebugDirectory string
|
||
|
|
InputDescription string
|
||
|
|
InputPath string
|
||
|
|
ExitCode int
|
||
|
|
}
|
||
|
|
|
||
|
|
// ReadinessChecker provides native pipeline inspection and preflight.
|
||
|
|
type ReadinessChecker interface {
|
||
|
|
Preflight(context.Context, ReadinessRequest) (ReadinessResult, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *SubprocessRunner) Preflight(ctx context.Context, req ReadinessRequest) (ReadinessResult, error) {
|
||
|
|
result := ReadinessResult{Pipeline: req.PipelineID, InputDescription: req.InputDescription, InputPath: req.InputPath}
|
||
|
|
if r == nil || r.run == nil {
|
||
|
|
return result, fmt.Errorf("notarius subprocess runner is nil")
|
||
|
|
}
|
||
|
|
references, err := validateRunRequest(req.RunRequest)
|
||
|
|
if err != nil {
|
||
|
|
return result, err
|
||
|
|
}
|
||
|
|
for _, path := range []string{req.InspectionPath, req.InspectionLogPath} {
|
||
|
|
if !filepath.IsAbs(path) {
|
||
|
|
return result, fmt.Errorf("notarius inspection paths must be absolute")
|
||
|
|
}
|
||
|
|
if err := validateLogDestination(path); err != nil {
|
||
|
|
return result, fmt.Errorf("notarius inspection destination: %w", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
seenPaths := map[string]bool{}
|
||
|
|
for _, path := range []string{req.InspectionPath, req.InspectionLogPath, req.ReceiptPath, req.LogPath} {
|
||
|
|
path = filepath.Clean(path)
|
||
|
|
if seenPaths[path] {
|
||
|
|
return result, fmt.Errorf("notarius readiness report and diagnostic paths must be distinct")
|
||
|
|
}
|
||
|
|
seenPaths[path] = true
|
||
|
|
}
|
||
|
|
env, names, err := subprocess.CredentialEnvironment(req.Credentials)
|
||
|
|
if err != nil {
|
||
|
|
return result, fmt.Errorf("notarius credentials: %w", err)
|
||
|
|
}
|
||
|
|
inspectArgs := []string{"pipeline", "show", req.PipelineID, "--config", req.ConfigPath}
|
||
|
|
if req.ConfigOverlay != nil {
|
||
|
|
inspectArgs = append(inspectArgs, "--overlay", *req.ConfigOverlay)
|
||
|
|
}
|
||
|
|
inspectArgs = append(inspectArgs, "--format", "json")
|
||
|
|
inspection, inspectErr := r.run(ctx, subprocess.RunRequest{Executable: req.Binary, Args: inspectArgs, WorkingDir: req.WorkingDirectory, Timeout: req.Timeout, DiagnosticOwner: "notarius pipeline inspection", StdoutLogPath: req.InspectionPath, StderrLogPath: req.InspectionLogPath, EnvOverrides: env, SensitiveEnvNames: names})
|
||
|
|
lanes, reportErr := loadPipelineLanes(req.InspectionPath, req.PipelineID)
|
||
|
|
if reportErr == nil {
|
||
|
|
result.InspectionComplete, result.Lanes = true, lanes
|
||
|
|
}
|
||
|
|
if inspectErr != nil {
|
||
|
|
return result, fmt.Errorf("notarius pipeline inspection: %w", errors.Join(inspectErr, reportErr))
|
||
|
|
}
|
||
|
|
if inspection.ExitCode != 0 {
|
||
|
|
return result, fmt.Errorf("notarius pipeline inspection exited with status %d", inspection.ExitCode)
|
||
|
|
}
|
||
|
|
if reportErr != nil {
|
||
|
|
return result, fmt.Errorf("notarius pipeline inspection incomplete: %w", reportErr)
|
||
|
|
}
|
||
|
|
available := make(map[string]bool, len(lanes))
|
||
|
|
for _, lane := range lanes {
|
||
|
|
available[lane] = true
|
||
|
|
}
|
||
|
|
for _, required := range req.RequiredLanes {
|
||
|
|
if !available[required] {
|
||
|
|
return result, fmt.Errorf("notarius pipeline %q does not define required lane %q", req.PipelineID, required)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
outcome, runErr := r.run(ctx, subprocess.RunRequest{Executable: req.Binary, Args: runArgs("preflight", req.RunRequest, references), WorkingDir: req.WorkingDirectory, Timeout: req.Timeout, DiagnosticOwner: "notarius preflight", StdoutLogPath: req.ReceiptPath, StderrLogPath: req.LogPath, EnvOverrides: env, SensitiveEnvNames: names})
|
||
|
|
result.ExitCode = outcome.ExitCode
|
||
|
|
assessment, assessmentErr := loadReadinessAssessment(req.ReceiptPath, req.PipelineID)
|
||
|
|
if assessmentErr == nil {
|
||
|
|
result.AssessmentComplete = true
|
||
|
|
result.Ready, result.Checks, result.DebugDirectory = assessment.Ready, assessment.Checks, assessment.DebugDirectory
|
||
|
|
}
|
||
|
|
if runErr != nil {
|
||
|
|
return result, fmt.Errorf("notarius preflight: %w", errors.Join(runErr, assessmentErr))
|
||
|
|
}
|
||
|
|
if outcome.ExitCode != 0 {
|
||
|
|
return result, fmt.Errorf("notarius preflight exited with status %d", outcome.ExitCode)
|
||
|
|
}
|
||
|
|
if assessmentErr != nil {
|
||
|
|
return result, fmt.Errorf("notarius preflight assessment incomplete: %w", assessmentErr)
|
||
|
|
}
|
||
|
|
if !result.Ready {
|
||
|
|
return result, fmt.Errorf("notarius pipeline %q is not ready", req.PipelineID)
|
||
|
|
}
|
||
|
|
for _, check := range result.Checks {
|
||
|
|
if check.Result == "failed" {
|
||
|
|
return result, fmt.Errorf("notarius preflight reported failed check %q: %s", check.ID, check.Message)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
type inspectedModule struct {
|
||
|
|
Module string `json:"module"`
|
||
|
|
ExecutionClass string `json:"execution_class"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func completeInspectedModule(module *inspectedModule) bool {
|
||
|
|
return module != nil && strings.TrimSpace(module.Module) != "" && strings.TrimSpace(module.ExecutionClass) != ""
|
||
|
|
}
|
||
|
|
|
||
|
|
func loadPipelineLanes(path, pipelineID string) ([]string, error) {
|
||
|
|
var doc struct {
|
||
|
|
Pipeline string `json:"pipeline"`
|
||
|
|
Input *inspectedModule `json:"input"`
|
||
|
|
Chunk *inspectedModule `json:"chunk"`
|
||
|
|
Output *inspectedModule `json:"output"`
|
||
|
|
Steps *[]struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
Lanes *[]struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
ArtifactKind string `json:"artifact_kind"`
|
||
|
|
Extract *inspectedModule `json:"extract"`
|
||
|
|
Merge *inspectedModule `json:"merge"`
|
||
|
|
Normalize *inspectedModule `json:"normalize"`
|
||
|
|
} `json:"lanes"`
|
||
|
|
} `json:"steps"`
|
||
|
|
}
|
||
|
|
if err := decodeBoundedJSON(path, maxReceiptBytes, &doc); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if doc.Pipeline != pipelineID || doc.Steps == nil || !completeInspectedModule(doc.Input) || !completeInspectedModule(doc.Chunk) || !completeInspectedModule(doc.Output) {
|
||
|
|
return nil, fmt.Errorf("missing or incompatible pipeline inspection")
|
||
|
|
}
|
||
|
|
seen := map[string]bool{}
|
||
|
|
var lanes []string
|
||
|
|
for _, step := range *doc.Steps {
|
||
|
|
if strings.TrimSpace(step.ID) == "" || step.Lanes == nil {
|
||
|
|
return nil, fmt.Errorf("incomplete pipeline step inspection")
|
||
|
|
}
|
||
|
|
for _, lane := range *step.Lanes {
|
||
|
|
if strings.TrimSpace(lane.ID) == "" || strings.TrimSpace(lane.ArtifactKind) == "" || !completeInspectedModule(lane.Extract) || !completeInspectedModule(lane.Merge) || !completeInspectedModule(lane.Normalize) || seen[lane.ID] {
|
||
|
|
return nil, fmt.Errorf("incomplete or duplicate lane inspection")
|
||
|
|
}
|
||
|
|
seen[lane.ID] = true
|
||
|
|
lanes = append(lanes, lane.ID)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
sort.Strings(lanes)
|
||
|
|
return lanes, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func loadReadinessAssessment(path, pipelineID string) (ReadinessResult, error) {
|
||
|
|
var doc struct {
|
||
|
|
Pipeline string `json:"pipeline"`
|
||
|
|
Ready *bool `json:"ready"`
|
||
|
|
Checks *[]ReadinessCheck `json:"checks"`
|
||
|
|
DebugDirectory string `json:"debug_directory"`
|
||
|
|
}
|
||
|
|
if err := decodeBoundedJSON(path, maxReceiptBytes, &doc); err != nil {
|
||
|
|
return ReadinessResult{}, err
|
||
|
|
}
|
||
|
|
if doc.Pipeline != pipelineID || doc.Ready == nil || doc.Checks == nil || (doc.DebugDirectory != "" && !filepath.IsAbs(doc.DebugDirectory)) {
|
||
|
|
return ReadinessResult{}, fmt.Errorf("missing or incompatible readiness assessment")
|
||
|
|
}
|
||
|
|
for _, check := range *doc.Checks {
|
||
|
|
var scope map[string]json.RawMessage
|
||
|
|
scopeErr := json.Unmarshal(check.Scope, &scope)
|
||
|
|
if strings.TrimSpace(check.ID) == "" || strings.TrimSpace(check.Message) == "" || scopeErr != nil || scope == nil || (check.Result != "passed" && check.Result != "failed" && check.Result != "warning") {
|
||
|
|
return ReadinessResult{}, fmt.Errorf("incomplete readiness check")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ReadinessResult{Pipeline: doc.Pipeline, Ready: *doc.Ready, Checks: *doc.Checks, DebugDirectory: doc.DebugDirectory}, nil
|
||
|
|
}
|