Add structured daemon lifecycle logs

VM start, image build, and network/setup failures were hard to diagnose because bangerd emitted almost no lifecycle logs and the Firecracker SDK logger was discarded. This adds a daemon-wide JSON logger with configurable log level so failures leave breadcrumbs instead of only side effects.

Log the main daemon and VM lifecycle stages, preserve raw Firecracker and image-build helper output in dedicated files, and include those log paths in daemon status and returned errors. Bridge SDK logrus output into the daemon logger at debug level so low-level Firecracker diagnostics are available without making normal info logs unreadable.

Validation: go test ./... and make build. Left unrelated worktree changes out of this commit, including internal/api/types.go, the deleted shell scripts, and my-rootfs.ext4.
This commit is contained in:
Thales Maciel 2026-03-16 16:16:28 -03:00
parent 5018bc6170
commit 644e60d739
No known key found for this signature in database
GPG key ID: 33112E6833C34679
13 changed files with 746 additions and 31 deletions

View file

@ -12,9 +12,19 @@ import (
"banger/internal/paths"
)
func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (model.Image, error) {
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 == "" {
@ -39,6 +49,16 @@ func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (m
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 == "" {
@ -78,10 +98,11 @@ func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (m
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 = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = logFile
cmd.Stderr = logFile
cmd.Stdin = nil
cmd.Dir = d.layout.StateDir
cmd.Env = append(
os.Environ(),
@ -98,7 +119,7 @@ func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (m
_ = os.RemoveAll(artifactDir)
return model.Image{}, err
}
image := model.Image{
image = model.Image{
ID: id,
Name: name,
Managed: true,
@ -116,6 +137,10 @@ func (d *Daemon) BuildImage(ctx context.Context, params api.ImageBuildParams) (m
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
}