2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2017-12-02 23:31:28 +01:00
|
|
|
"log"
|
2017-01-19 05:47:56 +01:00
|
|
|
"os"
|
2017-03-16 20:05:11 +01:00
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
2017-02-14 20:17:28 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2017-04-01 20:58:19 +02:00
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
2017-02-22 22:08:03 +01:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2017-02-15 17:53:19 +01:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-12-02 23:31:28 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Local) opRefresh(
|
2018-02-10 00:10:52 +01:00
|
|
|
stopCtx context.Context,
|
|
|
|
cancelCtx context.Context,
|
2017-01-19 05:47:56 +01:00
|
|
|
op *backend.Operation,
|
|
|
|
runningOp *backend.RunningOperation) {
|
|
|
|
// 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) {
|
2017-03-16 20:05:11 +01:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-01-19 05:47:56 +01:00
|
|
|
runningOp.Err = fmt.Errorf(
|
2017-03-16 20:05:11 +01:00
|
|
|
"There was an error reading the Terraform state that is needed\n"+
|
|
|
|
"for refreshing. The path and error are shown below.\n\n"+
|
|
|
|
"Path: %s\n\nError: %s",
|
|
|
|
b.StatePath, err)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 22:08:03 +01:00
|
|
|
// If we have no config module given to use, create an empty tree to
|
|
|
|
// avoid crashes when Terraform.Context is initialized.
|
|
|
|
if op.Module == nil {
|
|
|
|
op.Module = module.NewEmptyTree()
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Get our context
|
2017-02-02 00:16:16 +01:00
|
|
|
tfCtx, opState, err := b.context(op)
|
2017-01-19 05:47:56 +01:00
|
|
|
if err != nil {
|
|
|
|
runningOp.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:17:28 +01:00
|
|
|
if op.LockState {
|
2018-02-10 00:10:52 +01:00
|
|
|
lockCtx, cancel := context.WithTimeout(stopCtx, op.StateLockTimeout)
|
2017-04-01 21:42:13 +02:00
|
|
|
defer cancel()
|
|
|
|
|
2017-02-15 17:53:19 +01:00
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = op.Type.String()
|
2017-04-01 21:42:13 +02:00
|
|
|
lockID, err := clistate.Lock(lockCtx, opState, lockInfo, b.CLI, b.Colorize())
|
2017-02-15 17:53:19 +01:00
|
|
|
if err != nil {
|
|
|
|
runningOp.Err = errwrap.Wrapf("Error locking state: {{err}}", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:17:28 +01:00
|
|
|
defer func() {
|
2017-02-15 17:53:19 +01:00
|
|
|
if err := clistate.Unlock(opState, lockID, b.CLI, b.Colorize()); err != nil {
|
2017-02-14 20:17:28 +01:00
|
|
|
runningOp.Err = multierror.Append(runningOp.Err, err)
|
2017-02-02 00:16:16 +01:00
|
|
|
}
|
2017-02-14 20:17:28 +01:00
|
|
|
}()
|
|
|
|
}
|
2017-02-02 00:16:16 +01:00
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Set our state
|
2017-02-02 00:16:16 +01:00
|
|
|
runningOp.State = opState.State()
|
2017-03-16 20:05:11 +01:00
|
|
|
if runningOp.State.Empty() || !runningOp.State.HasResources() {
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output(b.Colorize().Color(
|
|
|
|
strings.TrimSpace(refreshNoState) + "\n"))
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2017-12-02 23:31:28 +01:00
|
|
|
// Perform the refresh in a goroutine so we can be interrupted
|
|
|
|
var newState *terraform.State
|
|
|
|
var refreshErr error
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
2018-01-10 16:40:20 +01:00
|
|
|
newState, refreshErr = tfCtx.Refresh()
|
|
|
|
log.Printf("[INFO] backend/local: refresh calling Refresh")
|
2017-12-02 23:31:28 +01:00
|
|
|
}()
|
|
|
|
|
2018-02-12 17:52:21 +01:00
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, tfCtx, opState) {
|
2018-02-10 00:10:52 +01:00
|
|
|
return
|
2017-12-02 23:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the resulting state to the running op
|
2017-01-19 05:47:56 +01:00
|
|
|
runningOp.State = newState
|
2017-12-02 23:31:28 +01:00
|
|
|
if refreshErr != nil {
|
|
|
|
runningOp.Err = errwrap.Wrapf("Error refreshing state: {{err}}", refreshErr)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write and persist the state
|
2017-02-02 00:16:16 +01:00
|
|
|
if err := opState.WriteState(newState); err != nil {
|
2017-01-19 05:47:56 +01:00
|
|
|
runningOp.Err = errwrap.Wrapf("Error writing state: {{err}}", err)
|
|
|
|
return
|
|
|
|
}
|
2017-02-02 00:16:16 +01:00
|
|
|
if err := opState.PersistState(); err != nil {
|
2017-01-19 05:47:56 +01:00
|
|
|
runningOp.Err = errwrap.Wrapf("Error saving state: {{err}}", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-03-16 20:05:11 +01:00
|
|
|
|
|
|
|
const refreshNoState = `
|
|
|
|
[reset][bold][yellow]Empty or non-existent state file.[reset][yellow]
|
|
|
|
|
|
|
|
Refresh will do nothing. Refresh does not error or return an erroneous
|
|
|
|
exit status because many automation scripts use refresh, plan, then apply
|
|
|
|
and may not have a state file yet for the first run.
|
|
|
|
`
|