updates

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: GPL-3.0 Imports: 5 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 updates reports and applies operating system package upgrades and container image refreshes across a fleet.

Everything here is split into a read that changes nothing and an apply that is explicit about what it will do, because an upgrade is the one routine operation that can take a working machine off the network.

Index

Constants

View Source
const (
	MarkerRebooting = "hlab:rebooting"
	MarkerNoReboot  = "hlab:no-reboot-needed"
)

Markers the reboot script prints.

View Source
const (
	MarkerUpgraded = "hlab:upgraded"
	MarkerNoop     = "hlab:nothing-to-do"
)

Markers the apply script prints so the outcome can be reported precisely.

View Source
const BootIDScript = `cat /proc/sys/kernel/random/boot_id 2>/dev/null || true`

BootIDScript reads the identifier the kernel generates once per boot.

This is how a reboot is proved rather than assumed. A machine that answers SSH again may simply never have gone down — if the reboot silently failed, waiting for it to "come back" succeeds immediately and reports a reboot that did not happen. The boot id changes only when the kernel starts.

View Source
const CheckScript = `
LC_ALL=C
export LC_ALL

s() { printf '` + SectionPrefix + `%s#\n' "$1"; }

# The package index and the container list are both root-only reads on most
# machines. Sudo is used where it changes the answer and only when it can be
# had without a prompt.
SUDO=
if [ "$(id -u 2>/dev/null)" != 0 ] && command -v sudo >/dev/null 2>&1; then
  if sudo -n true >/dev/null 2>&1; then
    SUDO='sudo -n'
  fi
fi

s manager
if command -v apt-get >/dev/null 2>&1; then
  echo apt
elif command -v apk >/dev/null 2>&1; then
  echo apk
elif command -v dnf >/dev/null 2>&1; then
  echo dnf
elif command -v pacman >/dev/null 2>&1; then
  echo pacman
else
  echo unknown
fi

s refresh
if command -v apt-get >/dev/null 2>&1; then
  if $SUDO apt-get -qq update >/dev/null 2>&1; then echo ok; else echo failed; fi
fi

s packages
# What an upgrade would actually install. This is the authoritative list: it
# respects held packages and anything apt would decline to touch.
if command -v apt-get >/dev/null 2>&1; then
  $SUDO apt-get -s upgrade 2>/dev/null | sed -n 's/^Inst \([^ ]*\) \[[^]]*\] (\([^ ]*\) .*/\1 \2/p'
fi

s upgradable
# Everything apt considers newer, including what "upgrade" would hold back
# because it would need to remove something. The difference between this and
# the section above is worth reporting rather than hiding.
if command -v apt-get >/dev/null 2>&1; then
  $SUDO apt list --upgradable 2>/dev/null | sed -n 's#^\([^/]*\)/\([^ ]*\) .*#\1 \2#p'
fi

s reboot
if [ -f /var/run/reboot-required ]; then
  echo required
  cat /var/run/reboot-required.pkgs 2>/dev/null
else
  echo no
fi

s kernel
uname -r 2>/dev/null

s containers
# A container still running an image that has since been replaced locally is
# behind its own stack. This is a purely local comparison — no registry is
# contacted, so the check stays a read.
if command -v docker >/dev/null 2>&1; then
  for c in $($SUDO docker ps -q 2>/dev/null); do
    name=$($SUDO docker inspect -f '{{.Name}}' "$c" 2>/dev/null | sed 's#^/##')
    tag=$($SUDO docker inspect -f '{{.Config.Image}}' "$c" 2>/dev/null)
    running=$($SUDO docker inspect -f '{{.Image}}' "$c" 2>/dev/null)
    current=$($SUDO docker image inspect -f '{{.Id}}' "$tag" 2>/dev/null)
    [ -n "$name" ] || continue
    if [ -n "$current" ] && [ "$running" != "$current" ]; then
      printf '%s %s stale\n' "$name" "$tag"
    else
      printf '%s %s current\n' "$name" "$tag"
    fi
  done
fi

s stacks
if command -v docker >/dev/null 2>&1; then
  for d in /opt/hlab/stacks/*/; do
    [ -f "$d/compose.yaml" ] || continue
    basename "$d"
  done
fi

s end
`

CheckScript reports what is out of date without changing anything that runs.

It refreshes the package index, which writes to the package cache and nothing else — asking what is upgradable is meaningless against a stale index. No package is installed, no container is touched.

POSIX sh only, and every tool is probed before use, so a machine without apt or without docker reports fewer sections rather than failing.

