agent

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package agent is the half of container exec mode that runs inside a sprite.

In container mode every sprite is a container (a Docker container, or a pod on Kubernetes) whose main process is `spritzer agent`. The agent supervises the sprite's services and serves the in-sprite management socket that `sprite-env` talks to, the same surface wisp's guest agent serves on /.sprite/api.sock. The spritzer binary is copied into the sprite at /.sprite/bin/spritzer (see Install), and the host side reaches the agent only by exec'ing that binary: `x-exec` wraps a user command, `x-relay` turns an exec into a byte stream to a socket, `x-fs` backs the filesystem API. No port inside the sprite has to be reachable from spritzer, which is what lets the same code work against Docker Desktop, where container IPs are not routable from the host, and against a cluster.

Index

Constants

View Source
const (
	ExitOK       = 0
	ExitNotFound = 2
	ExitUsage    = 64
	ExitFailed   = 70
)

Exit codes the host side reads from the x-* helpers.

View Source
const DefaultRoot = "/.sprite"

DefaultRoot is where the agent keeps its state inside a sprite, matching real Sprites and wisp: definitions in /.sprite/services, logs in /.sprite/logs/services, the socket at /.sprite/api.sock.

Variables

View Source
var (
	// ErrServiceNotFound is a name with no definition.
	ErrServiceNotFound = errors.New("service not found")
	// ErrServiceConflict is a definition that clashes with another one.
	ErrServiceConflict = errors.New("service conflict")
)

Functions

func ExecWrap

func ExecWrap(p Paths, args []string) int

ExecWrap is `spritzer x-exec`: it becomes the user's command, after setting the working directory and environment the exec asked for, in a process group of its own whose leader's pid is recorded under the exec id. When a client disconnects before the command exits, the host runs `x-kill <id>`, since neither Docker nor Kubernetes kills an exec'd process when its stream closes.

func FS

func FS(args []string) int

FS is `spritzer x-fs <read|write|list|delete> <path> [--recursive]`, backing the filesystem API in container mode. Missing paths exit ExitNotFound.

func Forget

func Forget(p Paths, id string) int

Forget is `spritzer x-forget <id>`: drop an exec's pid file after it exited.

func Handler

func Handler(sv *Supervisor) http.Handler

Handler is the management API on /.sprite/api.sock: services and tasks, in wisp's guest API shapes. Checkpoints from inside are refused, since container mode has no checkpoints.

func Install

func Install(dir string) error

Install copies the running binary to dir/spritzer and links dir/sprite-env to it. It is the init step that puts the agent into a sprite: the runtime runs `spritzer agent-install /.sprite/bin` from spritzer's own image into a volume the sprite container then mounts.

func Kill

func Kill(p Paths, id string) int

Kill is `spritzer x-kill <id>`: SIGTERM, then SIGKILL, to an exec's group.

func Listen

func Listen(path string) (net.Listener, error)

Listen creates the management socket, replacing a stale one.

func ParseSignal

func ParseSignal(s string) (syscall.Signal, error)

ParseSignal accepts TERM, SIGTERM, 15 and the like.

func Ping

func Ping(p Paths) int

Ping exits 0 once the agent answers on its socket.

func Relay

func Relay(p Paths, target string) int

Relay is `spritzer x-relay <target>`: it joins its stdin and stdout to a connection. The host dials the agent's socket and the sprite's HTTP port this way, over an exec, so nothing inside the sprite needs to be reachable.

target is unix:<path>, tcp:<host:port>, or "url", which is the port the sprite URL serves: the one service with an http_port, else 8080. A port with nothing listening yet is retried for a few seconds, as a service that has just been started may not have bound it.

func Run

func Run(p Paths, logger *slog.Logger) error

Run is `spritzer agent`: the sprite's main process. It starts the services defined on disk and serves the management socket until SIGTERM.

func SpriteEnv

func SpriteEnv(p Paths, args []string) int

SpriteEnv is the sprite-env CLI, compatible with wisp's for services. It returns the process exit code.

func URLPort

func URLPort(p Paths) int

URLPort is the port the sprite URL routes to: the service with an http_port, else 8080, as on Sprites and wisp.

Types

type DirEntry

type DirEntry struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Size int64  `json:"size,omitempty"`
}

DirEntry matches the interpreter's fs/list entries.

type Paths

type Paths struct {
	Root string
}

Paths names every file the agent uses. Root is /.sprite in a sprite; tests point it at a temporary directory.

func DefaultPaths

func DefaultPaths() Paths

