actionrpc

package
v1.39.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 30 Imported by: 1 module, 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 actionrpc is the protocol between the workflow runner and its actions. Each action runs in a process of its own, started from the probe binary, and is called over go-plugin's gRPC transport.

Index

Constants

View Source
const (
	// KindReadOnly is ReadOnly: the action refuses what writes.
	KindReadOnly = "read-only"
	// KindAllowHost is AllowHosts: the action refuses a host the guard does
	// not allow.
	KindAllowHost = "allow-host"
)

The kinds of guard, as an action declares the ones it keeps to.

View Source
const (
	NoteInfo     = "info"
	NoteWarning  = "warning"
	NoteCritical = "critical"
)

Levels of a note, how much attention it asks for, as Note.Level has them.

View Source
const ExitWait = TermGrace + time.Second

ExitWait is how long, at most, a step call that is given up on takes to return once its context is done: TermGrace for the action to exit on SIGTERM or be killed, and a second more for awaitExit to see it gone. That relies on command setting WaitDelay to TermGrace, so the process is already dead when Kill runs and its own graceful wait does not apply.

View Source
const TermGrace = 2 * time.Second

TermGrace is how long an action whose call is given up on is given to exit on SIGTERM before it is killed.

Variables

View Source
var (
	// BuiltinCmd is the subcommand of the probe binary that serves an action.
	BuiltinCmd = "builtin-actions"
	// Handshake is the go-plugin handshake both sides of an action agree on.
	Handshake = plugin.HandshakeConfig{ProtocolVersion: 1, MagicCookieKey: "probe", MagicCookieValue: "actions"}
	// PluginMap is the set of plugins the runner can dispense.
	PluginMap = map[string]plugin.Plugin{"actions": &Plugin{}}
)

Functions

func CleanupExecutable added in v1.27.0

func CleanupExecutable(ctx context.Context, path string, sum []byte, call CleanupCall, log hclog.Logger) error

CleanupExecutable starts the action served by the executable at path, as RunExecutable does, has it clean up after the run that call names, and stops it. Once ctx is done, the action is killed where it is, without the TermGrace the action of a step is given, and ctx's error is returned.

func DecodeParams added in v1.30.0

func DecodeParams(with map[string]any, dest any) error

DecodeParams fills dest, a pointer to a struct, from with, the parameters a step gives the action, as the built-in actions read theirs. A field takes the parameter its map tag names, such as `map:"url"`, and one tagged `validate:"required"` must be given.

A parameter is read strictly by the field's type: a number field takes a number or a decimal string that fits it, and refuses a fraction or a value out of its range rather than cutting it down; a bool field takes a bool, 0 or 1, or a string such as "true"; a map[string]string field takes a string, a number or a bool as its text and nil as an empty string, and refuses a list or a map. A nested struct, or a list of them, is filled from a map, or a list of maps, the same way. The error names the field.

Example:

type Req struct {
    URL     string            `map:"url" validate:"required"`
    Retries int               `map:"retries"`
    Headers map[string]string `map:"headers"`
}

req := Req{Headers: map[string]string{}}
if err := actionrpc.DecodeParams(with, &req); err != nil {
    return nil, err
}

func EncodeResult added in v1.30.0

func EncodeResult(v any) (map[string]any, error)

EncodeResult turns v, a struct or a pointer to one, into the map an action returns, keyed by the map tags of its fields, the reverse of DecodeParams. A nested struct becomes a map, a list of them a list of maps, and a []byte a string; any other field is kept as it is.

Example:

type Res struct {
    Code int    `map:"code"`
    Body string `map:"body"`
}

res, err := actionrpc.EncodeResult(Res{Code: 200, Body: "ok"})
// res: map[string]any{"code": 200, "body": "ok"}

func IsRefused

func IsRefused(err error) bool

IsRefused reports whether err is, or wraps, a Refused error.

func LogOutcome

func LogOutcome(log hclog.Logger, subject string, ret map[string]any, err error)

LogOutcome records how an action finished. subject names what was attempted, for example "http request", and is used to build the message. It is recorded at trace level, as LogParams records the parameters: a verbose run shows the step's response, and the error the action returns, which the runner prints itself.

func LogParams

func LogParams(log hclog.Logger, msg string, with map[string]any)

