update

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 30 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 update implements Aviary version checks and binary upgrades.

Index

Constants

View Source
const (
	ErrCodeGitBusy      = "git-busy"
	ErrCodeGitStaleLock = "git-stale-lock"
)

Error codes for git-mode update failures the dashboard explains itself.

View Source
const (
	// DefaultDownloadBase is the host release URLs are built on.
	DefaultDownloadBase = "https://github.com"
	// DownloadBaseEnv overrides DefaultDownloadBase, like the install scripts'
	// variable of the same name.
	DownloadBaseEnv = "AVIARY_DOWNLOAD_BASE"
)
View Source
const (
	// DefaultRepo is the canonical GitHub repository for Aviary releases.
	DefaultRepo = "lsegal/aviary"
	// CheckInterval is how often the server's self-updater looks for a new release.
	CheckInterval = 5 * time.Minute
)
View Source
const CheckoutEnv = "AVIARY_CHECKOUT"

CheckoutEnv is set by scripts/dev-go.mjs to the root of the source checkout the backend was built from. It is authoritative for git mode; without it a dev build looks for the checkout around its own executable, never in the process's working directory.

View Source
const HotReloadEnv = "FORGO_HOT_DIR"

HotReloadEnv is set by `forgo run --watch` (which scripts/dev-go.mjs runs) to the control directory its in-process agent and the watcher share. Each reload is a generation N: the watcher links link-N, which lists the reasons it refuses a reload on "refuse " lines, and the agent answers resp-N with "ok" or "err <reason>" once it has applied the image or failed to.

View Source
const RestartFileEnv = "AVIARY_RESTART_FILE"

RestartFileEnv is set by scripts/dev-go.mjs to a file the backend creates before exiting to ask for a restart. The script then runs the backend again, rebuilding it from the checkout's current sources. A dev update restarts only when forgo cannot hot-reload the pulled code (see PlanDevReload).

Variables

View Source
var DefaultHotReloadWait = HotReloadWait{
	Poll:    250 * time.Millisecond,
	Timeout: 5 * time.Minute,
	Answer:  45 * time.Second,
	Settle:  5 * time.Second,
}

DefaultHotReloadWait is how long a dev update waits for forgo.

View Source
var ErrGitBusy = errors.New("another git operation is running in the checkout")

ErrGitBusy reports that another git command kept a lock in the checkout for longer than the updater's retries waited.

Functions

func BinaryStaleness added in v0.7.26

func BinaryStaleness(head string) (msg string, stale bool)

BinaryStaleness compares the commit the running binary was built from with head, the checkout's HEAD, and says whether the binary runs older code. stale is true when the binary needs rebuilding to run head.

func CheckMessage

func CheckMessage(check CheckResult) string

CheckMessage returns a short human-readable summary for a version check result.

func CleanupStaging added in v0.7.0

func CleanupStaging(target string)

CleanupStaging removes what earlier upgrades left behind: the backup of the replaced binary next to target, which Windows cannot delete while the old process is still running, and stale staging directories in the temp dir.

func ClearCache added in v0.7.0

func ClearCache()

ClearCache forgets the cached version check, and any failure backoff, so the next Check asks GitHub or the checkout's upstream, and forgets the cached checkout HEAD so the next CurrentVersion reads it again. Callers use it after a pull or an install, or before a scheduled check.

func ConfigureEmulation

func ConfigureEmulation(raw string) error

ConfigureEmulation enables dev-only version emulation using <local>:<remote>.

func CurrentVersion

func CurrentVersion(ctx context.Context) string

CurrentVersion returns the effective local version, honoring emulation when enabled. A dev build reports the full commit SHA of the code it runs: the git-mode checkout's HEAD (see checkoutHead, which caches it for headTTL so request paths do not run git), else the VCS revision Go embedded in the binary, else "dev". ctx names the caller of any git command it runs (see WithCaller).

func DescribeInstall added in v0.7.24

func DescribeInstall() string

DescribeInstall explains the detected install type for aviary doctor.

func DevAssets added in v0.7.8

func DevAssets(embedded fs.FS, dir string) fs.FS

