Documentation
¶
Overview ¶
Package safety gates the commands that change or destroy something.
The shell toolkit had no preview, no confirmation and no dry run, and one tool scheduled a reboot when it was asked for its help text. Every command that powers off, reinstalls, drains or overwrites goes through here first.
Index ¶
Constants ¶
const EffectAnnotation = "clusterctl.effect"
EffectAnnotation is the key under which a command records its effect.
Variables ¶
var ErrDryRun = errDryRun{}
ErrDryRun is returned by Confirm when the gate is in dry run mode. A command treats it as "stop here, successfully".
Functions ¶
Types ¶
type Action ¶
type Action struct {
// Verb is what is being done, "power off" or "reinstall".
Verb string
// Targets are the hosts it happens to.
Targets *nodeset.NodeSet
// Detail is an extra line shown before the prompt, such as the command
// that will run.
Detail string
// NotNodes says the targets are not nodes, such as the accounting
// database, so the inventory has nothing to say about them.
NotNodes bool
}
Action describes what is about to happen, for the prompt and the preview.
type Effect ¶
type Effect string
Effect says what running a command does to the site. The command tree records it on every command, so that anything driving the tree on behalf of someone else, such as the MCP server, can tell a question from a change without keeping its own list.
const ( // EffectRead only looks. It may contact hosts, but changes nothing on // them. EffectRead Effect = "read" // EffectChange changes or destroys something, or may do so depending on // its arguments. EffectChange Effect = "change" // EffectInteractive needs a terminal, opens a program or keeps running // until it is stopped. EffectInteractive Effect = "interactive" )
type Gate ¶
type Gate struct {
// ProtectedHosts are the node set expressions of safety.protectedHosts.
// The hosts they name are touched by no destructive command unless
// Force is set.
ProtectedHosts []string
// Resolve turns one protected host entry into the machines it names,
// under the names the targets of an action are given. It is called the
// first time the protected hosts are needed, not when the gate is built,
// so that an entry naming a group asks its source only when a command
// is about to change something. NewGate sets nodeset.Parse.
Resolve func(expr string) (*nodeset.NodeSet, error)
// Known are the nodes the site knows. When it is set, an action on a
// name outside it is refused unless Force is set: a name the site does
// not know may be another spelling of a protected machine.
Known *nodeset.NodeSet
// ConfirmAbove asks for the host count to be typed back when an action
// targets more than this many hosts. At zero it is always typed.
ConfirmAbove int
// AssumeYes answers every prompt with yes, for -y and for scripts.
AssumeYes bool
// Force allows a protected host, or one the site does not know, to be
// touched.
Force bool
// DryRun reports what would happen and does nothing.
DryRun bool
// Interactive says whether a prompt can be answered at all. When it is
// false and AssumeYes is not set, an action is refused rather than
// silently carried out.
Interactive bool
// In and Out are the terminal the prompt uses.
In io.Reader
Out io.Writer
// Context is the one an interrupt cancels. Once it has ended nothing is
// confirmed, and a prompt waiting for an answer gives up.
Context context.Context
// contains filtered or unexported fields
}
Gate decides whether a destructive action may go ahead.
func NewGate ¶
func NewGate(spec v1alpha1.SafetySpec, resolve func(expr string) (*nodeset.NodeSet, error)) (*Gate, error)
NewGate builds a gate from the safety configuration of a site. resolve turns a protected host entry into the machines it names; nil parses it as a plain node set.
A limit that would switch a safeguard off is refused here, because it is the one place every setting of the site has passed through, --set included.
func (*Gate) Announce ¶ added in v0.3.0
Announce runs the checks of the gate and, when --force lets the action through, says so on the gate's output. It is for a command that goes ahead without asking; Confirm does the same for one that asks.
func (*Gate) Check ¶
Check refuses an action that touches a protected host, or a host the site does not know, unless the gate was forced. When the protected hosts cannot be worked out, every action is refused: an entry that cannot be resolved must not protect nothing. --force gets past that too, since it would get past the protected hosts anyway.
Check prints nothing. What --force lets through is named in the preview, which Confirm prints, and by Announce, so that a forced run never touches a protected or unknown host without saying which.
func (*Gate) Confirm ¶
Confirm runs the full gate: the protected host check, then the preview and the prompt. It returns nil when the action may proceed.
func (*Gate) Preview ¶
Preview runs the checks of the gate and describes the question it would ask, without asking it.
func (*Gate) Protected ¶
Protected returns every protected host. It resolves the entries once, and an entry that cannot be resolved is an error rather than an entry that protects nothing.
func (*Gate) ProtectedIn ¶ added in v0.3.0
ProtectedIn returns the targets that are protected hosts.
A target is protected when it is a protected host, and also when its short name is one: the name may have been written with a domain the site does not use, and the gate cannot tell whether that reaches the same machine, so it assumes it does.
type Preview ¶
type Preview struct {
// Verb is what is being done.
Verb string `json:"verb"`
// Targets is the folded node set the action touches.
Targets string `json:"targets"`
// Count is how many hosts that is.
Count int `json:"count"`
// Detail is the extra line shown before the question.
Detail string `json:"detail,omitempty"`
// Forced names what --force lets through, one sentence each: protected
// hosts, hosts the inventory does not know, and protected hosts that
// could not be worked out. It is shown with the detail.
Forced []string `json:"forced,omitempty"`
// CountRequired says that a yes is not enough: the host count has to be
// read off the preview and given back.
CountRequired bool `json:"countRequired"`
// ConfirmAbove is the host count above which the count is required.
ConfirmAbove int `json:"confirmAbove,omitempty"`
}
Preview is an action that passed the checks and waits for an answer. It carries everything the question is asked from, so that a caller without a terminal can put the same question some other way and have the answer judged by the same rule.
func (Preview) Accept ¶
Accept judges an answer to the question: the host count when it is required, a yes otherwise. It returns nil when the action may proceed.