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.
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.
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. Default: nothing is called.
- Client.Progress: reports the bytes received and the announced size, or -1, while the archive downloads. Default: no report.
Index ¶
- 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 Entry
- type Prepared
- type PreviousSignature
- type ReleaseFiles
- type Result
- type Target
- type TargetError
- type TargetReason
- type UnsupportedInstallationError
Constants ¶
This section is empty.
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
// AfterReplace runs once the program is replaced. 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)
}
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, the program last. If the program cannot be replaced, the companions are restored.
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 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 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 UnsupportedInstallationError ¶
UnsupportedInstallationError reports an installed file that cannot be replaced safely in place.
func (*UnsupportedInstallationError) Error ¶
func (e *UnsupportedInstallationError) Error() string