2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"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-02-14 20:17:28 +01:00
|
|
|
clistate "github.com/hashicorp/terraform/command/state"
|
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-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Local) opRefresh(
|
|
|
|
ctx context.Context,
|
|
|
|
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 {
|
2017-02-15 17:53:19 +01:00
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = op.Type.String()
|
|
|
|
lockID, err := clistate.Lock(opState, lockInfo, b.CLI, b.Colorize())
|
|
|
|
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
|
|
|
|
|
|
|
// Perform operation and write the resulting state to the running op
|
|
|
|
newState, err := tfCtx.Refresh()
|
|
|
|
runningOp.State = newState
|
|
|
|
if err != nil {
|
|
|
|
runningOp.Err = errwrap.Wrapf("Error refreshing state: {{err}}", err)
|
|
|
|
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.
|
|
|
|
`
|