LogParams records the parameters an action received, truncating long values so that a large request body does not flood the log. Actions are separate processes, so their log records reach the workflow runner as JSON on stderr and are re-filtered there by the runner's own level. The record is at trace level, shown only with -vv: a verbose run shows the step's request and response, which the parameters would only say again.

func ParamNames added in v1.30.0

func ParamNames(v any) []string

ParamNames returns the names the map tags of v's fields give, v being a struct or a pointer to one: the parameters DecodeParams fills it from, in the order of the fields, such as for the params of an action.yml. A struct with no tagged field gives an empty list, and anything else nil.

func ParseTimeout added in v1.30.0

func ParseTimeout(v any) (time.Duration, error)

ParseTimeout reads a timeout parameter as the built-in actions read theirs: a duration string such as "30s", a number of seconds, or a numeric string. It refuses anything else, and zero too, rather than falling back to a default, and its error says why.

func Refuse

func Refuse(format string, args ...any) error

Refuse returns a Refused error with the reason formatted.

func RunBuiltin added in v1.30.0

func RunBuiltin(ctx context.Context, name string, call Call, log hclog.Logger) (map[string]any, map[string]any, error)

RunBuiltin starts the built-in action named name from the running probe binary, calls it once for a step, and stops it. The action is told about the step and given the state it left in the job, and the state it leaves is returned, which is nil when it keeps none. The action's own log records reach log at the levels the action gave them, and go-plugin's records of starting, calling and stopping the process at trace level; log decides what of them is shown.

Once ctx is done, the action is sent SIGTERM, and killed if it has not exited TermGrace later, and ctx's error is returned.

func RunBuiltinFrom added in v1.37.0

func RunBuiltinFrom(ctx context.Context, probe, name string, call Call, log hclog.Logger) (map[string]any, map[string]any, error)

RunBuiltinFrom starts the built-in action named name from the probe executable at probe, as RunBuiltin does from the running one. An action that calls a built-in action runs it from the probe that runs the action, which the CLI names in PROBE_EXECUTABLE, as the action is not probe.

func RunExecutable

func RunExecutable(ctx context.Context, path string, sum []byte, call Call, log hclog.Logger) (map[string]any, map[string]any, error)

RunExecutable starts the action served by the executable at path, calls it once for a step, and stops it, as RunBuiltin does for a built-in one. When sum is not empty, the executable is started only if its SHA-256 digest is sum.

func Sendable

func Sendable(v map[string]any) (map[string]any, error)

Sendable returns a copy of v as an action in a process of its own sends it and the runner receives it: maps keyed by strings, lists, and plain values, with whole numbers as int64. The maps and lists are new, so the copy shares none of them with v. It fails for a value that cannot be sent, as sending it would.

func Serve

func Serve(newAction func(log hclog.Logger) Action)

Serve serves the action that newAction builds to the workflow runner that started this process, and returns once the runner is done with it. The action logs through the logger it is given, which writes JSON to stderr for the runner to filter.

The runner shows the action's records by their levels:

  • Warn is shown in every run. It is the level of what a person must see whatever the step's result says.
  • Error is shown only in a verbose run (-v): the error an action returns is what the step's result shows, as it is, so it is to say why the action failed in words a person reads, and a record at error level would only say it again.
  • Debug and Info are shown in a verbose run, alongside the step's request and response, which the runner shows itself. They are for what the action did that its result does not tell.
  • Trace is shown only with -vv, along with how the runner started, called and stopped the action's process. It is for what helps to debug the action, such as the parameters it received and the result it returned, or the error, which LogParams and LogOutcome log.

The runner marks the errors it sends as the action's own, apart from a lost connection. Of an action built against a version of this package from before that, an error with the code codes.Unavailable is taken for a lost connection until the runner has seen the process go on for a moment, which delays the step; building the action against this version spares it that.

Types

type Action

type Action interface {
	Run(with map[string]any) (map[string]any, error)
}

Action is what an action implements: it runs once with the step's `with` parameters and returns its result.

type Call

