Serve daemon-managed .vm names directly from bangerd on 127.0.0.1:42069 instead of shelling out to mapdns. This keeps DNS state tied to VM lifecycle and lets the daemon rebuild records from running VMs after startup or reconcile. Add a small in-process authoritative DNS server, register and remove records from the VM start/stop/delete paths, and show the listener in daemon status. Remove the mapdns config and preflight surface, stop helper-flow DNS publishing in customize.sh and interactive.sh, drop dns.sh from the runtime bundle, and update docs/tests for the new local-resolver integration model. Validated with GOCACHE=/tmp/banger-gocache go test ./..., GOCACHE=/tmp/banger-gocache make build, and bash -n customize.sh interactive.sh.
165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
|
|
"banger/internal/api"
|
|
"banger/internal/model"
|
|
"banger/internal/paths"
|
|
)
|
|
|
|
func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (image model.Image, err error) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
op := d.beginOperation("image.build")
|
|
buildLogPath := ""
|
|
defer func() {
|
|
if err != nil {
|
|
err = annotateLogPath(err, buildLogPath)
|
|
op.fail(err, imageLogAttrs(image)...)
|
|
return
|
|
}
|
|
op.done(imageLogAttrs(image)...)
|
|
}()
|
|
|
|
name := params.Name
|
|
if name == "" {
|
|
name = fmt.Sprintf("image-%d", model.Now().Unix())
|
|
}
|
|
if _, err := d.FindImage(ctx, name); err == nil {
|
|
return model.Image{}, fmt.Errorf("image name already exists: %s", name)
|
|
}
|
|
baseRootfs := params.BaseRootfs
|
|
if baseRootfs == "" {
|
|
baseRootfs = d.config.DefaultBaseRootfs
|
|
}
|
|
if baseRootfs == "" {
|
|
return model.Image{}, fmt.Errorf("base rootfs is required; %s", paths.RuntimeBundleHint())
|
|
}
|
|
id, err := model.NewID()
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
now := model.Now()
|
|
artifactDir := filepath.Join(d.layout.ImagesDir, id)
|
|
if err := os.MkdirAll(artifactDir, 0o755); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
buildLogDir := filepath.Join(d.layout.StateDir, "image-build")
|
|
if err := os.MkdirAll(buildLogDir, 0o755); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
buildLogPath = filepath.Join(buildLogDir, id+".log")
|
|
logFile, err := os.OpenFile(buildLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
defer logFile.Close()
|
|
rootfsPath := filepath.Join(artifactDir, "rootfs.ext4")
|
|
script := d.config.CustomizeScript
|
|
if script == "" {
|
|
return model.Image{}, fmt.Errorf("customize script not configured; %s", paths.RuntimeBundleHint())
|
|
}
|
|
if _, err := os.Stat(script); err != nil {
|
|
return model.Image{}, fmt.Errorf("customize.sh not found at %s; %s", script, paths.RuntimeBundleHint())
|
|
}
|
|
args := []string{script, baseRootfs, "--out", rootfsPath}
|
|
if params.Size != "" {
|
|
args = append(args, "--size", params.Size)
|
|
}
|
|
kernelPath := params.KernelPath
|
|
if kernelPath == "" {
|
|
kernelPath = d.config.DefaultKernel
|
|
}
|
|
if kernelPath != "" {
|
|
args = append(args, "--kernel", kernelPath)
|
|
}
|
|
initrdPath := params.InitrdPath
|
|
if initrdPath == "" {
|
|
initrdPath = d.config.DefaultInitrd
|
|
}
|
|
if initrdPath != "" {
|
|
args = append(args, "--initrd", initrdPath)
|
|
}
|
|
modulesDir := params.ModulesDir
|
|
if modulesDir == "" {
|
|
modulesDir = d.config.DefaultModulesDir
|
|
}
|
|
if modulesDir != "" {
|
|
args = append(args, "--modules", modulesDir)
|
|
}
|
|
if params.Docker {
|
|
args = append(args, "--docker")
|
|
}
|
|
if err := d.validateImageBuildPrereqs(ctx, baseRootfs, kernelPath, initrdPath, modulesDir); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
op.stage("launch_helper", "script", script, "build_log_path", buildLogPath, "artifact_dir", artifactDir)
|
|
cmd := exec.CommandContext(ctx, "bash", args...)
|
|
cmd.Stdout = logFile
|
|
cmd.Stderr = logFile
|
|
cmd.Stdin = nil
|
|
cmd.Dir = d.layout.StateDir
|
|
cmd.Env = append(
|
|
os.Environ(),
|
|
"BANGER_RUNTIME_DIR="+d.config.RuntimeDir,
|
|
"BANGER_STATE_DIR="+filepath.Join(d.layout.StateDir, "image-build"),
|
|
)
|
|
if err := cmd.Run(); err != nil {
|
|
_ = os.RemoveAll(artifactDir)
|
|
return model.Image{}, err
|
|
}
|
|
image = model.Image{
|
|
ID: id,
|
|
Name: name,
|
|
Managed: true,
|
|
ArtifactDir: artifactDir,
|
|
RootfsPath: rootfsPath,
|
|
KernelPath: kernelPath,
|
|
InitrdPath: initrdPath,
|
|
ModulesDir: modulesDir,
|
|
PackagesPath: d.config.DefaultPackagesFile,
|
|
BuildSize: params.Size,
|
|
Docker: params.Docker,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := d.store.UpsertImage(ctx, image); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
op.stage("persisted", "build_log_path", buildLogPath)
|
|
if d.logger != nil {
|
|
d.logger.Info("image build log preserved", append(imageLogAttrs(image), "build_log_path", buildLogPath)...)
|
|
}
|
|
return image, nil
|
|
}
|
|
|
|
func (d *Daemon) DeleteImage(ctx context.Context, idOrName string) (model.Image, error) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
|
|
image, err := d.FindImage(ctx, idOrName)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
vms, err := d.store.FindVMsUsingImage(ctx, image.ID)
|
|
if err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
if len(vms) > 0 {
|
|
return model.Image{}, fmt.Errorf("image %s is still referenced by %d VM(s)", image.Name, len(vms))
|
|
}
|
|
if err := d.store.DeleteImage(ctx, image.ID); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
if image.Managed && image.ArtifactDir != "" {
|
|
if err := os.RemoveAll(image.ArtifactDir); err != nil {
|
|
return model.Image{}, err
|
|
}
|
|
}
|
|
return image, nil
|
|
}
|