DevAssets returns the files embedded from the checkout-relative directory dir. In git mode (see GitCheckout) they are read from the checkout, so edits take effect without a rebuild; otherwise they come from embedded. Callers read only files under liveAssetDirs from the result.

func DisplayVersion added in v0.7.24

func DisplayVersion(v string) string

DisplayVersion formats a version for messages: a release as vX.Y.Z, and anything else (a commit, "dev") unchanged.

func EmulationActive

func EmulationActive() bool

EmulationActive reports whether dev-only version emulation is enabled.

func ErrorCode added in v0.7.15

func ErrorCode(err error) string

ErrorCode classifies an update failure as one of the ErrCode* constants, or "" for any other failure.

func GitCheckout added in v0.7.0

func GitCheckout() string

GitCheckout returns the source checkout the updater works on in git mode: a dev build running from an Aviary git checkout outside Docker, with version emulation off. It returns "" when the release updater is in force.

func GitCompareMessage added in v0.7.26

func GitCompareMessage(res GitPullResult) string

GitCompareMessage says what a git-mode update compared: the checkout's commit before the pull against its upstream's, whether the upstream was fetched first, and how far apart the two were.

func GitPullMessage added in v0.7.0

func GitPullMessage(res GitPullResult, plan DevReloadPlan) string

GitPullMessage summarizes a git-mode update for the user, given how the pulled code goes live.

func HotReloadDir added in v0.7.1

func HotReloadDir() string

HotReloadDir returns forgo's hot reload control directory when the backend runs under `forgo run --watch` and its agent started, or "" otherwise.

func HotReloadGeneration added in v0.7.1

func HotReloadGeneration(dir string) int

HotReloadGeneration returns the newest reload generation the watcher has linked in dir, or 0 before the first reload.

func InstallReasons added in v0.7.26

func InstallReasons() []string

InstallReasons explains how DetectInstall decided the install type, for aviary upgrade --verbose. It reruns checkout detection rather than reading the cached answer, so each step can say what it found.

func IsDevBuild

func IsDevBuild() bool

IsDevBuild reports whether the current binary version is not a clean semver release.

func IsReleaseVersion added in v0.7.24

func IsReleaseVersion(v string) bool

IsReleaseVersion reports whether v is a clean X.Y.Z release.

func NormalizeVersion added in v0.7.0

func NormalizeVersion(version string) string

NormalizeVersion returns version as a release tag with a leading "v", so "1.2.3" and "v1.2.3" name the same release. Empty input stays empty.

func RebuildCommand added in v0.7.26

func RebuildCommand(dir string) string

RebuildCommand is the command that rebuilds the running binary from the checkout at dir.

func Reexec added in v0.7.0

func Reexec(exe string, args []string, env []string) error

Reexec replaces the current process with exe, keeping its PID so service managers see the same process.

func ReleaseURL added in v0.7.0

func ReleaseURL(repo, version string) string

ReleaseURL returns the GitHub release page for version in repo.

func RemoveStaleIndexLock added in v0.7.15

func RemoveStaleIndexLock(ctx context.Context, dir string) error

RemoveStaleIndexLock removes the checkout's .git/index.lock when it is at least staleIndexLockAge old, holding the updater lock so no updater git command races the removal. A younger lock may belong to a running git command, so it is left alone and reported as ErrGitBusy.

func RequestRebuild added in v0.7.0

func RequestRebuild() bool

RequestRebuild asks scripts/dev-go.mjs to run the backend again from the checkout's sources once this process exits. It reports false when the backend is not supervised or the request could not be written, in which case the caller re-executes its own binary instead.

func SameVersion added in v0.7.0

func SameVersion(a, b string) bool

SameVersion reports whether a and b name the same release, ignoring a leading "v". Non-semver versions compare as plain strings.

func SourceCheckout added in v0.7.24

func SourceCheckout() string

SourceCheckout returns the checkout `aviary upgrade --source` updates: the one git mode would use, else the checkout containing the working directory. Only an explicit request for source mode looks at the working directory.

func Supervised added in v0.7.0

func Supervised() bool

Supervised reports whether a restart rebuilds the backend from its checkout.

func UpgradeSummary added in v0.7.26

func UpgradeSummary() string

UpgradeSummary says in one line which kind of install aviary upgrade is working on: a release at its path, or a dev build and its source checkout.

