212 lines
10 KiB
Go
212 lines
10 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
|
nodebuildimage "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildimage"
|
|
nodebuildnode "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode"
|
|
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
// StepAsker puts one step's ask on a build seat's work queue, at the subject accept names (the seat's accepted kind `step`
|
|
// on its stream `check`, design 47 G §22, §24). An ask asked twice under one id is kept once.
|
|
type StepAsker interface {
|
|
AskStep(accept, id string, body any) error
|
|
}
|
|
|
|
// acceptStep is where each seat is sent its step, from its contract: a build seat's on its queue `check` under the runner's
|
|
// own seat name, `accept.check.mesh-pipeline-runner` (design 47 G §600, A-163, A-170); mesh-builds and mesh-wants, which are
|
|
// no build seats, on their accept subject, which names no asker (A-294).
|
|
var acceptStep = map[string]string{
|
|
builds.Seat: builds.AcceptSubjectStep,
|
|
wants.Seat: wants.AcceptSubjectStep,
|
|
nodebuildgo.Seat: nodebuildgo.AcceptSubjectCheckStep + "." + seat.Seat,
|
|
nodebuildnode.Seat: nodebuildnode.AcceptSubjectCheckStep + "." + seat.Seat,
|
|
nodebuildimage.Seat: nodebuildimage.AcceptSubjectCheckStep + "." + seat.Seat,
|
|
}
|
|
|
|
// acceptRecovery is where a recovery's step is sent on the build seats' queue `delivery`, under the runner's seat name, ahead
|
|
// of the first round (§86, §415, A-170); the seats mesh-builds and mesh-wants have no such queue, so their steps stay on their
|
|
// own accept subject.
|
|
var acceptRecovery = map[string]string{
|
|
nodebuildgo.Seat: nodebuildgo.AcceptSubjectDeliveryStep + "." + seat.Seat,
|
|
nodebuildnode.Seat: nodebuildnode.AcceptSubjectDeliveryStep + "." + seat.Seat,
|
|
nodebuildimage.Seat: nodebuildimage.AcceptSubjectDeliveryStep + "." + seat.Seat,
|
|
}
|
|
|
|
// buildSteps are the steps an artifact's build seat runs, in the order they are asked (design 47 G §22).
|
|
var buildSteps = []string{seat.StepResultStepLint, seat.StepResultStepUnit, seat.StepResultStepIntegration}
|
|
|
|
// moduleSteps are the steps of the whole module: composition (the assigner's), review (the runner's own verb) and the
|
|
// approval, asked last (§22, §26).
|
|
var moduleSteps = []string{seat.StepResultStepComposition, seat.StepResultStepReview, seat.StepResultStepApproval}
|
|
|
|
// AskID is a step's ask id in the usual ask id form the holders accept (buildstep.CheckAskID): base 36 of started in
|
|
// milliseconds, then six hex digits of the sha256 of the pipeline, the artifact and the step, so the same placement makes
|
|
// the same id at every start and the bus dedupes on it; started is the pipeline's start time, or a rerun's own (design 47
|
|
// G A-297).
|
|
func AskID(started time.Time, pipeline, artifact, step string) string {
|
|
sum := sha256.Sum256([]byte(pipeline + "\n" + artifact + "\n" + step))
|
|
return strconv.FormatInt(started.UnixMilli(), 36) + hex.EncodeToString(sum[:3])
|
|
}
|
|
|
|
// startOf is a pipeline's start time, the time its step ask ids open with (A-297); now for a pipeline that records none.
|
|
func startOf(p seat.Pipeline, now time.Time) time.Time {
|
|
if p.Started != nil {
|
|
return *p.Started
|
|
}
|
|
return now
|
|
}
|
|
|
|
// Place places a pipeline's steps (design 47 G §21, §23, §323; A-91): only the steps the module's registration policy
|
|
// names, each artifact's lint, unit and integration steps beside the module's composition, review and approval. A step the
|
|
// policy leaves without a status is `optional` (§553). A `bypass` step is placed bypassed and asked of no one; a build step is placed running,
|
|
// asked of the build seat the artifact's language names in the catalog half (§323), or, where no seat names it, ended
|
|
// could not run naming the missing seat; the module's steps wait. It returns the steps and the asks to put on the seats'
|
|
// work queues, one per running step.
|
|
func Place(p seat.Pipeline, reg catalog.Registration, entries map[string]catalog.CatalogEntry, now time.Time, asker string) ([]seat.StepResult, []Asked) {
|
|
entry := entries[p.Module]
|
|
arts := append([]catalog.Artifact(nil), entry.Artifacts...)
|
|
sort.Slice(arts, func(i, j int) bool { return arts[i].Ref < arts[j].Ref })
|
|
var steps []seat.StepResult
|
|
var asks []Asked
|
|
// The build step is every pipeline's first, required whatever the policy names, asked of the seat mesh-builds for the
|
|
// module's own entry only (A-99, A-97): a group's other members are built by their own pipelines.
|
|
build := seat.StepResult{Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Fingerprint: p.Fingerprint,
|
|
Seat: builds.Seat, Outcome: seat.StepResultOutcomeRunning, Started: &now, Ask: AskID(startOf(p, now), p.Pipeline, "", seat.StepResultStepBuild)}
|
|
steps = append(steps, build)
|
|
asks = append(asks, Asked{Seat: builds.Seat, ID: build.Ask, Ask: builds.StepRun{Pipeline: p.Pipeline, Step: builds.StepRunStepBuild, Branch: p.Branch,
|
|
Revision: p.Revision, Source: p.Source, Entries: []builds.BuildEntry{{Module: p.Module, Commit: p.Commit, Fingerprint: p.Fingerprint}}}})
|
|
for _, a := range arts {
|
|
name := a.Ref
|
|
if _, after, ok := strings.Cut(a.Ref, ":"); ok {
|
|
name = after
|
|
}
|
|
for _, step := range buildSteps {
|
|
status := Status(reg, name, step)
|
|
why := "bypassed by the module's policy"
|
|
if sdkstep.PackedOrFetched(a.Language, a.Kind) {
|
|
status, why = seat.StepResultStatusBypass, fmt.Sprintf("an artifact of kind %s has no %s step: the builder packs or fetches it", a.Kind, step)
|
|
}
|
|
s := seat.StepResult{Step: step, Artifact: a.Ref, Status: status, Fingerprint: p.Fingerprint}
|
|
if status == seat.StepResultStatusBypass {
|
|
s.Outcome, s.Why = seat.StepResultOutcomeBypassed, why
|
|
steps = append(steps, s)
|
|
continue
|
|
}
|
|
on := a.Language
|
|
if on == "" && a.Kind == "image" {
|
|
on = seatdef.OnKindImage
|
|
}
|
|
bs, err := BuildSeat(entries, on)
|
|
if err != nil {
|
|
s.Outcome, s.Started, s.Ended, s.Why = seat.StepResultOutcomeCouldNotRun, &now, &now, err.Error()
|
|
steps = append(steps, s)
|
|
continue
|
|
}
|
|
s.Seat, s.Outcome, s.Started = bs, seat.StepResultOutcomeRunning, &now
|
|
s.Ask = AskID(startOf(p, now), p.Pipeline, a.Ref, step)
|
|
steps = append(steps, s)
|
|
ask := Asked{Seat: bs, ID: s.Ask, Ask: stepAsk(p, entry, a, step, s.Ask, asker, now)}
|
|
if a.Package != nil {
|
|
ask.Package = a.Package.Name
|
|
}
|
|
asks = append(asks, ask)
|
|
}
|
|
}
|
|
for _, step := range moduleSteps {
|
|
status := Status(reg, "", step)
|
|
if status == "" {
|
|
continue
|
|
}
|
|
s := seat.StepResult{Step: step, Status: status, Outcome: seat.StepResultOutcomeWaiting}
|
|
if status == seat.StepResultStatusBypass {
|
|
s.Outcome, s.Why = seat.StepResultOutcomeBypassed, "bypassed by the module's policy"
|
|
}
|
|
// Composition is the assigner's, an accepted kind step of the seat mesh-wants (§22): it is asked with the others,
|
|
// running at once, and its end is heard from that seat as the build seats' are. Review and approval wait.
|
|
if step == seat.StepResultStepComposition && status != seat.StepResultStatusBypass {
|
|
s.Seat, s.Outcome, s.Started, s.Fingerprint = wants.Seat, seat.StepResultOutcomeRunning, &now, p.Fingerprint
|
|
s.Ask = AskID(startOf(p, now), p.Pipeline, "", step)
|
|
asks = append(asks, Asked{Seat: wants.Seat, ID: s.Ask, Ask: wants.StepRun{Branch: p.Branch, Commit: p.Commit, Fingerprint: p.Fingerprint,
|
|
Module: p.Module, Pipeline: p.Pipeline, Revision: p.Revision, Source: p.Source, Step: wants.StepRunStepComposition}})
|
|
}
|
|
steps = append(steps, s)
|
|
}
|
|
return steps, asks
|
|
}
|
|
|
|
// Asked is one ask to put on a build seat's work queue.
|
|
type Asked struct {
|
|
Seat string
|
|
ID string
|
|
Ask any
|
|
// Package is the name of the artifact's package, from the catalog entry, for a step on a build seat; its kept results
|
|
// are the ask's (§25, §416).
|
|
Package string
|
|
}
|
|
|
|
func stepAsk(p seat.Pipeline, e catalog.CatalogEntry, a catalog.Artifact, step, id, asker string, now time.Time) nodebuildgo.StepAsk {
|
|
// A step is for its module alone (principle 10, A-447): the fetch always names the module's directory, and the
|
|
// artifact's named paths in it when the entry has them.
|
|
fetch := nodebuildgo.Fetch{Repository: p.Repository, Commit: p.Commit, Dir: e.Path}
|
|
if paths := artifactPaths(e, a.Ref); len(paths) > 0 {
|
|
fetch.Paths = paths
|
|
}
|
|
return nodebuildgo.StepAsk{V: 1, ID: id, Asker: asker, Kind: "step", Language: a.Language, At: &now,
|
|
Step: &nodebuildgo.StepRun{Artifact: a.Ref, Branch: p.Branch, Commit: p.Commit, Fetch: fetch, Fingerprint: p.Fingerprint,
|
|
Module: p.Module, Pipeline: p.Pipeline, Revision: p.Source, Step: step}}
|
|
}
|
|
|
|
// askAll puts each ask on its seat's work queue; the first refusal is named, the rest are still put.
|
|
func askAll(asker StepAsker, arm Arm, key string, asks []Asked, recovery bool) error {
|
|
var first error
|
|
for _, a := range asks {
|
|
accept, ok := acceptStep[a.Seat]
|
|
if to, named := acceptRecovery[a.Seat]; recovery && named {
|
|
accept = to
|
|
}
|
|
err := fmt.Errorf("the seat %s is no build seat the runner asks", a.Seat)
|
|
if ok {
|
|
// A build seat's holder says ask-taken: the ask's bound is armed before it is put (§535, §647).
|
|
if arm != nil && BoundSeat(a.Seat) {
|
|
if aerr := arm(a.ID); aerr != nil {
|
|
err = fmt.Errorf("its bound is not armed: %w", aerr)
|
|
ok = false
|
|
}
|
|
}
|
|
}
|
|
if ok {
|
|
err = asker.AskStep(accept, a.ID, a.Ask)
|
|
}
|
|
if err != nil && first == nil {
|
|
first = fmt.Errorf("the step %s of %s is not asked of %s: %w", a.ID, key, a.Seat, err)
|
|
}
|
|
}
|
|
return first
|
|
}
|
|
|
|
// artifactPaths are the named paths the entry keeps for the artifact, by its reference or its name.
|
|
func artifactPaths(e catalog.CatalogEntry, ref string) []string {
|
|
if e.Inputs == nil {
|
|
return nil
|
|
}
|
|
if p, ok := e.Inputs.Paths[ref]; ok {
|
|
return p
|
|
}
|
|
_, name, _ := strings.Cut(ref, ":")
|
|
return e.Inputs.Paths[name]
|
|
}
|