DefaultPaths honours SPRITZER_AGENT_ROOT (tests), else /.sprite.

func (Paths) BinDir

func (p Paths) BinDir() string

BinDir holds the spritzer binary and the sprite-env link. It is a volume the runtime mounts, so it is the same for every image.

func (Paths) Binary

func (p Paths) Binary() string

Binary is the spritzer binary inside the sprite.

func (Paths) ExecDir

func (p Paths) ExecDir() string

ExecDir holds the pid files of running execs, so the host can kill an exec whose client went away.

func (Paths) LogsDir

func (p Paths) LogsDir() string

LogsDir holds one <name>.log per service.

func (Paths) ServicesDir

func (p Paths) ServicesDir() string

ServicesDir holds one <name>.json definition per service.

func (Paths) Socket

func (p Paths) Socket() string

Socket is the management socket sprite-env and the host relay talk to.

type ServiceDef

type ServiceDef struct {
	Name     string            `json:"name"`
	Cmd      string            `json:"cmd"`
	Args     []string          `json:"args"`
	Needs    []string          `json:"needs"`
	HTTPPort *int              `json:"http_port,omitempty"`
	Env      map[string]string `json:"env,omitempty"`
	Dir      string            `json:"dir,omitempty"`
}

ServiceDef is what a caller defines.

type ServiceEvent

type ServiceEvent struct {
	Type      string            `json:"type"` // started, stdout, stderr, exit, error, stopping, stopped, complete
	Data      string            `json:"data,omitempty"`
	ExitCode  *int              `json:"exit_code,omitempty"`
	Timestamp int64             `json:"timestamp"`
	LogFiles  map[string]string `json:"log_files,omitempty"`
}

ServiceEvent is one NDJSON line of a create/start/stop/restart stream.

type ServiceState

type ServiceState struct {
	Name         string     `json:"name"`
	Status       string     `json:"status"` // stopped, running, stopping, failed
	PID          int        `json:"pid,omitempty"`
	StartedAt    *time.Time `json:"started_at,omitempty"`
	Error        string     `json:"error,omitempty"`
	RestartCount int        `json:"restart_count,omitempty"`
}

ServiceState is the live half.

type ServiceWithState

type ServiceWithState struct {
	ServiceDef
	State ServiceState `json:"state"`
}

ServiceWithState is the GET shape: the definition with its state beside it.

type Supervisor

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

Supervisor owns every service in one sprite.

func NewSupervisor

func NewSupervisor(p Paths) *Supervisor

NewSupervisor loads the definitions on disk and starts them all, needs first.

func (*Supervisor) Define

func (sv *Supervisor) Define(def ServiceDef) error

Define creates or replaces a definition and writes it to disk. A replaced service that is running is stopped; the caller starts it again.

func (*Supervisor) Delete

func (sv *Supervisor) Delete(name string) error

Delete stops a service and removes its definition and log.

func (*Supervisor) Get

func (sv *Supervisor) Get(name string) (ServiceWithState, error)

Get returns one service.

func (*Supervisor) List

func (sv *Supervisor) List() []ServiceWithState

List returns every service in name order.

func (*Supervisor) LogPath

func (sv *Supervisor) LogPath(name string) string

LogPath is a service's combined log.

func (*Supervisor) Restart

func (sv *Supervisor) Restart(name string, timeout time.Duration) error

Restart stops then starts.

func (*Supervisor) Signal

func (sv *Supervisor) Signal(name string, sig syscall.Signal) error

Signal delivers sig to the service's process group.

func (*Supervisor) Start

func (sv *Supervisor) Start(name string) error

Start starts a service, and first whatever it needs. Starting a running service is a no-op.

func (*Supervisor) Stop

func (sv *Supervisor) Stop(name string, timeout time.Duration) error

Stop sends SIGTERM to the service's process group and SIGKILL after timeout.

func (*Supervisor) StopAll

func (sv *Supervisor) StopAll(timeout time.Duration)

StopAll stops every service (agent shutdown).

func (*Supervisor) Subscribe

func (sv *Supervisor) Subscribe(name string) (<-chan ServiceEvent, func(), error)

Subscribe streams a service's events until cancel is called.

type Task

type Task struct {
	Name      string    `json:"name"`
	StartedAt time.Time `json:"started_at"`
	ExpiresAt time.Time `json:"expires_at"`
}

Task is a keep-awake hold. Container sprites never sleep, so tasks are only recorded, but a box's keep-awake loop must get a 2xx for its PUT.

Jump to

Keyboard shortcuts

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