selfupdate

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 15 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 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

View Source
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.

View Source
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.

View Source
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

View Source
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

func BootCount(dir string) int

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

func Decide(running, image, candidate string, force bool) error

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 Dir

func Dir(configDir string) string

Dir is the update folder inside the config folder.

func Install

func Install(dir, tmp, version, sum string) error

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

func ParseChecksum(s string) (string, error)

ParseChecksum reads a SHA-256 given as 64 hex digits (any case).

func Receive

func Receive(dir string, body io.Reader, max int64) (path, sum string, n int64, err error)

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

func Remove(dir string) error

Remove deletes everything installed, so the next start uses the image's own program. It does not touch the running process.

func RunningPushed

func RunningPushed(dir string) bool

RunningPushed reports whether this process is the installed program.

func State

func State(dir string) (pushed *Pushed, failed *Failed)

State reads what is installed. Problems reading a file just mean "nothing there".

func TakeSafeOnce

func TakeSafeOnce(dir string) bool

TakeSafeOnce reports whether a safe-mode start was asked for, and removes the request so it applies to this start only.

func TakeStuck

func TakeStuck(dir string) (reason string, at time.Time, ok bool)

TakeStuck returns the reason and time of a restart the app made because it stopped answering, and removes the mark.

func VersionLine

func VersionLine(version string) string

VersionLine is what `app --version-check` prints. The entrypoint parses the same line with the shell.

func WriteSafeOnce

func WriteSafeOnce(dir string) error

WriteSafeOnce asks for the next start to run in safe mode, once.

func WriteStuck

func WriteStuck(dir, reason string) error

WriteStuck records that the app is about to restart itself, and why.

Types

type Failed

type Failed struct {
	Version string    `json:"version"`
	At      time.Time `json:"at"`
}

Failed describes a program the entrypoint put aside because it kept stopping right after it started.

type Probe

type Probe struct {
	Version string
	OS      string
	Arch    string
}

Probe is what a program says about itself with --version-check.

func Inspect

func Inspect(ctx context.Context, path string) (Probe, error)

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

func ParseVersionLine(out string) (Probe, error)

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.

Jump to

Keyboard shortcuts

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