type Call struct {
	// With holds the step's with parameters, as Action.Run is given them.
	With map[string]any
	// State is the state the action left in the job, or nil when it left
	// none. The runner keeps the state the action returns without reading
	// it, and gives it to the action again in the next step of the job that
	// uses it. The state starts empty in each run of a job, and a nil state
	// returned keeps it as it was.
	State map[string]any
	// Step is the step the action runs for.
	Step Step
	// Guard is what the run allows the action to do. An action that keeps
	// to it returns a Refused error for what it does not allow.
	Guard Guard
	// Actions are the names the workflow gives its external actions, each
	// with the action it stands for in full, for an action that runs steps
	// of its own, as embedded does, to read a uses by. A local action is an
	// absolute path here.
	Actions map[string]string
	// Cleanups is not nil when the runner cleans up once the run is over:
	// it calls Cleanup of the action, when its action.yml sets cleanup, and
	// of those named here. It is nil for a runner that does not, such as a
	// Probe from before there was a cleanup, under which what a step leaves
	// behind stays; an action can refuse to leave anything then.
	Cleanups *Cleanups
	// Processes records the processes the action leaves running in the
	// background, for the runner to stop once the run is over. A runner
	// from before there were processes does not hear of them, and leaves
	// them running.
	Processes *Processes
	// Contract records the check the action made of the step against a
	// contract, such as an OpenAPI document, which the runner takes as the
	// step's test. A runner from before there were contracts to report
	// does not hear of it, and takes the step as unchecked.
	Contract *Contract
}

Call is what a StepAction is given to run once for a step.

type Cleaner added in v1.27.0

type Cleaner interface {
	Action
	Cleanup(call CleanupCall) error
}

Cleaner is an Action that removes what its steps left behind in a run, such as the containers they started, once the run is over. The runner calls Cleanup only on an action whose action.yml sets cleanup, and once however many steps used it, so the action has to find what to remove by the run alone, such as by a label of the run's ID that its steps put on what they made.

Cleanup is called when the run ends by a signal as well, and then a step of the action may still be running, or may have been stopped halfway. It can be called again for the same run, and for a run that left nothing, so it must not fail for what is already gone.

type CleanupCall added in v1.27.0

type CleanupCall struct {
	// RunID names the run that is over, as Step.RunID named it to each of
	// its steps.
	RunID string
}

CleanupCall is what a Cleaner is given to clean up after a run.

type Cleanups added in v1.27.0

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

Cleanups is given to an action by a runner that cleans up once the run is over. It gathers the external actions that are to be cleaned up then, for an action that runs steps of its own to pass on those its steps used. A nil Cleanups gathers nothing.

func (*Cleanups) Add added in v1.27.0

func (c *Cleanups) Add(uses ...string)

Add names external actions to clean up once the run is over, each as a step's uses names it in full, with a local one as an absolute path. One already named is not named again.

func (*Cleanups) List added in v1.27.0

func (c *Cleanups) List() []string

List returns the actions named so far, in the order they were first named.

type Client

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

Client calls an Action in the process that serves it. Each of its methods calls the method of the same name of StepAction or Cleaner, and takes a ctx as well, to give up on the call by. It is not an Action itself: the runner always calls an action for a step, and one that is not a StepAction is run with the parameters alone.

func (*Client) Cleanup added in v1.27.0

func (m *Client) Cleanup(ctx context.Context, call CleanupCall) error

Cleanup tells the action that the run is over, for it to remove what its steps left behind. It gives up once ctx is done.

func (*Client) RunStep

func (m *Client) RunStep(ctx context.Context, call Call) (map[string]any, map[string]any, error)

RunStep calls the action for a step, and returns the state it leaves, which is nil when the action keeps none. An action that is not a StepAction is run with the parameters alone. It gives up once ctx is done.

type Contract added in v1.30.0

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

Contract records the check an action made of a step against a contract, for the runner to take as the step's test. A nil Contract records nothing.

func (*Contract) Check added in v1.30.0

func (c *Contract) Check() *ContractCheck

Check returns the check the action reported, or nil when it checked the step against no contract.

func (*Contract) Report added in v1.30.0

func (c *Contract) Report(check ContractCheck)

Report records the check the action made, in place of one it reported before.

type ContractCheck added in v1.30.0

type ContractCheck struct {
	// Matched is what the contract matched the step to; nil when it has
	// nothing that matches it.
	Matched *ContractMatch
	// Violations are what the contract does not allow in the request and
	// the response, empty when it allows all of it.
	Violations []Violation
}

ContractCheck is the check of a step against a contract, such as an OpenAPI document, which the runner takes as the step's test: the step fails when the contract does not allow its request or response, and is checked even without a test of its own.

func ContractCheckOf added in v1.30.0

