mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
This extends statemgr.Persistent, statemgr.Locker and remote.Client to all expect context.Context parameters, and then updates all of the existing implementations of those interfaces to support them. All of the calls to statemgr.Persistent and statemgr.Locker methods outside of tests are consistently context.TODO() for now, because the caller landscape of these interfaces has some complications: 1. statemgr.Locker is also used by the clistate package for its state implementation that was derived from statemgr.Filesystem's predecessor, even though what clistate manages is not actually "state" in the sense of package statemgr. The callers of that are not yet ready to provide real contexts. In a future commit we'll either need to plumb context through to all of the clistate callers, or continue the effort to separate statemgr from clistate by introducing a clistate-specific "locker" API for it to use instead. 2. We call statemgr.Persistent and statemgr.Locker methods in situations where the active context might have already been cancelled, and so we'll need to make sure to ignore cancellation when calling those. This is mainly limited to PersistState and Unlock, since both need to be able to complete after a cancellation, but there are various codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so it isn't yet clear where is the best place to enforce the invariant that Persist and Unlock must not be called with a cancelable context. We'll deal with that more in subsequent commits. Within the various state manager and remote client implementations the contexts _are_ wired together as best as possible with how these subsystems are already laid out, and so once we deal with the problems above and make callers provide suitable contexts they should be able to reach all of the leaf API clients that might want to generate OpenTelemetry traces. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package local
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/opentofu/opentofu/internal/backend"
|
|
"github.com/opentofu/opentofu/internal/logging"
|
|
"github.com/opentofu/opentofu/internal/states"
|
|
"github.com/opentofu/opentofu/internal/states/statemgr"
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
|
)
|
|
|
|
func (b *Local) opRefresh(
|
|
stopCtx context.Context,
|
|
cancelCtx context.Context,
|
|
op *backend.Operation,
|
|
runningOp *backend.RunningOperation) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
// For the moment we have a bit of a tangled mess of context.Context here, for
|
|
// historical reasons. Hopefully we'll clean this up one day, but here's the
|
|
// guide for now:
|
|
// - ctx is used only for its values, and should be connected to the top-level ctx
|
|
// from "package main" so that we can obtain telemetry objects, etc from it.
|
|
// - stopCtx is cancelled to trigger a graceful shutdown.
|
|
// - cancelCtx is cancelled for a graceless shutdown.
|
|
ctx := context.WithoutCancel(stopCtx)
|
|
|
|
// Check if our state exists if we're performing a refresh operation. We
|
|
// only do this if we're managing state with this backend.
|
|
if b.Backend == nil {
|
|
if _, err := os.Stat(b.StatePath); err != nil {
|
|
if os.IsNotExist(err) {
|
|
err = nil
|
|
}
|
|
|
|
if err != nil {
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
tfdiags.Error,
|
|
"Cannot read state file",
|
|
fmt.Sprintf("Failed to read %s: %s", b.StatePath, err),
|
|
))
|
|
op.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// Refresh now happens via a plan, so we need to ensure this is enabled
|
|
op.PlanRefresh = true
|
|
|
|
// Get our context
|
|
lr, _, opState, contextDiags := b.localRun(ctx, op)
|
|
diags = diags.Append(contextDiags)
|
|
if contextDiags.HasErrors() {
|
|
op.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
// the state was locked during successful context creation; unlock the state
|
|
// when the operation completes
|
|
defer func() {
|
|
diags := op.StateLocker.Unlock()
|
|
if diags.HasErrors() {
|
|
op.View.Diagnostics(diags)
|
|
runningOp.Result = backend.OperationFailure
|
|
}
|
|
}()
|
|
|
|
// If we succeed then we'll overwrite this with the resulting state below,
|
|
// but otherwise the resulting state is just the input state.
|
|
runningOp.State = lr.InputState
|
|
if !runningOp.State.HasManagedResourceInstanceObjects() {
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
tfdiags.Warning,
|
|
"Empty or non-existent state",
|
|
"There are currently no remote objects tracked in the state, so there is nothing to refresh.",
|
|
))
|
|
}
|
|
|
|
// get schemas before writing state
|
|
schemas, moreDiags := lr.Core.Schemas(ctx, lr.Config, lr.InputState)
|
|
diags = diags.Append(moreDiags)
|
|
if moreDiags.HasErrors() {
|
|
op.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
// Perform the refresh in a goroutine so we can be interrupted
|
|
var newState *states.State
|
|
var refreshDiags tfdiags.Diagnostics
|
|
doneCh := make(chan struct{})
|
|
panicHandler := logging.PanicHandlerWithTraceFn()
|
|
go func() {
|
|
defer panicHandler()
|
|
defer close(doneCh)
|
|
newState, refreshDiags = lr.Core.Refresh(ctx, lr.Config, lr.InputState, lr.PlanOpts)
|
|
log.Printf("[INFO] backend/local: refresh calling Refresh")
|
|
}()
|
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, lr.Core, opState, op.View) {
|
|
return
|
|
}
|
|
|
|
// Write the resulting state to the running op
|
|
runningOp.State = newState
|
|
diags = diags.Append(refreshDiags)
|
|
if refreshDiags.HasErrors() {
|
|
op.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
err := statemgr.WriteAndPersist(context.TODO(), opState, newState, schemas)
|
|
if err != nil {
|
|
diags = diags.Append(fmt.Errorf("failed to write state: %w", err))
|
|
op.ReportResult(runningOp, diags)
|
|
return
|
|
}
|
|
|
|
// Show any remaining warnings before exiting
|
|
op.ReportResult(runningOp, diags)
|
|
}
|