opentofu/internal/tofu/hook.go

198 lines
9.2 KiB
Go
Raw Normal View History

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2023-09-20 07:16:53 -05:00
package tofu
2014-06-26 18:52:15 -05:00
import (
"github.com/zclconf/go-cty/cty"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/states"
)
2014-06-26 18:52:15 -05:00
// HookAction is an enum of actions that can be taken as a result of a hook
// callback. This allows you to modify the behavior of OpenTofu at runtime.
2014-06-26 18:52:15 -05:00
type HookAction byte
const (
// HookActionContinue continues with processing as usual.
HookActionContinue HookAction = iota
// HookActionHalt halts immediately: no more hooks are processed
// and the action that OpenTofu was about to take is cancelled.
2014-06-26 18:52:15 -05:00
HookActionHalt
)
// Hook is the interface that must be implemented to hook into various
// parts of OpenTofu, allowing you to inspect or change behavior at runtime.
2014-06-26 18:52:15 -05:00
//
// There are MANY hook points into OpenTofu. If you only want to implement
2014-06-26 18:52:15 -05:00
// some hook points, but not all (which is the likely case), then embed the
// NilHook into your struct, which implements all of the interface but does
// nothing. Then, override only the functions you want to implement.
type Hook interface {
// PreApply and PostApply are called before and after an action for a
// single instance is applied. The error argument in PostApply is the
// error, if any, that was returned from the provider Apply call itself.
PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error)
PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error)
// PreDiff and PostDiff are called before and after a provider is given
// the opportunity to customize the proposed new state to produce the
// planned new state.
PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error)
PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error)
// The provisioning hooks signal both the overall start end end of
// provisioning for a particular instance and of each of the individual
// configured provisioners for each instance. The sequence of these
// for a given instance might look something like this:
2014-10-04 18:31:48 -05:00
//
// PreProvisionInstance(aws_instance.foo[1], ...)
// PreProvisionInstanceStep(aws_instance.foo[1], "file")
// PostProvisionInstanceStep(aws_instance.foo[1], "file", nil)
// PreProvisionInstanceStep(aws_instance.foo[1], "remote-exec")
// ProvisionOutput(aws_instance.foo[1], "remote-exec", "Installing foo...")
// ProvisionOutput(aws_instance.foo[1], "remote-exec", "Configuring bar...")
// PostProvisionInstanceStep(aws_instance.foo[1], "remote-exec", nil)
// PostProvisionInstance(aws_instance.foo[1], ...)
//
// ProvisionOutput is called with output sent back by the provisioners.
// This will be called multiple times as output comes in, with each call
// representing one line of output. It cannot control whether the
// provisioner continues running.
PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error)
PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error)
PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error)
PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error)
ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string)
2014-07-27 11:00:34 -05:00
2014-06-26 19:17:10 -05:00
// PreRefresh and PostRefresh are called before and after a single
// resource state is refreshed, respectively.
PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error)
PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error)
// PreImportState and PostImportState are called before and after
// (respectively) each state import operation for a given resource address when
// using the legacy import command.
PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error)
PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error)
// PrePlanImport and PostPlanImport are called during a plan before and after planning to import
// a new resource using the configuration-driven import workflow.
PrePlanImport(addr addrs.AbsResourceInstance, importID string) (HookAction, error)
PostPlanImport(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error)
// PreApplyImport and PostApplyImport are called during an apply for each imported resource when
// using the configuration-driven import workflow.
PreApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error)
PostApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error)
// Stopping is called if an external signal requests that OpenTofu
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
// gracefully abort an operation in progress.
//
// This notification might suggest that the user wants OpenTofu to exit
// ASAP and in that case it's possible that if OpenTofu runs for too much
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
// longer then it'll get killed un-gracefully, and so this hook could be
// an opportunity to persist any transient data that would be lost under
// a subsequent kill signal. However, implementations must take care to do
// so in a way that won't cause corruption if the process _is_ killed while
// this hook is still running.
//
// This hook cannot control whether OpenTofu continues, because the
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
// graceful shutdown process is typically already running by the time this
// function is called.
Stopping()
// PostStateUpdate is called each time the state is updated. It receives
// a deep copy of the state, which it may therefore access freely without
// any need for locks to protect from concurrent writes from the caller.
PostStateUpdate(new *states.State) (HookAction, error)
2014-06-26 18:52:15 -05:00
}
// NilHook is a Hook implementation that does nothing. It exists only to
// simplify implementing hooks. You can embed this into your Hook implementation
// and only implement the functions you are interested in.
type NilHook struct{}
var _ Hook = (*NilHook)(nil)
func (*NilHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
2014-06-27 00:09:16 -05:00
return HookActionContinue, nil
}
func (*NilHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) {
2014-06-27 00:09:16 -05:00
return HookActionContinue, nil
}
func (*NilHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) {
2014-06-26 19:17:10 -05:00
return HookActionContinue, nil
}
func (*NilHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
2014-06-26 19:17:10 -05:00
return HookActionContinue, nil
}
func (*NilHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return HookActionContinue, nil
}
func (*NilHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return HookActionContinue, nil
}
func (*NilHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return HookActionContinue, nil
}
func (*NilHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return HookActionContinue, nil
}
func (*NilHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) {
}
func (*NilHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) {
2014-06-26 18:52:15 -05:00
return HookActionContinue, nil
}
func (*NilHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) {
2014-06-26 18:52:15 -05:00
return HookActionContinue, nil
}
func (*NilHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
return HookActionContinue, nil
}
func (*NilHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
return HookActionContinue, nil
}
func (h *NilHook) PrePlanImport(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
return HookActionContinue, nil
}
func (h *NilHook) PostPlanImport(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
return HookActionContinue, nil
}
func (h *NilHook) PreApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
return HookActionContinue, nil
}
func (h *NilHook) PostApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
return HookActionContinue, nil
}
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
func (*NilHook) Stopping() {
// Does nothing at all by default
}
func (*NilHook) PostStateUpdate(new *states.State) (HookAction, error) {
return HookActionContinue, nil
}