Documentation
¶
Overview ¶
Package client talks to a running daemon and starts one when needed.
Index ¶
- Variables
- func DaemonRunning() bool
- func DashboardURL(port int) string
- func LivePort() int
- func Sandboxed() bool
- type APIError
- type Client
- func (c *Client) ContainerAction(ctx context.Context, name, action string, build, pull bool) (docker.JobResult, error)
- func (c *Client) DialWebSocket(ctx context.Context, p string) (*websocket.Conn, error)
- func (c *Client) Do(ctx context.Context, method, path string, body, out any) error
- func (c *Client) Ensure(ctx context.Context) (Health, error)
- func (c *Client) On(ctx context.Context, name string) (*Client, *Connection, error)
- func (c *Client) Path(p string) string
- func (c *Client) Ping(ctx context.Context) (Health, error)
- func (c *Client) Running(ctx context.Context) (Health, error)
- func (c *Client) Token() string
- func (c *Client) Unverified() error
- func (c *Client) WebSocketURL(p string) string
- type Connection
- type Health
- type Holder
Constants ¶
This section is empty.
Variables ¶
var ErrNotRunning = errors.New("daemon is not running")
ErrNotRunning means no daemon answered and none was started.
var ErrSandboxed = errors.New("not starting the daemon from inside a sandbox")
ErrSandboxed means no daemon answered and none was started because the caller runs in an agent's sandbox: a daemon started there would inherit the sandbox's file and network limits, and every server it starts too.
var ErrUnverified = errors.New("cannot prove it is this account's daemon")
ErrUnverified means that what answers on the daemon's port could not prove it holds this account's token, so nothing signed is sent to it: another program holds the port, or a daemon from before verified requests.
Functions ¶
func DaemonRunning ¶
func DaemonRunning() bool
DaemonRunning reports whether a daemon holds this state directory's lock. The test takes a shared lock for a moment; a daemon starting meanwhile retries its own.
func DashboardURL ¶
DashboardURL returns the dashboard's address for the daemon on port; a browser signs in there through `open`.
Types ¶
type APIError ¶
type APIError struct {
Status int
Msg string
Code string
Body json.RawMessage
}
APIError is an error response from the daemon.
type Client ¶
Client is an HTTP client bound to the daemon's port.
func New ¶
New returns a client for the daemon on port. It dials IPv4 loopback by address, never a name that could resolve to another listener.
func NewAt ¶
NewAt returns a client for the daemon on port at the loopback address host, such as "::1" for a check of the daemon's IPv6 listener.
func (*Client) ContainerAction ¶
func (c *Client) ContainerAction(ctx context.Context, name, action string, build, pull bool) (docker.JobResult, error)
ContainerAction uses the daemon's engine and compose context. Recreate waits until its job ends and returns a bounded output tail, including on failure.
func (*Client) DialWebSocket ¶
DialWebSocket opens a signed WebSocket to an API path, routed as Path does.
func (*Client) Do ¶
Do sends a signed request with an optional JSON body and decodes a JSON response into out. The daemon must have proved itself first, and its reply must carry a proof as well; a request refused for an old epoch is sent again once after a new handshake.
func (*Client) Ensure ¶
Ensure makes sure a daemon answers and proves itself, starting one in the background if none answers. When what answers cannot prove itself, it returns its claimed identity and an error wrapping ErrUnverified. When another program, or another account's daemon, holds the port and no daemon of this account runs, it starts one anyway: a daemon without a configured port then listens on another port, which the client follows. It starts none while this product of this account holds the port (a daemon with another state directory), and says which one.
func (*Client) On ¶
On returns a client whose API requests go to the connected computer called name (its name, DNS label or DNS name), relayed by this daemon, and the connection. It fails when this daemon is not connected to that computer.
func (*Client) Path ¶
Path returns where the daemon serves an API path: the path itself, or the same path on the connected computer for a client made by On.
func (*Client) Ping ¶
Ping returns the identity of this computer's daemon (also for a client made by On), or an error if none answers. It repeats the handshake, so Verified tells whether what answers proved itself.
func (*Client) Running ¶ added in v0.3.0
Running makes sure a daemon that already runs answers and proves itself, and never starts one. It follows the port a running daemon recorded. It returns an error wrapping ErrNotRunning when nothing listens.
func (*Client) Unverified ¶
Unverified is the error for a daemon that did not prove itself.
func (*Client) WebSocketURL ¶
WebSocketURL returns the ws:// URL of an API path, routed as Path does.
type Connection ¶
type Connection struct {
Peer struct {
Name string `json:"name"`
Label string `json:"label"`
DNSName string `json:"dnsName"`
} `json:"peer"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
LocalPort int `json:"localPort"`
Mirrors []struct {
Port int `json:"port"`
Local int `json:"local"`
State string `json:"state"`
} `json:"mirrors"`
}
Connection is this daemon's connection to another computer, as GET /api/remote reports it.
func (*Connection) Is ¶
func (cn *Connection) Is(name string) bool
Is reports whether name is the connected computer's name, DNS label or DNS name.
func (*Connection) LocalURL ¶
func (cn *Connection) LocalURL(server, raw string) string
LocalURL returns how this computer opens a URL of the connected computer's server: at the local port its port is forwarded to, else through this daemon's proxy at <server>.<computer>.localhost.
The other computer supplies the URL, so only its scheme (http or https), port, path and query are used; the host is always this computer's, and a server name that is not a valid host label gets no URL.
type Health ¶
type Health struct {
App string `json:"app"`
Version string `json:"version"`
PID int `json:"pid"`
Port int `json:"port"`
StartedAt time.Time `json:"startedAt"`
// Verified is true when it proved that it holds this account's token;
// only then does the client send it signed requests.
Verified bool `json:"-"`
}
Health is the daemon's identity.
type Holder ¶
type Holder struct {
PID int
Command string
// Own is true when lsof or ss name the listening process, it belongs to
// this account and its command is this product's binary. A pid claimed
// only by what answers on the port, or a process of this account that
// forwards the port (such as an ssh tunnel to another computer's
// daemon), is not taken for this product.
Own bool
// StateDir is the state directory of such a daemon, when its lock file
// can be seen among its open files.
StateDir string
}
Holder is what listens on a port that this account's daemon wants.
func PortHolder ¶
PortHolder finds out what listens on port on loopback. Only lsof or ss are trusted to name the process (another account's are usually invisible to them), and its command name to tell whether it is this product; what answers on the port cannot prove either.