2026-11-28 04:20:29 +00:00
package cli
import (
2026-11-28 04:45:29 +00:00
"errors"
2026-11-28 04:20:29 +00:00
"fmt"
2026-11-28 04:45:29 +00:00
"os"
2026-11-28 04:20:29 +00:00
"text/template"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
2026-11-28 04:26:30 +00:00
frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug"
2026-11-28 04:20:29 +00:00
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
"gitea.maximumdirect.net/eric/notarius/internal/preflight"
"go.maximumdirect.net/commandkit"
)
type preflightScope struct {
2026-11-28 04:45:29 +00:00
Pipeline string ` json:"pipeline,omitempty" yaml:"pipeline,omitempty" `
Path string ` json:"path,omitempty" yaml:"path,omitempty" `
Reference string ` json:"reference,omitempty" yaml:"reference,omitempty" `
Profile string ` json:"profile,omitempty" yaml:"profile,omitempty" `
Backend string ` json:"backend,omitempty" yaml:"backend,omitempty" `
Bindings [ ] preflight . BindingScope ` json:"bindings,omitempty" yaml:"bindings,omitempty" `
2026-11-28 04:20:29 +00:00
}
type preflightCheck struct {
ID string ` json:"id" yaml:"id" `
Scope preflightScope ` json:"scope" yaml:"scope" `
Result string ` json:"result" yaml:"result" `
Message string ` json:"message" yaml:"message" `
Code string ` json:"code,omitempty" yaml:"code,omitempty" `
}
type preflightReport struct {
Pipeline string ` json:"pipeline" yaml:"pipeline" `
Ready bool ` json:"ready" yaml:"ready" `
Checks [ ] preflightCheck ` json:"checks" yaml:"checks" `
DebugDirectory string ` json:"debug_directory,omitempty" yaml:"debug_directory,omitempty" `
}
var preflightTemplate = template . Must ( commandkit . ParseTextTemplate ( "pipeline-preflight" , ` Pipeline { { printf "%q" . Pipeline } } is { { if . Ready } } ready { { else } } not ready { { end } } .
{ { range . Checks } } { { if ne . Result "passed" } } { { . Result } } : { { . Message } }
{ { end } } { { end } } ` , nil ) )
2026-11-28 04:26:30 +00:00
func pipelinePreflightCommand ( version string , newState func ( ) * nativeLoaders , modules func ( ) ( pipeline . Registries , error ) , options Options ) commandkit . Command {
options = defaultOptions ( options )
2026-11-28 04:20:29 +00:00
return commandkit . NewCommand ( commandkit . CommandSpec [ pipelineRunInput ] { Name : "preflight" , Summary : "Check readiness for a configured pipeline invocation." , Flags : runFlags ( ) , Arguments : pipelineRunArguments ( ) , CheckInput : checkPipelineRunInput ,
Run : func ( invocation commandkit . Invocation , input pipelineRunInput ) commandkit . Outcome {
2026-11-28 04:26:30 +00:00
return executePreflight ( invocation , input , newState ( ) , modules , version , options )
2026-11-28 04:20:29 +00:00
} ,
} )
}
2026-11-28 04:26:30 +00:00
func executePreflight ( invocation commandkit . Invocation , input pipelineRunInput , state * nativeLoaders , modules func ( ) ( pipeline . Registries , error ) , version string , options Options ) commandkit . Outcome {
2026-11-28 04:20:29 +00:00
ctx := invocation . Context
loaded , failure := loadPipelineInvocation ( ctx , invocation , input , state )
if failure != nil {
return * failure
}
report := preflightReport { Pipeline : input . PipelineID , Checks : [ ] preflightCheck { } }
2026-11-28 04:26:30 +00:00
var capture * frameworkdebug . Recorder
2026-11-28 04:20:29 +00:00
add := func ( id string , scope preflightScope , result , message string ) {
report . Checks = append ( report . Checks , preflightCheck { ID : id , Scope : scope , Result : result , Message : message } )
}
finish := func ( outcome commandkit . Outcome ) commandkit . Outcome {
2026-11-28 04:26:30 +00:00
report . DebugDirectory = capture . Path ( )
if err := capture . Finalize ( report , outcome . Cause ) ; err != nil {
if outcome . Status == 0 {
outcome = commandkit . Failure ( "Could not complete debug capture." ) . WithCause ( err )
} else {
outcome = outcome . WithCause ( err )
}
}
if outcome . Status != 0 && capture . Path ( ) != "" {
outcome = outcome . WithExplanation ( outcome . Explanation . Message + runLocations ( "" , capture . Path ( ) ) , outcome . Explanation . Details ... )
}
2026-11-28 04:20:29 +00:00
for _ , check := range report . Checks {
if check . Result == "passed" {
_ = invocation . Reporter ( ) . Info ( check . Message )
}
}
if err := invocation . Reporter ( ) . Report ( commandkit . Report { Value : report , TextTemplate : preflightTemplate } ) ; err != nil {
if outcome . Status == 0 {
2026-11-28 04:26:30 +00:00
return commandkit . Failure ( "Could not present pipeline readiness." + runLocations ( "" , capture . Path ( ) ) )
2026-11-28 04:20:29 +00:00
}
}
return outcome
}
2026-11-28 04:26:30 +00:00
directory , err := input . Debug . directory ( loaded . config . Debug . Directory )
if err != nil {
add ( "debug" , preflightScope { Path : input . Debug . Directory . Value } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
if directory != "" {
startedAt := options . Now ( ) . UTC ( )
runID , err := options . RunIDGenerator ( startedAt )
if err == nil {
err = validateRunID ( runID )
}
if err == nil {
capture , err = startRunDebug ( input . Debug , loaded . config . Debug . Directory , runID , startedAt )
}
if err != nil {
add ( "debug" , preflightScope { Path : directory } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
add ( "debug" , preflightScope { Path : capture . Path ( ) } , "passed" , "Debug capture is accessible." )
if err := capture . WriteJSON ( "context.json" , map [ string ] any { "application_version" : version , "pipeline" : input . PipelineID , "input_path" : input . InputFile . Value , "run_id" : runID , "started_at" : startedAt , "lanes" : input . Lanes , "references" : input . References , "unbinds" : input . Unbinds , "resources" : input . Overrides . Resources , "execution_overrides" : input . Overrides . Execution } ) ; err != nil {
add ( "debug" , preflightScope { Path : capture . Path ( ) } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
if err := captureRunConfiguration ( capture , loaded . result ) ; err != nil {
add ( "debug" , preflightScope { Path : capture . Path ( ) } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
}
2026-11-28 04:20:29 +00:00
selected , failure := selectPipelineInvocation ( ctx , loaded , input , modules )
if failure != nil {
add ( "selection" , preflightScope { Pipeline : input . PipelineID } , "failed" , failure . Explanation . Message )
return finish ( * failure )
}
add ( "selection" , preflightScope { Pipeline : input . PipelineID } , "passed" , "Pipeline selection and resources are valid." )
local , failure := preparePipelineInvocation ( loaded , selected , input )
for _ , diagnostic := range local . diagnostics {
for _ , sample := range diagnostic . Samples {
message := sample . Message
if diagnostic . Disposition == contracts . DiagnosticDispositionWarning {
2026-11-28 04:45:29 +00:00
report . Checks = append ( report . Checks , preflightCheck { ID : "reference" , Scope : preflightScope { Reference : sample . Scope } , Result : "warning" , Code : diagnostic . ReasonCode , Message : message } )
2026-11-28 04:20:29 +00:00
} else {
_ = invocation . Reporter ( ) . Info ( message )
}
}
}
2026-11-28 04:45:29 +00:00
if failure != nil {
scope := preflightScope { Pipeline : input . PipelineID }
if local . failureTarget == "input" {
scope . Path = input . InputFile . Value
} else if local . failureTarget == "references" {
var pathErr * os . PathError
if errors . As ( failure . Cause , & pathErr ) {
scope . Path = pathErr . Path
}
}
add ( local . failureTarget , scope , "failed" , failure . Explanation . Message )
return finish ( * failure )
}
2026-11-28 04:20:29 +00:00
add ( "preparation" , preflightScope { Pipeline : input . PipelineID } , "passed" , "References and modules are prepared." )
if _ , err := local . prepared . ParseSource ( ctx , contracts . ParseRequest { Path : input . InputFile . Value , Raw : local . raw } ) ; err != nil {
add ( "input" , preflightScope { Path : input . InputFile . Value } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
add ( "input" , preflightScope { Path : input . InputFile . Value } , "passed" , "Source input is valid." )
outputErr , cacheErr := probePipelineDestinations ( loaded . config . Output . Directory , loaded . config . CacheDirectory , local . resolved )
if outputErr != nil {
add ( "output" , preflightScope { Path : loaded . config . Output . Directory } , "failed" , outputErr . Error ( ) )
return finish ( commandkit . Failure ( outputErr . Error ( ) ) . WithCause ( outputErr ) )
}
add ( "output" , preflightScope { Path : loaded . config . Output . Directory } , "passed" , "Output destination is accessible." )
if pipelineUsesCache ( local . resolved ) {
if cacheErr != nil {
add ( "cache" , preflightScope { Path : loaded . config . CacheDirectory } , "warning" , cacheErr . Error ( ) )
} else {
add ( "cache" , preflightScope { Path : loaded . config . CacheDirectory } , "passed" , "Cache destination is accessible." )
}
}
targets , err := preflight . DiscoverTargets ( ctx , selected . effective . ResolvedPipeline , selected . catalog , selected . engine )
if err != nil {
2026-11-28 04:45:29 +00:00
scope := preflightScope { Pipeline : input . PipelineID }
var targetErr * preflight . TargetError
if errors . As ( err , & targetErr ) {
scope . Bindings = [ ] preflight . BindingScope { targetErr . Scope }
}
add ( "targets" , scope , "failed" , err . Error ( ) )
2026-11-28 04:20:29 +00:00
return finish ( pipelineResourceFailure ( err ) )
}
2026-11-28 04:45:29 +00:00
if err := capture . WriteJSON ( "targets.json" , targets ) ; err != nil {
add ( "debug" , preflightScope { Path : capture . Path ( ) } , "failed" , err . Error ( ) )
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
2026-11-28 04:26:30 +00:00
evidence := newRunEvidenceRecorder ( capture , loaded . result )
evidenceIndex := 0
observations , err := preflight . RunSmokeChecksObserved ( ctx , selected . engine , targets , input . Overrides . Execution . Execution , input . Overrides . Execution . TransientRetries , func ( observation preflight . SmokeObservation ) error {
if observation . Report != nil {
err := evidence . WriteJSON ( fmt . Sprintf ( "smoke/%03d.json" , evidenceIndex ) , observation . Report )
evidenceIndex ++
return err
}
return nil
} )
failedCheck := false
2026-11-28 04:20:29 +00:00
for _ , observation := range observations {
if ! observation . Applicable {
continue
}
scope := preflightScope { Profile : observation . ProfileID , Backend : observation . BackendID , Bindings : observation . Bindings }
if observation . Err != nil {
2026-11-28 04:26:30 +00:00
failedCheck = true
2026-11-28 04:20:29 +00:00
add ( string ( observation . Kind ) , scope , "failed" , observation . Err . Error ( ) )
} else {
add ( string ( observation . Kind ) , scope , "passed" , fmt . Sprintf ( "%s check passed for profile %q." , observation . Kind , observation . ProfileID ) )
}
}
2026-11-28 16:10:49 +00:00
// Recording failure is independent of a completed diagnostic assessment.
report . Ready = ! failedCheck && len ( observations ) == 2 * len ( targets . Access ) + len ( targets . Profiles ) && ctx . Err ( ) == nil
2026-11-28 04:20:29 +00:00
if err != nil {
2026-11-28 16:10:49 +00:00
if ! failedCheck && ! report . Ready {
2026-11-28 04:45:29 +00:00
if capture . Err ( ) != nil {
add ( "debug" , preflightScope { Path : capture . Path ( ) } , "failed" , err . Error ( ) )
} else {
add ( "diagnostics" , preflightScope { Pipeline : input . PipelineID } , "failed" , err . Error ( ) )
}
2026-11-28 04:26:30 +00:00
}
2026-11-28 04:20:29 +00:00
return finish ( commandkit . Failure ( err . Error ( ) ) . WithCause ( err ) )
}
return finish ( commandkit . Success ( ) )
}