Documentation
¶
Overview ¶
Package selfupdate selects, verifies and installs signed GitHub releases of the running program.
A Client reads the releases of one repository through its API. Check reports the newest eligible stable release, List offers versions to choose from, and Target selects an exact one, an older one for a rollback included. Update installs the newest stable release in one call when it is newer. For a chosen target, Prepare downloads and verifies the release without changing anything, and InstallPrepared installs it. Consequences returns the data the application words its question before an installation from; all texts belong to the application.
The application supplies the repository, the versions, the archive name of each version, the program name, the signature namespace, the name of the signature file, the trusted keys and every installation path; the signature file has no default.
Always enforced ¶
The repository API address must be https, or http on the local machine, and name /repos/owner/repository. A release is eligible only if it is published, carries a valid version not below MinimumVersion, and lists its archive, checksums.txt and the signature file exactly once each, at asset URLs under the repository's own API path. A build without a release version is refused with ErrDevelopmentBuild.
Prepare checks the signature with release.VerifyChecksums before it downloads the archive, then the archive's checksum and its contents with release.ProgramSHA256. Every archive entry must have a safe path that no other entry shares; a single leading "./" is accepted. The program is "bin/<program>" in the archive, with ".exe" added in a .zip archive, and each Companion must be one non-empty regular file. The program is at most 256 MiB and a companion at most 2 MiB. Metadata files and the archive are read only up to a size limit, and a download ends after 30 seconds without data. The release API never follows a redirect. An asset download follows a redirect only to https hosts of GitHub, at most ten times. The token is sent to the repository's API host only, never along a redirect.
When checksums.txt lists release.DirectUpdateAsset, the release must carry that file exactly once under a valid asset URL, and its SHA-256 must match the signed checksums.txt before it is parsed; it is read up to 4 KiB and parsed strictly, so unknown fields, trailing data and an unknown format number make the release ineligible. Prepare, and so Update, then refuses an update to a newer release from a running version below the oldest direct version with a *DirectUpdateError, before it downloads the archive and before anything is changed. The check does not apply to an older release, so a rollback is never refused by it. A file that checksums.txt does not list is ignored.
A Prepared release is bound to the repository and keys of the client that prepared it and to the namespace, signature file, archive, program and companion entries of its version; InstallPrepared refuses it under any other policy. It refuses a program path that is not a regular file named after the program, or for a release of the old set after the current or the previous program, and companion paths that are not regular files named after the entries of the release's set, that are not absolute or that are not unique. An older release is installed only after ValidateRollback accepted it. Companions are installed first and the program last, each staged next to its target and renamed into place; on Windows the running program is moved aside first and CleanupLeftovers removes it later. If the program cannot be replaced, every companion is restored.
After every installation, a rollback included, InstallPrepared runs the transition step of the program it just installed at Client.Executable, never a path from the release or the environment, with Client.TransitionArgs and the version installed before as the last argument. The step runs in the caller's environment under the context of InstallPrepared; once the step or the context ended, InstallPrepared waits at most one second for output still held open. The new release carries its own transitions of setup and stored state in that step, so an installed version never needs to know them. The arguments are a lasting contract of the application, because every older installation calls the new program with them; an application without transitions answers them with success. Until the step ended, a copy of the previous program and of every replaced companion stays next to its target. Exit code 0 means success, and only then AfterReplace runs. Exit code ExitTransitionReverted means that the step failed and took back everything it changed: the previous program and then the previous companions are put back, on Windows with the new program moved aside as at the installation, Result.Updated is false and the *TransitionError reports Reverted. If the previous program cannot be put back, the new program and its companions stay; if a previous companion cannot, the previous program stays back. RevertErr says why. Every other end of the step, another exit code, a program that cannot start, an abort or the end of the context, leaves the new program in place with Result.Updated true and a *TransitionError without Reverted, so the application can have the step run again. A process that BeforeReplace prepared for a handover waits for AfterReplace only for a limited time, vaultproc for HandoverTimeout; a step that fails or outlasts that time ends the handover on the hook's failure path, which for vaultproc locks the vault.
Options ¶
- Client.Token: a bearer token for the repository's API, for example for a private repository. Default: no token is sent.
- Client.HTTPClient: the HTTP client for the release API and the downloads, copied with its redirect rules replaced. Default: a client with a 30 second timeout for the API; downloads are bounded by the stall limit only.
- Client.Companions: files the archive carries next to the program, such as a manpage, installed with it and restored if the program cannot be replaced. Default: the program only.
- Client.PreviousSignature: after an application renamed its signature namespace and file, names the old set and the newest release signed only with it. Releases up to and including that version are verified with the old set alone, every later release with the current set alone, under the same trusted keys and the same archive checks. The old set may also name the previous program and companion entries, with which the archives of those releases are read and installed. Which archive name a version has stays with ArchiveName. The installers of the release package always use the current set. Default: one set for every release.
- Client.ValidateRollback: checks the prepared program of an older release, for example with Prepared.ReadsConfig against an isolated copy of a configuration. Default: installing an older release is refused.
- Client.BeforeReplace: receives the verified release files right before the first installed file is replaced, for example to hand a running process over; an error ends the installation with nothing replaced. Default: nothing is called.
- Client.AfterReplace: receives the verified release files once the program is replaced and its transition step succeeded. Default: nothing is called.
- Client.TransitionArgs: the arguments of the transition step, which the application chooses. They are required and have no default.
- Client.TransitionStdout and Client.TransitionStderr: receive the output of the transition step. Default: the output is discarded.
- Client.Progress: reports the bytes received and the announced size, or -1, while the archive downloads. Default: no report.
Index ¶
- Constants
- Variables
- func CleanupLeftovers(executable string) error
- func OtherProcesses(name string) int
- type Client
- func (c *Client) Check(ctx context.Context) (Result, error)
- func (c *Client) Consequences(t Target) Consequences
- func (c *Client) InstallPrepared(ctx context.Context, p *Prepared) (Result, error)
- func (c *Client) List(ctx context.Context) ([]Entry, error)
- func (c *Client) Prepare(ctx context.Context, target Target) (*Prepared, error)
- func (c *Client) Target(ctx context.Context, tag string) (Target, error)
- func (c *Client) Update(ctx context.Context) (Result, error)
- type Companion
- type ConfigCheck
- type Consequences
- type DirectUpdateError
- type Entry
- type Prepared
- type PreviousSignature
- type ReleaseFiles
- type Result
- type Target
- type TargetError
- type TargetReason
- type TransitionError
- type UnsupportedInstallationError
Constants ¶
const ExitTransitionReverted = 79
ExitTransitionReverted is the exit code with which the transition step of a newly installed program reports that it failed and took back everything it changed. Exit code 0 reports success.
Variables ¶
var ErrDevelopmentBuild = errors.New("self-update is unavailable in a development build")
ErrDevelopmentBuild reports a running build without a release version.
Functions ¶
func CleanupLeftovers ¶
CleanupLeftovers removes a prior Windows replacement at the explicit executable path.
func OtherProcesses ¶
OtherProcesses counts the processes of the current user besides this one whose command name is name, read through /proc. A failure is -1, because the count only informs a question.
Types ¶
type Client ¶
type Client struct {
RepositoryAPI string
CurrentVersion string
MinimumVersion string
ArchiveName func(version string) string
UserAgent string
HTTPClient *http.Client
// Token, when set, is sent as a bearer token to the host of RepositoryAPI and to no other host.
Token string
Namespace string
// SignatureAsset is the name of the release asset that holds the SSH signature over checksums.txt,
// verified under Namespace. It is required and has no default: a plain file name that is neither
// checksums.txt nor the archive name.
SignatureAsset string
TrustedKeys []ssh.PublicKey
// PreviousSignature, when set, makes releases up to and including its LastVersion verify only under
// the previous signature set instead of Namespace and SignatureAsset, and read the previous program
// and companion entries where it names them.
PreviousSignature *PreviousSignature
ProgramName string
// Companions are installed together with the program and restored if the program cannot be replaced.
Companions []Companion
Executable string
// ValidateRollback must check the prepared program against an isolated copy of the application's
// configuration, for example with Prepared.ReadsConfig. Installing an older release requires it.
ValidateRollback func(context.Context, *Prepared) error
// BeforeReplace runs once everything is verified and staged, right before the first installed file is
// replaced. An error ends the installation with nothing replaced.
BeforeReplace func(context.Context, ReleaseFiles) error
// TransitionArgs are the arguments with which the newly installed program runs its transition step
// after every installation; the version installed before is appended as the last argument. They are
// required.
TransitionArgs []string
// TransitionStdout and TransitionStderr receive the output of the transition step; nil discards it.
TransitionStdout, TransitionStderr io.Writer
// AfterReplace runs once the program is replaced and its transition step succeeded. The installation
// is complete by then.
AfterReplace func(context.Context, ReleaseFiles)
// Progress is called while the archive downloads, with the bytes received and the announced size, or -1
// when the server does not announce it.
Progress func(done, total int64)
// contains filtered or unexported fields
}
Client holds the release source and policy supplied by the application.
func (*Client) Check ¶
Check reports the newest eligible stable release without downloading artifacts.
func (*Client) Consequences ¶
func (c *Client) Consequences(t Target) Consequences
Consequences describes what installing t does on this platform now.
func (*Client) InstallPrepared ¶
InstallPrepared installs a verified release at the caller supplied paths, companions first and the program last, and then runs the transition step of the installed program. If the program cannot be replaced, the companions are restored. A step that does not succeed is a *TransitionError; when it exited with ExitTransitionReverted, the previous program and companions are put back.
func (*Client) List ¶
List shows ten newest stable releases, newer prereleases, and the running version if eligible.
func (*Client) Prepare ¶
Prepare downloads and verifies the selected release after caller confirmation. It verifies the signature over checksums.txt before it downloads the archive, and the archive before it reads it.
type Companion ¶
type Companion struct {
// Entry is the canonical slash-separated path in the archive, for example "share/man/man1/tool.1".
Entry string
// Path is the absolute installation path. Its base name must equal the base name of Entry.
Path string
}
Companion is a file that the release archive carries next to the program, such as a manpage.
type ConfigCheck ¶
type ConfigCheck struct {
// Config holds the bytes of the configuration. The program only ever sees a copy.
Config []byte
// FileName is the plain file name of the copy, such as "config.yaml".
FileName string
// Args returns the program's arguments for checking the copy at configPath.
Args func(configPath string) []string
// Timeout bounds the run; zero means 20 seconds.
Timeout time.Duration
}
ConfigCheck asks the program of a prepared release whether it reads a configuration.
type Consequences ¶
type Consequences struct {
Version string
Prerelease bool
Older bool
// RenamesRunning reports that the running executable is moved aside to a ".old" file, which
// CleanupLeftovers removes later, because the platform cannot replace a running program in place.
RenamesRunning bool
// OtherProcesses is the number of other processes of the current user that run the program, or -1 when
// it cannot be counted.
OtherProcesses int
}
Consequences is the data an application words its question before an installation from.
type DirectUpdateError ¶ added in v0.28.0
type DirectUpdateError struct {
// Version is the release that was refused.
Version string
// OldestDirect is the oldest version from which Version updates directly.
OldestDirect string
}
DirectUpdateError reports a release that cannot be installed directly from the running version because the running version is older than the oldest version from which the release updates directly. The application words the next step and may offer OldestDirect through Target.
func (*DirectUpdateError) Error ¶ added in v0.28.0
func (e *DirectUpdateError) Error() string
type Prepared ¶
type Prepared struct {
// contains filtered or unexported fields
}
Prepared holds a downloaded and verified release and can only be made by Prepare outside this package.
func (*Prepared) ArchiveName ¶
ArchiveName is the file name of the archive, by which checksums.txt lists it.
func (*Prepared) ReadsConfig ¶
ReadsConfig runs the verified program of the release in a fresh private directory, against a copy of the configuration, with an environment that points every home, configuration, data, cache, runtime and temporary directory into that directory and carries nothing else of the caller but PATH and the Windows system root. Exit status 0 means the program reads the configuration, any other exit status means it does not. A run that cannot be carried out is an error, not an answer. The program's output is discarded, because it is not trusted and may quote the configuration.
type PreviousSignature ¶ added in v0.13.0
type PreviousSignature struct {
// LastVersion is the newest release signed only with the previous set, such as "v1.4.0".
LastVersion string
// Namespace is the signature namespace of those releases.
Namespace string
// SignatureAsset is the name of the release asset that holds their signature over checksums.txt, with
// the same rules as Client.SignatureAsset.
SignatureAsset string
// ProgramName, when set, is the program name in the archives of those releases, which then carry
// "bin/<ProgramName>". It must differ from Client.ProgramName. Empty keeps Client.ProgramName.
ProgramName string
// ReplaceCompanions makes Companions the companions of those releases instead of Client.Companions;
// an empty Companions then means that their archives carry none. Without it Companions must be empty.
ReplaceCompanions bool
// Companions are the companion entries of those releases and their installation paths, with the rules
// of Client.Companions.
Companions []Companion
}
PreviousSignature describes the release set of the oldest releases after an application renamed its signature namespace and signature file, and possibly its program and companion files. A release up to and including LastVersion is verified only with this namespace and signature file, and its archive is read with this program name and these companions where they are given; every later release uses only the current Client.Namespace, Client.SignatureAsset, Client.ProgramName and Client.Companions. A later release that still carries the previous file is not verified with it. The same Client.TrustedKeys and the same archive checks apply to both sets, and the installers of the release package always use the current set.
type ReleaseFiles ¶
type ReleaseFiles struct {
Checksums, Signature, Archive string
// ArchiveName is the archive's file name in the release, by which Checksums lists it.
ArchiveName string
// Rollback reports that the release is older than the running version.
Rollback bool
}
ReleaseFiles names copies of the verified release files in a private directory that exists until InstallPrepared returns. All paths are absolute.
type Result ¶
type Result struct {
Current string
Latest string
UpdateAvailable bool
// Updated reports that the program of the release is installed at Client.Executable; after a
// TransitionError it is false only once the previous version is back.
Updated bool
// Companions lists the companion paths that were written; a companion whose content is unchanged is
// left alone.
Companions []string
}
Result describes the newest eligible stable release or, from InstallPrepared, the installed release.
type Target ¶
type Target struct {
Entry
// contains filtered or unexported fields
}
Target is a selected release. Its metadata is retained for later verification.
type TargetError ¶
type TargetError struct {
Version string
Reason TargetReason
}
TargetError reports a release that cannot be chosen. The application words its message from Reason.
func (*TargetError) Error ¶
func (e *TargetError) Error() string
type TargetReason ¶
type TargetReason int
TargetReason says why a release cannot be chosen.
const ( // TargetInvalidVersion is a version that is not a v-prefixed semantic version. TargetInvalidVersion TargetReason = iota + 1 // TargetBelowMinimum is a version older than Client.MinimumVersion. TargetBelowMinimum // TargetNotFound is a version the repository has no published release for. TargetNotFound // TargetIneligible is a release that is a draft, unsigned, incomplete, or has assets outside the // repository. TargetIneligible // TargetRunning is the running version, which has nothing to install. TargetRunning )
type TransitionError ¶ added in v0.28.0
type TransitionError struct {
// Err is why the step did not succeed.
Err error
// Reverted reports that the previous program and companions are installed again.
Reverted bool
// RevertErr is why the previous program or a previous companion could not be put back after the step
// asked for it.
RevertErr error
}
TransitionError reports that the transition step of the newly installed program did not succeed.
func (*TransitionError) Error ¶ added in v0.28.0
func (e *TransitionError) Error() string
func (*TransitionError) Unwrap ¶ added in v0.28.0
func (e *TransitionError) Unwrap() error
type UnsupportedInstallationError ¶
UnsupportedInstallationError reports an installed file that cannot be replaced safely in place.
func (*UnsupportedInstallationError) Error ¶
func (e *UnsupportedInstallationError) Error() string