(d *Daemon).PullImage downloads an OCI image, flattens it into an
ext4 rootfs, and registers the result as a managed banger image.
Flow (internal/daemon/images_pull.go):
1. Parse + validate the OCI ref via go-containerregistry/name.
2. Derive a friendly default name from the ref ("debian-bookworm")
when --name is omitted.
3. Reject if an image with that name already exists.
4. Resolve kernel info via the new shared resolveKernelInputs
helper (refactored out of RegisterImage); ValidateKernelPaths
checks the kernel triple alone.
5. Acquire imageOpsMu, generate a fresh image id, and stage at
<ImagesDir>/<id>.staging.
6. imagepull.Pull → cache layers under OCICacheDir;
imagepull.Flatten → temp rootfs tree under os.TempDir (so the
state filesystem doesn't temporarily double in size).
7. Default size: max(treeSize × 1.25, 1 GiB); --size override
accepted.
8. imagepull.BuildExt4 produces the rootfs.ext4 in the staging dir.
9. imagemgr.StageBootArtifacts stages the kernel/initrd/modules
into the same dir (reused unchanged).
10. Atomic os.Rename(staging, finalDir) publishes the artifact dir.
11. Persist model.Image with Managed=true. Failure at any step
removes the staging dir; failure post-rename removes finalDir.
The pullAndFlatten field on Daemon is the test seam: tests stub it
to write a fixture tree into destDir and skip the real registry.
Refactor: extracted the "kernel-ref vs direct paths" resolution
out of RegisterImage into d.resolveKernelInputs so PullImage and
RegisterImage share one source of truth for that policy. Split
ValidateRegisterPaths into a kernel-only ValidateKernelPaths so
PullImage (which produces the rootfs itself) can validate just
the kernel triple without the rootfs check.
API: ImagePullParams { Ref, Name, KernelPath, InitrdPath,
ModulesDir, KernelRef, SizeBytes }. RPC dispatch case image.pull
mirrors image.register.
Tests cover: happy-path producing a managed image with all four
artifacts present + staging cleaned up, name-collision rejection,
missing-kernel rejection, and staging cleanup on a failed pull.
defaultImageNameFromRef handles tag/digest/no-suffix cases.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
376 lines
11 KiB
Go
376 lines
11 KiB
Go
package api
|
|
|
|
import (
|
|
"time"
|
|
|
|
"banger/internal/model"
|
|
)
|
|
|
|
type Empty struct{}
|
|
|
|
type PingResult struct {
|
|
Status string `json:"status"`
|
|
PID int `json:"pid"`
|
|
WebURL string `json:"web_url,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
Commit string `json:"commit,omitempty"`
|
|
BuiltAt string `json:"built_at,omitempty"`
|
|
}
|
|
|
|
type ShutdownResult struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type VMCreateParams struct {
|
|
Name string `json:"name,omitempty"`
|
|
ImageName string `json:"image_name,omitempty"`
|
|
VCPUCount *int `json:"vcpu_count,omitempty"`
|
|
MemoryMiB *int `json:"memory_mib,omitempty"`
|
|
SystemOverlaySize string `json:"system_overlay_size,omitempty"`
|
|
WorkDiskSize string `json:"work_disk_size,omitempty"`
|
|
NATEnabled bool `json:"nat_enabled,omitempty"`
|
|
NoStart bool `json:"no_start,omitempty"`
|
|
}
|
|
|
|
type VMCreateStatusParams struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
type VMCreateOperation struct {
|
|
ID string `json:"id"`
|
|
VMID string `json:"vm_id,omitempty"`
|
|
VMName string `json:"vm_name,omitempty"`
|
|
Stage string `json:"stage,omitempty"`
|
|
Detail string `json:"detail,omitempty"`
|
|
StartedAt time.Time `json:"started_at,omitempty"`
|
|
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
|
Done bool `json:"done"`
|
|
Success bool `json:"success"`
|
|
Error string `json:"error,omitempty"`
|
|
VM *model.VMRecord `json:"vm,omitempty"`
|
|
}
|
|
|
|
type VMCreateBeginResult struct {
|
|
Operation VMCreateOperation `json:"operation"`
|
|
}
|
|
|
|
type VMCreateStatusResult struct {
|
|
Operation VMCreateOperation `json:"operation"`
|
|
}
|
|
|
|
type ImageBuildStatusParams struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
type ImageBuildOperation struct {
|
|
ID string `json:"id"`
|
|
ImageID string `json:"image_id,omitempty"`
|
|
ImageName string `json:"image_name,omitempty"`
|
|
Stage string `json:"stage,omitempty"`
|
|
Detail string `json:"detail,omitempty"`
|
|
BuildLogPath string `json:"build_log_path,omitempty"`
|
|
StartedAt time.Time `json:"started_at,omitempty"`
|
|
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
|
Done bool `json:"done"`
|
|
Success bool `json:"success"`
|
|
Error string `json:"error,omitempty"`
|
|
Image *model.Image `json:"image,omitempty"`
|
|
}
|
|
|
|
type ImageBuildBeginResult struct {
|
|
Operation ImageBuildOperation `json:"operation"`
|
|
}
|
|
|
|
type ImageBuildStatusResult struct {
|
|
Operation ImageBuildOperation `json:"operation"`
|
|
}
|
|
|
|
type VMRefParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
}
|
|
|
|
type VMKillParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
Signal string `json:"signal,omitempty"`
|
|
}
|
|
|
|
type VMSetParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
VCPUCount *int `json:"vcpu_count,omitempty"`
|
|
MemoryMiB *int `json:"memory_mib,omitempty"`
|
|
WorkDiskSize string `json:"work_disk_size,omitempty"`
|
|
NATEnabled *bool `json:"nat_enabled,omitempty"`
|
|
}
|
|
|
|
type VMListResult struct {
|
|
VMs []model.VMRecord `json:"vms"`
|
|
}
|
|
|
|
type VMShowResult struct {
|
|
VM model.VMRecord `json:"vm"`
|
|
}
|
|
|
|
type VMStatsResult struct {
|
|
VM model.VMRecord `json:"vm"`
|
|
Stats model.VMStats `json:"stats"`
|
|
}
|
|
|
|
type VMLogsResult struct {
|
|
LogPath string `json:"log_path"`
|
|
}
|
|
|
|
type VMSSHResult struct {
|
|
Name string `json:"name"`
|
|
GuestIP string `json:"guest_ip"`
|
|
}
|
|
|
|
type VMHealthResult struct {
|
|
Name string `json:"name"`
|
|
Healthy bool `json:"healthy"`
|
|
}
|
|
|
|
type VMPingResult struct {
|
|
Name string `json:"name"`
|
|
Alive bool `json:"alive"`
|
|
}
|
|
|
|
type VMPort struct {
|
|
Proto string `json:"proto"`
|
|
BindAddress string `json:"bind_address,omitempty"`
|
|
Port int `json:"port"`
|
|
PID int `json:"pid,omitempty"`
|
|
Process string `json:"process,omitempty"`
|
|
Command string `json:"command,omitempty"`
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
}
|
|
|
|
type VMPortsResult struct {
|
|
Name string `json:"name"`
|
|
DNSName string `json:"dns_name,omitempty"`
|
|
Ports []VMPort `json:"ports"`
|
|
}
|
|
|
|
type GuestSessionStartParams struct {
|
|
VMIDOrName string `json:"vm_id_or_name"`
|
|
Name string `json:"name,omitempty"`
|
|
Command string `json:"command"`
|
|
Args []string `json:"args,omitempty"`
|
|
CWD string `json:"cwd,omitempty"`
|
|
Env map[string]string `json:"env,omitempty"`
|
|
StdinMode string `json:"stdin_mode,omitempty"`
|
|
Tags map[string]string `json:"tags,omitempty"`
|
|
RequiredCommands []string `json:"required_commands,omitempty"`
|
|
}
|
|
|
|
type GuestSessionRefParams struct {
|
|
VMIDOrName string `json:"vm_id_or_name"`
|
|
SessionIDOrName string `json:"session_id_or_name"`
|
|
}
|
|
|
|
type GuestSessionLogsParams struct {
|
|
VMIDOrName string `json:"vm_id_or_name"`
|
|
SessionIDOrName string `json:"session_id_or_name"`
|
|
Stream string `json:"stream,omitempty"`
|
|
TailLines int `json:"tail_lines,omitempty"`
|
|
}
|
|
|
|
type GuestSessionAttachBeginParams struct {
|
|
VMIDOrName string `json:"vm_id_or_name"`
|
|
SessionIDOrName string `json:"session_id_or_name"`
|
|
}
|
|
|
|
type GuestSessionListResult struct {
|
|
Sessions []model.GuestSession `json:"sessions"`
|
|
}
|
|
|
|
type GuestSessionShowResult struct {
|
|
Session model.GuestSession `json:"session"`
|
|
}
|
|
|
|
type GuestSessionLogsResult struct {
|
|
Session model.GuestSession `json:"session"`
|
|
Stream string `json:"stream"`
|
|
Path string `json:"path,omitempty"`
|
|
Content string `json:"content,omitempty"`
|
|
}
|
|
|
|
type GuestSessionAttachBeginResult struct {
|
|
Session model.GuestSession `json:"session"`
|
|
AttachID string `json:"attach_id"`
|
|
TransportKind string `json:"transport_kind"`
|
|
TransportTarget string `json:"transport_target"`
|
|
SocketPath string `json:"socket_path,omitempty"`
|
|
StreamFormat string `json:"stream_format"`
|
|
}
|
|
|
|
type GuestSessionSendParams struct {
|
|
VMIDOrName string `json:"vm_id_or_name"`
|
|
SessionIDOrName string `json:"session_id_or_name"`
|
|
Payload []byte `json:"payload"`
|
|
}
|
|
|
|
type GuestSessionSendResult struct {
|
|
Session model.GuestSession `json:"session"`
|
|
BytesWritten int `json:"bytes_written"`
|
|
}
|
|
|
|
type WorkspaceExportParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
GuestPath string `json:"guest_path,omitempty"`
|
|
BaseCommit string `json:"base_commit,omitempty"`
|
|
}
|
|
|
|
type WorkspaceExportResult struct {
|
|
GuestPath string `json:"guest_path"`
|
|
BaseCommit string `json:"base_commit"`
|
|
Patch []byte `json:"patch"`
|
|
ChangedFiles []string `json:"changed_files"`
|
|
HasChanges bool `json:"has_changes"`
|
|
}
|
|
|
|
type VMWorkspacePrepareParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
SourcePath string `json:"source_path"`
|
|
GuestPath string `json:"guest_path,omitempty"`
|
|
Branch string `json:"branch,omitempty"`
|
|
From string `json:"from,omitempty"`
|
|
Mode string `json:"mode,omitempty"`
|
|
ReadOnly bool `json:"readonly,omitempty"`
|
|
}
|
|
|
|
type VMWorkspacePrepareResult struct {
|
|
Workspace model.WorkspacePrepareResult `json:"workspace"`
|
|
}
|
|
|
|
type ImageBuildParams struct {
|
|
Name string `json:"name,omitempty"`
|
|
FromImage string `json:"from_image,omitempty"`
|
|
Size string `json:"size,omitempty"`
|
|
KernelPath string `json:"kernel_path,omitempty"`
|
|
InitrdPath string `json:"initrd_path,omitempty"`
|
|
ModulesDir string `json:"modules_dir,omitempty"`
|
|
Docker bool `json:"docker,omitempty"`
|
|
}
|
|
|
|
type ImageRegisterParams struct {
|
|
Name string `json:"name,omitempty"`
|
|
RootfsPath string `json:"rootfs_path,omitempty"`
|
|
WorkSeedPath string `json:"work_seed_path,omitempty"`
|
|
KernelPath string `json:"kernel_path,omitempty"`
|
|
InitrdPath string `json:"initrd_path,omitempty"`
|
|
ModulesDir string `json:"modules_dir,omitempty"`
|
|
KernelRef string `json:"kernel_ref,omitempty"`
|
|
Docker bool `json:"docker,omitempty"`
|
|
}
|
|
|
|
type ImagePullParams struct {
|
|
Ref string `json:"ref"`
|
|
Name string `json:"name,omitempty"`
|
|
KernelPath string `json:"kernel_path,omitempty"`
|
|
InitrdPath string `json:"initrd_path,omitempty"`
|
|
ModulesDir string `json:"modules_dir,omitempty"`
|
|
KernelRef string `json:"kernel_ref,omitempty"`
|
|
SizeBytes int64 `json:"size_bytes,omitempty"`
|
|
}
|
|
|
|
type ImageRefParams struct {
|
|
IDOrName string `json:"id_or_name"`
|
|
}
|
|
|
|
type ImageListResult struct {
|
|
Images []model.Image `json:"images"`
|
|
}
|
|
|
|
type ImageShowResult struct {
|
|
Image model.Image `json:"image"`
|
|
}
|
|
|
|
type KernelEntry struct {
|
|
Name string `json:"name"`
|
|
Distro string `json:"distro,omitempty"`
|
|
Arch string `json:"arch,omitempty"`
|
|
KernelVersion string `json:"kernel_version,omitempty"`
|
|
SHA256 string `json:"sha256,omitempty"`
|
|
Source string `json:"source,omitempty"`
|
|
ImportedAt string `json:"imported_at,omitempty"`
|
|
KernelPath string `json:"kernel_path,omitempty"`
|
|
InitrdPath string `json:"initrd_path,omitempty"`
|
|
ModulesDir string `json:"modules_dir,omitempty"`
|
|
}
|
|
|
|
type KernelListResult struct {
|
|
Entries []KernelEntry `json:"entries"`
|
|
}
|
|
|
|
type KernelRefParams struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type KernelShowResult struct {
|
|
Entry KernelEntry `json:"entry"`
|
|
}
|
|
|
|
type KernelImportParams struct {
|
|
Name string `json:"name"`
|
|
FromDir string `json:"from_dir"`
|
|
Distro string `json:"distro,omitempty"`
|
|
Arch string `json:"arch,omitempty"`
|
|
}
|
|
|
|
type KernelPullParams struct {
|
|
Name string `json:"name"`
|
|
Force bool `json:"force,omitempty"`
|
|
}
|
|
|
|
type KernelCatalogEntry struct {
|
|
Name string `json:"name"`
|
|
Distro string `json:"distro,omitempty"`
|
|
Arch string `json:"arch,omitempty"`
|
|
KernelVersion string `json:"kernel_version,omitempty"`
|
|
SizeBytes int64 `json:"size_bytes,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
Pulled bool `json:"pulled"`
|
|
}
|
|
|
|
type KernelCatalogResult struct {
|
|
Entries []KernelCatalogEntry `json:"entries"`
|
|
}
|
|
|
|
type SudoStatus struct {
|
|
Available bool `json:"available"`
|
|
Command string `json:"command,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
type HostSummary struct {
|
|
CPUCount int `json:"cpu_count"`
|
|
TotalMemoryBytes int64 `json:"total_memory_bytes"`
|
|
StateFilesystemTotalBytes int64 `json:"state_filesystem_total_bytes"`
|
|
StateFilesystemFreeBytes int64 `json:"state_filesystem_free_bytes"`
|
|
}
|
|
|
|
type BangerSummary struct {
|
|
ImageCount int `json:"image_count"`
|
|
ManagedImageCount int `json:"managed_image_count"`
|
|
VMCount int `json:"vm_count"`
|
|
RunningVMCount int `json:"running_vm_count"`
|
|
ConfiguredVCPUCount int `json:"configured_vcpu_count"`
|
|
ConfiguredMemoryBytes int64 `json:"configured_memory_bytes"`
|
|
ConfiguredDiskBytes int64 `json:"configured_disk_bytes"`
|
|
UsedSystemOverlayBytes int64 `json:"used_system_overlay_bytes"`
|
|
UsedWorkDiskBytes int64 `json:"used_work_disk_bytes"`
|
|
RunningCPUPercent float64 `json:"running_cpu_percent"`
|
|
RunningRSSBytes int64 `json:"running_rss_bytes"`
|
|
RunningVSZBytes int64 `json:"running_vsz_bytes"`
|
|
}
|
|
|
|
type DashboardSummary struct {
|
|
GeneratedAt time.Time `json:"generated_at"`
|
|
Host HostSummary `json:"host"`
|
|
Sudo SudoStatus `json:"sudo"`
|
|
Banger BangerSummary `json:"banger"`
|
|
}
|
|
|
|
type DashboardSummaryResult struct {
|
|
Summary DashboardSummary `json:"summary"`
|
|
}
|