Documentation
¶
Overview ¶
Package goupd provides an auto-update mechanism for Go applications built with the make-go build system. It handles checking for updates, downloading new versions, and performing in-place binary replacement with automatic restart.
The package works by connecting to an update server to check for newer versions based on git tags and build dates. When an update is found, it downloads the new binary, replaces the current executable atomically, and restarts the program.
Basic usage:
func main() {
goupd.AutoUpdate(false) // Start auto-updater in production mode
// ... rest of your application
}
Index ¶
- Variables
- func AutoUpdate(allowTest bool)
- func Busy()
- func Fetch(projectName, curTag, os, arch, channel string, ...) errordeprecated
- func GetUpdate(projectName, curTag, os, arch, channel string) (string, string, string, error)deprecated
- func GetVars() map[string]string
- func Restart()
- func RestartProgram() error
- func RunAutoUpdateCheck() bool
- func SignalVersion(git, build string)
- func SignalVersionChannel(git, build, channel string)
- func SwitchChannel(channel string) bool
- func TryBusy() bool
- func Unbusy()
- type Version
Constants ¶
This section is empty.
Variables ¶
var ( // PROJECT_NAME is the project identifier used for update lookups. // It is auto-detected from the module path at runtime, or can be // overridden via ldflags if needed. PROJECT_NAME string = "unconfigured" // MODE indicates the build mode, either "DEV" or "PROD". // Auto-updates only run when MODE is "PROD". MODE string = "DEV" // CHANNEL is the release channel (git branch name) for updates. // Typically set at build time from: git rev-parse --abbrev-ref HEAD CHANNEL string = "" // GIT_TAG is the first 7 characters of the git commit hash, // auto-detected from vcs.revision build info. Used to identify // the current version and compare with available updates. GIT_TAG string = "" // DATE_TAG is the commit timestamp in YYYYMMDDhhmmss format, // auto-detected from vcs.time build info. Used as a secondary // version comparison when git tags differ. DATE_TAG string = "0" // VERSION is the full version string from Go module info. VERSION string = "" // HOST is the base URL of the update distribution server. HOST string = "https://dist-go.tristandev.net/" )
var BeforeRestart func() = shutdown.RunDefer
BeforeRestart is called just before the program is restarted, and can be used to prepare for restart, such as duplicating fds before exec/etc.
var RestartFunction func() error = RestartProgram
RestartFunction is the function that actually performs the restart, and by default will be RestartProgram which is an OS dependent implementation
var UnameArchMatch = map[string]string{
"x86_64": "amd64",
"amd64": "amd64",
"i686": "386",
"powerpc": "ppc",
"ppc": "ppc",
"ppc64": "ppc64",
"armv7l": "arm",
"armv6l": "arm",
"armv7b": "armbe",
"armv6b": "armbe",
"aarch64": "arm64",
"aarch64_be": "arm64be",
"armv8b": "arm64be",
"armv8l": "arm64",
"mips": "mips",
}
uname -m
var UnameOsMatch = map[string]string{
"Linux": "linux",
"CYGWIN_*": "windows",
"MINGW*": "windows",
"WIN32": "windows",
"WINNT": "windows",
"Windows": "windows",
"Darwin*": "darwin",
"FreeBSD": "freebsd",
}
uname -s
Functions ¶
func AutoUpdate ¶
func AutoUpdate(allowTest bool)
AutoUpdate initializes the auto-updater system. It logs the current version, sets up a SIGHUP signal handler to trigger manual update checks, and starts a background goroutine that checks for updates hourly.
If allowTest is true, updates are checked even in non-production mode. Otherwise, updates only run when MODE is "PROD".
The function respects the following environment variables:
- GOUPD_DELAY: If set, delays startup by that many seconds (used after restarts)
- GOUPD_NOW: If set, triggers an immediate update check instead of waiting
Updates can also be triggered by emitting a "check_update" event via the global emitter.
func Busy ¶ added in v0.1.3
func Busy()
Busy marks the program as busy, preventing updates and restarts until Unbusy is called. Multiple goroutines can call Busy concurrently; the program will remain busy until all have called Unbusy. This is useful for protecting critical sections that should not be interrupted by an update.
func GetVars ¶ added in v0.2.6
GetVars returns all configuration variables as a map of strings. This is useful for debugging or logging the current build configuration.
func Restart ¶ added in v0.4.0
func Restart()
Restart triggers a program restart. It first waits for any busy operations to complete (with a 1-hour timeout), calls BeforeRestart for cleanup, then executes RestartFunction to replace the current process with a new instance.
func RestartProgram ¶ added in v0.2.2
func RestartProgram() error
func RunAutoUpdateCheck ¶
func RunAutoUpdateCheck() bool
RunAutoUpdateCheck will perform the update check, update the executable and return false if no update was performed. In case of update the program should restart and RunAutoUpdateCheck() should not return, but if it does, it'll return true.
func SignalVersion ¶
func SignalVersion(git, build string)
SignalVersion is called when seeing another peer running the same software to notify of its version. This will check if the peer is updated compared to us, and call RunAutoUpdateCheck() if necessary
func SignalVersionChannel ¶ added in v0.3.1
func SignalVersionChannel(git, build, channel string)
SignalVersionChannel performs the same as SignalVersion but will also check channel
func SwitchChannel ¶ added in v0.3.0
SwitchChannel will update the current running daemon to run on the given channel. It will return false if the running instance is already the latest version on that channel
Types ¶
type Version ¶ added in v0.3.3
type Version struct {
ProjectName string // project's name
Channel string // project channel (typically, branch name)
DateTag string // version's date tag in YYYYMMDDhhmmss format
GitTag string // version's git tag (first 7 characters of git hash)
UpdatePrefix string // internally used prefix for constructing download URLs
}
Version represents a specific version of a project available on the update server. It contains all the information needed to download and install the update, including version identifiers and download URL components.
func GetLatest ¶ added in v0.3.3
GetLatest returns the latest version information for a given projectName and channel
func (*Version) CheckArch ¶ added in v0.3.3
CheckArch checks if the provided version is compatible with the provided os and arch
func (*Version) Download ¶ added in v0.3.3
func (v *Version) Download(os, arch string) (io.ReadCloser, error)
Download returns a ReadCloser that allows reading the updated executable data. It will handle any decompression that might be needed, so the data can be read directly. Make sure to close the returned ReadCloser after usage.
func (*Version) Install ¶ added in v0.4.0
Install will download the update and replace the currently running executable data
func (*Version) IsCurrent ¶ added in v0.4.0
IsCurrent returns true if the version matches the currently running program