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

121 lines
4.7 KiB
Go

package apply
import (
"context"
"fmt"
"sort"
"strings"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/compose/resolution"
"github.com/novox/mesh-host/internal/store"
"github.com/novox/mesh-host/internal/system"
)
// 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
}
switch r.Type {
case string(declaration.TypeService):
if r.Stateless {
continue
}
if strings.HasSuffix(r.Target, ".timer") {
timers = append(timers, r)
} else {
services = append(services, r)
}
case string(declaration.TypeContainer):
containers = append(containers, r)
case string(declaration.TypeProcess):
// The tool runner is never stopped, whichever module's owner pass asks (H-A-641).
if strings.HasSuffix(r.ID, "."+resolution.RuntimeProcessID()) {
continue
}
timers = append(timers, store.Applied{ID: r.ID, Type: string(declaration.TypeService), Target: r.Target + ".timer"})
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)
if state, err := sys.ServiceState(ctx, manager, r.Target); err != nil {
if strings.Contains(err.Error(), "does not exist on this machine") {
continue
}
return stopped, fmt.Errorf("stopping %s of %s: %w", r.Target, module, err)
} else if state == "stopped" {
continue
}
if err := sys.SetServiceState(ctx, manager, r.Target, "stopped"); err != nil {
return stopped, fmt.Errorf("stopping %s of %s: %w", r.Target, module, err)
}
stopped = append(stopped, r.Target)
}
if len(containers) == 0 {
return stopped, nil
}
cri, err := containerRuntime(ctx, run)
if err != nil {
return stopped, err
}
sort.Slice(containers, func(i, j int) bool { return containers[i].Target < containers[j].Target })
for _, r := range containers {
if _, err := run(ctx, cri, "stop", r.Target); err != nil {
return stopped, fmt.Errorf("stopping the container %s of %s: %w", r.Target, module, err)
}
stopped = append(stopped, r.Target)
}
return stopped, nil
}