Make installed banger self-contained

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).
This commit is contained in:
Thales Maciel 2026-03-16 14:26:50 -03:00
parent 375900cf65
commit ce1be52047
No known key found for this signature in database
GPG key ID: 33112E6833C34679
13 changed files with 437 additions and 107 deletions

View file

@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
@ -66,57 +65,64 @@ func Ensure(layout Layout) error {
return nil
}
func DetectRepoRoot() string {
if env := os.Getenv("BANGER_REPO_ROOT"); env != "" {
return env
}
candidates := []string{}
if wd, err := os.Getwd(); err == nil {
candidates = append(candidates, wd)
}
if exe, err := os.Executable(); err == nil {
candidates = append(candidates, filepath.Dir(exe))
}
if look, err := exec.LookPath("firecracker"); err == nil {
candidates = append(candidates, filepath.Dir(look))
}
for _, candidate := range candidates {
if root := walkForRepoRoot(candidate); root != "" {
return root
var executablePath = os.Executable
func ResolveRuntimeDir(configuredRuntimeDir, deprecatedRepoRoot string) string {
for _, candidate := range []string{
os.Getenv("BANGER_RUNTIME_DIR"),
os.Getenv("BANGER_REPO_ROOT"),
configuredRuntimeDir,
deprecatedRepoRoot,
} {
if candidate = strings.TrimSpace(candidate); candidate != "" {
return filepath.Clean(candidate)
}
}
exe, err := executablePath()
if err != nil {
return ""
}
exeDir := filepath.Dir(exe)
if filepath.Base(exeDir) == "bin" {
installRuntimeDir := filepath.Clean(filepath.Join(exeDir, "..", "lib", "banger"))
if HasRuntimeBundle(installRuntimeDir) {
return installRuntimeDir
}
}
if HasRuntimeBundle(exeDir) {
return exeDir
}
return ""
}
func walkForRepoRoot(start string) string {
current := start
for {
if hasRepoArtifacts(current) {
return current
}
parent := filepath.Dir(current)
if parent == current {
return ""
}
current = parent
func HasRuntimeBundle(dir string) bool {
if strings.TrimSpace(dir) == "" {
return false
}
required := []string{
"firecracker",
"customize.sh",
"packages.apt",
"wtf/root/boot/vmlinux-6.8.0-94-generic",
}
}
func hasRepoArtifacts(dir string) bool {
required := []string{"firecracker", "README.md"}
for _, name := range required {
if _, err := os.Stat(filepath.Join(dir, name)); err != nil {
return false
}
}
return true
for _, name := range []string{"rootfs-docker.ext4", "rootfs.ext4"} {
if _, err := os.Stat(filepath.Join(dir, name)); err == nil {
return true
}
}
return false
}
func BangerdPath() (string, error) {
if env := os.Getenv("BANGER_DAEMON_BIN"); env != "" {
return env, nil
}
exe, err := os.Executable()
exe, err := executablePath()
if err != nil {
return "", err
}
@ -129,16 +135,6 @@ func BangerdPath() (string, error) {
return candidate, nil
}
}
if root := DetectRepoRoot(); root != "" {
for _, candidate := range []string{
filepath.Join(root, "bangerd"),
filepath.Join(root, "bangerd.exe"),
} {
if _, err := os.Stat(candidate); err == nil {
return candidate, nil
}
}
}
return "", errors.New("bangerd binary not found next to banger; build ./cmd/bangerd")
}

View file

@ -0,0 +1,69 @@
package paths
import (
"os"
"path/filepath"
"testing"
)
func TestResolveRuntimeDirPrefersEnv(t *testing.T) {
t.Setenv("BANGER_RUNTIME_DIR", "/env/runtime")
if got := ResolveRuntimeDir("/config/runtime", "/deprecated/repo"); got != "/env/runtime" {
t.Fatalf("ResolveRuntimeDir() = %q, want /env/runtime", got)
}
}
func TestResolveRuntimeDirUsesInstalledLayout(t *testing.T) {
root := t.TempDir()
runtimeDir := filepath.Join(root, "lib", "banger")
createRuntimeBundle(t, runtimeDir)
origExecutablePath := executablePath
executablePath = func() (string, error) {
return filepath.Join(root, "bin", "banger"), nil
}
t.Cleanup(func() {
executablePath = origExecutablePath
})
if got := ResolveRuntimeDir("", ""); got != runtimeDir {
t.Fatalf("ResolveRuntimeDir() = %q, want %q", got, runtimeDir)
}
}
func TestResolveRuntimeDirUsesExecutableDirectoryBundle(t *testing.T) {
root := t.TempDir()
createRuntimeBundle(t, root)
origExecutablePath := executablePath
executablePath = func() (string, error) {
return filepath.Join(root, "banger"), nil
}
t.Cleanup(func() {
executablePath = origExecutablePath
})
if got := ResolveRuntimeDir("", ""); got != root {
t.Fatalf("ResolveRuntimeDir() = %q, want %q", got, root)
}
}
func createRuntimeBundle(t *testing.T, runtimeDir string) {
t.Helper()
for _, rel := range []string{
"firecracker",
"customize.sh",
"packages.apt",
"rootfs-docker.ext4",
"wtf/root/boot/vmlinux-6.8.0-94-generic",
} {
path := filepath.Join(runtimeDir, rel)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
}
if err := os.WriteFile(path, []byte("test"), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
}