134 lines
6.4 KiB
Go
134 lines
6.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"text/template"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/preflight"
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
type preflightScope struct {
|
|
Pipeline string `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
|
|
Path string `json:"path,omitempty" yaml:"path,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"`
|
|
}
|
|
|
|
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))
|
|
|
|
func pipelinePreflightCommand(newState func() *nativeLoaders, modules func() (pipeline.Registries, error)) commandkit.Command {
|
|
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 {
|
|
return executePreflight(invocation, input, newState(), modules)
|
|
},
|
|
})
|
|
}
|
|
|
|
func executePreflight(invocation commandkit.Invocation, input pipelineRunInput, state *nativeLoaders, modules func() (pipeline.Registries, error)) commandkit.Outcome {
|
|
ctx := invocation.Context
|
|
loaded, failure := loadPipelineInvocation(ctx, invocation, input, state)
|
|
if failure != nil {
|
|
return *failure
|
|
}
|
|
report := preflightReport{Pipeline: input.PipelineID, Checks: []preflightCheck{}}
|
|
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 {
|
|
report.Ready = outcome.Status == 0
|
|
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 {
|
|
return commandkit.Failure("Could not present pipeline readiness.")
|
|
}
|
|
}
|
|
return outcome
|
|
}
|
|
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)
|
|
if failure != nil {
|
|
add("preparation", preflightScope{Pipeline: input.PipelineID}, "failed", failure.Explanation.Message)
|
|
return finish(*failure)
|
|
}
|
|
for _, diagnostic := range local.diagnostics {
|
|
for _, sample := range diagnostic.Samples {
|
|
message := sample.Message
|
|
if diagnostic.Disposition == contracts.DiagnosticDispositionWarning {
|
|
report.Checks = append(report.Checks, preflightCheck{ID: "reference", Scope: preflightScope{Path: sample.Scope}, Result: "warning", Code: diagnostic.ReasonCode, Message: message})
|
|
} else {
|
|
_ = invocation.Reporter().Info(message)
|
|
}
|
|
}
|
|
}
|
|
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 {
|
|
add("targets", preflightScope{Pipeline: input.PipelineID}, "failed", err.Error())
|
|
return finish(pipelineResourceFailure(err))
|
|
}
|
|
observations, err := preflight.RunSmokeChecks(ctx, selected.engine, targets, input.Overrides.Execution.Execution, input.Overrides.Execution.TransientRetries)
|
|
for _, observation := range observations {
|
|
if !observation.Applicable {
|
|
continue
|
|
}
|
|
scope := preflightScope{Profile: observation.ProfileID, Backend: observation.BackendID, Bindings: observation.Bindings}
|
|
if observation.Err != nil {
|
|
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))
|
|
}
|
|
}
|
|
if err != nil {
|
|
return finish(commandkit.Failure(err.Error()).WithCause(err))
|
|
}
|
|
return finish(commandkit.Success())
|
|
}
|