View Source
const RebootTimeout = 6 * time.Minute

RebootTimeout bounds how long a machine is given to come back.

Generous on purpose: a Raspberry Pi on a slow card with a new kernel and a filesystem check can take minutes, and concluding it is gone when it is merely slow would send an operator to the cupboard for nothing.

View Source
const SectionPrefix = "##HLAB#"

SectionPrefix delimits the sections of the check script's output.

Variables

This section is empty.

Functions

func BuildImageScript

func BuildImageScript() string

BuildImageScript renders the container image refresh script.

func BuildRebootScript

func BuildRebootScript(force bool) string

BuildRebootScript renders the reboot script.

force reboots whether or not the machine says one is pending; without it a machine with nothing to apply is left alone, so the flag is safe to pass to a whole selector.

func BuildUpgradeScript

func BuildUpgradeScript(allowNew bool) string

BuildUpgradeScript renders the package upgrade script.

allowNew adds --with-new-pkgs, which lets apt install packages that did not previously exist while still never removing one. It is what kernel updates need: bumping linux-image-generic means installing a new linux-image-<ver> package, so a plain `upgrade` holds the kernel back indefinitely and the machine never receives a kernel security fix. It is not the default because installing new packages is a bigger step than updating existing ones, and the operator should say so.

Types

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker asks many hosts at once what is out of date.

func NewChecker

func NewChecker(exec *sshx.Executor) *Checker

NewChecker wraps an SSH executor, reusing its fan-out and concurrency limiting rather than reimplementing them.

func (*Checker) Check

func (c *Checker) Check(ctx context.Context, targets []sshx.Target) []Status

Check runs the read-only script on every target and parses the output. A host that could not be reached is reported through its own Status.Err rather than failing the run — one unreachable machine should not hide what the rest of the fleet needs.

type Container

type Container struct {
	Name  string `json:"name" yaml:"name"`
	Image string `json:"image" yaml:"image"`
	Stale bool   `json:"stale,omitempty" yaml:"stale,omitempty"`
}

Container is one running container and whether it is behind its own image.

type Package

type Package struct {
	Name    string `json:"name" yaml:"name"`
	Version string `json:"version" yaml:"version"`
	// Security is set when the new version comes from a security archive.
	Security bool `json:"security,omitempty" yaml:"security,omitempty"`
}

Package is one upgradable package.

type Status

type Status struct {
	Host    string `json:"host" yaml:"host"`
	Manager string `json:"manager,omitempty" yaml:"manager,omitempty"`

	// Packages is what an upgrade would install.
	Packages []Package `json:"packages,omitempty" yaml:"packages,omitempty"`
	// HeldBack is upgradable but would not be installed by `upgrade`, because
	// doing so would mean removing something. Reported rather than hidden: it
	// is the difference between "up to date" and "as up to date as is safe".
	HeldBack []string `json:"heldBack,omitempty" yaml:"heldBack,omitempty"`

	Containers []Container `json:"containers,omitempty" yaml:"containers,omitempty"`
	Stacks     []string    `json:"stacks,omitempty" yaml:"stacks,omitempty"`

	Reboot     bool     `json:"reboot" yaml:"reboot"`
	RebootPkgs []string `json:"rebootPkgs,omitempty" yaml:"rebootPkgs,omitempty"`
	Kernel     string   `json:"kernel,omitempty" yaml:"kernel,omitempty"`

	// IndexStale is set when the package index could not be refreshed, so the
	// counts below it describe whatever the machine last knew.
	IndexStale bool `json:"indexStale,omitempty" yaml:"indexStale,omitempty"`
	// Partial means the output was cut short and the picture is incomplete.
	Partial bool `json:"partial,omitempty" yaml:"partial,omitempty"`

	Err string `json:"error,omitempty" yaml:"error,omitempty"`
}

Status is what one machine reported.

func Parse

func Parse(host, raw string) Status

Parse turns raw check output into a Status.

Lenient by design, like fact collection: an unparseable line is skipped rather than failing the host. The one hard signal is the trailing end marker, whose absence means the output was truncated.

func (Status) SecurityCount

func (s Status) SecurityCount() int

SecurityCount reports how many pending packages come from a security archive.

func (Status) StaleContainers

func (s Status) StaleContainers() []Container

StaleContainers reports containers running an image their stack has moved on from.

func (Status) UpToDate

func (s Status) UpToDate() bool

UpToDate reports whether there is nothing at all to do.

Jump to

Keyboard shortcuts

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