banger/internal/system/preflight.go
Thales Maciel fcedacba5c
Make runtime defaults portable
Stop assuming one workstation layout for runtime artifacts, mapdns, and host tooling. The daemon and shell helpers now use portable mapdns configuration, and runtime bundles can carry bundle.json metadata for their default kernel, initrd, modules, rootfs, and helper paths.

Load bundle metadata through config with a legacy layout fallback, thread mapdns_bin/mapdns_data_file through the Go and shell paths, and add command-scoped preflight checks for VM start, NAT, image build, work-disk resize, and SSH so missing tools or artifacts fail with actionable errors.

Update the runtime-bundle manifest, docs, and tests to match the new model. Verified with go test ./..., make build, and bash -n customize.sh interactive.sh dns.sh make-rootfs.sh verify.sh.
2026-03-16 15:30:08 -03:00

112 lines
2.5 KiB
Go

package system
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
type Preflight struct {
problems []string
}
func NewPreflight() *Preflight {
return &Preflight{}
}
func (p *Preflight) RequireCommand(name, hint string) {
value := strings.TrimSpace(name)
if value == "" {
p.add("command name is not configured%s", formatHint(hint))
return
}
if _, err := exec.LookPath(value); err != nil {
p.add("required command %q not found%s", value, formatHint(hint))
}
}
func (p *Preflight) RequireExecutable(pathOrName, label, hint string) {
value := strings.TrimSpace(pathOrName)
if value == "" {
p.add("%s is not configured%s", label, formatHint(hint))
return
}
if strings.ContainsRune(value, filepath.Separator) {
info, err := os.Stat(value)
if err != nil {
p.add("%s not found at %s%s", label, value, formatHint(hint))
return
}
if info.IsDir() || info.Mode()&0o111 == 0 {
p.add("%s is not executable at %s%s", label, value, formatHint(hint))
}
return
}
if _, err := exec.LookPath(value); err != nil {
p.add("missing %s %q%s", label, value, formatHint(hint))
}
}
func (p *Preflight) RequireFile(path, label, hint string) {
value := strings.TrimSpace(path)
if value == "" {
p.add("%s is not configured%s", label, formatHint(hint))
return
}
info, err := os.Stat(value)
if err != nil {
p.add("%s not found at %s%s", label, value, formatHint(hint))
return
}
if info.IsDir() {
p.add("%s expected a file at %s%s", label, value, formatHint(hint))
}
}
func (p *Preflight) RequireDir(path, label, hint string) {
value := strings.TrimSpace(path)
if value == "" {
p.add("%s is not configured%s", label, formatHint(hint))
return
}
info, err := os.Stat(value)
if err != nil {
p.add("%s not found at %s%s", label, value, formatHint(hint))
return
}
if !info.IsDir() {
p.add("%s expected a directory at %s%s", label, value, formatHint(hint))
}
}
func (p *Preflight) Addf(format string, args ...any) {
p.add(format, args...)
}
func (p *Preflight) Err(prefix string) error {
if len(p.problems) == 0 {
return nil
}
var builder strings.Builder
builder.WriteString(strings.TrimSpace(prefix))
for _, problem := range p.problems {
builder.WriteString("\n- ")
builder.WriteString(problem)
}
return errors.New(builder.String())
}
func (p *Preflight) add(format string, args ...any) {
p.problems = append(p.problems, fmt.Sprintf(format, args...))
}
func formatHint(hint string) string {
hint = strings.TrimSpace(hint)
if hint == "" {
return ""
}
return " (" + hint + ")"
}