client

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 25 Imported by: 1 module, 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 client checks for, verifies and applies updates to the running program. Call Boot first thing in main, then CheckAndUpdate whenever the host decides to look for an update; ota schedules nothing itself.

Index

Constants

View Source
const AnyPlatform = "any"

AnyPlatform is the os and arch of an artifact that is the same everywhere: data, such as a model bundle, rather than a binary.

View Source
const StepDrain = "drain"

StepDrain names the host's drain step in the update chain.

Variables

This section is empty.

Functions

func DataStateDir

func DataStateDir(binary, program string) string

DataStateDir is where a data program fetched beside binary keeps its state and versions: next to the binary's own .ota directory, so one install holds both.

Types

type BootResult

type BootResult struct {
	// Gate delivers the health gate's outcome when this start is an
	// uncommitted new version; nil otherwise.
	Gate <-chan Result
}

BootResult reports what Boot found.

type Client

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

Client applies updates to one installed binary.

func New

func New(opts Options) (*Client, error)

New validates opts and returns a Client.

func (*Client) Available

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

Available checks for an offer and verifies it without downloading or changing anything. It is the `update --dry-run` path.

func (*Client) BinaryPath

func (c *Client) BinaryPath() string

BinaryPath is the installed binary this client updates.

func (*Client) Boot

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

Boot finishes whatever an earlier run left: it repairs an interrupted swap, rolls back a version that crash-looped or missed its deadline, starts the health gate for a version on its first starts, and sweeps temp files from crashed runs. Call it before the program does real work.

If Boot rolls back, it relaunches the previous binary and does not return on success.

func (*Client) CheckAndUpdate

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

CheckAndUpdate asks for an offer and, if one verifies and is accepted, downloads, verifies, swaps and restarts. With the default SelfExec restarter it does not return when the update is applied: the process is replaced by the new version, whose Boot runs the health gate.

ctx is the only deadline: give it one, or a server that stops sending holds the update lock until the process exits.

func (*Client) Commit

func (c *Client) Commit()

Commit confirms the running version is healthy. With health.CommitOnly it is the only thing that keeps a new version; with other sources it commits early. Calling it when nothing is pending does nothing.

func (*Client) CommitDeadline

func (c *Client) CommitDeadline() time.Duration

CommitDeadline is how long a new version has to prove itself.

func (*Client) Fetch

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

Fetch is CheckAndUpdate for a data program: the offer's artifact is os/arch "any", and applying it means putting it on disk, with no swap, restart or health gate. It is held to the same rules as a binary: the offer's signature, the replay floor, the artifact's size and hash.

A new version is written beside the old and only then made current, so a failed or interrupted fetch leaves the previous version in place. The version before the new one is kept as well; older ones are removed.

func (*Client) StateDir

func (c *Client) StateDir() string

StateDir is where this client keeps its state, for a launcher or CLI that coordinates with it.

func (*Client) WaitOutcome

func (c *Client) WaitOutcome(ctx context.Context, version string, timeout time.Duration) (Result, error)

WaitOutcome waits for an installed version to commit or roll back, for a caller that handed the restart to something else (a launcher, a service manager) and needs the verdict. It watches the same state the new process's health gate writes, so it works whoever runs the gate.

type Fetched

type Fetched struct {
	// Dir is <StateDir>/data/<version>; it holds the artifact and its digest.
	Dir     string
	Path    string
	Version string
	// SHA256 is the digest the signed offer named and the download matched.
	SHA256 string
}

Fetched is a verified data artifact on disk.

func Current

func Current(stateDir string) (Fetched, error)

Current returns the version an earlier Fetch made current in stateDir, without a network call: what a reader loads at startup. fs.ErrNotExist means nothing has been fetched yet.

type HTTPTransport

type HTTPTransport struct {
	// BaseURL is the server root, e.g. https://updates.example.com.
	BaseURL string
	// Client defaults to http.DefaultClient. It must not have a total request
	// timeout shorter than the largest download; use context deadlines instead.
	Client *http.Client
	// Authorize, if set, decorates every request (a bearer token, a signed header).
	Authorize func(*http.Request) error
}

