Documentation
¶
Overview ¶
Package selfupdate installs a new Mediarium program file next to the container image's own, without Docker access and without extra privileges.
The container image starts /app/app. When a newer program file sits in <config>/update/app, the image's entrypoint (docker-entrypoint.sh) starts that one instead, and falls back to the image's own program if the new one keeps failing. This package is the app's half: it receives a file (from an administrator's upload, or from a signed GitHub release), checks it, puts it in place, and keeps the previous one so the change can be undone. Files it writes:
app the installed program (mode 0755) app.sha256 its SHA-256, checked by the entrypoint before every start VERSION its version number app.previous the program that was installed before (and VERSION.previous) app.failed a program the entrypoint gave up on (and VERSION.failed) boot-count starts since the program last proved itself healthy
Index ¶
- Constants
- Variables
- func BootCount(dir string) int
- func CleanTemp(dir string)
- func Decide(running, image, candidate string, force bool) error
- func Dir(configDir string) string
- func Install(dir, tmp, version, sum string) error
- func MarkHealthy(dir string)
- func ParseChecksum(s string) (string, error)
- func Receive(dir string, body io.Reader, max int64) (path, sum string, n int64, err error)
- func Remove(dir string) error
- func RunningPushed(dir string) bool
- func State(dir string) (pushed *Pushed, failed *Failed)
- func TakeSafeOnce(dir string) bool
- func TakeStuck(dir string) (reason string, at time.Time, ok bool)
- func VersionLine(version string) string
- func WriteSafeOnce(dir string) error
- func WriteStuck(dir, reason string) error
- type Failed
- type Probe
- type Pushed
Constants ¶
const ( FileSafeOnce = "safe-once" // start once with automation paused FileStuck = "stuck-restart" // the app restarted itself because it stopped answering )
Small marker files in the update folder. They carry a message across a restart, from the process that is stopping to the one that starts.
const ( DirName = "update" FileApp = "app" FileSum = "app.sha256" FileVersion = "VERSION" FilePrevious = "app.previous" FilePrevVer = "VERSION.previous" FileFailed = "app.failed" FileFailedVer = "VERSION.failed" FileBootCount = "boot-count" )
File names inside the update folder. The entrypoint script uses the same names; keep the two in step.
const ( // MaxBinaryBytes is the biggest program file accepted (the real one is // about 45 MB). MaxBinaryBytes = 200 << 20 // HealthyAfter is how long an installed program must keep running before // it counts as good and the start counter is cleared. A program that dies // sooner, MaxBootAttempts times in a row, is put aside by the entrypoint. HealthyAfter = 20 * time.Second MaxBootAttempts = 3 )
Limits and timings.
Variables ¶
var ( ErrTooLarge = errors.New("the file is too large") ErrEmpty = errors.New("the file is empty") ErrChecksumFormat = errors.New("the checksum is not a 64-character hex SHA-256") ErrChecksumMismatch = errors.New("the file does not match the checksum") ErrNotMediarium = errors.New("the file is not a Mediarium program") ErrWrongPlatform = errors.New("the file is for another system") ErrBadVersion = errors.New("the version number cannot be compared") ErrNotNewer = errors.New("the version is not newer than the running one") ErrOlderThanImage = errors.New("the version is older than the one in the container image") )
Errors the callers turn into plain sentences.
Functions ¶
func BootCount ¶
BootCount is how many times the installed program has been started without yet proving itself.
func CleanTemp ¶
func CleanTemp(dir string)
CleanTemp removes upload and download leftovers, for example from a request that was cut off.
func Decide ¶
Decide applies the version rules for installing candidate:
- it must be a version number;
- it may not be older than the version inside the container image, because the entrypoint would ignore it and start the image's own program;
- unless force is set it must be newer than the running version (force allows the same or an older one, for going back after a bad update).
running and image may be empty or "dev" when unknown: then only force allows the install.
func Install ¶
Install puts the checked program at tmp in place as the installed update: the current one (if any) becomes app.previous, the new one becomes app, and the version and checksum files are written. Every step is a rename inside one folder, so a crash leaves either the old program or the new one, never half of a file. The start counter and any earlier "failed" mark are cleared so the new program gets its full number of tries.
func MarkHealthy ¶
func MarkHealthy(dir string)
MarkHealthy clears the start counter. The app calls it once it has been running and answering for HealthyAfter.
func ParseChecksum ¶
ParseChecksum reads a SHA-256 given as 64 hex digits (any case).
func Receive ¶
Receive copies body into a new temporary file in dir, hashing it as it goes, and stops at max bytes. It returns the file's path, its SHA-256 and its size; the file is removed if anything goes wrong.
func Remove ¶
Remove deletes everything installed, so the next start uses the image's own program. It does not touch the running process.
func RunningPushed ¶
RunningPushed reports whether this process is the installed program.
func TakeSafeOnce ¶
TakeSafeOnce reports whether a safe-mode start was asked for, and removes the request so it applies to this start only.
func TakeStuck ¶
TakeStuck returns the reason and time of a restart the app made because it stopped answering, and removes the mark.
func VersionLine ¶
VersionLine is what `app --version-check` prints. The entrypoint parses the same line with the shell.
func WriteSafeOnce ¶
WriteSafeOnce asks for the next start to run in safe mode, once.
func WriteStuck ¶
WriteStuck records that the app is about to restart itself, and why.
Types ¶
type Failed ¶
Failed describes a program the entrypoint put aside because it kept stopping right after it started.
type Probe ¶
Probe is what a program says about itself with --version-check.
func Inspect ¶
Inspect makes sure path is a Mediarium program for this system by looking at its header and then running it once with --version-check, which only prints the version and platform and exits. It is run with an empty environment, from its own folder, and gives up after ten seconds.
func ParseVersionLine ¶
ParseVersionLine reads the output of --version-check.
type Pushed ¶
type Pushed struct {
Version string `json:"version"`
SHA256 string `json:"sha256,omitempty"`
InstalledAt time.Time `json:"installedAt"`
HasPrevious bool `json:"hasPrevious"`
PreviousVersion string `json:"previousVersion,omitempty"`
Running bool `json:"running"` // this very process is that program
}
Pushed describes the program installed in the update folder.