E3: compose every setting as the word the one settings reader takes
A module's settings reached its own process only as files its manifest named, and its tools bundles not at all. The node-engine now composes each setting the module declares as MESH_CONFIG_<KEY>, its value as text, into each of its processes' environments and its slice of the tool runner's MESH_TOOL_ENV alike, and the tool runner's own into its process; the runner's drain-bound is a Go duration like every time setting. Builds: design 47 H2 §781; H-A-350 DMC-13, H-A-370, H-A-377
This commit is contained in:
@@ -1025,6 +1025,11 @@ func resolutionCompose(r mcat.Resolution, with mcat.Rendering, h Holding, filter
|
||||
if err := contributionsInto(copied, m, contributing, thisMachine, with, r.Capabilities, unplaced); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// And its settings, each as the word the one settings reader takes it from (design 47 H2 §781, H-A-370), after
|
||||
// every placeholder pass: a value is the operator's text, never a placeholder to fill.
|
||||
if err := settingWordsInto(copied, m, with.Settings[m.Module]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copied["id"] = m.Module + "." + fmt.Sprint(resource["id"])
|
||||
// A service saying what it reflects names resources within its own module, so those
|
||||
// are prefixed too or they would point at nothing.
|
||||
|
||||
@@ -3,9 +3,10 @@ package resolution
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
@@ -164,6 +165,14 @@ func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering, h Holding)
|
||||
sort.Strings(restartOn)
|
||||
|
||||
env := map[string]string{mcat.RuntimeToolModules: strings.Join(served, ","), mcat.RuntimeBrokerFile: credential.Path}
|
||||
// The tool runner's own settings, as every module's process is given its own (design 47 H2 §781, H-A-370).
|
||||
own, err := settingWords(*runtime, with.Settings[runtime.Module])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for word, value := range own {
|
||||
env[word] = value
|
||||
}
|
||||
if len(given) > 0 {
|
||||
// Marshalled from maps, whose keys encoding/json sorts: the same words, the same unit.
|
||||
body, err := json.Marshal(given)
|
||||
@@ -201,42 +210,29 @@ func resolutionRuntimeProcess(r mcat.Resolution, with mcat.Rendering, h Holding)
|
||||
return process, nil
|
||||
}
|
||||
|
||||
// DrainBoundSetting is the tool runner module's setting of how long, in seconds, it finishes its calls in flight once
|
||||
// stopped (design 47 H2 §813 (12)); DefaultDrainBound is its default where no layer sets it (H-A-358).
|
||||
// DrainBoundSetting is the tool runner module's setting of how long it finishes its calls in flight once stopped, a Go
|
||||
// duration such as "30s" (design 47 H2 §813 (12), H-A-377); DefaultDrainBound is its default where no layer sets it
|
||||
// (H-A-358).
|
||||
const (
|
||||
DrainBoundSetting = "drain-bound"
|
||||
DefaultDrainBound = 30
|
||||
DefaultDrainBound = 30 * time.Second
|
||||
)
|
||||
|
||||
// drainBoundOf is the tool runner's drain bound in seconds, from its setting drain-bound, else DefaultDrainBound (design
|
||||
// 47 H2 §813 (12), H-A-358): the node-engine writes its unit's stop from it. A value that is no whole number of seconds
|
||||
// above zero is refused by name.
|
||||
// drainBoundOf is the tool runner's drain bound in whole seconds, the declaration's process field, from its setting
|
||||
// drain-bound, else DefaultDrainBound (design 47 H2 §813 (12), H-A-358, H-A-377): the node-engine writes its unit's stop
|
||||
// from it. The setting is a Go duration, as every time setting is (H-A-370); part of a second counts as a whole one, so
|
||||
// the bound is never shorter than the setting. A value that is no duration above zero is refused by name.
|
||||
func drainBoundOf(m mcat.Manifest, layers []mcat.Layer) (int, error) {
|
||||
v, set := settingValue(WithDefaults(m, layers), DrainBoundSetting)
|
||||
if !set {
|
||||
return DefaultDrainBound, nil
|
||||
return int(DefaultDrainBound / time.Second), nil
|
||||
}
|
||||
var n float64
|
||||
switch x := v.(type) {
|
||||
case float64:
|
||||
n = x
|
||||
case int:
|
||||
n = float64(x)
|
||||
case int64:
|
||||
n = float64(x)
|
||||
case string:
|
||||
parsed, err := strconv.Atoi(strings.TrimSpace(x))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s's setting %s is %q, and it is a whole number of seconds", m.Module, DrainBoundSetting, x)
|
||||
}
|
||||
n = float64(parsed)
|
||||
default:
|
||||
return 0, fmt.Errorf("%s's setting %s is %v, and it is a whole number of seconds", m.Module, DrainBoundSetting, v)
|
||||
text, ok := v.(string)
|
||||
d, err := time.ParseDuration(strings.TrimSpace(text))
|
||||
if !ok || err != nil || d <= 0 {
|
||||
return 0, fmt.Errorf("%s's setting %s is %v, and it is a Go duration above zero, such as \"30s\" (H-A-377)", m.Module, DrainBoundSetting, v)
|
||||
}
|
||||
if n < 1 || n != float64(int(n)) {
|
||||
return 0, fmt.Errorf("%s's setting %s is %v, and it is a whole number of seconds above zero", m.Module, DrainBoundSetting, v)
|
||||
}
|
||||
return int(n), nil
|
||||
return int((d + time.Second - 1) / time.Second), nil
|
||||
}
|
||||
|
||||
// NoRuntimeToRunError is a module of its own account with no runtime program on its node to run it from. Held says the
|
||||
@@ -400,7 +396,16 @@ func bundleWords(m mcat.Manifest, with mcat.Rendering) (map[string]string, error
|
||||
out[word] = value
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
// And the module's settings, as its processes are given them (design 47 H2 §781, H-A-370), for a module the runtime
|
||||
// loads a bundle of.
|
||||
if !slices.ContainsFunc(m.Bundles, func(b mcat.Bundle) bool { return len(b.Loads) > 0 }) {
|
||||
return out, nil
|
||||
}
|
||||
settings, err := settingWords(m, with.Settings[m.Module])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return withSettingWords(out, settings, m.Module, "bundles' words")
|
||||
}
|
||||
|
||||
// givenTo makes what a bundle's words name readable by the account the runtime runs as (novox/hq
|
||||
|
||||
@@ -125,7 +125,11 @@ func TestTheMachineRunsOneRuntimeLoadingEveryDeliveredBundle(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := mcat.Resolution{Node: "anchor", Account: "ops", Modules: []mcat.Manifest{nftables, showcase, theRuntime(t)}}
|
||||
// Design 47 H2 §781, H-A-370: a module's settings reach its slice of the runtime's words, and the runtime's own its process.
|
||||
showcase.Settings = map[string]mcat.SettingDeclaration{"greeting": {Kind: mcat.KindPreference, Default: "hello", Why: "polite"}}
|
||||
runner := theRuntime(t)
|
||||
runner.Settings = map[string]mcat.SettingDeclaration{DrainBoundSetting: {Kind: mcat.KindPreference, Default: "30s", Why: "a call's length"}}
|
||||
r := mcat.Resolution{Node: "anchor", Account: "ops", Modules: []mcat.Manifest{nftables, showcase, runner}}
|
||||
out, err := Declaration(r, with)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -146,6 +150,9 @@ func TestTheMachineRunsOneRuntimeLoadingEveryDeliveredBundle(t *testing.T) {
|
||||
"showcase="+mcat.BundleRoot+"/showcase/code/tools/index.js" {
|
||||
t.Errorf("the runtime is told to serve %q: every loaded file, by module, and nothing a bundle runs", env[mcat.RuntimeToolModules])
|
||||
}
|
||||
if env["MESH_CONFIG_DRAIN_BOUND"] != "30s" || env[mcat.RuntimeToolEnv] != `{"showcase":{"MESH_CONFIG_GREETING":"hello"}}` {
|
||||
t.Errorf("the settings reach the runtime as %q and its served modules as %s", env["MESH_CONFIG_DRAIN_BOUND"], env[mcat.RuntimeToolEnv])
|
||||
}
|
||||
if env[mcat.RuntimeBrokerFile] != "/var/lib/mesh/"+"node-tools"+"/broker" {
|
||||
t.Errorf("the runtime reads its credential at %q, not where the module's own secret is placed", env[mcat.RuntimeBrokerFile])
|
||||
}
|
||||
@@ -168,16 +175,17 @@ func TestTheMachineRunsOneRuntimeLoadingEveryDeliveredBundle(t *testing.T) {
|
||||
if names[len(names)-1] != "node-tools"+"."+RuntimeProcessID() {
|
||||
t.Errorf("the runtime's process is not last: %v", names)
|
||||
}
|
||||
// Design 47 H2 §813 (12), H-A-358: the runtime's process carries the module's drain bound, 30 s where no layer sets it,
|
||||
// the value a layer sets, and a value that is no number of seconds is refused by name.
|
||||
if process["drain-bound"] != DefaultDrainBound {
|
||||
// Design 47 H2 §813 (12), H-A-358, H-A-377: the runtime's process carries the module's drain bound in whole seconds, 30
|
||||
// where no layer sets it, the Go duration a layer sets (part of a second a whole one), and a value that is no duration
|
||||
// above zero is refused by name.
|
||||
if process["drain-bound"] != 30 {
|
||||
t.Errorf("the runtime's process drains for %v, not the default 30 s", process["drain-bound"])
|
||||
}
|
||||
for _, row := range []struct {
|
||||
value any
|
||||
want int
|
||||
words string
|
||||
}{{float64(45), 45, ""}, {"60", 60, ""}, {"soon", 0, "whole number of seconds"}, {float64(0), 0, "above zero"}} {
|
||||
}{{"45s", 45, ""}, {"1500ms", 2, ""}, {"60", 0, "Go duration"}, {float64(45), 0, "Go duration"}, {"0s", 0, "above zero"}} {
|
||||
set := with
|
||||
set.Settings = mcat.SettingsBy{"node-tools": {{From: "mesh", Values: map[string]any{DrainBoundSetting: row.value}}}}
|
||||
out, err := Declaration(r, set)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package resolution
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -64,7 +65,10 @@ func theNodeOverTheMeshOverTheDefault(t *testing.T) {
|
||||
// layer overrides it.
|
||||
func TestAComposedMachineGetsTheDefaultAndTheNodesValue(t *testing.T) {
|
||||
r := anAdoptedAnchor()
|
||||
r.Modules = append(r.Modules, notifier())
|
||||
n := notifier()
|
||||
n.Resources = append(n.Resources, map[string]any{"id": "daemon", "type": "process", "name": "notifier",
|
||||
"run": []any{"/usr/bin/notifier"}, "env": map[string]any{"LANG": "C"}})
|
||||
r.Modules = append(r.Modules, n)
|
||||
with := anchorRendering(false)
|
||||
composed, err := Compose(r, with)
|
||||
if err != nil {
|
||||
@@ -83,6 +87,17 @@ func TestAComposedMachineGetsTheDefaultAndTheNodesValue(t *testing.T) {
|
||||
if c := byID(composed.Resources)["notifier.configuration"]["content"]; c != "font = Inter 13\nwidth = 250\n" {
|
||||
t.Fatalf("composed with the node's font size as %q", c)
|
||||
}
|
||||
// Design 47 H2 §781, H-A-370: and each setting it declares reaches its process as the word the one settings reader
|
||||
// takes it from, the value as text, beside the process's own words; one its own words give otherwise is refused.
|
||||
env := fmt.Sprint(byID(composed.Resources)["notifier.daemon"]["env"])
|
||||
if env != "map[LANG:C MESH_CONFIG_FONT_SIZE:13 MESH_CONFIG_WIDTH:250]" {
|
||||
t.Errorf("the notifier's process is given %s", env)
|
||||
}
|
||||
n.Resources[1]["env"] = map[string]any{"MESH_CONFIG_WIDTH": "9"}
|
||||
r.Modules[len(r.Modules)-1] = n
|
||||
if _, err = Compose(r, with); err == nil || !strings.Contains(err.Error(), "MESH_CONFIG_WIDTH") {
|
||||
t.Errorf("a process whose own word is its setting's composed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A default fills ${setting:…} only: it never becomes a key of a mergeable file (issue 168).
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package resolution
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// settingWords is every setting m declares that a layer over it sets, its default among them, as the word the one
|
||||
// settings reader takes it from (design 47 H2 §781, H-A-370): stdio.SettingWord(<key>), MESH_CONFIG_<KEY>, holding the
|
||||
// value as text (a duration as "10m", a number as a program reads it, a list or an object as JSON). Composed alike into
|
||||
// each of m's processes' environments and into m's slice of the tool runner's MESH_TOOL_ENV. Nil when it sets none.
|
||||
func settingWords(m mcat.Manifest, layers []mcat.Layer) (map[string]string, error) {
|
||||
keys := declaredConfig(m)
|
||||
sort.Strings(keys)
|
||||
var out map[string]string
|
||||
for _, key := range keys {
|
||||
value, set := settingValue(WithDefaults(m, layers), key)
|
||||
if !set {
|
||||
continue
|
||||
}
|
||||
text := plainly(value)
|
||||
switch value.(type) {
|
||||
case map[string]any, []any:
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's setting %s cannot be written as text: %w", m.Module, key, err)
|
||||
}
|
||||
text = string(raw)
|
||||
}
|
||||
if out == nil {
|
||||
out = map[string]string{}
|
||||
}
|
||||
out[stdio.SettingWord(key)] = text
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// withSettingWords is words with each of the setting words laid in, refusing a word the module's own words give
|
||||
// another value: a module's settings reach its code under one word each (H-A-370).
|
||||
func withSettingWords(words, settings map[string]string, module, where string) (map[string]string, error) {
|
||||
for word, value := range settings {
|
||||
if was, had := words[word]; had && was != value {
|
||||
return nil, fmt.Errorf("%s's %s gives %s the value %q, and the word is its setting's, composed as %q (design 47 H2 §781)",
|
||||
module, where, word, was, value)
|
||||
}
|
||||
if words == nil {
|
||||
words = map[string]string{}
|
||||
}
|
||||
words[word] = value
|
||||
}
|
||||
return words, nil
|
||||
}
|
||||
|
||||
// settingWordsInto lays m's setting words into a process resource's environment (H-A-370); any other resource is left.
|
||||
func settingWordsInto(resource map[string]any, m mcat.Manifest, layers []mcat.Layer) error {
|
||||
if fmt.Sprint(resource["type"]) != "process" {
|
||||
return nil
|
||||
}
|
||||
settings, err := settingWords(m, layers)
|
||||
if err != nil || len(settings) == 0 {
|
||||
return err
|
||||
}
|
||||
env, _ := resource["env"].(map[string]any)
|
||||
words := map[string]string{}
|
||||
for k, v := range env {
|
||||
words[k] = fmt.Sprint(v)
|
||||
}
|
||||
if _, err := withSettingWords(words, settings, m.Module, fmt.Sprintf("process %v", resource["id"])); err != nil {
|
||||
return err
|
||||
}
|
||||
filled := map[string]any{}
|
||||
for k, v := range env {
|
||||
filled[k] = v
|
||||
}
|
||||
for word, value := range settings {
|
||||
filled[word] = value
|
||||
}
|
||||
resource["env"] = filled
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user