runtime

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package runtime abstracts a container runtime (Docker or Podman) behind the small set of operations Ballast needs: discover containers and their mounts, watch the socket for lifecycle changes, exec into a container to quiesce or dump it, and stop or start it for a cold backup.

The interface is deliberately kept free of any tool-specific type. It was shaped against exactly one real consumer (Ballast) before being lifted into github.com/tagwright/core, which is the discipline that keeps the abstraction honest.

The Docker adapter lands first. The Podman adapter follows behind the same interface, talking to Podman's Docker-compatible compat API and absorbing the socket-path and compose-label differences.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("runtime: not implemented")

ErrNotImplemented is returned by adapter methods that are not wired up yet.

Functions

This section is empty.

Types

type Container

type Container struct {
	ID      string
	Name    string
	State   string // running, exited, paused, ...
	Labels  map[string]string
	Mounts  []Mount
	Project string // com.docker.compose.project, empty if not a compose service
	Service string // com.docker.compose.service, empty if not a compose service
}

Container is the normalized view of a container across runtimes.

type DockerRuntime

type DockerRuntime struct {
	// contains filtered or unexported fields
}

DockerRuntime is the Docker adapter for Runtime. It talks to the Docker Engine API over the socket Ballast mounts read-only, using the request and mapping machinery in engine.go that it shares with PodmanRuntime.

The client is created lazily on first use and cached, so constructing a DockerRuntime never touches the socket: nothing fails until a method that actually needs the daemon is called.

func NewDocker

func NewDocker(socket string) *DockerRuntime

NewDocker returns a Docker adapter bound to the given API socket path.

func (DockerRuntime) Close

func (e DockerRuntime) Close() error

Close releases the underlying client.

func (DockerRuntime) Exec

func (e DockerRuntime) Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)

Exec runs a command inside a running container and returns a handle whose Stdout streams the command's standard output as it is produced, which matters because the caller pipes a live dump into restic --stdin rather than buffering it. Standard error is captured separately and folded into the error Wait returns on a non-zero exit.

func (DockerRuntime) Inspect

func (e DockerRuntime) Inspect(ctx context.Context, id string) (Container, error)

Inspect returns a single container by ID or name.

func (DockerRuntime) List

func (e DockerRuntime) List(ctx context.Context) ([]Container, error)

List returns every container the runtime knows about, running or not.

func (DockerRuntime) Start

func (e DockerRuntime) Start(ctx context.Context, id string) error

Start starts a stopped container.

func (DockerRuntime) Stop

func (e DockerRuntime) Stop(ctx context.Context, id string, timeoutSeconds int) error

Stop stops a running container, waiting up to timeoutSeconds before a kill.

func (DockerRuntime) Watch

func (e DockerRuntime) Watch(ctx context.Context) (<-chan Event, <-chan error)

Watch streams lifecycle events until ctx is cancelled. The error channel carries a terminal error and is then closed alongside the event channel.

type Event

type Event struct {
	Type   EventType
	ID     string
	Name   string
	Labels map[string]string
}

Event is a single lifecycle change on the socket.

type EventType

type EventType string

EventType is a container lifecycle transition.

const (
	EventStart   EventType = "start"
	EventStop    EventType = "stop"
	EventDie     EventType = "die"
	EventDestroy EventType = "destroy"
)

type ExecHandle

type ExecHandle struct {
	Stdout io.Reader
	Wait   func() (exitCode int, err error)
}

ExecHandle is a running exec. The caller reads Stdout to completion, then calls Wait to learn the exit code. Stderr is captured separately for logging.

type ExecSpec

type ExecSpec struct {
	Cmd  []string
	User string // empty means the container's default user
}

ExecSpec describes a command to run inside a container.

type Mount

type Mount struct {
	Type        MountType
	Name        string // named-volume name, empty for binds and tmpfs
	Source      string // host-side path, empty for tmpfs
	Destination string // container-side path
	ReadOnly    bool
}

Mount is one filesystem mount attached to a container.

type MountType

type MountType string

MountType distinguishes the kinds of mount Ballast cares about.

const (
	MountVolume MountType = "volume"
	MountBind   MountType = "bind"
	MountTmpfs  MountType = "tmpfs"
)

type PodmanRuntime

type PodmanRuntime struct {
	// contains filtered or unexported fields
}

PodmanRuntime is the Podman adapter for Runtime. Podman's REST API includes a Docker-compatible compat layer (documented against the Docker v1.40 API) on the same socket as its native libpod API, so PodmanRuntime talks to it with the exact request and mapping machinery DockerRuntime uses, in engine.go; the two adapters differ only in their default socket path and in how compose project/service identity is read off a container's labels.

The client is created lazily on first use and cached, so constructing a PodmanRuntime never touches the socket: nothing fails until a method that actually needs the daemon is called.

func NewPodman

func NewPodman(socket string) *PodmanRuntime

NewPodman returns a Podman adapter bound to the given API socket path. An empty socket resolves to a sensible default: the rootless per-user socket derived from XDG_RUNTIME_DIR (or /run/user/<uid> when that is unset, matching systemd's own convention) for a non-root caller, and the rootful system-service socket for a root caller with no XDG_RUNTIME_DIR set.

func (PodmanRuntime) Close

func (e PodmanRuntime) Close() error

Close releases the underlying client.

func (PodmanRuntime) Exec

func (e PodmanRuntime) Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)

Exec runs a command inside a running container and returns a handle whose Stdout streams the command's standard output as it is produced, which matters because the caller pipes a live dump into restic --stdin rather than buffering it. Standard error is captured separately and folded into the error Wait returns on a non-zero exit.

func (PodmanRuntime) Inspect

func (e PodmanRuntime) Inspect(ctx context.Context, id string) (Container, error)

Inspect returns a single container by ID or name.

func (PodmanRuntime) List

func (e PodmanRuntime) List(ctx context.Context) ([]Container, error)

List returns every container the runtime knows about, running or not.

func (PodmanRuntime) Start

func (e PodmanRuntime) Start(ctx context.Context, id string) error

Start starts a stopped container.

func (PodmanRuntime) Stop

func (e PodmanRuntime) Stop(ctx context.Context, id string, timeoutSeconds int) error

Stop stops a running container, waiting up to timeoutSeconds before a kill.

func (PodmanRuntime) Watch

func (e PodmanRuntime) Watch(ctx context.Context) (<-chan Event, <-chan error)

Watch streams lifecycle events until ctx is cancelled. The error channel carries a terminal error and is then closed alongside the event channel.

type Runtime

type Runtime interface {
	// List returns every container the runtime knows about, running or not.
	List(ctx context.Context) ([]Container, error)

	// Inspect returns a single container by ID or name.
	Inspect(ctx context.Context, id string) (Container, error)

	// Watch streams lifecycle events until ctx is cancelled. The error channel
	// carries a terminal error and is then closed alongside the event channel.
	Watch(ctx context.Context) (<-chan Event, <-chan error)

	// Exec runs a command inside a running container and returns a handle whose
	// Stdout the caller reads (for stream backups it is piped straight into the
	// engine's stdin) before calling Wait for the exit code.
	Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)

	// Stop stops a running container, waiting up to timeoutSeconds before a kill.
	Stop(ctx context.Context, id string, timeoutSeconds int) error

	// Start starts a stopped container.
	Start(ctx context.Context, id string) error

	// Close releases the underlying client.
	Close() error
}

Runtime is a container runtime Ballast can drive. Implementations must be safe for concurrent use by multiple goroutines.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL