H2, H8 (H-A-735): the owner pass at the engine's start stops and starts the module's units; composition-diff keeps the entry's name, the words go to the journal
Builds: design 47 H2 H-A-641, H-A-701, H-A-710 (4), H-A-735
This commit is contained in:
@@ -2128,6 +2128,16 @@ func testTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes(
|
||||
if _, err := b.ag.CompositionDiff(context.Background(), "db", 0, ""); err == nil {
|
||||
t.Error("composition-diff with neither a revision nor a digest was answered")
|
||||
}
|
||||
var leftSaid []string
|
||||
wasLine := journalLine
|
||||
journalLine = func(l string) { leftSaid = append(leftSaid, l) }
|
||||
defer func() { journalLine = wasLine }()
|
||||
fingerprintLeftSaid.Clear()
|
||||
defer func() {
|
||||
if len(leftSaid) != 1 || !strings.Contains(leftSaid[0], "web.box") || !strings.Contains(leftSaid[0], "left from before H2, taken out at the container's next recreate") {
|
||||
t.Errorf("the container's difference was said as %q, want one journal line", leftSaid)
|
||||
}
|
||||
}()
|
||||
if got := DiffResources([]json.RawMessage{json.RawMessage(`{"id":"web.key","type":"file","sealed":"a","content":"x"}`),
|
||||
json.RawMessage(`{"id":"web.run","type":"process","env":{"MODE":"a","GONE":"1"}}`),
|
||||
json.RawMessage(`{"id":"web.box","type":"container","env":{"MESH_FINGERPRINT":"src2:a"}}`),
|
||||
@@ -2138,7 +2148,7 @@ func testTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes(
|
||||
json.RawMessage(`{"id":"web.d","type":"process","env":{"MESH_FINGERPRINT":"src2:b"}}`)}); len(got) != 4 ||
|
||||
!slices.Equal(got[2].Entries, []nodeengine.CompositionDiffEntry{{Name: "content", Masked: true}, {Name: "sealed", Masked: true}}) ||
|
||||
// H-A-710 (4): a container's difference of MESH_FINGERPRINT is left from before H2; a daemon's is an ordinary entry.
|
||||
!slices.Equal(got[0].Entries, []nodeengine.CompositionDiffEntry{{Name: "env.MESH_FINGERPRINT (left from before H2, taken out at the container's next recreate)", Running: "src2:a"}}) ||
|
||||
!slices.Equal(got[0].Entries, []nodeengine.CompositionDiffEntry{{Name: "env.MESH_FINGERPRINT", Running: "src2:a"}}) ||
|
||||
!slices.Equal(got[1].Entries, []nodeengine.CompositionDiffEntry{{Name: "env.MESH_FINGERPRINT", Running: "src2:a", Other: "src2:b"}}) ||
|
||||
!slices.Equal(got[3].Entries, []nodeengine.CompositionDiffEntry{{Name: "env.GONE", Running: "1"}, {Name: "env.MODE", Running: "a", Other: "b"}}) {
|
||||
t.Errorf("the entries that differ, a secret masked, read %+v", got)
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
@@ -409,6 +411,11 @@ func (a *Agent) CompositionDiff(ctx context.Context, module string, revision int
|
||||
// fingerprintLeft says what a container's difference of its env MESH_FINGERPRINT is (design 47 H2, H-A-710 (4)).
|
||||
const fingerprintLeft = "left from before H2, taken out at the container's next recreate"
|
||||
|
||||
// fingerprintLeftSaid is each container whose difference was said, once per node-engine process; journalLine says a line
|
||||
// in the node-engine's journal (its standard error), a variable so a test reads it.
|
||||
var fingerprintLeftSaid sync.Map
|
||||
var journalLine = func(line string) { fmt.Fprintf(os.Stderr, "mesh-host: %s\n", line) }
|
||||
|
||||
// DiffResources is, per resource by its id, the entries that differ from running to other (design 47 H2 §813 (3)): each
|
||||
// top-level field by its name, the environment's entries each by `env.<name>`, and a value the declaration keeps secret
|
||||
// (a file's sealed, vault-sealed or secrets, and the content of a file that has any of them) masked. Resources in id
|
||||
@@ -442,7 +449,10 @@ func DiffResources(running, other []json.RawMessage) []nodeengine.CompositionDif
|
||||
e := nodeengine.CompositionDiffEntry{Name: name}
|
||||
if name == "env."+engcomp.FingerprintVar && (w.values["type"] == string(declaration.TypeContainer) || n.values["type"] == string(declaration.TypeContainer)) {
|
||||
// A container is handed none since H2 (H-A-710 (4)): what a composition from before holds is no change to make.
|
||||
e.Name = name + " (" + fingerprintLeft + ")"
|
||||
// The entry keeps its name; the words are the verb's description and one journal line per container and process.
|
||||
if _, said := fingerprintLeftSaid.LoadOrStore(id, true); !said {
|
||||
journalLine(fmt.Sprintf("%s: %s: %s", id, name, fingerprintLeft))
|
||||
}
|
||||
}
|
||||
if w.secret[name] || n.secret[name] {
|
||||
e.Masked = true
|
||||
|
||||
@@ -251,3 +251,56 @@ func TestAnOwnerPassRunsWithTheModulesUnitsStopped(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H2 H-A-641 (answered by H-A-735): the pass at the engine's start, where no apply follows, stops the module's
|
||||
// units before it and starts them after it; one whose pass fails leaves them stopped.
|
||||
func TestTheOwnerPassAtTheEnginesStartStopsAndStartsTheUnits(t *testing.T) {
|
||||
me, myGroup := os.Getuid(), os.Getgid()
|
||||
for name, fails := range map[string]bool{"the pass completes": false, "the pass fails": true} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "state")
|
||||
if err := os.Mkdir(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "webhook.secret"), []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := parse(t, fmt.Sprintf(`{"declaration":1,"resources":[
|
||||
{"id":"app.state","type":"directory","path":%q,"mode":"0700","owner":"%d:%d"}]}`, dir, me+4242, myGroup+4242))
|
||||
m := unitsMachine(map[string]*fakeUnit{"app.service": {active: "active", enabled: "enabled"}})
|
||||
var events []string
|
||||
run := func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
out, err := m.run(ctx, name, args...)
|
||||
if name == "systemctl" {
|
||||
args = oldShape(name, args)
|
||||
if args[0] == "stop" || args[0] == "start" {
|
||||
events = append(events, args[0]+" "+args[len(args)-1])
|
||||
}
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
|
||||
t.Cleanup(func() { lchownContent, ownDirectory, fchownDirectory = wasContent, wasOwn, wasF })
|
||||
lchownContent = func(path string, uid, gid int) error {
|
||||
if fails {
|
||||
return syscall.EPERM
|
||||
}
|
||||
events = append(events, "give "+filepath.Base(path))
|
||||
return nil
|
||||
}
|
||||
ownDirectory = func(string, string) error { events = append(events, "own directory"); return nil }
|
||||
fchownDirectory = func(int, int, int) error { events = append(events, "own directory"); return nil }
|
||||
known := store.State{}
|
||||
known.Record(store.Applied{ID: "app.run", Type: "service", Target: "app.service"})
|
||||
known.Record(store.Applied{ID: "app.state", Type: "directory", Target: dir})
|
||||
ConvergeDirectories(context.Background(), archHost(t), d, known, run)
|
||||
want := "stop app.service,give webhook.secret,own directory,start app.service"
|
||||
if fails {
|
||||
want = "stop app.service"
|
||||
}
|
||||
if got := strings.Join(events, ","); got != want {
|
||||
t.Fatalf("events %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package apply
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/declaration"
|
||||
"github.com/novox/mesh-host/internal/store"
|
||||
"github.com/novox/mesh-host/internal/system"
|
||||
)
|
||||
|
||||
// ConvergeDirectories gives each directory entry of d whose record known holds, found on disk with another owner or mode
|
||||
@@ -14,12 +16,15 @@ import (
|
||||
// drift lasts at most until then, and with no apply of an unchanged composition. One the mesh never made (its record used
|
||||
// as found, issue 339) is left as found and said. Answered as one line per directory set right or left; a directory
|
||||
// declared that is missing, that has no record, or that is as declared, says nothing.
|
||||
func ConvergeDirectories(d *declaration.Declaration, known store.State) []string {
|
||||
func ConvergeDirectories(ctx context.Context, sys system.System, d *declaration.Declaration, known store.State, run Runner) []string {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
var lines []string
|
||||
placed := placedPaths(d.Resources)
|
||||
// H-A-641: the owner pass runs here with the module's units stopped, as in an apply, and with no apply after it they are
|
||||
// started again once every directory of the module is through; one a pass failed on leaves them stopped (H-A-701).
|
||||
stopped, failed := map[string]bool{}, map[string]bool{}
|
||||
for _, r := range d.Resources {
|
||||
dir, ok := r.(*declaration.Directory)
|
||||
if !ok {
|
||||
@@ -37,9 +42,28 @@ func ConvergeDirectories(d *declaration.Declaration, known store.State) []string
|
||||
lines = append(lines, fmt.Sprintf("%s (%s) cannot be judged at this start: %v", dir.Path, dir.ID, err))
|
||||
continue
|
||||
}
|
||||
module, ours := d.ModuleOfID(dir.ID)
|
||||
if ours && !stopped[module] && ownerPassRuns(dir, rec, true) {
|
||||
stopped[module] = true
|
||||
var err error
|
||||
if sys == nil {
|
||||
err = errors.New("this host is built for no system it knows")
|
||||
} else {
|
||||
_, err = StopModule(ctx, sys, known, module, run)
|
||||
}
|
||||
if err != nil {
|
||||
failed[module] = true
|
||||
lines = append(lines, fmt.Sprintf("%s (%s) is %s and is not set right at this start: the units of %s could not be stopped: %v", dir.Path, dir.ID, drift, module, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
if failed[module] {
|
||||
continue
|
||||
}
|
||||
out, err := applyDirectory(dir, rec, true, placed)
|
||||
switch {
|
||||
case err != nil:
|
||||
failed[module] = true
|
||||
lines = append(lines, fmt.Sprintf("%s (%s) is %s and cannot be set right at this start: %v", dir.Path, dir.ID, drift, err))
|
||||
case out.asFound:
|
||||
lines = append(lines, fmt.Sprintf("%s (%s) is %s, and is left as found at this start: %s", dir.Path, dir.ID, drift, out.Detail))
|
||||
@@ -47,6 +71,14 @@ func ConvergeDirectories(d *declaration.Declaration, known store.State) []string
|
||||
lines = append(lines, fmt.Sprintf("%s (%s) was %s, and is set right at this start", dir.Path, dir.ID, drift))
|
||||
}
|
||||
}
|
||||
for module := range stopped {
|
||||
if failed[module] || sys == nil {
|
||||
continue
|
||||
}
|
||||
if _, err := StartModule(ctx, sys, known, module, run); err != nil {
|
||||
lines = append(lines, fmt.Sprintf("the units of %s stopped for its owner pass could not be started again: %v", module, err))
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
|
||||
@@ -56,13 +56,13 @@ func testADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs(t *testing.T) {
|
||||
if drifts := DirectoryDrifts(postgres, state); len(drifts) != 1 || drifts[0].Drift != "mode 0700, 0750 declared" {
|
||||
t.Errorf("the drift read is %+v", drifts)
|
||||
}
|
||||
if lines := ConvergeDirectories(postgres, state); len(lines) != 1 || !strings.Contains(lines[0], "was mode 0700, 0750 declared, and is set right") {
|
||||
if lines := ConvergeDirectories(context.Background(), archHost(t), postgres, state, noServices); len(lines) != 1 || !strings.Contains(lines[0], "was mode 0700, 0750 declared, and is set right") {
|
||||
t.Errorf("a drifted directory the mesh made was converged as %q", lines)
|
||||
}
|
||||
if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 {
|
||||
t.Errorf("it was not set right at the start: %04o", info.Mode().Perm())
|
||||
}
|
||||
if lines := ConvergeDirectories(postgres, state); len(lines) != 0 {
|
||||
if lines := ConvergeDirectories(context.Background(), archHost(t), postgres, state, noServices); len(lines) != 0 {
|
||||
t.Errorf("a directory as declared was said at the start: %q", lines)
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func testADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs(t *testing.T) {
|
||||
if !strings.Contains(strings.Join(said, "\n"), "WARNING notes.data") {
|
||||
t.Errorf("a directory found outside the mesh's tree was not used as found: %q", said)
|
||||
}
|
||||
if lines := ConvergeDirectories(declare(t, decl), found); len(lines) != 1 || !strings.Contains(lines[0], "left as found") {
|
||||
if lines := ConvergeDirectories(context.Background(), archHost(t), declare(t, decl), found, noServices); len(lines) != 1 || !strings.Contains(lines[0], "left as found") {
|
||||
t.Errorf("a directory the mesh never made was converged as %q", lines)
|
||||
}
|
||||
if drifts := DirectoryDrifts(declare(t, decl), found); len(drifts) != 0 {
|
||||
|
||||
+49
-8
@@ -12,14 +12,9 @@ import (
|
||||
"github.com/novox/mesh-host/internal/system"
|
||||
)
|
||||
|
||||
// StopModule stops the units and containers the node's record holds for a module and removes nothing: no unit, file,
|
||||
// container, data or record, so what the module kept survives and a later Apply starts the build it is given (design 47
|
||||
// G A-98, §297). Units come first, a timer before the service it starts, then containers; a unit the mesh never held
|
||||
// (Stateless) is left as it is. The module's processes are stopped with them (design 47 H2, H-A-641): the timer and the
|
||||
// service of each, but never the tool runner's own process, which serves every other module's bundles. It answers what it
|
||||
// stopped, and the first failure.
|
||||
func StopModule(ctx context.Context, sys system.System, known store.State, module string, run Runner) ([]string, error) {
|
||||
var timers, services, containers []store.Applied
|
||||
// unitsOf is the timers, services and containers the node's record holds for a module, its processes' units among them but
|
||||
// never the tool runner's own (H-A-641).
|
||||
func unitsOf(known store.State, module string) (timers, services, containers []store.Applied) {
|
||||
for _, r := range known.Resources {
|
||||
if store.OwnerOf(r, declaration.ModuleGuess) != module {
|
||||
continue
|
||||
@@ -45,6 +40,52 @@ func StopModule(ctx context.Context, sys system.System, known store.State, modul
|
||||
services = append(services, store.Applied{ID: r.ID, Type: string(declaration.TypeService), Target: r.Target + ".service"})
|
||||
}
|
||||
}
|
||||
return timers, services, containers
|
||||
}
|
||||
|
||||
// StartModule starts again what StopModule stopped: the module's services and timers found stopped, services first, and its
|
||||
// containers found stopped (design 47 H2, H-A-641: a pass at the engine's start, with no apply after it, starts the units
|
||||
// it stopped). It answers what it started, and the first failure.
|
||||
func StartModule(ctx context.Context, sys system.System, known store.State, module string, run Runner) ([]string, error) {
|
||||
timers, services, containers := unitsOf(known, module)
|
||||
var started []string
|
||||
for _, r := range append(services, timers...) {
|
||||
manager := managerFor(r.Scope, r.User, run)
|
||||
if state, err := sys.ServiceState(ctx, manager, r.Target); err != nil || state != "stopped" {
|
||||
continue
|
||||
}
|
||||
if err := sys.SetServiceState(ctx, manager, r.Target, "running"); err != nil {
|
||||
return started, fmt.Errorf("starting %s of %s: %w", r.Target, module, err)
|
||||
}
|
||||
started = append(started, r.Target)
|
||||
}
|
||||
if len(containers) == 0 {
|
||||
return started, nil
|
||||
}
|
||||
cri, err := containerRuntime(ctx, run)
|
||||
if err != nil {
|
||||
return started, err
|
||||
}
|
||||
for _, r := range containers {
|
||||
if state, err := containerState(ctx, r.Target, run); err != nil || state.Running {
|
||||
continue
|
||||
}
|
||||
if _, err := run(ctx, cri, "start", r.Target); err != nil {
|
||||
return started, fmt.Errorf("starting the container %s of %s: %w", r.Target, module, err)
|
||||
}
|
||||
started = append(started, r.Target)
|
||||
}
|
||||
return started, nil
|
||||
}
|
||||
|
||||
// StopModule stops the units and containers the node's record holds for a module and removes nothing: no unit, file,
|
||||
// container, data or record, so what the module kept survives and a later Apply starts the build it is given (design 47
|
||||
// G A-98, §297). Units come first, a timer before the service it starts, then containers; a unit the mesh never held
|
||||
// (Stateless) is left as it is. The module's processes are stopped with them (design 47 H2, H-A-641): the timer and the
|
||||
// service of each, but never the tool runner's own process, which serves every other module's bundles. It answers what it
|
||||
// stopped, and the first failure.
|
||||
func StopModule(ctx context.Context, sys system.System, known store.State, module string, run Runner) ([]string, error) {
|
||||
timers, services, containers := unitsOf(known, module)
|
||||
var stopped []string
|
||||
for _, r := range append(timers, services...) {
|
||||
manager := managerFor(r.Scope, r.User, run)
|
||||
|
||||
Reference in New Issue
Block a user