HTTPTransport speaks the ota server's /v1 API.

func (*HTTPTransport) Check

Check implements Transport.

func (*HTTPTransport) Fetch

func (t *HTTPTransport) Fetch(ctx context.Context, a ota.Artifact, offset int64) (io.ReadCloser, int64, error)

Fetch implements Transport.

func (*HTTPTransport) Report

func (t *HTTPTransport) Report(ctx context.Context, rs []ota.Report) error

Report implements Transport.

type Hooks

type Hooks struct {
	// BeforeApply decides whether a verified offer is applied now, later, or never.
	BeforeApply func(ctx context.Context, o ota.Offer) ota.Decision
	// Drain shuts the program down gracefully before the swap. ctx carries the
	// deadline; overrunning it abandons the attempt without swapping.
	Drain func(ctx context.Context) error
	// DrainTimeout bounds Drain. Default 30s.
	DrainTimeout time.Duration
	// AfterApply runs when a new version commits.
	AfterApply func(ctx context.Context, r Result)
	// OnRollback runs when a new version is rolled back.
	OnRollback func(ctx context.Context, r Result)
}

Hooks let the host take part in an update. ota draws no UI; a host that wants a person to approve asks them inside BeforeApply.

type Options

type Options struct {
	Program string
	// Channel defaults to "stable".
	Channel string
	// ClientID is the host's stable identifier for this installation; ota
	// never invents one.
	ClientID string
	// CurrentVersion is the version compiled into this binary.
	CurrentVersion string
	// BinaryPath defaults to os.Executable with symlinks resolved.
	BinaryPath string
	// StateDir defaults to <dir of BinaryPath>/.ota/<binary name>. Set it when
	// the binary's directory changes between versions (side-by-side installs),
	// so every version shares one replay floor and lock.
	StateDir  string
	Transport Transport
	// Verifier is required and has no default, so nothing ships unverified by
	// omission.
	Verifier verify.Verifier
	// Restarter defaults to restart.SelfExec, or restart.Child when the
	// process was started by the resident launcher.
	Restarter restart.Restarter
	// Health defaults to health.CommitOnly: only an explicit Commit keeps the
	// new version.
	Health health.Source
	Hooks  Hooks
	// OnEvent receives every stage transition.
	OnEvent func(ota.Event)
	// Reporter, if set, delivers progress reports instead of the Transport,
	// for a host that sends them over its own channel. Undelivered reports are
	// kept and retried either way.
	Reporter func(ctx context.Context, rs []ota.Report) error
	Logger   *slog.Logger
	// MirrorClient fetches artifact mirror URLs; defaults to http.DefaultClient.
	MirrorClient *http.Client

	ClockTolerance time.Duration // default 5m
	SteadyWindow   time.Duration // default 30s
	CommitDeadline time.Duration // default 5m
	// MaxAttempts is how many starts an uncommitted version gets before it is
	// treated as crash-looping and rolled back. Default 3.
	MaxAttempts int
	// HealthPoll is how often Health is asked. Default 1s.
	HealthPoll time.Duration
	// EditChain, if set, may insert, remove or reorder the named steps of an
	// update (drain, swap, restart, and whatever the restarter adds) before
	// they run.
	EditChain func(*lifecycle.Manager)
	Now       func() time.Time
}

Options configures a Client. Program, ClientID, CurrentVersion, Transport and Verifier are required.

type Result

type Result struct {
	Stage    ota.Stage
	Version  string
	UpToDate bool
	Reason   string
}

Result describes the outcome of CheckAndUpdate or of a health gate.

type Transport

type Transport interface {
	// Check returns the offer the server chose for this client, or nil if none.
	Check(ctx context.Context, req ota.CheckRequest) (*ota.SignedOffer, error)
	// Fetch opens the artifact at offset, returning where the body starts.
	Fetch(ctx context.Context, a ota.Artifact, offset int64) (io.ReadCloser, int64, error)
	// Report delivers progress reports.
	Report(ctx context.Context, rs []ota.Report) error
}

Transport carries offers, artifact bytes and reports. HTTPTransport talks to an ota server; a host with its own channel implements this instead.

Jump to

Keyboard shortcuts

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