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.
236 lines
7.4 KiB
Go
236 lines
7.4 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
toml "github.com/pelletier/go-toml"
|
|
|
|
"banger/internal/model"
|
|
"banger/internal/paths"
|
|
"banger/internal/runtimebundle"
|
|
)
|
|
|
|
type fileConfig struct {
|
|
RuntimeDir string `toml:"runtime_dir"`
|
|
RepoRoot string `toml:"repo_root"`
|
|
LogLevel string `toml:"log_level"`
|
|
FirecrackerBin string `toml:"firecracker_bin"`
|
|
MapDNSBin string `toml:"mapdns_bin"`
|
|
MapDNSDataFile string `toml:"mapdns_data_file"`
|
|
SSHKeyPath string `toml:"ssh_key_path"`
|
|
NamegenPath string `toml:"namegen_path"`
|
|
CustomizeScript string `toml:"customize_script"`
|
|
DefaultImageName string `toml:"default_image_name"`
|
|
DefaultRootfs string `toml:"default_rootfs"`
|
|
DefaultBaseRootfs string `toml:"default_base_rootfs"`
|
|
DefaultKernel string `toml:"default_kernel"`
|
|
DefaultInitrd string `toml:"default_initrd"`
|
|
DefaultModulesDir string `toml:"default_modules_dir"`
|
|
DefaultPackages string `toml:"default_packages_file"`
|
|
AutoStopStaleAfter string `toml:"auto_stop_stale_after"`
|
|
StatsPollInterval string `toml:"stats_poll_interval"`
|
|
MetricsPoll string `toml:"metrics_poll_interval"`
|
|
BridgeName string `toml:"bridge_name"`
|
|
BridgeIP string `toml:"bridge_ip"`
|
|
CIDR string `toml:"cidr"`
|
|
DefaultDNS string `toml:"default_dns"`
|
|
}
|
|
|
|
func Load(layout paths.Layout) (model.DaemonConfig, error) {
|
|
cfg := model.DaemonConfig{
|
|
LogLevel: "info",
|
|
AutoStopStaleAfter: 0,
|
|
StatsPollInterval: model.DefaultStatsPollInterval,
|
|
MetricsPollInterval: model.DefaultMetricsPollInterval,
|
|
BridgeName: model.DefaultBridgeName,
|
|
BridgeIP: model.DefaultBridgeIP,
|
|
CIDR: model.DefaultCIDR,
|
|
DefaultDNS: model.DefaultDNS,
|
|
DefaultImageName: "default",
|
|
}
|
|
|
|
path := filepath.Join(layout.ConfigDir, "config.toml")
|
|
info, err := os.Stat(path)
|
|
var file fileConfig
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return cfg, err
|
|
}
|
|
} else if !info.IsDir() {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
if err := toml.Unmarshal(data, &file); err != nil {
|
|
return cfg, err
|
|
}
|
|
}
|
|
|
|
cfg.RuntimeDir = paths.ResolveRuntimeDir(file.RuntimeDir, file.RepoRoot)
|
|
if err := applyRuntimeDefaults(&cfg); err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
if file.FirecrackerBin != "" {
|
|
cfg.FirecrackerBin = file.FirecrackerBin
|
|
}
|
|
if file.LogLevel != "" {
|
|
cfg.LogLevel = file.LogLevel
|
|
}
|
|
if file.MapDNSBin != "" {
|
|
cfg.MapDNSBin = file.MapDNSBin
|
|
}
|
|
if file.MapDNSDataFile != "" {
|
|
cfg.MapDNSDataFile = file.MapDNSDataFile
|
|
}
|
|
if file.SSHKeyPath != "" {
|
|
cfg.SSHKeyPath = file.SSHKeyPath
|
|
}
|
|
if file.NamegenPath != "" {
|
|
cfg.NamegenPath = file.NamegenPath
|
|
}
|
|
if file.CustomizeScript != "" {
|
|
cfg.CustomizeScript = file.CustomizeScript
|
|
}
|
|
if file.DefaultImageName != "" {
|
|
cfg.DefaultImageName = file.DefaultImageName
|
|
}
|
|
if file.DefaultRootfs != "" {
|
|
cfg.DefaultRootfs = file.DefaultRootfs
|
|
}
|
|
if file.DefaultBaseRootfs != "" {
|
|
cfg.DefaultBaseRootfs = file.DefaultBaseRootfs
|
|
}
|
|
if file.DefaultKernel != "" {
|
|
cfg.DefaultKernel = file.DefaultKernel
|
|
}
|
|
if file.DefaultInitrd != "" {
|
|
cfg.DefaultInitrd = file.DefaultInitrd
|
|
}
|
|
if file.DefaultModulesDir != "" {
|
|
cfg.DefaultModulesDir = file.DefaultModulesDir
|
|
}
|
|
if file.DefaultPackages != "" {
|
|
cfg.DefaultPackagesFile = file.DefaultPackages
|
|
}
|
|
if file.BridgeName != "" {
|
|
cfg.BridgeName = file.BridgeName
|
|
}
|
|
if file.BridgeIP != "" {
|
|
cfg.BridgeIP = file.BridgeIP
|
|
}
|
|
if file.CIDR != "" {
|
|
cfg.CIDR = file.CIDR
|
|
}
|
|
if file.DefaultDNS != "" {
|
|
cfg.DefaultDNS = file.DefaultDNS
|
|
}
|
|
if file.AutoStopStaleAfter != "" {
|
|
duration, err := time.ParseDuration(file.AutoStopStaleAfter)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
cfg.AutoStopStaleAfter = duration
|
|
}
|
|
if file.StatsPollInterval != "" {
|
|
duration, err := time.ParseDuration(file.StatsPollInterval)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
cfg.StatsPollInterval = duration
|
|
}
|
|
if file.MetricsPoll != "" {
|
|
duration, err := time.ParseDuration(file.MetricsPoll)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
cfg.MetricsPollInterval = duration
|
|
}
|
|
if value := os.Getenv("BANGER_MAPDNS_BIN"); value != "" {
|
|
cfg.MapDNSBin = value
|
|
}
|
|
if value := os.Getenv("BANGER_MAPDNS_DATA_FILE"); value != "" {
|
|
cfg.MapDNSDataFile = value
|
|
}
|
|
if value := os.Getenv("BANGER_LOG_LEVEL"); value != "" {
|
|
cfg.LogLevel = value
|
|
}
|
|
if cfg.MapDNSBin == "" {
|
|
cfg.MapDNSBin = "mapdns"
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func applyRuntimeDefaults(cfg *model.DaemonConfig) error {
|
|
if cfg.RuntimeDir == "" {
|
|
return nil
|
|
}
|
|
meta, err := runtimebundle.LoadBundleMetadata(cfg.RuntimeDir)
|
|
switch {
|
|
case err == nil:
|
|
applyBundleMetadataDefaults(cfg, cfg.RuntimeDir, meta)
|
|
case errors.Is(err, os.ErrNotExist):
|
|
applyLegacyRuntimeDefaults(cfg)
|
|
default:
|
|
return err
|
|
}
|
|
if cfg.DefaultRootfs == "" {
|
|
cfg.DefaultRootfs = firstExistingRuntimePath(
|
|
filepath.Join(cfg.RuntimeDir, "rootfs-docker.ext4"),
|
|
filepath.Join(cfg.RuntimeDir, "rootfs.ext4"),
|
|
)
|
|
}
|
|
if cfg.DefaultBaseRootfs == "" {
|
|
cfg.DefaultBaseRootfs = firstExistingRuntimePath(
|
|
filepath.Join(cfg.RuntimeDir, "rootfs.ext4"),
|
|
cfg.DefaultRootfs,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func applyBundleMetadataDefaults(cfg *model.DaemonConfig, runtimeDir string, meta runtimebundle.BundleMetadata) {
|
|
cfg.FirecrackerBin = defaultRuntimePath(cfg.FirecrackerBin, runtimeDir, meta.FirecrackerBin)
|
|
cfg.SSHKeyPath = defaultRuntimePath(cfg.SSHKeyPath, runtimeDir, meta.SSHKeyPath)
|
|
cfg.NamegenPath = defaultRuntimePath(cfg.NamegenPath, runtimeDir, meta.NamegenPath)
|
|
cfg.CustomizeScript = defaultRuntimePath(cfg.CustomizeScript, runtimeDir, meta.CustomizeScript)
|
|
cfg.DefaultKernel = defaultRuntimePath(cfg.DefaultKernel, runtimeDir, meta.DefaultKernel)
|
|
cfg.DefaultInitrd = defaultRuntimePath(cfg.DefaultInitrd, runtimeDir, meta.DefaultInitrd)
|
|
cfg.DefaultModulesDir = defaultRuntimePath(cfg.DefaultModulesDir, runtimeDir, meta.DefaultModulesDir)
|
|
cfg.DefaultPackagesFile = defaultRuntimePath(cfg.DefaultPackagesFile, runtimeDir, meta.DefaultPackages)
|
|
cfg.DefaultRootfs = defaultRuntimePath(cfg.DefaultRootfs, runtimeDir, meta.DefaultRootfs)
|
|
cfg.DefaultBaseRootfs = defaultRuntimePath(cfg.DefaultBaseRootfs, runtimeDir, meta.DefaultBaseRootfs)
|
|
}
|
|
|
|
func applyLegacyRuntimeDefaults(cfg *model.DaemonConfig) {
|
|
cfg.FirecrackerBin = defaultRuntimePath(cfg.FirecrackerBin, cfg.RuntimeDir, "firecracker")
|
|
cfg.SSHKeyPath = defaultRuntimePath(cfg.SSHKeyPath, cfg.RuntimeDir, "id_ed25519")
|
|
cfg.NamegenPath = defaultRuntimePath(cfg.NamegenPath, cfg.RuntimeDir, "namegen")
|
|
cfg.CustomizeScript = defaultRuntimePath(cfg.CustomizeScript, cfg.RuntimeDir, "customize.sh")
|
|
cfg.DefaultKernel = defaultRuntimePath(cfg.DefaultKernel, cfg.RuntimeDir, "wtf/root/boot/vmlinux-6.8.0-94-generic")
|
|
cfg.DefaultInitrd = defaultRuntimePath(cfg.DefaultInitrd, cfg.RuntimeDir, "wtf/root/boot/initrd.img-6.8.0-94-generic")
|
|
cfg.DefaultModulesDir = defaultRuntimePath(cfg.DefaultModulesDir, cfg.RuntimeDir, "wtf/root/lib/modules/6.8.0-94-generic")
|
|
cfg.DefaultPackagesFile = defaultRuntimePath(cfg.DefaultPackagesFile, cfg.RuntimeDir, "packages.apt")
|
|
}
|
|
|
|
func defaultRuntimePath(current, runtimeDir, relative string) string {
|
|
if current != "" || relative == "" {
|
|
return current
|
|
}
|
|
return filepath.Join(runtimeDir, relative)
|
|
}
|
|
|
|
func firstExistingRuntimePath(paths ...string) string {
|
|
for _, candidate := range paths {
|
|
if candidate == "" {
|
|
continue
|
|
}
|
|
if _, err := os.Stat(candidate); err == nil {
|
|
return candidate
|
|
}
|
|
}
|
|
return ""
|
|
}
|