Fix the misleading make install path where banger and bangerd still depended on a repo checkout for Firecracker, guest artifacts, image builds, and the SSH key. Replace repo-root inference with an explicit runtime bundle model: resolve a runtime_dir from env/config/install layout, derive concrete artifact paths from it, and update the daemon, CLI, and image-build flow to use those paths. Keep repo_root only as an explicit compatibility alias instead of auto-detecting it. Teach customize.sh to run from a read-only bundled runtime tree while writing transient state under XDG/BANGER_STATE_DIR, and make make install copy the runtime assets into PREFIX/lib/banger so installed binaries stay usable outside the repo. Validate with go test ./..., make build, bash -n customize.sh, and make install DESTDIR=/tmp/banger-install PREFIX=/usr. An out-of-repo installed-binary smoke test was attempted, but this sandbox blocked bangerd from binding its Unix socket (setsockopt: operation not permitted).
181 lines
5.3 KiB
Go
181 lines
5.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
toml "github.com/pelletier/go-toml"
|
|
|
|
"banger/internal/model"
|
|
"banger/internal/paths"
|
|
)
|
|
|
|
type fileConfig struct {
|
|
RuntimeDir string `toml:"runtime_dir"`
|
|
RepoRoot string `toml:"repo_root"`
|
|
FirecrackerBin string `toml:"firecracker_bin"`
|
|
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{
|
|
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)
|
|
applyRuntimeDefaults(&cfg)
|
|
|
|
if file.FirecrackerBin != "" {
|
|
cfg.FirecrackerBin = file.FirecrackerBin
|
|
}
|
|
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
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func applyRuntimeDefaults(cfg *model.DaemonConfig) {
|
|
if cfg.RuntimeDir == "" {
|
|
return
|
|
}
|
|
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")
|
|
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,
|
|
)
|
|
}
|
|
}
|
|
|
|
func defaultRuntimePath(current, runtimeDir, relative string) string {
|
|
if current != "" {
|
|
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 ""
|
|
}
|