Files
mesh-host/internal/apply/converge_dirs.go
T

145 lines
5.0 KiB
Go

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
// than declared, its declared owner and mode (design 47 H2's E lines, H-A-374, H-A-392): at the engine's start, so a
// 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(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 {
continue
}
rec, held := known.Find(dir.ID)
if !held || rec.Type != string(declaration.TypeDirectory) {
continue
}
drift, err := directoryDrift(dir)
switch {
case errors.Is(err, os.ErrNotExist) || (err == nil && drift == ""):
continue
case err != nil:
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))
default:
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
}
// DirectoryDrift is one directory entry the mesh made, as its record holds, whose owner or mode on disk differ from
// what is declared (H-A-392): its id, its path and how it differs, in words.
type DirectoryDrift struct {
ID string `json:"id"`
Path string `json:"path"`
Drift string `json:"drift"`
}
// DirectoryDrifts is each directory entry of d the mesh made, by its record in known (not used as found), whose owner or
// mode on disk differ from its entry's (H-A-392): read-only, what the verb composition's diff names as drift. A directory
// that is missing, has no record, or cannot be read says nothing here.
func DirectoryDrifts(d *declaration.Declaration, known store.State) []DirectoryDrift {
if d == nil {
return nil
}
var out []DirectoryDrift
for _, r := range d.Resources {
dir, ok := r.(*declaration.Directory)
if !ok {
continue
}
if rec, held := known.Find(dir.ID); !held || rec.Type != string(declaration.TypeDirectory) || rec.AsFound {
continue
}
if drift, err := directoryDrift(dir); err == nil && drift != "" {
out = append(out, DirectoryDrift{ID: dir.ID, Path: dir.Path, Drift: drift})
}
}
return out
}
// directoryDrift says how a directory on disk differs from its entry's owner and mode, in words; empty when it does not.
func directoryDrift(dir *declaration.Directory) (string, error) {
mode, err := dirModeOf(dir.Mode, 0o755)
if err != nil {
return "", err
}
info, err := os.Lstat(dir.Path)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", dir.Path)
}
owned, err := ownedBy(dir.Path, dir.Owner)
if err != nil {
return "", err
}
var drift string
if dirBits(info.Mode()) != dirBits(mode) {
drift = fmt.Sprintf("mode %s, %s declared", modeText(dirBits(info.Mode())), modeText(mode))
}
if !owned {
if drift != "" {
drift += ", and "
}
drift += fmt.Sprintf("owned by %s, %s declared", ownerText(info), dir.Owner)
}
return drift, nil
}