func ContractCheckOf(violations []any, matched map[string]any) ContractCheck

ContractCheckOf returns the check that violations and matched tell, in the shapes a step shows them in res.violations and res.contract: each violation a map of in, field, reason and message, and matched a map of spec, operation and response, or nil.

type ContractMatch added in v1.30.0

type ContractMatch struct {
	// Spec is the contract, such as the path of an OpenAPI document.
	Spec string
	// Operation is the operation, such as GET /users/{id}.
	Operation string
	// Response is the response the operation declares for the step, such as
	// 200; empty when it declares none.
	Response string
}

ContractMatch is what a contract matched a step to, which a report of the coverage of the contract counts.

type ExitError added in v1.30.0

type ExitError struct {
	// Status is how the process exited, such as "exit status 2", or empty
	// when it is not known.
	Status string
	// Output is the first lines the process wrote to stderr that are not
	// its log records, such as the message a panic starts with. It is empty
	// when it wrote none.
	Output string
	// Err is what the call failed with.
	Err error
}

ExitError is the error of an action whose process exited before it returned, as one that panics does. What the call failed with tells only that the connection was lost, so the error tells how the process exited and what it wrote to stderr that is not one of its log records, such as the message of the panic.

func (*ExitError) Error added in v1.30.0

func (e *ExitError) Error() string

func (*ExitError) Unwrap added in v1.30.0

func (e *ExitError) Unwrap() error

type Guard

type Guard struct {
	// ReadOnly allows only what reads, and nothing that writes, such as an
	// HTTP POST or an UPDATE.
	ReadOnly bool
	// AllowHosts are the hosts actions may connect to: a name or an address,
	// with a port or without one for any port, or *.example.com for the
	// names under example.com. Empty allows any host.
	AllowHosts []string
	// AllowActions are the actions run under the guard although they do not
	// keep to it, as the person running probe asks.
	AllowActions []string
	// Keeps holds the kinds of guard each action says it keeps to itself,
	// keyed by the action as a step's uses names it: a built-in action by
	// what its package declares, an external one by its action.yml. An
	// action is refused under a guard it does not keep to every kind of,
	// unless AllowActions names it.
	Keeps map[string][]string
}

Guard is what a run allows its actions to do. It is set by whoever runs probe, on the command line, rather than by the workflow, so that a workflow cannot loosen it. The zero Guard allows everything.

A guard keeps a workflow from writing to, or reaching, what it was not meant to; it is not a sandbox. An action keeps to it only as far as the action can tell what it is about to do.

func (Guard) Active

func (g Guard) Active() bool

Active reports whether the guard limits anything.

func (Guard) AllowsHost

func (g Guard) AllowsHost(hostport string) bool

AllowsHost reports whether the guard allows connecting to hostport, a host with or without a port.

func (Guard) CheckHost

func (g Guard) CheckHost(hostport string) error

CheckHost returns a Refused error when the guard does not allow connecting to hostport.

func (Guard) Kinds

func (g Guard) Kinds() []string

Kinds returns the kinds of guard that limit the run, in the order of the constants.

func (Guard) Missing

func (g Guard) Missing(uses string) []string

Missing returns the kinds of guard that limit the run and that the action named uses does not keep to.

func (Guard) Runs

func (g Guard) Runs(uses string) bool

Runs reports whether the action named uses may run under the guard: it keeps to every kind of guard that limits the run, or the person running probe allowed it.

func (Guard) WithKeeps

func (g Guard) WithKeeps(uses string, kinds []string) Guard

WithKeeps returns a copy of the guard that also holds that the action named uses keeps to kinds, leaving the guard it was made from as it was.

type Note added in v1.33.0

type Note struct {
	// Job is the name of the job, and JobID its id.
	Job, JobID string
	// Step is the name of the step, and Index its index in the job.
	Step  string
	Index int
	// Status is the status of the step: passed, failed or untested.
	Status string
	// Level is NoteInfo, NoteWarning or NoteCritical.
	Level string
	// Title is a line naming the note, empty when it has none.
	Title string
	// Body is the note in Markdown.
	Body string
	// Run is, for a step of a job with repeat, the run the note is of,
	// from 1 without the warmup runs, and 0 otherwise.
	Run int
}