func WithCaller added in v0.7.15

func WithCaller(ctx context.Context, caller string) context.Context

WithCaller names who runs the updater git commands made with ctx, so the logs show where lock contention comes from.

Types

type CheckResult

type CheckResult struct {
	CurrentVersion   string    `json:"currentVersion"`
	LatestVersion    string    `json:"latestVersion,omitempty"`
	UpgradeAvailable bool      `json:"upgradeAvailable"`
	UpgradeSupported bool      `json:"upgradeSupported"`
	Emulated         bool      `json:"emulated"`
	CheckedAt        time.Time `json:"checkedAt,omitempty"`
	Message          string    `json:"message,omitempty"`
	// ReleaseURL links the latest version to its release page.
	ReleaseURL string `json:"releaseUrl,omitempty"`
	// Git is set in git mode, when a dev build runs from a source checkout:
	// updates are pulled from the checkout's upstream instead of a release.
	Git *GitState `json:"git,omitempty"`
	// NoCheckout is set for a dev build that would update in git mode but
	// cannot locate its source checkout, so it has no way to check for updates.
	NoCheckout bool `json:"noCheckout,omitempty"`
	// InstallType is the upgrade mode the check ran in: release or source,
	// or dev for a dev build with no checkout.
	InstallType InstallType `json:"installType,omitempty"`
}

CheckResult describes the local version and the latest known release.

func Check

func Check(ctx context.Context, client *http.Client) (CheckResult, error)

Check compares the current version with the latest available release, or in git mode with the source checkout's upstream branch.

func CheckMode added in v0.7.24

func CheckMode(ctx context.Context, client *http.Client, mode InstallType) (CheckResult, error)

CheckMode is Check in a forced mode: InstallRelease checks GitHub releases even on a dev build, and InstallSource checks the checkout SourceCheckout finds even on a release build. An empty mode picks it as Check does.

type DevReloadPlan added in v0.7.1

type DevReloadPlan struct {
	// GoChanged is true when the pull changed Go sources forgo watches.
	GoChanged bool
	// RestartReason, when set, is why only a full restart runs the pulled
	// code; hot reload cannot.
	RestartReason string
}

DevReloadPlan decides how pulled commits go live in a dev checkout.

func PlanDevReload added in v0.7.1

func PlanDevReload(ctx context.Context, dir, from, to string, hotReload bool) (DevReloadPlan, error)

PlanDevReload inspects the files changed between from and to in the dev checkout dir. A change to go.mod or go.sum, or to a file a Go package embeds, needs a restart, since forgo watches only Go sources. Embedded files a dev build reads from the checkout (see DevAssets) are the exception. Otherwise Go changes hot-reload when hotReload (forgo is active) is set, and need a restart when it is not.

type GitPullResult added in v0.7.0

type GitPullResult struct {
	Dir string `json:"dir"`
	// Upstream is the branch pulled from and UpstreamHead its fetched
	// commit; Ahead counts local commits the upstream lacks.
	Upstream     string `json:"upstream,omitempty"`
	UpstreamHead string `json:"upstreamHead,omitempty"`
	Ahead        int    `json:"ahead"`
	Fetched      bool   `json:"fetched"`
	From         string `json:"from"`
	To           string `json:"to"`
	// Head is the full commit checked out after the pull.
	Head       string `json:"head,omitempty"`
	Commits    int    `json:"commits"`
	Supervised bool   `json:"supervised"`
	HotReload  bool   `json:"hotReload"`
}

GitPullResult describes a git-mode update.

func GitPull added in v0.7.0

func GitPull(ctx context.Context, dir string) (res GitPullResult, err error)

GitPull fast-forwards the checkout to its upstream. It refuses to run when the tree has local changes or the pull would not fast-forward. Concurrent pulls on one checkout, from this or other processes, run one at a time, so a pull that waited finds the checkout already current.

type GitState added in v0.7.0

