Documentation
¶
Overview ¶
Package dockerpkg knows which of a Debian-family box's packages are Docker's, where the installed Docker came from, and what an apt simulation says it would do to them.
ReCasaOS updates system packages with an apt install of a list, and an upgrade of docker-ce or containerd.io restarts the Docker daemon, which stops every container until it is back. The System packages update therefore leaves this family out and shows it on a line of its own; this package is what tells the two apart. It runs nothing: callers give it text apt printed, and it gives back what is in it. Nothing here is ever built into a shell command from a version string, and a package name reaches a command only if it is one of the fixed names below.
Index ¶
- Constants
- Variables
- func ContractPattern(protectDocker bool) string
- func EngineVersion(dpkgVersion string) string
- func FailedMarker(nonce string, at time.Time, reason string) string
- func IsDistributionDocker(name string) bool
- func IsEngineName(name string) bool
- func IsFamily(name string) bool
- func IsMarkerLine(line string) bool
- func Major(engineVersion string) string
- func MajorJump(from, to string) bool
- func ManualCommand(origin Origin, pending []string) string
- func ManualCommandHeld(origin Origin, pending, installed, held []string) string
- func NewNonce() (string, error)
- func PlanMarker(nonce, from, to string, majorJump bool) string
- func PolicyVersions(policy string) (installed, candidate string)
- func QueuedMarker(nonce string, at time.Time) string
- func RestartsEngine(names []string) bool
- func RollbackCommand(previous []string) string
- func RollbackCommandHeld(previous, held []string) string
- func ValidName(name string) bool
- func ValidNonce(s string) bool
- func ValidVersion(v string) bool
- type Container
- type DaemonInfo
- type NotReturned
- type Origin
- type Plan
- type PlanPackage
- type Port
- type Run
- type Touched
Constants ¶
const ( // TerminalSuccess is an engine that was installed, runs, and answers. TerminalSuccess = "success" // TerminalRestartPending is an engine installed whose daemon is still the old one. TerminalRestartPending = "restart_pending" // TerminalFailed is a run that stopped, with a reason (Run.FailReason). TerminalFailed = "failed" )
How a run ended (Run.Terminal).
const ( FailGuard = "guard" FailDownload = "download" FailInstall = "install" FailDaemon = "daemon" // FailStart is a unit that systemd could not start: the core writes it, in the last line of // the log, since the unit never wrote anything. Nothing was changed. FailStart = "start" )
Why a run failed (Run.FailReason).
const ( // PhasePreparing is the start of the unit, up to the snapshot of what is installed. PhasePreparing = "preparing" // PhaseDownloading is the check of the plan and the download of the packages: nothing is // changed, and the apps run. PhaseDownloading = "downloading" // PhaseInstalling is the install, which restarts the daemon and stops the containers. PhaseInstalling = "installing" // PhaseWaitingDocker is the wait for the daemon to answer after the install. PhaseWaitingDocker = "waiting_docker" // PhaseWaitingContainers is the wait for the containers to come back. PhaseWaitingContainers = "waiting_containers" )
Where a run is (Run.Phase). The page tells the owner what the apps are going through from it.
const ContainerInspectFormat = `` /* 213-byte string literal not displayed */
ContainerInspectFormat is the `docker inspect --format` template that prints one JSON object per container, on one line: `docker inspect --format <this> $(docker ps -q)`. Every free-text field goes through {{json}}, so that no name or image, whatever it holds, can break the line or the object. ParseContainers reads it.
const DaemonInfoFormat = `{"ServerVersion":{{json .ServerVersion}},"SwarmState":{{json .Swarm.LocalNodeState}}}`
DaemonInfoFormat is the `docker info --format` template ParseDaemonInfo reads: one JSON object on one line.
const InvalidName = "(invalid name)"
InvalidName stands in, in Offending, for a package name that is not fit to be shown: the list goes to a log and to the page, and what apt printed there is not trusted until it has passed ValidName.
const MaxNewPackages = 10
MaxNewPackages is how many packages that are not installed yet an update of the engine may bring in. Docker 29 needs nftables, and nftables its libraries: a handful. Ten is room for that, and for a repository that moves a dependency or two, and not for an apt that has understood something else.
const ReturnWaitSeconds = 90
ReturnWaitSeconds is how long the unit waits, after the daemon is back, for the containers that start again by themselves. A container with a restart policy that is still not running after it is reported all the same, with its policy.
const SwarmStateInvalid = "invalid"
SwarmStateInvalid is the SwarmState of a daemon that said something that is not a word.
const TooManyNew = "(more than 10 new packages)"
TooManyNew stands in, in Offending, for the count: a plan that installs more than MaxNewPackages packages that are not installed yet. It is not a name, and tells itself so.
Variables ¶
var EngineNames = []string{
"containerd.io",
"docker-buildx-plugin",
"docker-ce",
"docker-ce-cli",
"docker-ce-rootless-extras",
"docker-compose-plugin",
"docker-model-plugin",
}
EngineNames are the only packages the button may upgrade, sorted. An upgrade of any other package in the transaction apt would run, or a removal, makes it refuse; so does a new package that is not a dependency (see Plan.Offending). The distribution's own docker.io, containerd, docker-compose-v2 and docker-buildx are of the family (see IsFamily) and are not in this list on purpose: they are not Docker's repository, and they conflict with it, so they are refused as new packages too.
It is a variable so that it can be listed and shown; IsEngineName does not read it, so changing it at run time does not widen what the button may touch.
Functions ¶
func ContractPattern ¶
ContractPattern is an extended regular expression (for grep -E) that matches a line of an apt simulation that breaks the contract of the update: any removal, and, when the box has a Docker engine to protect, an install or upgrade of a package of the Docker family. The unit's shell line runs the simulation again just before it installs, and installs nothing if a line matches.
func EngineVersion ¶
EngineVersion is the upstream version in a dpkg version: "5:29.8.1-1~ubuntu.22.04~jammy" is "29.8.1". It is "" for text that does not start with one.
func FailedMarker ¶ added in v0.4.67
FailedMarker is the terminal line of a run that failed, as the unit writes it: the core writes it itself for a unit that could not be started, so that the log is a whole run. reason is one of the Fail constants.
func IsDistributionDocker ¶ added in v0.4.67
IsDistributionDocker reports whether name is one of the distribution's own Docker packages (docker.io, containerd, docker-compose-v2, docker-buildx): of the Docker family and not of the engine's. They conflict with Docker's own packages, and apt bringing one in beside docker-ce would be apt choosing the other Docker.
func IsEngineName ¶ added in v0.4.67
IsEngineName reports whether name is one of EngineNames, with or without the architecture apt adds to a foreign package ("containerd.io:armhf").
func IsMarkerLine ¶ added in v0.4.67
IsMarkerLine reports whether a line of the log starts like a marker. It is what a caller that reads a long log keeps of it for ParseRun, besides the first line: a line that does not start like a marker is never one.
func Major ¶ added in v0.4.67
Major is the first number of an engine version: "29.8.0" is "29", "24.0.5" is "24". It takes the version Docker reports, or a whole dpkg version ("5:29.8.0-1~debian.11~bullseye", whose epoch is not the major). It is "" for text that does not start with digits. It is the number in its shortest form, so that "029" and "29" are the same major.
func MajorJump ¶ added in v0.4.67
MajorJump reports whether an upgrade from one engine version to another changes the major version. It is false when either of them has none.
func ManualCommand ¶
ManualCommand is what an owner types to update Docker themselves, for an origin, or "" when there is no command this can stand behind. It names the packages apt says are pending when it said some, so that running it leaves nothing behind, and only names from the fixed family: nothing apt printed reaches it unless it is one of them.
func ManualCommandHeld ¶ added in v0.4.66
ManualCommandHeld is ManualCommand for a box that has Docker's packages on hold (held: the names of the ones that are, installed: the ones that are installed). apt fails when it is told only the package that is behind and a held sibling has to move with it (docker-ce 29 needs a newer containerd.io), so the command names every installed package of the engine, the same list the dashboard's plan is made of, and says apt may change what is held. dpkg drops the hold of every package it unpacks, so the command ends by putting the hold back on the ones that were held. The hold is joined with `;` and not `&&`: an install that fails may already have unpacked packages, and dpkg has dropped their hold by then, so the hold goes back whatever apt-get returned (apt-mark hold is idempotent). The `update && install` part keeps its `&&`. For a box that holds nothing, and for an origin that has no apt command, it is ManualCommand, byte for byte.
func NewNonce ¶ added in v0.4.67
NewNonce is 16 random bytes from the system's source, in lower case hex.
func PlanMarker ¶ added in v0.4.68
PlanMarker is the second line of the log, which the core writes in the same write as the first: "CASAOS_DOCKER_UPDATE_PLAN <nonce> <from> <to> <0|1>", the engine versions the plan goes from and to (digits and dots, "-" for one that is not known) and whether the plan changes the engine's major version. A version that is not digits and dots is written as unknown, so that the line holds nothing else. The unit never writes one: the core tells the page of a major jump from it.
func PolicyVersions ¶ added in v0.4.66
PolicyVersions reads the installed and the candidate version from the output of `apt-cache policy <package>`: the candidate is the newest version apt would install. Each is "" when apt says "(none)" or says nothing.
func QueuedMarker ¶ added in v0.4.67
QueuedMarker is the first line of the log, which the core writes (and so truncates the log with) before it starts the unit: the nonce is read from it, and from no other line.
func RestartsEngine ¶
RestartsEngine reports whether upgrading any of names restarts Docker.
func RollbackCommand ¶ added in v0.4.67
RollbackCommand is the command that puts the engine back as it was, from the "name=version" pins of Run.Previous: "sudo apt-get install --allow-downgrades name=version ...". It is built from the pins that pass validation, and only from those, so that no shell character is in it; "" when there is none. Nothing runs it: it is shown to the owner.
func RollbackCommandHeld ¶ added in v0.4.70
RollbackCommandHeld is RollbackCommand for a run that went ahead of a hold (held: Run.Held, the packages that were on hold when the unit started, which it put back when it ended). The owner types it as the update was run: with the flag of the terminal command (apt-get install with -y refuses to change a package that is on hold unless it is told it may; without -y it only asks, and the flag is there to say so), and since dpkg drops the hold of every package it unpacks, it ends by putting the hold back on the packages that were held. The hold is joined with `;` and not `&&`: this command is shown for a run that failed, where an install that fails again may already have unpacked packages and dpkg has dropped their hold, so it goes back whatever apt-get returned (apt-mark hold is idempotent). For a run that held nothing it is RollbackCommand, byte for byte.
func ValidName ¶
ValidName reports whether name looks like a Debian package name (with an optional architecture), the only kind of text that may be put into an apt command line.
func ValidNonce ¶ added in v0.4.67
ValidNonce reports whether s is 32 lower case hex digits, what NewNonce makes.
func ValidVersion ¶ added in v0.4.67
ValidVersion reports whether v is a dpkg version made only of the characters of one: an optional epoch, then a digit and letters, digits and . + ~ -. It is the test a version passes before it is printed in a log, sent to the page or put in an environment variable or an apt argument ("name=version"). No shell character can pass it.
Types ¶
type Container ¶ added in v0.4.67
type Container struct {
Name string `json:"name"`
Image string `json:"image"`
// RestartPolicy is Docker's: "no" or "" (it will not come back by itself), "always",
// "unless-stopped" or "on-failure".
RestartPolicy string `json:"restart_policy"`
// HostNetwork is a container that shares the box's network: its ports are the box's ports.
HostNetwork bool `json:"host_network"`
// DockerSocket is a container with Docker's socket bind-mounted into it: it talks to the
// daemon directly, with a client of its own that a new Docker may no longer understand.
// Only there when it is true.
DockerSocket bool `json:"docker_socket,omitempty"`
// Ports are the ports it publishes, never nil.
Ports []Port `json:"ports"`
}
Container is a running container, as the confirmation of an update lists it.
func ParseContainers ¶ added in v0.4.67
ParseContainers reads the output of `docker inspect --format ContainerInspectFormat`. It is tolerant and strict at once: a line that is not a JSON object of that shape is skipped (so is a container whose name does not follow Docker's rule or whose restart policy is not a policy's name), and what is kept has been through validation: the name without its leading "/", the image cleaned of control characters and cut to a bound, the published ports only. The result is sorted by name and never nil.
type DaemonInfo ¶ added in v0.4.67
type DaemonInfo struct {
// ServerVersion is the version the running daemon reports.
ServerVersion string
// SwarmActive is a node that is part of a swarm and has joined it ("active"): what the update refuses on.
SwarmActive bool
// SwarmState is the state of the node's swarm as the daemon says it: "inactive" (or nothing) for a node
// that is of none, and "active", "pending", "locked" or "error" for one that is, whatever the state. A
// state that is not a word of 16 lower-case letters or fewer is SwarmStateInvalid, which is no state of
// no swarm.
SwarmState string
}
DaemonInfo is what the update needs to know of the Docker daemon.
func ParseDaemonInfo ¶ added in v0.4.67
func ParseDaemonInfo(output string) (DaemonInfo, error)
ParseDaemonInfo reads the output of `docker info --format DaemonInfoFormat`. It is an error when no daemon answered: the CLI then prints nothing, or what it knows of itself without a ServerVersion, or an error text. The first line that is a JSON object is the answer, so a warning before it does no harm.
type NotReturned ¶ added in v0.4.67
NotReturned is a container that was running before the update and is not running after it.
type Origin ¶
type Origin string
Origin is where the installed Docker came from.
const ( // OriginDockerRepo is docker-ce from Docker's own apt repository, the one the // installer sets up: https://download.docker.com/linux/. OriginDockerRepo Origin = "docker-repository" // OriginDistribution is the docker.io package of Debian, Ubuntu or Raspberry Pi OS. OriginDistribution Origin = "distribution" // OriginSnap is the snap. OriginSnap Origin = "snap" // OriginUnknown is a Docker whose source this does not recognise: a mirror, a plain // http repository, a package installed by hand. OriginUnknown Origin = "unknown" )
func OriginFromPolicy ¶
OriginFromPolicy tells where docker-ce comes from, from the output of `apt-cache policy docker-ce`: Docker's repository when some version of the package is offered by https://download.docker.com/linux/, anything else is unknown.
func (Origin) CanSeeUpdates ¶
CanSeeUpdates reports whether apt can tell of an update for an installation from this origin: of a snap, or of a Docker whose source is not known, it cannot.
type Plan ¶ added in v0.4.67
type Plan struct {
// Packages are the Inst lines, in the order apt printed them.
Packages []PlanPackage
// Removed is every package it would remove (a Remv or Purg line), as apt printed it:
// show Offending, not this.
Removed []string
// Held are the packages of the engine that are on hold, and that the update goes ahead of
// because the owner said so (dpkg drops the hold of those it unpacks, and the unit puts it
// back): valid names of EngineNames, sorted. Apt's simulation does not tell, so the caller,
// which asked dpkg, sets it. ID covers it: a hold that is set, removed or moved after the
// owner confirmed is another plan to confirm.
Held []string
}
Plan is what an apt simulation says it would do.
func ParsePlan ¶ added in v0.4.67
ParsePlan reads the Inst, Remv and Purg lines of `apt-get -s`:
Inst docker-ce [5:28.0.4-1~debian.11~bullseye] (5:29.8.0-1~debian.11~bullseye Docker CE:bullseye [amd64]) Inst docker-model-plugin (1.0.0-1~debian.11~bullseye Docker CE:bullseye [amd64]) Remv docker-compose-v2 [2.1.0]
It fails closed: a line that begins like one of those and is not shaped like it still counts, as a package with what could be read of it, so that Offending names it.
func (Plan) Dependencies ¶ added in v0.4.67
Dependencies are the names of the packages that are new and are not the engine's own, sorted: what the new Docker needs and the box does not have (nftables, say). Under the same rule as Pins. Never nil.
func (Plan) Engine ¶ added in v0.4.67
Engine is the engine version (digits and dots, see EngineVersion) the plan upgrades docker-ce from and to. When docker-ce is not in the plan, because only a plugin or containerd.io moves, both are "" and the caller says what is installed.
func (Plan) ID ¶ added in v0.4.67
ID identifies the plan: the lower case hex SHA-256 of the lines "name current>candidate" of its packages, sorted and joined with "\n", and of one line more, "held name name ...", when packages of the engine are on hold. It does not depend on the order apt printed them in and changes with any name or version, and with a hold, so that "I confirmed this" can be told from "apt now says something else". A plan with no hold has the id it has always had.
func (Plan) Names ¶ added in v0.4.67
Names are the names of every package the plan allows apt to install, sorted: the upgrades of Pins and the packages that are new. It is the list the unit's guard holds each line of apt's simulation against, under the same rule as Pins. Never nil.
func (Plan) Offending ¶ added in v0.4.67
Offending lists, sorted and once each, what makes the plan unacceptable to the button. A plan is acceptable when nothing is removed, ever; every upgrade is of one of EngineNames; every package that is new (not installed yet: a dependency of the new Docker, say) is not a distribution's Docker package; there are at most MaxNewPackages of those; and every name and version passes ValidName and ValidVersion. What is not acceptable is named: a package that would be removed, an upgrade of a package that is not the engine's, a new package that is a distribution's Docker, anything with a name or a version that fails validation.
Every item is a valid name, or a fixed text that cannot be one: InvalidName, and TooManyNew for a plan with too many new packages (their names are not listed). It is empty (not nil) when the plan is within bounds; a plan with no package at all is within bounds, and it is for the caller to say that there is nothing to update.
func (Plan) Pins ¶ added in v0.4.67
Pins are the "name=candidate" arguments that make apt upgrade the packages of this plan to the versions that were confirmed, sorted. They are the upgrades only: with --only-upgrade apt ignores a named package that is not on the box, so a pin of a new package would pin nothing, and the new packages are the ones apt itself brings in as dependencies (Names is what bounds them). Only a package that Offending does not name is in them, so nothing that failed validation can get into an argument by a caller that forgot to ask. Never nil.
type PlanPackage ¶ added in v0.4.67
type PlanPackage struct {
Name string `json:"name"`
Current string `json:"current_version"`
Candidate string `json:"candidate_version"`
// New is a package that is not installed yet: apt printed no [current version] at all (a
// pair of brackets with nothing in them is not that, and is refused).
New bool `json:"new,omitempty"`
}
PlanPackage is one `Inst` line of an apt simulation: a package it would install or upgrade. The versions are the whole dpkg strings ("5:29.8.0-1~debian.11~bullseye"), epoch and all.
type Port ¶ added in v0.4.67
type Port struct {
Port int `json:"port"`
Protocol string `json:"protocol"`
HostPort int `json:"host_port"`
}
Port is a port a container publishes on the box: container port 80 reached on host port 8080.
type Run ¶ added in v0.4.67
type Run struct {
Nonce string
// QueuedAt is the time on the first line; StartedAt the unit's first word; DownloadedAt the
// end of the download and InstalledAt the end of the install, zero if it never got there;
// CompletedAt the terminal marker's.
QueuedAt, StartedAt, DownloadedAt, InstalledAt, CompletedAt time.Time
// PreviousSeen is whether the unit wrote its snapshot of the installed packages, which
// Previous may be empty of.
PreviousSeen bool
// Terminal is TerminalSuccess, TerminalRestartPending, TerminalFailed, or "" while the
// log has no valid terminal marker. The last valid one counts.
Terminal string
// FailReason is FailGuard, FailDownload, FailInstall, FailDaemon or FailStart, when Terminal
// is TerminalFailed.
FailReason string
// Previous are the engine's packages as they were installed before the update, as
// "name=version", each validated: what a rollback would install.
Previous []string
// DaemonVersion is the version the daemon reported once it was back, "" if it never did.
DaemonVersion string
// NotReturned are the containers that were running before and were not after.
NotReturned []NotReturned
// From and To are the engine versions the plan goes from and to (digits and dots), "" when the
// plan did not know one or the log has no valid PLAN line; MajorJump is the plan's own word on
// whether it changes the major version, and is false with no PLAN line.
From, To string
MajorJump bool
// Held is what the unit saw on hold when it started, among the engine's packages the core named to
// it (CASAOS_DU_HELD, which are the ones the check reports): names of the engine's, sorted, empty
// (not nil) for a "-", nil when the unit did not say (the first valid HELD line counts). Holds is
// what it saw on hold of those when it ended, after it had put back the hold that dpkg drops from
// every package it unpacks, in the same way (the last valid HOLDS line counts).
Held []string
Holds []string
}
Run is what a log says of a Docker update. Its zero value is "no run": the log has no valid first line. Previous and NotReturned are nil when the log has none.
func ParseRun ¶ added in v0.4.67
ParseRun reads the log of a Docker update. The nonce is the one on line 1, which must be exactly the core's QUEUED marker; with no such line it is the zero Run. After that a line is read only if it is "CASAOS_DOCKER_UPDATE_<KIND> <that nonce>" with the fields its KIND takes; every field is validated again; and what is not a marker is ignored, whatever it looks like: apt's output, a container named like a marker, a forged nonce. The last terminal marker wins, the first STARTED, INSTALLED, PREVIOUS and PLAN, the last DAEMON. A PLAN is the core's, written on line 2, so the first valid one is the plan and no later one changes it.
A caller that reads only the end of a long log must put line 1 back in front of it.
func (Run) HoldsKept ¶ added in v0.4.70
HoldsKept says that the holds are in place again at the end of the update: the unit saw packages on hold when it started, and every one of them is on hold in what it said when it ended (a name more is no hold lost). A run that held nothing has no hold to say is back, and a run whose unit did not say the end of it (it was lost) has none that it can say is.
func (Run) Phase ¶ added in v0.4.67
Phase is the step a run is at, from the markers it has written so far: PhasePreparing until the snapshot of what is installed, then PhaseDownloading until DOWNLOADED, PhaseInstalling until INSTALLED, PhaseWaitingDocker until DAEMON, and PhaseWaitingContainers until the terminal marker. It is "" for no run and for a run that has ended. The guard writes nothing when it passes, so it is part of the downloading: it is a few seconds, and the apps run through it.