agent

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 31 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 boxpool-agent, the process inside every sandbox that runs commands for the host, and a client for it. docs/agent-protocol.md describes the wire protocol.

Index

Constants

View Source
const (
	Stdin  byte = 0
	Stdout byte = 1
	Stderr byte = 2
)

Stream ids: the first byte of every binary WebSocket message.

View Source
const DefaultShutdownGrace = 10 * time.Second

DefaultShutdownGrace is how long guest processes get to exit after SIGTERM when the request does not say.

View Source
const FeatureDesktop = "desktop"

FeatureDesktop advertises an image with an on-demand virtual desktop.

View Source
const FeatureShutdown = "shutdown"

FeatureShutdown in Version.Features means the agent serves POST /v1/shutdown. Agents older than it, and those in containers, lack it.

View Source
const FeatureTCP = "tcp"

FeatureTCP in Version.Features means the agent serves GET /v1/tcp.

View Source
const ProtocolVersion = 1

ProtocolVersion is the agent protocol version reported by GET /v1/version. It changes only when a change would break existing clients.

Variables

This section is empty.

Functions

func DesktopViewer

func DesktopViewer() http.Handler

DesktopViewer serves the embedded noVNC client below /v1/desktop/. Both the host binary and guest agent embed it; no CDN is required.

func DialTCP

func DialTCP(ctx context.Context, c net.Conn, port int) (net.Conn, error)

DialTCP asks the agent at the other end of c, a new connection to it, to connect to port on the guest's loopback, and returns c carrying that connection. It closes c if the agent refuses.

func ExtractTar

func ExtractTar(r io.Reader, dest string, check func(*tar.Header) error) error

ExtractTar extracts a tar into the existing directory dest, as PUT /v1/files does. If check is not nil, it sees each header first and may change it or refuse the entry. Only regular files, directories and symlinks are extracted, with their modes and modification times. No entry is written outside dest: absolute names and .. are refused, parents must be real directories, never symlinks, and an existing entry is unlinked, not written through. Symlinks themselves may point anywhere.

func Listen

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

Listen listens on addr, which is unix:<path> or vsock:<port>.

func PrepareGuestNetwork

func PrepareGuestNetwork() error

PrepareGuestNetwork brings up loopback before a VM agent serves requests. A VM without a NIC has no kernel IP autoconfiguration to do this for it. Docker agents must not call this: the Engine configures their network, and they intentionally lack CAP_NET_ADMIN. No external interface is changed.

func Serve

func Serve(ctx context.Context, l net.Listener, version string, shutdown *Shutdown) error

Serve serves the agent API on l until ctx is cancelled. Cancelling ctx also ends every exec session and kills its processes; Serve returns once they are gone. With shutdown nil, the agent does not serve POST /v1/shutdown.

func WriteTar

func WriteTar(w io.Writer, src string) error

WriteTar writes src, a file or a directory tree, as a tar rooted at its base name, the format of GET /v1/files: modes and modification times are kept, symlinks stay symlinks, owners are dropped. Other file types are refused.

Types

type Client

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

Client talks to one agent.

func NewClient

func NewClient(dial func(ctx context.Context) (net.Conn, error)) *Client

NewClient returns a client that reaches the agent through dial, for example over a unix socket or a vsock connection.

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections closes connections kept open for reuse, which otherwise stay open to the agent for as long as the process runs.

func (*Client) Download

func (c *Client) Download(ctx context.Context, src string) (io.ReadCloser, error)

Download returns a tar rooted at the base name of src. The caller must read to EOF to detect interrupted transfers, then close the body.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, req ExecRequest, stdout, stderr io.Writer) (*Session, error)

Exec starts a command and copies its output to stdout and stderr until it exits. With req.TTY all output goes to stdout.

func (*Client) Ports

func (c *Client) Ports(ctx context.Context) ([]Port, error)

Ports returns a snapshot of the guest's listening TCP sockets.

func (*Client) Shutdown

func (c *Client) Shutdown(ctx context.Context, grace time.Duration) error

Shutdown asks the agent to stop every guest process, giving them grace after SIGTERM before SIGKILL, remount / read-only and reboot the guest. It returns once / is read-only; the reboot follows, and can cut the reply short.

func (*Client) StartDesktop

func (c *Client) StartDesktop(ctx context.Context) (Desktop, error)

StartDesktop starts the image's display without tying it to this request.

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, dest string, tar io.Reader) error

Upload extracts a tar stream into the existing directory dest.

func (*Client) Version

func (c *Client) Version(ctx context.Context) (Version, error)

Version returns the agent's protocol and build version.

type Desktop

type Desktop struct {
	Display      string `json:"display"`
	VNC          string `json:"vnc"`
	HumanControl bool   `json:"human_control"`
}

Desktop describes the display and its private RFB endpoint. HumanControl is cooperative: agents should pause their browser automation while it is true.

type ExecRequest

type ExecRequest struct {
	Argv []string `json:"argv"`
	Env  []string `json:"env,omitempty"`
	Cwd  string   `json:"cwd,omitempty"`
	TTY  bool     `json:"tty,omitempty"`
	Rows uint16   `json:"rows,omitempty"`
	Cols uint16   `json:"cols,omitempty"`
}

ExecRequest is the first message of an exec session.

type Port

type Port struct {
	Proto   string `json:"proto"`
	Address string `json:"address"`
	Port    int    `json:"port"`
}

Port is a listening TCP socket reported by GET /v1/ports.

type Session

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

Session is a running command. Cancelling the context passed to Exec ends the session, and the agent kills the command.

func Start

func Start(ctx context.Context, conn *websocket.Conn, req ExecRequest, stdout, stderr io.Writer) (*Session, error)

Start runs an exec session on conn, an open exec WebSocket to an agent or to something that relays one, such as the daemon. The session owns conn.

func (*Session) CloseStdin

func (s *Session) CloseStdin() error

CloseStdin closes the command's stdin. It does nothing in TTY mode, where the client sends Ctrl-D instead.

func (*Session) Resize

func (s *Session) Resize(rows, cols uint16) error

Resize sets the PTY's window size. It does nothing outside TTY mode.

func (*Session) Wait

func (s *Session) Wait() (int, error)

Wait waits for the command to exit and returns its exit code.

func (*Session) Write

func (s *Session) Write(p []byte) (int, error)

Write sends p to the command's stdin.

type Shutdown

type Shutdown struct {
	// Pids lists live processes. It may leave out zombies and kernel
	// threads, which do not exit on a signal.
	Pids func() ([]int, error)
	Kill func(pid int, sig syscall.Signal) error
	// Remount syncs and remounts the root filesystem read-only.
	Remount func() error
	// Reboot ends the VM. It returns only if it fails.
	Reboot func() error
}

Shutdown is how the agent serves POST /v1/shutdown. Only the agent in a VM serves it: GuestShutdown returns the real one.

func GuestShutdown

func GuestShutdown() *Shutdown

GuestShutdown is the Shutdown of the agent in a VM. Reboot ends the VM: Firecracker exits on a guest reboot, with reboot=k on x86_64 and through PSCI on arm64.

type ShutdownRequest

type ShutdownRequest struct {
	GraceMS int64 `json:"grace_ms,omitempty"`
}

ShutdownRequest is the body of POST /v1/shutdown.

type Version

type Version struct {
	Protocol int      `json:"protocol"`
	Agent    string   `json:"agent"`
	Features []string `json:"features,omitempty"`
}

Version is the body of GET /v1/version.

Jump to

Keyboard shortcuts

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