Documentation
¶
Overview ¶
Package execution applies provider-evaluated hooks and extra arguments to Terraform commands.
A configuration provider (for example the native Terragrunt plugin) evaluates hooks and extra arguments but never runs them. The host owns execution: it matches them against the command being run, runs hooks with the invocation's environment, working directory, cancellation, and I/O, and surfaces failures with their exit codes. The rules follow Terragrunt's own: a command is matched by name, `apply -destroy` counts as `destroy`, before hooks gate the command, after hooks need run_on_error to follow a failure, and error hooks match on the failure text.
Index ¶
- Constants
- func CheckRequiredVarFiles(items []schema.ComponentExtraArguments, command, workingDirectory string) error
- func CommandName(command string, flags []string) string
- func ExcludeMatches(actions []string, command string) bool
- func HooksFor(hooks []schema.ComponentHook, canonical string, aliases ...string) []schema.ComponentHook
- func ListsCommand(hooks []schema.ComponentHook, commands ...string) bool
- func Run(ctx context.Context, config *schema.ComponentExecution, command string, ...) error
- func Validate(config *schema.ComponentExecution) error
- type ArgumentsRequest
- type ArgumentsResult
- type Attempt
- type HookKind
- type Output
- type Recoverer
- type RecovererOption
- type Reporter
- type Runner
- type TextOutput
Constants ¶
const ( // CommandReadConfig runs after_hooks once the unit's configuration is read. CommandReadConfig = "terragrunt-read-config" // CommandReadConfigAlias is the documented spelling of CommandReadConfig. CommandReadConfigAlias = "read-config" // CommandInitFromModule runs hooks around the module source being fetched. CommandInitFromModule = "init-from-module" )
Lifecycle commands name stages of a provider's own pipeline instead of Terraform commands. A hook lists them in `commands` like any command, and they run in the unit's configuration directory.
const SignalsFileName = "error-signals.json"
SignalsFileName is the file that an ignore policy writes its signals to, in the module directory.
Variables ¶
This section is empty.
Functions ¶
func CheckRequiredVarFiles ¶
func CheckRequiredVarFiles(items []schema.ComponentExtraArguments, command, workingDirectory string) error
CheckRequiredVarFiles verifies the required variable files of every block that lists the command, without building any arguments. It lets a caller reject a missing file before implicit init or workspace subprocesses, and their hooks, run for that command.
func CommandName ¶
CommandName returns the name that hooks and extra arguments match against. It is "destroy" for `destroy` and for `apply` with the destroy flag set, and the Terraform command itself otherwise, so `plan -destroy` still matches "plan". Like Terragrunt, only the command is matched, never its subcommand: `state list` and `state rm` both match "state".
func ExcludeMatches ¶
ExcludeMatches reports whether an exclude block's actions name the command. "all" matches every command and "all_except_output" every command but output; other entries match case-insensitively.
func HooksFor ¶
func HooksFor(hooks []schema.ComponentHook, canonical string, aliases ...string) []schema.ComponentHook
HooksFor returns copies of the hooks that list any of the commands, each rewritten to list only the canonical command, so a hook written with an alias still matches the phase that runs it.
func ListsCommand ¶
func ListsCommand(hooks []schema.ComponentHook, commands ...string) bool
ListsCommand reports whether any hook lists one of the commands.
func Run ¶
func Run(ctx context.Context, config *schema.ComponentExecution, command string, runner Runner, action func() (Output, error)) error
Run executes the command lifecycle: before hooks, the action, after hooks, then error hooks.
A failing before hook stops the action. After hooks run after a failure only when they set run_on_error. Error hooks run when any earlier phase failed and an on_errors expression matches that phase's error or output. Cancellation stops every remaining hook. The action's error is returned unchanged when nothing else failed.
func Validate ¶
func Validate(config *schema.ComponentExecution) error
Validate rejects hooks the host cannot run, so a malformed provider response fails before any command starts instead of silently skipping a hook.
Types ¶
type ArgumentsRequest ¶
type ArgumentsRequest struct {
// Command is the matching name from CommandName.
Command string
// Args is the full argument vector, starting with the command.
Args []string
// InsertAt is the index after the command and its subcommands where extra arguments go.
InsertAt int
// PlanFileArgument reports that the last argument is a saved plan. Terraform rejects -var
// options beside a saved plan, so they are dropped.
PlanFileArgument bool
// WorkingDirectory resolves relative variable file paths. Terraform runs there.
WorkingDirectory string
}
ArgumentsRequest describes one Terraform invocation for ApplyExtraArguments.
type ArgumentsResult ¶
type ArgumentsResult struct {
Args []string
// Environment holds KEY=value entries sorted by key. Later blocks override earlier ones.
Environment []string
}
ArgumentsResult is the rewritten invocation.
func ApplyExtraArguments ¶
func ApplyExtraArguments(items []schema.ComponentExtraArguments, request *ArgumentsRequest) (ArgumentsResult, error)
ApplyExtraArguments adds the arguments, variable files, and environment of every block that lists the command, in declaration order. The input arguments are never modified.
type Attempt ¶
Attempt runs the command once. It returns the error text the policies match against, usually the command's standard error, and the command's error.
type Output ¶
type Output interface {
// Open returns a new reader positioned at the start of the output. A reader that
// implements io.Closer is closed after use. An error reports a failed capture.
Open() (io.Reader, error)
}
Output is captured phase output. Error hook matching reads it as a stream, so output of any size is never loaded into memory as a whole.
type Recoverer ¶
type Recoverer struct {
// contains filtered or unexported fields
}
Recoverer applies a provider's errors policy to a failing command: an ignore policy turns the failure into a success, and a retry policy runs the command again after a pause. Ignore policies are checked before retry policies, each in declaration order, and the first match decides, like Terragrunt's errors block.
func NewRecoverer ¶
func NewRecoverer(policy *schema.ComponentErrors, options ...RecovererOption) *Recoverer
NewRecoverer builds a Recoverer for a policy. A nil or empty policy never recovers.
func (*Recoverer) Run ¶
Run runs the attempt until it succeeds, fails with an error no policy covers, or exhausts a retry policy. It returns ignored = true when an ignore policy swallowed the failure.
func (*Recoverer) WriteSignals ¶
func (r *Recoverer) WriteSignals(policy *schema.ComponentIgnorePolicy)
WriteSignals writes signals for an already selected policy, retaining non-fatal write handling.
type RecovererOption ¶
type RecovererOption func(*Recoverer)
RecovererOption configures a Recoverer.
func WithReporter ¶
func WithReporter(reporter Reporter) RecovererOption
WithReporter sets where decisions are reported.
func WithSignalsFile ¶
func WithSignalsFile(path string) RecovererOption
WithSignalsFile sets the file an ignore policy writes its signals to. Without it no signals are written.
func WithSignalsPath ¶
func WithSignalsPath(resolve func() string) RecovererOption
WithSignalsPath sets the file an ignore policy writes its signals to, resolved when the signals are written rather than when the recoverer is built. An empty path writes no signals.
type Reporter ¶
type Reporter interface {
// Retrying reports that the command failed with a retryable error and will run again.
Retrying(policy string, attempt, maxAttempts int, sleep time.Duration)
// Ignored reports that a failure was turned into a success.
Ignored(policy, message string)
// SignalsWritten reports where an ignore policy wrote its signals.
SignalsWritten(path string)
}
Reporter tells the user what error handling decided. Implementations print on the UI channel.
type Runner ¶
type Runner interface {
RunHook(ctx context.Context, kind HookKind, hook *schema.ComponentHook) (output Output, err error)
}
Runner executes one hook and reports its failure and captured output. The output feeds error hook matching and is never printed again. A nil output means nothing was captured.