package main // What the mesh says about its own tools, as the agent's instructions need it (novox/hq ADR 0245): every seat verb // and module tool that says what shell command it replaces. // // Read from the bus at start and whenever a followed state changes (issue 1126 B13; the controller that answered this before is // not placed, novox/hq ADR 0590): the seats' verbs from the seats' contract (mesh-contracts), the modules' // own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and where // each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so // a render never waits for the bus, and rendered whenever it changes. Nothing here is written by hand: a verb // that gains a `replaces` is in the next render's table. import ( "encoding/json" "errors" "fmt" "os" "path/filepath" "sort" "strings" "sync" "time" assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" mesh "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/seatdef" "git.novox.be/novox/mesh-sdk/go/seatlist" ) // Replacement is one verb or tool and the shell commands it is the mesh's way to do. type Replacement struct { // Address is how it is called: `.`, or `.`; NodeScoped says a machine goes // before it, `/`. Address string `json:"address"` NodeScoped bool `json:"node_scoped,omitempty"` Seat bool `json:"seat,omitempty"` Does string `json:"does,omitempty"` Replaces []string `json:"replaces"` // On is where a module's tool runs; empty for a seat's verb, which every holder serves. On []string `json:"on,omitempty"` } // MeshTools is what the bus said, as kept in the module's state. type MeshTools struct { Replacements []Replacement `json:"replacements"` // Asked is when the bus was last read whole. Asked string `json:"asked,omitempty"` } func (p Paths) meshTools() string { return filepath.Join(p.State, "mesh-tools.json") } // ReadMeshTools is what was kept, or nil. func ReadMeshTools(p Paths) *MeshTools { var m MeshTools if !readJSON(p.meshTools(), &m) { return nil } return &m } // The states read, each declared in the manifest's reads. const ( AssignmentsRead = (assignerModule + "." + mesh.StateAssignments) ) // States reads the module states the manifest declares in its reads: a state's keys, and one key's value. type States interface { Keys(read string) ([]string, error) Get(read, key string) ([]byte, bool, error) } // ModuleTools is one module's own tools that replace a command, as its manifest says (`replaces`), and where it runs. type ModuleTools struct { Module string On []string Replaces map[string][]string } // assignments is every node's assignments, from the seat mesh-module-assigner's state. func assignments(st States) (map[string]mesh.Assignments, error) { keys, err := st.Keys(AssignmentsRead) if err != nil { return nil, fmt.Errorf("%s: %w", AssignmentsRead, err) } read := assignerclient.NewKeys() for _, k := range keys { v, found, err := st.Get(AssignmentsRead, k) if err != nil { return nil, fmt.Errorf("%s %s: %w", AssignmentsRead, k, err) } if found { _, _ = read.Put(k, v, 0, false) // a key that does not read places nothing } } return read.All(), nil } // runsOn is, by module, the nodes assignments puts it on, sorted. func runsOn(placed map[string]mesh.Assignments) map[string][]string { out := map[string][]string{} for node, p := range placed { for _, m := range p.Modules { out[m.Module] = append(out[m.Module], node) } } for _, nodes := range out { sort.Strings(nodes) } return out } // NodesRunning is the nodes assignments puts module on. func NodesRunning(st States, module string) ([]string, error) { placed, err := assignments(st) if err != nil { return nil, err } return runsOn(placed)[module], nil } // modulesTools is every placed module's own tools that replace a command, by module, from the manifest its catalog // graph entry carries. A module whose manifest cannot be read is left out and said in the error. func modulesTools(cat CatalogHalf, on map[string][]string) ([]ModuleTools, error) { var out []ModuleTools var problems []error if err := cat.Ready(); err != nil { return nil, err } modules := make([]string, 0, len(on)) for m := range on { modules = append(modules, m) } sort.Strings(modules) for _, module := range modules { entry, found, err := cat.Entry(module) if err != nil || !found { if err != nil { problems = append(problems, fmt.Errorf("the catalog half's entry of %s: %w", module, err)) } continue } kept, err := json.Marshal(entry.Manifest) if err != nil { problems = append(problems, fmt.Errorf("the manifest of %s: %w", module, err)) continue } var man struct { Replaces map[string][]string `json:"replaces"` } if json.Unmarshal(kept, &man) != nil || len(man.Replaces) == 0 { continue } out = append(out, ModuleTools{Module: module, On: on[module], Replaces: man.Replaces}) } return out, errors.Join(problems...) } // firstSentence is what a verb does, short: its description to the first full stop, at most 70 characters. func firstSentence(s string) string { s = strings.TrimSpace(s) if i := strings.Index(s, ". "); i >= 0 { s = s[:i] } s = strings.TrimSuffix(s, ".") if i := strings.IndexAny(s, ":;("); i > 20 { s = strings.TrimSpace(s[:i]) } if r := []rune(s); len(r) > 70 { s = strings.TrimSpace(string(r[:69])) + "…" } return s } // ReplacementsOf is every seat verb and module tool that says it replaces a command: the seats' verbs first, in the // seats' order, then the modules' own tools by module and tool. func ReplacementsOf(defined []seatdef.Definition, modules []ModuleTools) []Replacement { var out []Replacement for _, s := range defined { for _, v := range s.Verbs { if len(v.Replaces) == 0 { continue } out = append(out, Replacement{Address: s.Seat + "." + v.Name, NodeScoped: s.Scope == "node", Seat: true, Does: firstSentence(v.Description), Replaces: v.Replaces}) } } sorted := append([]ModuleTools(nil), modules...) sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].Module < sorted[j].Module }) for _, m := range sorted { names := make([]string, 0, len(m.Replaces)) for t := range m.Replaces { names = append(names, t) } sort.Strings(names) for _, t := range names { if len(m.Replaces[t]) == 0 { continue } out = append(out, Replacement{Address: m.Module + "." + t, NodeScoped: true, Replaces: m.Replaces[t], On: m.On, Does: strings.ReplaceAll(strings.TrimPrefix(t, strings.ReplaceAll(m.Module, "-", "_")+"_"), "_", " ")}) } } return out } // ReadMeshToolsFrom reads the bus now: the seats' verbs and the placed modules' own tools. A part that cannot be read is left out and said in the error; what was read is kept. func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition) (MeshTools, error) { var m MeshTools placed, err := assignments(st) if err != nil { m.Replacements = ReplacementsOf(defined, nil) return m, err } modules, merr := modulesTools(cat, runsOn(placed)) m.Replacements = ReplacementsOf(defined, modules) return m, merr } // KeepMeshTools writes what was asked to the module's state when it differs from what was kept, and // answers whether it did. When was asked is not a difference. func KeepMeshTools(p Paths, m MeshTools) bool { if was := ReadMeshTools(p); was != nil { a, b := *was, m a.Asked, b.Asked = "", "" x, _ := json.Marshal(a) y, _ := json.Marshal(b) if string(x) == string(y) { return false } } m.Asked = time.Now().UTC().Format(time.RFC3339) raw, _ := indented(m) tmp := p.meshTools() + ".tmp" if os.WriteFile(tmp, raw, 0o600) != nil { return false } return os.Rename(tmp, p.meshTools()) == nil } // hookFiles is what the plugin's hooks run: this module's own executable and the waits directory. None where the // executable cannot be named — then no hook is written, rather than one that cannot run. func hookFiles(p Paths) *HookFiles { exe, err := os.Executable() if err != nil { return nil } if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved } return &HookFiles{Exe: exe, WaitsDir: WaitsDirPath(p)} } // MeshToolsFirst is how long the first read waits for the first views to be answered. const MeshToolsFirst = 2 * time.Minute // refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes, and renders when what it said changed. func refreshMeshTools(p Paths) { select { case <-meshViews.Ready(): case <-time.After(MeshToolsFirst): say("the first views of the tools and the assignments were not answered in %s; reading what is held", MeshToolsFirst) } startSeatList(seatlist.StdioBus()) for { m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats()) if err != nil { say("reading what each tool replaces: %v", err) } if (err == nil || len(m.Replacements) > 0) && KeepMeshTools(p, m) { if _, err := RenderNow(p, writeManaged); err != nil { say("rendering what the bus said of the tools: %v", err) } else { say("the mesh's tools changed: %d replace a command; rendered", len(m.Replacements)) } } <-meshChanged time.Sleep(2 * time.Second) // a burst of events is one read select { case <-meshChanged: default: } } } // seatList is the seat list this module holds: the definitions of the seats, read from the seat mesh-catalog and // followed on its events (novox/hq ADR 0635 §1, ADR 0636), the foundation's bootstrapped ones until it answers. var seatList *seatlist.Reader // startSeatList starts the reader once the runtime serves; a change of the list is told like any followed state, so the // instructions' table is read again. func startSeatList(bus seatlist.Bus) { r, err := seatlist.Start(seatlist.Config{Bus: bus, Bootstrap: seatlist.Bootstrapped(), OnChange: func([]catalogue.Seat) { select { case meshChanged <- struct{}{}: default: } }}) if err != nil { say("following the seat list: %v", err) } seatListMu.Lock() seatList = r seatListMu.Unlock() } var seatListMu sync.Mutex // definedSeats are the held definitions in reading order, mesh seats first, then node seats, each by name; empty until // the reader has started. func definedSeats() []seatdef.Definition { seatListMu.Lock() r := seatList seatListMu.Unlock() if r == nil { return nil } held := r.Definitions() var out []seatdef.Definition for _, s := range r.Seats() { if d, ok := held[s.Name]; ok { out = append(out, d) } } return out }