Builds: design 47 G §464, §680 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
|
|
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
)
|
|
|
|
// toolchainReporter says `toolchain-reported` on the build seat's subject of its node (design 47 G §492): once at the
|
|
// module's start and each time its toolchains key of the language changes (a write that changes nothing says nothing).
|
|
// Language is the seat's ask language (go, node, image).
|
|
type toolchainReporter struct {
|
|
AskLanguage, Node string
|
|
// Publish is stdio.SeatPublish: the event is de-duplicated by its id.
|
|
Publish func(subject string, body any, id string) error
|
|
|
|
mu sync.Mutex
|
|
last map[string]sdkstep.Toolchain
|
|
}
|
|
|
|
// Report says the toolchain written under key at revision rev when it differs from the last one said for the key.
|
|
func (r *toolchainReporter) Report(key string, rev uint64, t sdkstep.Toolchain) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if prev, ok := r.last[key]; ok && prev.Version == t.Version && prev.Recipe == t.Recipe && prev.Digest == t.Digest && prev.Language == t.Language {
|
|
return nil
|
|
}
|
|
seat, err := sdkstep.BuildSeat(r.AskLanguage)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := sdkstep.ToolchainKey(r.Node, t.Language); err != nil {
|
|
return err
|
|
}
|
|
body := map[string]string{"node": r.Node, "language": t.Language, "version": t.Version, "recipe": t.Recipe, "digest": t.Digest}
|
|
subject := "mesh.seat." + seat + ".event.toolchain-reported." + r.Node
|
|
if err := r.Publish(subject, body, fmt.Sprintf("toolchain-reported.%s.%d", key, rev)); err != nil {
|
|
return err
|
|
}
|
|
if r.last == nil {
|
|
r.last = map[string]sdkstep.Toolchain{}
|
|
}
|
|
r.last[key] = t
|
|
return nil
|
|
}
|
|
|
|
// putReporting wraps a toolchains Put so each write that changes the toolchain is then said by r. A write that failed
|
|
// says nothing; an event that failed is passed to say and does not refuse the write.
|
|
func putReporting(put steprun.Put, r *toolchainReporter, digest func(version string) string, say func(format string, a ...any)) steprun.Put {
|
|
return func(key string, value any) (uint64, error) {
|
|
raw, merr := json.Marshal(value)
|
|
var t sdkstep.Toolchain
|
|
if merr != nil || json.Unmarshal(raw, &t) != nil {
|
|
return put(key, value) // not a toolchain value: written as it is
|
|
}
|
|
// The value carries the digest of the toolchain program beside the version and the recipe (design 47 G §472).
|
|
if t.Digest == "" && digest != nil {
|
|
t.Digest = digest(t.Version)
|
|
}
|
|
enc, eerr := sdkstep.EncodeToolchain(t)
|
|
if eerr != nil {
|
|
return put(key, value)
|
|
}
|
|
rev, err := put(key, json.RawMessage(enc))
|
|
if err != nil || r == nil || r.Publish == nil {
|
|
return rev, err
|
|
}
|
|
if eerr := r.Report(key, rev, t); eerr != nil && say != nil {
|
|
say("toolchain-reported could not be said: %v", eerr)
|
|
}
|
|
return rev, nil
|
|
}
|
|
}
|
|
|
|
// ProgramDigest is the sha256: digest of the file at path, a toolchain program (design 47 G §472).
|
|
func programFileDigest(path string) (string, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer f.Close()
|
|
h := sha256.New()
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
return "", err
|
|
}
|
|
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
|
|
}
|