type GitState struct {
	Dir      string `json:"dir"`
	Branch   string `json:"branch"`
	Upstream string `json:"upstream,omitempty"`
	Behind   int    `json:"behind"`
	Ahead    int    `json:"ahead"`
	Dirty    bool   `json:"dirty"`
	// Fetched is true once the upstream was fetched before comparing.
	Fetched bool `json:"fetched"`
	// Supervised is true when a restart rebuilds the backend from the
	// checkout (see RestartFileEnv), so pulled sources go live by restarting.
	Supervised bool `json:"supervised"`
	// HotReload is true when the backend runs under forgo's hot reload, so
	// pulled Go changes go live without a restart where forgo can apply them.
	HotReload bool `json:"hotReload"`
	// HotReloadDir is forgo's control directory when HotReload is set, so
	// aviary upgrade can wait for the reload of the code it pulled.
	HotReloadDir string `json:"hotReloadDir,omitempty"`
}

GitState describes a dev checkout's position relative to its upstream.

func ReadGitState added in v0.7.0

func ReadGitState(ctx context.Context, dir string) (GitState, error)

ReadGitState fetches the checkout's upstream and reports how far behind it is and whether the working tree has local changes. It waits for any other updater's git work on the checkout, in this or another process, to finish.

type HotReloadResult added in v0.7.1

type HotReloadResult struct {
	// Applied is true when the newest reload went live in this process.
	Applied bool
	// Generation is the reload generation the verdict is about.
	Generation int
	// Reason says why the pulled code is not running when Applied is false.
	Reason string
}

HotReloadResult is forgo's verdict on the reloads after a pull.

func WaitHotReload added in v0.7.1

func WaitHotReload(ctx context.Context, dir string, since int, opts HotReloadWait) HotReloadResult

WaitHotReload waits for forgo to reload the code a pull brought in, and reports whether the newest reload after generation since applied. A reload forgo refuses or fails, or no reload at all before the timeout (a pulled tree that does not compile never links), is reported with its reason.

type HotReloadWait added in v0.7.1

type HotReloadWait struct {
	// Poll is how often the control directory is read.
	Poll time.Duration
	// Timeout bounds the whole wait.
	Timeout time.Duration
	// Answer bounds how long a linked generation may go unanswered; the
	// watcher itself gives the agent 30 seconds.
	Answer time.Duration
	// Settle is added to the time the verdict took to arrive to decide how
	// long to keep watching for a later generation: one that started on a
	// partly written tree is followed by another once the first finishes,
	// and that one takes about as long again.
	Settle time.Duration
}

HotReloadWait tunes WaitHotReload.

type InstallOptions

type InstallOptions struct {
	Version    string
	TargetPath string
	Repo       string
	// DownloadBase is the release host; see downloadBase.
	DownloadBase string
	Client       *http.Client
}

InstallOptions controls how a release is downloaded and installed.

type InstallResult

type InstallResult struct {
	Version  string `json:"version"`
	Emulated bool   `json:"emulated"`
	Noop     bool   `json:"noop"`
}

InstallResult describes the outcome of an attempted upgrade install.

func Install

func Install(ctx context.Context, opts InstallOptions) (InstallResult, error)

Install downloads and installs the requested Aviary release into TargetPath.

type InstallType added in v0.7.24

type InstallType string

InstallType says how the running binary was built and installed, which decides how it upgrades.

const (
	// InstallRelease is a release build: it upgrades by downloading the
	// latest GitHub release over its own executable.
	InstallRelease InstallType = "release"
	// InstallSource is a dev build run from an Aviary checkout: it upgrades
	// by pulling the checkout.
	InstallSource InstallType = "source"
	// InstallDev is a dev build with no checkout to pull, such as go run
	// without AVIARY_CHECKOUT, or a dev build inside Docker.
	InstallDev InstallType = "dev"
)

func DetectInstall added in v0.7.24

func DetectInstall() InstallType

DetectInstall reports the running binary's install type. It depends only on the version stamped at build time and on where the executable (or AVIARY_CHECKOUT) points, never on the working directory, so a release binary run from inside a clone is still a release install.

type StaleLockError added in v0.7.15

type StaleLockError struct {
	Path string
	Age  time.Duration
}

StaleLockError reports a .git/index.lock old enough that no running git command is likely to own it. RemoveStaleIndexLock clears it.

func (*StaleLockError) Error added in v0.7.15

func (e *StaleLockError) Error() string

Jump to

Keyboard shortcuts

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