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
- Variables
- func ExecWrap(p Paths, args []string) int
- func FS(args []string) int
- func Forget(p Paths, id string) int
- func Handler(sv *Supervisor) http.Handler
- func Install(dir string) error
- func Kill(p Paths, id string) int
- func Listen(path string) (net.Listener, error)
- func ParseSignal(s string) (syscall.Signal, error)
- func Ping(p Paths) int
- func Relay(p Paths, target string) int
- func Run(p Paths, logger *slog.Logger) error
- func SpriteEnv(p Paths, args []string) int
- func URLPort(p Paths) int
- type DirEntry
- type Paths
- type ServiceDef
- type ServiceEvent
- type ServiceState
- type ServiceWithState
- type Supervisor
- func (sv *Supervisor) Define(def ServiceDef) error
- func (sv *Supervisor) Delete(name string) error
- func (sv *Supervisor) Get(name string) (ServiceWithState, error)
- func (sv *Supervisor) List() []ServiceWithState
- func (sv *Supervisor) LogPath(name string) string
- func (sv *Supervisor) Restart(name string, timeout time.Duration) error
- func (sv *Supervisor) Signal(name string, sig syscall.Signal) error
- func (sv *Supervisor) Start(name string) error
- func (sv *Supervisor) Stop(name string, timeout time.Duration) error
- func (sv *Supervisor) StopAll(timeout time.Duration)
- func (sv *Supervisor) Subscribe(name string) (<-chan ServiceEvent, func(), error)
- type Task
Constants ¶
const ( ExitOK = 0 ExitNotFound = 2 ExitUsage = 64 ExitFailed = 70 )
Exit codes the host side reads from the x-* helpers.
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 ¶
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 ¶
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 ¶
FS is `spritzer x-fs <read|write|list|delete> <path> [--mode <octal>] [--recursive]`, backing the filesystem API in container mode. `--mode` applies to write only, defaulting to 0644 when absent, as the Sprites API and wisp do. Missing paths exit ExitNotFound.
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 ¶
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 ParseSignal ¶
ParseSignal accepts TERM, SIGTERM, 15 and the like.
func Relay ¶
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 ¶
Run is `spritzer agent`: the sprite's main process. It starts the services defined on disk and serves the management socket until SIGTERM.
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 ¶
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) ExecDir ¶
ExecDir holds the pid files of running execs, so the host can kill an exec whose client went away.
func (Paths) ServicesDir ¶
ServicesDir holds one <name>.json definition per service.
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.