selfupdate

package
v2.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 27 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 selfupdate updates a binary install from the UI: download the release asset, check checksums.txt and its ed25519 signature, probe the new binary, swap it in keeping the old one, restart, and roll back when the new one does not start. Containers, read-only installs and open ports without auth get the commands to run instead.

Index

Constants

View Source
const (
	PhaseIdle            = "idle"
	PhaseDownloading     = "downloading"
	PhaseVerifying       = "verifying"
	PhaseInstalling      = "installing"
	PhaseRestarting      = "restarting"
	PhaseDone            = "done"
	PhaseFailed          = "failed"
	PhaseRolledBack      = "rolled_back"
	PhaseRestartRequired = "restart_required"
)

Phases of State.

View Source
const (
	ModeSelf   = "self"   // the button: download, verify, replace, restart
	ModeManual = "manual" // no button: the state carries commands to copy
	ModeOff    = "off"    // nothing to offer
)

Modes of GET /api/update.

View Source
const (
	InstallBinary     = "binary"
	InstallDocker     = "docker"
	InstallKubernetes = "kubernetes"
)

Install kinds.

View Source
const (
	SettingAuto = "auto" // self when safe: a container, a read-only dir or an open port without auth give manual
	SettingOn   = "on"   // also self on an open port without auth
	SettingOff  = "off"  // no update from the UI
)

Setting values of update.self_update.

Variables

View Source
var (
	ErrBusy       = errors.New("an update is already running")
	ErrNotLatest  = errors.New("only the latest release can be installed")
	ErrNotAllowed = errors.New("update from the UI is not allowed")
)

Errors of Start, mapped to HTTP codes by the API.

Functions

func AssetName

func AssetName(goos, goarch string) string

AssetName is the release file of this platform.

func Boot

func Boot(version string) io.Writer

Boot runs first in main. It reads the state an update left next to the binary: a first start of the new version keeps its output (for the rolled_back log) and is rolled back by Exit until Ready; a start that finds the last start of this version never got ready rolls back at once. The returned writer is where the log goes.

func Exit

func Exit(code int)

Exit replaces os.Exit on start-up failures: a post-update boot that has not served yet restores the previous binary and database and runs it.

func LoopbackOnly

func LoopbackOnly(addr string) bool

LoopbackOnly reports whether a listen address takes local connections only. ":8000" and "0.0.0.0:8000" listen on every interface.

func Ready

func Ready()

Ready marks a post-update boot as good: the state turns done and the database copy goes. Call it once the server serves.

func Reexec

func Reexec()

Reexec runs the binary in place of this process (the new one after a swap).

func Restarting

func Restarting() bool

Restarting reports that an update asked for a restart: main calls Reexec once everything is closed.

Types

type Command

type Command struct {
	ID      string `json:"id"`
	Label   string `json:"label"`
	Command string `json:"command"`
}

Command is one line the user copies for a manual update.

func Commands

func Commands(install, goos, exe, version string) []Command

Commands are the manual update lines for the install kind; version is the release to install ("" = latest).

type Config

type Config struct {
	Checker   *updates.Checker // nil: the update check is off
	Setting   string           // update.self_update
	AuthOn    bool
	HTTPAddr  string
	DSN       string
	DB        *sql.DB
	Broadcast func(State) // sends update.progress; may be nil
	Restart   func()      // stops the app so main re-execs; may be nil
}

Config wires an Updater.

type Facts

type Facts struct {
	Version   string // running version, "dev" for a build without one
	Release   bool   // Version is a release version
	CheckOn   bool   // update_check.enabled
	Setting   string // update.self_update
	AuthOn    bool
	HTTPAddr  string
	GOOS      string
	Exe       string // resolved path of the running binary
	Container string // "", InstallDocker or InstallKubernetes
	Writable  bool   // the binary's dir takes a new file
}

Facts are what the mode is decided from. Detect fills them for the running process; tests build them by hand.

type Info

type Info struct {
	Current       string     `json:"current"`
	Latest        string     `json:"latest"`
	Available     bool       `json:"available"`
	URL           string     `json:"url"`
	Notes         string     `json:"notes"`
	CheckedAt     *time.Time `json:"checked_at"`
	CheckEnabled  bool       `json:"check_enabled"`
	Mode          string     `json:"mode"`
	Reason        string     `json:"reason"`
	Install       string     `json:"install"`
	OS            string     `json:"os"`
	Commands      []Command  `json:"commands"`
	MemoryDB      bool       `json:"memory_db"`
	RestartManual bool       `json:"restart_manual"`
	State         State      `json:"state"`
}

Info is the body of GET /api/update.

type Mode

type Mode struct {
	Mode          string
	Reason        string
	Install       string
	RestartManual bool
}

Mode is the decision: what the UI may offer and why.

func Decide

func Decide(f Facts) Mode

Decide picks the mode. Order: off reasons, then where it runs, then safety.

type State

type State struct {
	Phase      string     `json:"phase"`
	Version    string     `json:"version"`
	Previous   string     `json:"previous"`
	FailedStep string     `json:"failed_step"`
	Bytes      int64      `json:"bytes"`
	Total      int64      `json:"total"`
	Error      string     `json:"error"`
	Log        string     `json:"log"`
	UpdatedAt  *time.Time `json:"updated_at"`
}

State is the progress of an update, sent to the UI as is.

type Updater

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

Updater runs one update at a time.

func New

func New(cfg Config, version string) *Updater

New decides the mode for this process.

func (*Updater) CheckNow

func (u *Updater) CheckNow(ctx context.Context) (wait time.Duration, err error)

CheckNow asks GitHub at once, at most once a minute; wait > 0 is how long until the next check is allowed.

func (*Updater) Info

func (u *Updater) Info() Info

Info is the full answer of GET /api/update.

func (*Updater) Register

func (u *Updater) Register(mux *http.ServeMux, protect func(http.Handler) http.Handler)

Register adds GET /api/update, POST /api/update/check and POST /api/update behind the API's auth middleware.

func (*Updater) Start

func (u *Updater) Start(version string) error

Start begins the update to version in the background.

Jump to

Keyboard shortcuts

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