Documentation
¶
Overview ¶
Package swarm talks to the machines over ssh: it lists them, probes them, streams their logs and descriptions, opens shells and runs actions.
Index ¶
- Constants
- func DescribeMachine() string
- func DescribeUnit(unit string) string
- func Reboot() string
- func RunProbe(ctx context.Context, h inventory.Host, timeout time.Duration) (string, error)
- func Stream(ctx context.Context, h inventory.Host, cmd string, follow bool) <-chan string
- func UnitAction(v Verb, unit string) string
- func UnitLogs(unit string) string
- type Condition
- type Container
- type ContainerInfo
- type Disk
- type Docker
- func (d Docker) ContainerAction(v Verb, container string) string
- func (d Docker) ContainerLogs(container string) string
- func (d Docker) ContainerShell(container string) string
- func (d Docker) DescribeContainer(container string) string
- func (d Docker) DescribeStack(stack string) string
- func (d Docker) StackAction(v Verb, stack string) string
- func (d Docker) StackLogs(stack string) string
- type Machine
- type Process
- type Report
- type Stack
- type StackContainer
- type StackInfo
- type Stats
- type Unit
- type UnitInfo
- type Verb
Constants ¶
const FullDisk = 90
FullDisk is the use, in percent, from which the fullest disk is a problem.
Variables ¶
This section is empty.
Functions ¶
func DescribeMachine ¶
func DescribeMachine() string
DescribeMachine is the describe command of a machine, for ParseMachine. top's first frame averages since boot: the second covers the last second. A home that doesn't exist keeps the user's top settings (~/.toprc: a line per CPU, other columns...) out of the format parseTop reads. df leaves out pseudo filesystems, container layers and snaps. lscpu runs in the C locale, so that its field names are the ones sed looks for.
func DescribeUnit ¶
DescribeUnit is the describe command of a systemd unit, for ParseUnit.
func Reboot ¶ added in v1.2.0
func Reboot() string
Reboot is the command that reboots a machine, for remote.Run, 2 seconds later: once ssh has ended. It names systemctl by its full path, which systemd's own search path may lack (NixOS).
func RunProbe ¶
RunProbe returns the probe's raw output, without control characters. It gives up after timeout.
func Stream ¶
Stream runs cmd on h and sends its output lines: the last ones, or with follow, those that come next too, cmd being a logs command, which -f follows. A followed command gets a terminal, so that ending ssh hangs it up; another stops on the host after oneShotTimeout. The channel closes when the command ends or ctx is cancelled.
func UnitAction ¶ added in v1.2.0
UnitAction is the command that starts, stops or restarts a systemd unit, for remote.Run.
Types ¶
type Condition ¶ added in v1.3.0
type Condition int
Condition is how something on a machine is doing, from idle to broken.
const ( Idle Condition = iota // there, but doing nothing: an inactive unit Fine // running, or done well Warning // on its way, or held: starting, paused, just restarted; a machine with problems Broken // failed, crashed, unhealthy; a machine that's down )
The conditions, from the best to the worst but for Idle, which is neither.
type Container ¶
type Container struct {
Name, State string // State: running, exited, created...
Status string // as docker words it, e.g. "Up 2 hours (healthy)"; "" if unknown
Health string // its healthcheck's: healthy, unhealthy or starting; "" without one
ExitCode int // its last exit's
Since time.Time // when it last started or stopped; zero if never, or unknown
Restarts int // by docker (its restart policy), since it was last started by hand
RecentRestart bool // the last of them was less than restartWindow ago
}
Container is one container of a compose stack.
func (Container) Condition ¶ added in v1.3.0
Condition rates c: fine running and not unhealthy, or done with exit code 0, as init containers are; a warning on its way or held: health starting, just restarted on its own, created, paused, being removed; else broken: crashed, restarting (a crash loop), dead.
func (Container) JustRestarted ¶ added in v1.4.0
JustRestarted says whether c is a warning for having just restarted on its own, which its status, "Up 40 seconds", wouldn't tell.
type ContainerInfo ¶
type ContainerInfo struct {
Container // but its Status, which docker inspect doesn't word
Image, Command string
Project, Service string // its compose stack and service, if any
Running bool
StartedAt time.Time
RestartPolicy string
HealthLast string // its healthcheck's last output
HealthStreak int // failed checks in a row
Ports, Networks, Mounts []string
EnvNames []string
CPUs int
Stats *Stats // nil unless docker stats answered
Now time.Time // the host's clock, when described: ages are computed against it
}
ContainerInfo is what DescribeContainer found about a container. The values of its environment are left out on purpose: they're often secrets.
func ParseContainer ¶
func ParseContainer(out string) (ContainerInfo, error)
ParseContainer reads DescribeContainer's output.
type Docker ¶ added in v1.3.0
type Docker string
Docker is the command that reaches docker on a host, as the probe found it, e.g. "sudo -n docker".
func (Docker) ContainerAction ¶ added in v1.3.0
ContainerAction is the command that starts, stops or restarts a container, for remote.Run.
func (Docker) ContainerLogs ¶ added in v1.3.0
ContainerLogs is the log command of one container, for Stream.
func (Docker) ContainerShell ¶ added in v1.3.0
ContainerShell is the command that opens a shell in a container, for remote.Interactive: bash if the image has it, else sh.
func (Docker) DescribeContainer ¶ added in v1.3.0
DescribeContainer is the describe command of a container, for ParseContainer: what it uses, then everything docker knows about it.
func (Docker) DescribeStack ¶ added in v1.3.0
DescribeStack is the describe command of a compose stack, for ParseStack: its containers, what docker inspect says of them, then what the running ones use. Without any container to name, docker inspect would fail, and docker stats list every container: they're named.
func (Docker) StackAction ¶ added in v1.3.0
StackAction is the command that starts, stops or restarts a compose stack's containers, for remote.Run.
type Machine ¶
type Machine struct {
OS, Kernel, Arch, Virt, Model string // Virt: e.g. kvm, "none" on bare metal
CPUs int
Uptime time.Duration
Load [3]float64 // over 1, 5 and 15 minutes
CPUBusy *float64 // percent of all CPUs busy over a second; nil without top
MemTotal, MemAvailable, SwapTotal, SwapFree uint64 // bytes
Disks []Disk
Processes []Process // the busiest, from top
}
Machine is what DescribeMachine found on a machine.
func ParseMachine ¶
ParseMachine reads DescribeMachine's output.
type Report ¶
type Report struct {
Host inventory.Host
Err error
Errors []string
OS string // e.g. Debian GNU/Linux 12 (bookworm)
Now time.Time // the host's clock: ages are computed against it
Uptime time.Duration
Deployed time.Time // NixOS: the last deploy; zero elsewhere
RebootPending bool
Load *float64 // percent, nil if unknown: the 1-minute load per CPU, past 100 when tasks wait
Memory *float64 // percent in use (not available to programs), nil if unknown
Disk *float64 // percent used of the fullest local disk, nil if unknown
Revision string // NixOS: the git commit the system was built from, if set
Units []Unit
Docker Docker // "" without docker
Stacks []Stack
}
Report is what one probe found on one host. Err is set when the probe failed; Errors lists the probe's commands that failed while the rest worked.
func Parse ¶
Parse reads a probe's output, as RunProbe returns it. Only a missing meta section is an error: a command that failed, or output that doesn't parse, becomes one of Report.Errors.
func ProbeAll ¶
ProbeAll probes the hosts in parallel, each within timeout. The reports keep the hosts' order.
func (Report) Condition ¶ added in v1.3.0
Condition rates r's machine: broken when it's down, a warning when it has problems, else fine.
func (Report) Problems ¶
Problems lists what's wrong: the host is down, or failed commands, a disk nearly full, failed units, broken containers, and what just restarted on its own.
type Stack ¶
Stack is a compose project and its containers.
type StackContainer ¶
StackContainer is one of a stack's containers; Stats is nil unless it runs.
type StackInfo ¶
type StackInfo struct {
CPUs int
Containers []StackContainer
}
StackInfo is what DescribeStack found about a compose stack.
func ParseStack ¶
ParseStack reads DescribeStack's output.
type Stats ¶
type Stats struct {
CPU, Mem float64 // percent: CPU of one CPU, as docker counts; memory of its limit
MemUsage, NetIO, BlockIO, PIDs string // as docker prints them: "620MiB / 15.2GiB"
}
Stats is what docker stats says of a running container.
type Unit ¶
type Unit struct {
Name, Active, Sub, Description string
Result, UnitFileState string
Since time.Time // last state change; zero if never
Restarts int // by systemd (Restart=), since it was last started by hand
RecentRestart bool // the last of them was less than restartWindow ago
}
Unit is a systemd service on a host.
func (Unit) Condition ¶ added in v1.3.0
Condition rates u by its active state: failed is broken, changing a warning, active fine unless it just restarted on its own, and anything else idle.
func (Unit) Interesting ¶
Interesting says whether u is worth watching: broken, just restarted on its own, or enabled to run. The rest are static or inactive units: hundreds of them, rarely of interest.
func (Unit) JustRestarted ¶ added in v1.4.0
JustRestarted says whether u is a warning for having just restarted on its own.
type UnitInfo ¶
type UnitInfo struct {
Unit
File, Restart, TriggeredBy string
MainPID, ExitStatus int // the main process, 0 if none; its last exit status
Memory, MemoryMax uint64 // bytes, as docker counts a container's; 0: unknown, or no limit
CPUTime time.Duration
Tasks uint64
Journal []string // its last lines
Now time.Time // the host's clock, when described: ages are computed against it
}
UnitInfo is what DescribeUnit found about a systemd unit.