Note is an entry of the notes context, as a step gives it to an action in with, such as with notes: "{{ notes }}": a note an earlier action left for people, with the job and the step it comes from. The values Probe hides are hidden in Title and Body, and in Job and Step, as <secret:NAME> or <redacted>.

func ParseNotes added in v1.33.0

func ParseNotes(v any) ([]Note, error)

ParseNotes reads v, the value a step gives an action for the notes context, or a part of it such as filter(notes, .level == 'critical'). An action that passes notes on reads them with it, rather than its own copy of what an entry holds.

An entry needs a level of info, warning or critical and a body; the other fields are read when they are there. The error says which entry is wrong and why, starting with notes, as notes[1]: level "high" is not one of info, warning or critical, for the action to put with. before it.

type Plugin

type Plugin struct {
	plugin.Plugin
	Impl Action
}

Plugin serves an Action over gRPC, and is what the runner dispenses to call one.

func (*Plugin) GRPCClient

func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (any, error)

func (*Plugin) GRPCServer

func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error

type Process added in v1.30.0

type Process struct {
	// PID leads a process group of its own, which is stopped with it.
	PID int
	// Log is the file its output goes to, which is removed once it is
	// stopped; empty for none.
	Log string
}

Process is a process an action left running in the background, for the runner to stop once the run is over.

type Processes added in v1.30.0

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

Processes gathers the processes an action leaves running in the background, which the runner stops once the run is over: an action runs in a process that exits once its step is done, and one it starts in a session of its own to outlive it would otherwise run on. A nil Processes gathers nothing.

func (*Processes) Add added in v1.30.0

func (p *Processes) Add(procs ...Process)

Add records processes the action left running.

func (*Processes) List added in v1.30.0

func (p *Processes) List() []Process

List returns the processes recorded so far, in the order they were added.

type Refused

type Refused struct {
	Reason string
}

Refused is the error an action returns for what the guard of the run does not allow. The runner tells it from other errors of an action.

func (*Refused) Error

func (r *Refused) Error() string

type Server

type Server struct {
	Impl Action
	// contains filtered or unexported fields
}

Server answers the runner's calls with an Action. It logs at trace level alone: its records tell how a call went through it, and what it returns, an error as well, the runner tells itself.

func (*Server) Cleanup added in v1.27.0

func (m *Server) Cleanup(ctx context.Context, req *pb.CleanupRequest) (*pb.CleanupResponse, error)

Cleanup has the action clean up after the run the request names. An action that is not a Cleaner has nothing to do it with, which is an error: the runner calls only an action that asked to be.

func (*Server) Run

func (m *Server) Run(ctx context.Context, req *pb.RunRequest) (_ *pb.RunResponse, err error)

Run runs the action with the request's parameters.

type StartError added in v1.30.0

type StartError struct {
	// Output is the first lines the process wrote to stderr that are not
	// its log records, which may say why it did not start. It is empty when
	// it wrote none.
	Output string
	// Err is what starting it failed with, as go-plugin tells it.
	Err error
}

StartError is the error of an action whose process could not be started or connected to.

func (*StartError) Error added in v1.30.0

func (e *StartError) Error() string

func (*StartError) Unwrap added in v1.30.0

func (e *StartError) Unwrap() error

type Step

type Step struct {
	// RunID names the run of probe, the same for every step of every job in
	// it, and for a job run by the embedded action.
	RunID   string
	JobID   string
	JobName string
	// Index is the position of the step in the job, from 0.
	Index int
	// ID is the id the step is given, or step_ and where it is written in
	// its job when it has none, as step_1.
	ID   string
	Name string
	// Repeat is which run of a repeated job this is, from 0.
	Repeat int
	// Attempt is which attempt of a retried step this is, from 1.
	Attempt int
}

Step tells an action about the step it runs for.

type StepAction

type StepAction interface {
	Action
	RunStep(call Call) (result, newState map[string]any, err error)
}

StepAction is an Action that is told about the step it runs for, and may keep state in a job from one step that uses it to the next, such as the cookies a server set. What it does with either is up to the action.

type Violation added in v1.30.0

type Violation struct {
	// In is where it is: request or response.
	In string
	// Field is the field, such as $.id; empty when it is not in one.
	Field string
	// Reason is the kind of violation; empty when there is none to tell.
	Reason  string
	Message string
}

Violation is what a contract does not allow in a request or a response.

Jump to

Keyboard shortcuts

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