# Conflicts: # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline-tools/main_test.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/findingwriter.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go # modules/mesh-delivery-pipeline/go.mod # modules/mesh-delivery-pipeline/internal/pipeline/read_test.go # modules/mesh-delivery-pipeline/internal/pipeline/start.go # modules/mesh-delivery-pipeline/manifest_test.go # modules/mesh-delivery-pipeline/module.json
241 lines
7.7 KiB
Go
241 lines
7.7 KiB
Go
// Package pipeline is the pipeline runner's own logic, holder of the seat mesh-pipeline-runner (design 47 G §21). It names
|
|
// no module, branch or repository (§7): every name comes from what it reads.
|
|
package pipeline
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
)
|
|
|
|
// Kept is one of the runner's old state sections as it reads them: every key, and one key's value (nil when absent).
|
|
type Kept interface {
|
|
Keys() ([]string, error)
|
|
Value(key string) (json.RawMessage, error)
|
|
}
|
|
|
|
// Reads answers the runner's read verbs (§83, §306) from its own states pipelines and kept, never another part's.
|
|
type Reads struct {
|
|
Pipelines Kept
|
|
Kept Kept
|
|
// Fill is each of the runner's stores' fill now, pipelines, step-outputs, kept and step-log, read through the bundle's calls
|
|
// (the state's `mesh/state.info`, the object store's and the log's fill; design 47 G §366, §623, A-214); nil says none.
|
|
Fill func() ([]seat.Fill, error)
|
|
}
|
|
|
|
// fills is the stores' fill, or an error saying it could not be read: an answer never leaves it out unsaid.
|
|
func (r Reads) fills() ([]seat.Fill, error) {
|
|
if r.Fill == nil {
|
|
return nil, nil
|
|
}
|
|
f, err := r.Fill()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the stores' fill cannot be read: %w", err)
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
// Show is one pipeline whole, with its steps and the names of their outputs in step-outputs (§83).
|
|
func (r Reads) Show(in seat.ShowInput) (seat.ShowAnswer, error) {
|
|
p, ok, err := r.pipeline(in.Pipeline)
|
|
if err != nil {
|
|
return seat.ShowAnswer{}, err
|
|
}
|
|
if !ok {
|
|
return seat.ShowAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
|
|
}
|
|
fills, err := r.fills()
|
|
if err != nil {
|
|
return seat.ShowAnswer{}, err
|
|
}
|
|
a := seat.ShowAnswer{Pipeline: p, Fill: fills}
|
|
for _, s := range p.Steps {
|
|
if s.Output != "" {
|
|
a.Outputs = append(a.Outputs, s.Output)
|
|
}
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// List is the pipelines and runs of a module, a branch, a commit or a run, newest started first, at most Limit when it
|
|
// names one (§83).
|
|
func (r Reads) List(in seat.ListInput) (seat.ListAnswer, error) {
|
|
all, err := r.pipelines()
|
|
if err != nil {
|
|
return seat.ListAnswer{}, err
|
|
}
|
|
out := []seat.Pipeline{}
|
|
for _, p := range all {
|
|
if (in.Module == "" || p.Module == in.Module) && (in.Branch == "" || p.Branch == in.Branch) &&
|
|
(in.Commit == "" || p.Commit == in.Commit) && (in.Run == "" || p.Run == in.Run) {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return startedOf(out[i]).After(startedOf(out[j])) })
|
|
if in.Limit > 0 && int64(len(out)) > in.Limit {
|
|
out = out[:in.Limit]
|
|
}
|
|
return seat.ListAnswer{Pipelines: out}, nil
|
|
}
|
|
|
|
// Stands is every step that has started and not ended, with what it waits on and since when (§75, §83): the watchman
|
|
// seeds its open waits from it.
|
|
func (r Reads) Stands() (seat.StandsAnswer, error) {
|
|
all, err := r.pipelines()
|
|
if err != nil {
|
|
return seat.StandsAnswer{}, err
|
|
}
|
|
out := []seat.Wait{}
|
|
for _, p := range all {
|
|
for _, s := range p.Steps {
|
|
if s.Ended != nil || s.Started == nil {
|
|
continue
|
|
}
|
|
if s.Outcome != seat.StepResultOutcomeRunning && s.Outcome != seat.StepResultOutcomeWaiting {
|
|
continue
|
|
}
|
|
out = append(out, seat.Wait{Pipeline: p.Pipeline, Module: p.Module, Branch: p.Branch, Commit: p.Commit,
|
|
Group: p.Group, Run: p.Run, Source: p.Source, Revision: p.Revision, Fingerprint: p.Fingerprint, Step: s.Step,
|
|
Artifact: s.Artifact, On: s.On, Since: *s.Started, V: 1})
|
|
}
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Since.Before(out[j].Since) })
|
|
return seat.StandsAnswer{Stands: out}, nil
|
|
}
|
|
|
|
// Waits is the pipelines held for the runner's new version, each as the wait of step `pipeline` it says (design 47 G §266, §276,
|
|
// §534, A-195): the acting part awaited by its fingerprint, since when, and the pipeline.
|
|
func (r Reads) Waits() (seat.StandsAnswer, error) {
|
|
all, err := r.pipelines()
|
|
if err != nil {
|
|
return seat.StandsAnswer{}, err
|
|
}
|
|
out := []seat.Wait{}
|
|
for _, p := range all {
|
|
if p.Held == nil || p.Outcome != seat.PipelineOutcomeRunning {
|
|
continue
|
|
}
|
|
out = append(out, seat.Wait{Pipeline: p.Pipeline, Module: p.Module, Branch: p.Branch, Commit: p.Commit, Group: p.Group, Source: p.Source,
|
|
Revision: p.Revision, Fingerprint: p.Held.Fingerprint, Step: seat.WaitStepPipeline, On: seat.WaitOnBuildNode, Since: p.Held.Since, V: 1})
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].Since.Before(out[j].Since) })
|
|
return seat.StandsAnswer{Stands: out}, nil
|
|
}
|
|
|
|
// WhyCarried is where a step's result was carried from (§25, §83): the pipeline it names.
|
|
func (r Reads) WhyCarried(in seat.WhyCarriedInput) (seat.WhyCarriedAnswer, error) {
|
|
p, ok, err := r.pipeline(in.Pipeline)
|
|
if err != nil {
|
|
return seat.WhyCarriedAnswer{}, err
|
|
}
|
|
if !ok {
|
|
return seat.WhyCarriedAnswer{}, fmt.Errorf("no pipeline %q in %s", in.Pipeline, seat.StatePipelines)
|
|
}
|
|
for _, s := range p.Steps {
|
|
if s.Step != in.Step || (in.Artifact != "" && s.Artifact != in.Artifact) {
|
|
continue
|
|
}
|
|
if s.Outcome != seat.StepResultOutcomeCarried {
|
|
return seat.WhyCarriedAnswer{}, fmt.Errorf("step %s of %s %s was not carried: it ended %q", s.Step,
|
|
in.Pipeline, s.Artifact, s.Outcome)
|
|
}
|
|
return seat.WhyCarriedAnswer{CarriedFrom: s.CarriedFrom}, nil
|
|
}
|
|
return seat.WhyCarriedAnswer{}, fmt.Errorf("pipeline %q has no step %s %s", in.Pipeline, in.Step, in.Artifact)
|
|
}
|
|
|
|
// KeptResults is the kept passing results, by level, digest or package (§83).
|
|
func (r Reads) KeptResults(in seat.KeptInput) (seat.KeptAnswer, error) {
|
|
keys, err := r.Kept.Keys()
|
|
if err != nil {
|
|
return seat.KeptAnswer{}, fmt.Errorf("%s cannot be read: %w", seat.StateKept, err)
|
|
}
|
|
sort.Strings(keys)
|
|
out := []seat.KeptResult{}
|
|
for _, k := range keys {
|
|
level, digest, ok := seat.ParseKeptByLevelDigestKey(k)
|
|
if !ok || (in.Level != "" && level != in.Level) || (in.Digest != "" && digest != in.Digest) {
|
|
continue
|
|
}
|
|
raw, err := r.Kept.Value(k)
|
|
if err != nil {
|
|
return seat.KeptAnswer{}, fmt.Errorf("%s %s cannot be read: %w", seat.StateKept, k, err)
|
|
}
|
|
if raw == nil {
|
|
continue
|
|
}
|
|
v, err := seat.DecodeKeptByLevelDigestValue(raw)
|
|
if err != nil {
|
|
return seat.KeptAnswer{}, fmt.Errorf("%s %s: %w", seat.StateKept, k, err)
|
|
}
|
|
if in.Package != "" && v.Package != in.Package {
|
|
continue
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
fills, err := r.fills()
|
|
if err != nil {
|
|
return seat.KeptAnswer{}, err
|
|
}
|
|
a := seat.KeptAnswer{Kept: out}
|
|
for _, f := range fills {
|
|
if f.Store == seat.StateKept {
|
|
f := f
|
|
a.Fill = &f
|
|
}
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (r Reads) pipeline(key string) (seat.Pipeline, bool, error) {
|
|
key = strings.TrimSpace(key)
|
|
if key == "" {
|
|
return seat.Pipeline{}, false, fmt.Errorf("name the pipeline's key, <branch>.<revision>.<module>")
|
|
}
|
|
raw, err := r.Pipelines.Value(key)
|
|
if err != nil {
|
|
return seat.Pipeline{}, false, fmt.Errorf("%s %s cannot be read: %w", seat.StatePipelines, key, err)
|
|
}
|
|
if raw == nil {
|
|
return seat.Pipeline{}, false, nil
|
|
}
|
|
p, err := seat.DecodePipelinesByBranchRevisionModuleValue(raw)
|
|
if err != nil {
|
|
return seat.Pipeline{}, false, fmt.Errorf("%s %s: %w", seat.StatePipelines, key, err)
|
|
}
|
|
if p.Pipeline == "" {
|
|
p.Pipeline = key
|
|
}
|
|
return p, true, nil
|
|
}
|
|
|
|
func (r Reads) pipelines() ([]seat.Pipeline, error) {
|
|
keys, err := r.Pipelines.Keys()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s cannot be read: %w", seat.StatePipelines, err)
|
|
}
|
|
sort.Strings(keys)
|
|
var out []seat.Pipeline
|
|
for _, k := range keys {
|
|
p, ok, err := r.pipeline(k)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ok {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func startedOf(p seat.Pipeline) time.Time {
|
|
if p.Started == nil {
|
|
return time.Time{}
|
|
}
|
|
return *p.Started
|
|
}
|