Files
mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go/main.go
T

119 lines
4.4 KiB
Go

// mesh-build-go: the Go build module (novox/hq ADR 0405, ADR 0466 §1, ADR 0469, ADR 0470; design 47 R4c-3, issue
// 1483). A Go tools bundle the node's runtime launches as the module's own account (runs-as). It builds with the
// go its node has installed (GOTOOLCHAIN=local, ADR 0477 §1; issue 1572), names no Go version, writes the version it
// finds to <node>.go of its `toolchains` state at start and before every step, and refuses a step only when go
// cannot be run: its health is then unhealthy, so the controller raises the node's condition. What it says, it says on stderr; stdout is the MCP channel.
package main
import (
"context"
"fmt"
"os"
"os/user"
"strings"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/busstate"
)
// The directories the module's manifest places outside its home (ADR 0466 §1): the workspaces, and the Go module
// and build caches.
const (
WorkDir = "/var/lib/mesh-build-go-work"
CacheDir = "/var/lib/mesh-build-go-cache"
)
func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-build-go] "+format+"\n", a...) }
func main() {
if len(os.Args) > 1 && os.Args[1] == busArg {
os.Exit(runBus(os.Args[2:]))
}
if len(os.Args) > 1 && os.Args[1] == levelArg {
os.Exit(runLevelProcess(os.Args[2:]))
}
ctx := context.Background()
m := &Module{
SayEvent: stdio.SeatPublish,
Node: strings.TrimSpace(os.Getenv("MESH_NODE")),
Recipe: Current(),
Toolchains: stdio.State(seat.StateToolchains).Put,
SayToolchain: stdio.SeatPublish,
Steps: stdio.State(seat.StateSteps).Put,
Detected: stdio.State(seat.StateDetectedSteps).Put,
ReadDetected: readOf(stdio.State(seat.StateDetectedSteps)).Get,
ForgetDetected: stdio.State(seat.StateDetectedSteps).Delete,
RecordAct: handActRecorder,
ReadSteps: readOf(stdio.State(seat.StateSteps)),
Pauses: stdio.State(sdkstep.PausesState).Put,
ReadPauses: readOf(stdio.State(sdkstep.PausesState)),
ForgetPause: stdio.State(sdkstep.PausesState).Delete,
WatchPauses: stdio.State(sdkstep.PausesState).Watch,
ReadToolchainsState: readOf(stdio.State(seat.StateToolchains)),
Ctx: ctx,
Say: logf,
Work: WorkDir,
Cache: CacheDir,
}
m.Floor = floorOf(m.Node)
if u, err := user.Current(); err == nil {
m.Home = u.HomeDir
}
m.Run = StepRunner(m.Home)
m.GoSeatFile = m.Home + "/go-package-registry.json"
m.ArtifactStoreFile = m.Home + "/artifact-store.json"
m.Settings = m.Home + "/settings.json"
m.ReadEntry = askEntry
m.AskInput = stdio.Ask
m.KeptBuilds = busstate.ObjectsOf(KeptBuildsStore)
m.Outputs = busstate.ObjectsOf(steprun.OutputsStore)
m.Self = strings.TrimSpace(os.Getenv("MESH_MODULE"))
m.Probe = ProbeGo(m.Home, m.Cache)
m.Bubblewrap = ProbeBubblewrap
// Outside a launch by the runtime — a build, a check — there is no node and no state to write.
if m.Node != "" {
go m.Start(ctx)
findings, err := newFindingsWriter()
if err != nil {
logf("the finding writer: %v", err)
os.Exit(1)
}
manifestFindings = findings
// the runtime answers the writer's subscription only once Serve has its channel
go func() {
<-stdio.Served()
if err := findings.Start(ctx); err != nil {
logf("the finding writer: %v", err)
os.Exit(1)
}
}()
}
go startKeeper(ctx)
// Every state it keeps, said on the bus before it serves (ADR 0495 §2; issue 1790).
if err := keepStates(); err != nil {
logf("announcing the states it keeps: %v", err)
os.Exit(1)
}
steps := m.StepTools()
booted := time.Now()
puller := m.Puller(steps, nil)
m.Held = puller
if m.Node != "" {
// A step a previous process left running is ended (issue 1890): one that names another owner, or none and
// started before booted.
go m.endLeftRunning(ctx, m.stepScan(), booted, logf)
}
if m.Node != "" {
// The seat's asks, pulled once Serve has the runtime's channel; a pull before it is said and tried again.
go func() { _ = puller.Pull(ctx) }()
}
if err := stdio.Serve("", append(m.Tools(steps), m.SeatTools(puller)...)); err != nil {
logf("%v", err)
os.Exit(1)
}
}