2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-02-22 01:07:27 +01:00
|
|
|
"errors"
|
2017-01-19 05:47:56 +01:00
|
|
|
"fmt"
|
2017-02-22 01:07:27 +01:00
|
|
|
"io/ioutil"
|
2018-02-12 17:52:21 +01:00
|
|
|
"log"
|
2017-02-22 01:07:27 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2017-06-22 19:15:30 +02:00
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
"sync"
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2018-02-23 02:43:21 +01:00
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/config/configschema"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/mitchellh/colorstring"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
2017-02-22 01:07:27 +01:00
|
|
|
const (
|
2017-05-31 02:13:43 +02:00
|
|
|
DefaultWorkspaceDir = "terraform.tfstate.d"
|
|
|
|
DefaultWorkspaceFile = "environment"
|
2017-02-22 01:07:27 +01:00
|
|
|
DefaultStateFilename = "terraform.tfstate"
|
|
|
|
DefaultBackupExtension = ".backup"
|
|
|
|
)
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Local is an implementation of EnhancedBackend that performs all operations
|
|
|
|
// locally. This is the "default" backend and implements normal Terraform
|
|
|
|
// behavior as it is well known.
|
|
|
|
type Local struct {
|
|
|
|
// CLI and Colorize control the CLI output. If CLI is nil then no CLI
|
|
|
|
// output will be done. If CLIColor is nil then no coloring will be done.
|
|
|
|
CLI cli.Ui
|
|
|
|
CLIColor *colorstring.Colorize
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// ShowDiagnostics prints diagnostic messages to the UI.
|
|
|
|
ShowDiagnostics func(vals ...interface{})
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// The State* paths are set from the backend config, and may be left blank
|
|
|
|
// to use the defaults. If the actual paths for the local backend state are
|
2017-02-22 01:07:27 +01:00
|
|
|
// needed, use the StatePaths method.
|
|
|
|
//
|
2017-01-19 05:47:56 +01:00
|
|
|
// StatePath is the local path where state is read from.
|
|
|
|
//
|
|
|
|
// StateOutPath is the local path where the state will be written.
|
|
|
|
// If this is empty, it will default to StatePath.
|
|
|
|
//
|
|
|
|
// StateBackupPath is the local path where a backup file will be written.
|
2017-02-22 01:07:27 +01:00
|
|
|
// Set this to "-" to disable state backup.
|
2017-03-01 20:34:45 +01:00
|
|
|
//
|
2017-05-31 02:13:43 +02:00
|
|
|
// StateWorkspaceDir is the path to the folder containing data for
|
|
|
|
// non-default workspaces. This defaults to DefaultWorkspaceDir if not set.
|
|
|
|
StatePath string
|
|
|
|
StateOutPath string
|
|
|
|
StateBackupPath string
|
|
|
|
StateWorkspaceDir string
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2017-02-27 22:43:31 +01:00
|
|
|
// We only want to create a single instance of a local state, so store them
|
|
|
|
// here as they're loaded.
|
|
|
|
states map[string]state.State
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Terraform context. Many of these will be overridden or merged by
|
|
|
|
// Operation. See Operation for more details.
|
|
|
|
ContextOpts *terraform.ContextOpts
|
|
|
|
|
|
|
|
// OpInput will ask for necessary input prior to performing any operations.
|
|
|
|
//
|
|
|
|
// OpValidation will perform validation prior to running an operation. The
|
|
|
|
// variable naming doesn't match the style of others since we have a func
|
|
|
|
// Validate.
|
|
|
|
OpInput bool
|
|
|
|
OpValidation bool
|
|
|
|
|
|
|
|
// Backend, if non-nil, will use this backend for non-enhanced behavior.
|
|
|
|
// This allows local behavior with remote state storage. It is a way to
|
|
|
|
// "upgrade" a non-enhanced backend to an enhanced backend with typical
|
|
|
|
// behavior.
|
|
|
|
//
|
|
|
|
// If this is nil, local performs normal state loading and storage.
|
|
|
|
Backend backend.Backend
|
|
|
|
|
2017-09-09 02:14:37 +02:00
|
|
|
// RunningInAutomation indicates that commands are being run by an
|
|
|
|
// automated system rather than directly at a command prompt.
|
|
|
|
//
|
|
|
|
// This is a hint not to produce messages that expect that a user can
|
|
|
|
// run a follow-up command, perhaps because Terraform is running in
|
|
|
|
// some sort of workflow automation tool that abstracts away the
|
|
|
|
// exact commands that are being run.
|
|
|
|
RunningInAutomation bool
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
opLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Local) ConfigSchema() *configschema.Block {
|
|
|
|
if b.Backend != nil {
|
|
|
|
return b.Backend.ConfigSchema()
|
2018-07-04 12:11:35 +02:00
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
return &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"path": {
|
|
|
|
Type: cty.String,
|
2018-07-04 12:11:35 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
2018-03-21 02:43:02 +01:00
|
|
|
"workspace_dir": {
|
|
|
|
Type: cty.String,
|
2018-07-04 12:11:35 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
2018-03-21 02:43:02 +01:00
|
|
|
// environment_dir was previously a deprecated alias for
|
|
|
|
// workspace_dir, but now removed.
|
2018-07-04 12:11:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Local) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
|
|
|
|
if b.Backend != nil {
|
|
|
|
return b.Backend.ValidateConfig(obj)
|
2018-07-04 12:11:35 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
if val := obj.GetAttr("path"); !val.IsNull() {
|
|
|
|
p := val.AsString()
|
|
|
|
if p == "" {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid local state file path",
|
|
|
|
`The "path" attribute value must not be empty.`,
|
|
|
|
cty.Path{cty.GetAttrStep{Name: "path"}},
|
|
|
|
))
|
2018-07-04 12:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
if val := obj.GetAttr("workspace_dir"); !val.IsNull() {
|
|
|
|
p := val.AsString()
|
|
|
|
if p == "" {
|
|
|
|
diags = diags.Append(tfdiags.AttributeValue(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid local workspace directory path",
|
|
|
|
`The "workspace_dir" attribute value must not be empty.`,
|
|
|
|
cty.Path{cty.GetAttrStep{Name: "workspace_dir"}},
|
|
|
|
))
|
2018-07-04 12:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
return diags
|
2018-07-04 12:11:35 +02:00
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Local) Configure(obj cty.Value) tfdiags.Diagnostics {
|
2017-01-19 05:47:56 +01:00
|
|
|
if b.Backend != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
return b.Backend.Configure(obj)
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Path string `hcl:"path,optional"`
|
|
|
|
WorkspaceDir string `hcl:"workspace_dir,optional"`
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
if val := obj.GetAttr("path"); !val.IsNull() {
|
|
|
|
p := val.AsString()
|
|
|
|
b.StatePath = p
|
|
|
|
b.StateOutPath = p
|
|
|
|
} else {
|
|
|
|
b.StatePath = DefaultStateFilename
|
|
|
|
b.StateOutPath = DefaultStateFilename
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
|
|
|
|
if val := obj.GetAttr("workspace_dir"); !val.IsNull() {
|
|
|
|
p := val.AsString()
|
|
|
|
b.StateWorkspaceDir = p
|
|
|
|
} else {
|
|
|
|
b.StateWorkspaceDir = DefaultWorkspaceDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Local) State(name string) (state.State, error) {
|
|
|
|
statePath, stateOutPath, backupPath := b.StatePaths(name)
|
|
|
|
|
|
|
|
// If we have a backend handling state, delegate to that.
|
2017-02-22 19:40:04 +01:00
|
|
|
if b.Backend != nil {
|
2018-07-04 12:11:35 +02:00
|
|
|
return b.Backend.State(name)
|
2017-02-22 19:40:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
if s, ok := b.states[name]; ok {
|
|
|
|
return s, nil
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
2018-07-04 12:11:35 +02:00
|
|
|
|
|
|
|
if err := b.createState(name); err != nil {
|
2017-02-27 22:43:31 +01:00
|
|
|
return nil, err
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
// Otherwise, we need to load the state.
|
|
|
|
var s state.State = &state.LocalState{
|
|
|
|
Path: statePath,
|
|
|
|
PathOut: stateOutPath,
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
// If we are backing up the state, wrap it
|
|
|
|
if backupPath != "" {
|
|
|
|
s = &state.BackupState{
|
|
|
|
Real: s,
|
|
|
|
Path: backupPath,
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 01:07:27 +01:00
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
if b.states == nil {
|
|
|
|
b.states = map[string]state.State{}
|
|
|
|
}
|
|
|
|
b.states[name] = s
|
|
|
|
return s, nil
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteState removes a named state.
|
|
|
|
// The "default" state cannot be removed.
|
|
|
|
func (b *Local) DeleteState(name string) error {
|
2017-02-22 19:40:04 +01:00
|
|
|
// If we have a backend handling state, defer to that.
|
|
|
|
if b.Backend != nil {
|
2017-02-27 22:43:31 +01:00
|
|
|
return b.Backend.DeleteState(name)
|
2017-02-22 19:40:04 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:07:27 +01:00
|
|
|
if name == "" {
|
|
|
|
return errors.New("empty state name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return errors.New("cannot delete default state")
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:43:31 +01:00
|
|
|
delete(b.states, name)
|
2017-05-31 02:13:43 +02:00
|
|
|
return os.RemoveAll(filepath.Join(b.stateWorkspaceDir(), name))
|
2017-02-21 16:48:00 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Local) States() ([]string, error) {
|
|
|
|
// If we have a backend handling state, defer to that.
|
2017-02-22 19:40:04 +01:00
|
|
|
if b.Backend != nil {
|
2018-07-04 12:11:35 +02:00
|
|
|
return b.Backend.States()
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
// the listing always start with "default"
|
|
|
|
envs := []string{backend.DefaultStateName}
|
2017-02-22 01:07:27 +01:00
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
entries, err := ioutil.ReadDir(b.stateWorkspaceDir())
|
|
|
|
// no error if there's no envs configured
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return envs, nil
|
2017-02-02 00:16:16 +01:00
|
|
|
}
|
2018-07-04 12:11:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
var listed []string
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
listed = append(listed, filepath.Base(entry.Name()))
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
sort.Strings(listed)
|
|
|
|
envs = append(envs, listed...)
|
|
|
|
|
|
|
|
return envs, nil
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Operation implements backend.Enhanced
|
|
|
|
//
|
|
|
|
// This will initialize an in-memory terraform.Context to perform the
|
|
|
|
// operation within this process.
|
|
|
|
//
|
|
|
|
// The given operation parameter will be merged with the ContextOpts on
|
|
|
|
// the structure with the following rules. If a rule isn't specified and the
|
|
|
|
// name conflicts, assume that the field is overwritten if set.
|
|
|
|
func (b *Local) Operation(ctx context.Context, op *backend.Operation) (*backend.RunningOperation, error) {
|
|
|
|
// Determine the function to call for our operation
|
2018-02-10 00:10:52 +01:00
|
|
|
var f func(context.Context, context.Context, *backend.Operation, *backend.RunningOperation)
|
2017-01-19 05:47:56 +01:00
|
|
|
switch op.Type {
|
|
|
|
case backend.OperationTypeRefresh:
|
|
|
|
f = b.opRefresh
|
|
|
|
case backend.OperationTypePlan:
|
|
|
|
f = b.opPlan
|
|
|
|
case backend.OperationTypeApply:
|
|
|
|
f = b.opApply
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Unsupported operation type: %s\n\n"+
|
|
|
|
"This is a bug in Terraform and should be reported. The local backend\n"+
|
|
|
|
"is built-in to Terraform and should always support all operations.",
|
|
|
|
op.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock
|
|
|
|
b.opLock.Lock()
|
|
|
|
|
|
|
|
// Build our running operation
|
2018-02-10 00:10:52 +01:00
|
|
|
// the runninCtx is only used to block until the operation returns.
|
|
|
|
runningCtx, done := context.WithCancel(context.Background())
|
|
|
|
runningOp := &backend.RunningOperation{
|
|
|
|
Context: runningCtx,
|
|
|
|
}
|
|
|
|
|
|
|
|
// stopCtx wraps the context passed in, and is used to signal a graceful Stop.
|
|
|
|
stopCtx, stop := context.WithCancel(ctx)
|
|
|
|
runningOp.Stop = stop
|
|
|
|
|
|
|
|
// cancelCtx is used to cancel the operation immediately, usually
|
|
|
|
// indicating that the process is exiting.
|
|
|
|
cancelCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
runningOp.Cancel = cancel
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-02-23 02:43:21 +01:00
|
|
|
if op.LockState {
|
|
|
|
op.StateLocker = clistate.NewLocker(stopCtx, op.StateLockTimeout, b.CLI, b.Colorize())
|
|
|
|
} else {
|
|
|
|
op.StateLocker = clistate.NewNoopLocker()
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Do it
|
|
|
|
go func() {
|
2018-02-10 00:10:52 +01:00
|
|
|
defer done()
|
|
|
|
defer stop()
|
|
|
|
defer cancel()
|
|
|
|
|
2018-02-23 02:43:21 +01:00
|
|
|
// the state was locked during context creation, unlock the state when
|
|
|
|
// the operation completes
|
|
|
|
defer func() {
|
2018-03-21 02:43:02 +01:00
|
|
|
err := op.StateLocker.Unlock(nil)
|
|
|
|
if err != nil {
|
|
|
|
b.ShowDiagnostics(err)
|
|
|
|
}
|
|
|
|
runningOp.Result = backend.OperationFailure
|
2018-02-23 02:43:21 +01:00
|
|
|
}()
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
defer b.opLock.Unlock()
|
2018-02-10 00:10:52 +01:00
|
|
|
f(stopCtx, cancelCtx, op, runningOp)
|
2017-01-19 05:47:56 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return runningOp, nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 20:12:33 +02:00
|
|
|
// opWait waits for the operation to complete, and a stop signal or a
|
2018-02-12 17:52:21 +01:00
|
|
|
// cancelation signal.
|
|
|
|
func (b *Local) opWait(
|
|
|
|
doneCh <-chan struct{},
|
|
|
|
stopCtx context.Context,
|
|
|
|
cancelCtx context.Context,
|
|
|
|
tfCtx *terraform.Context,
|
|
|
|
opState state.State) (canceled bool) {
|
|
|
|
// Wait for the operation to finish or for us to be interrupted so
|
|
|
|
// we can handle it properly.
|
|
|
|
select {
|
|
|
|
case <-stopCtx.Done():
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("stopping operation...")
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to force a PersistState just in case the process is terminated
|
|
|
|
// before we can complete.
|
|
|
|
if err := opState.PersistState(); err != nil {
|
|
|
|
// We can't error out from here, but warn the user if there was an error.
|
|
|
|
// If this isn't transient, we will catch it again below, and
|
|
|
|
// attempt to save the state another way.
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Error(fmt.Sprintf(earlyStateWriteErrorFmt, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop execution
|
|
|
|
go tfCtx.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-cancelCtx.Done():
|
|
|
|
log.Println("[WARN] running operation canceled")
|
|
|
|
// if the operation was canceled, we need to return immediately
|
|
|
|
canceled = true
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
case <-cancelCtx.Done():
|
|
|
|
// this should not be called without first attempting to stop the
|
|
|
|
// operation
|
|
|
|
log.Println("[ERROR] running operation canceled without Stop")
|
|
|
|
canceled = true
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// ReportResult is a helper for the common chore of setting the status of
|
|
|
|
// a running operation and showing any diagnostics produced during that
|
|
|
|
// operation.
|
|
|
|
//
|
|
|
|
// If the given diagnostics contains errors then the operation's result
|
|
|
|
// will be set to backend.OperationFailure. It will be set to
|
|
|
|
// backend.OperationSuccess otherwise. It will then use b.ShowDiagnostics
|
|
|
|
// to show the given diagnostics before returning.
|
|
|
|
//
|
|
|
|
// Callers should feel free to do each of these operations separately in
|
|
|
|
// more complex cases where e.g. diagnostics are interleaved with other
|
|
|
|
// output, but terminating immediately after reporting error diagnostics is
|
|
|
|
// common and can be expressed concisely via this method.
|
|
|
|
func (b *Local) ReportResult(op *backend.RunningOperation, diags tfdiags.Diagnostics) {
|
|
|
|
if diags.HasErrors() {
|
|
|
|
op.Result = backend.OperationFailure
|
|
|
|
} else {
|
|
|
|
op.Result = backend.OperationSuccess
|
|
|
|
}
|
|
|
|
b.ShowDiagnostics(diags)
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Colorize returns the Colorize structure that can be used for colorizing
|
|
|
|
// output. This is gauranteed to always return a non-nil value and so is useful
|
|
|
|
// as a helper to wrap any potentially colored strings.
|
|
|
|
func (b *Local) Colorize() *colorstring.Colorize {
|
|
|
|
if b.CLIColor != nil {
|
|
|
|
return b.CLIColor
|
|
|
|
}
|
|
|
|
|
|
|
|
return &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func (b *Local) schemaConfigure(ctx context.Context) error {
|
|
|
|
d := schema.FromContextBackendConfig(ctx)
|
|
|
|
|
|
|
|
// Set the path if it is set
|
|
|
|
pathRaw, ok := d.GetOk("path")
|
|
|
|
if ok {
|
|
|
|
path := pathRaw.(string)
|
|
|
|
if path == "" {
|
|
|
|
return fmt.Errorf("configured path is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StatePath = path
|
|
|
|
b.StateOutPath = path
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw, ok := d.GetOk("workspace_dir"); ok {
|
|
|
|
path := raw.(string)
|
|
|
|
if path != "" {
|
|
|
|
b.StateWorkspaceDir = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy name, which ConflictsWith workspace_dir
|
|
|
|
if raw, ok := d.GetOk("environment_dir"); ok {
|
|
|
|
path := raw.(string)
|
|
|
|
if path != "" {
|
|
|
|
b.StateWorkspaceDir = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-22 01:07:27 +01:00
|
|
|
// StatePaths returns the StatePath, StateOutPath, and StateBackupPath as
|
2017-02-27 22:43:31 +01:00
|
|
|
// configured from the CLI.
|
2017-03-01 01:18:16 +01:00
|
|
|
func (b *Local) StatePaths(name string) (string, string, string) {
|
2017-02-22 01:07:27 +01:00
|
|
|
statePath := b.StatePath
|
|
|
|
stateOutPath := b.StateOutPath
|
|
|
|
backupPath := b.StateBackupPath
|
|
|
|
|
2017-02-27 22:43:31 +01:00
|
|
|
if name == "" {
|
|
|
|
name = backend.DefaultStateName
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
if statePath == "" {
|
2017-02-28 19:13:03 +01:00
|
|
|
statePath = DefaultStateFilename
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
2017-02-27 22:43:31 +01:00
|
|
|
} else {
|
2017-05-31 02:13:43 +02:00
|
|
|
statePath = filepath.Join(b.stateWorkspaceDir(), name, DefaultStateFilename)
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
2017-02-27 22:43:31 +01:00
|
|
|
|
2017-02-22 01:07:27 +01:00
|
|
|
if stateOutPath == "" {
|
|
|
|
stateOutPath = statePath
|
|
|
|
}
|
|
|
|
|
|
|
|
switch backupPath {
|
|
|
|
case "-":
|
|
|
|
backupPath = ""
|
|
|
|
case "":
|
|
|
|
backupPath = stateOutPath + DefaultBackupExtension
|
|
|
|
}
|
|
|
|
|
2017-03-01 01:18:16 +01:00
|
|
|
return statePath, stateOutPath, backupPath
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:43:31 +01:00
|
|
|
// this only ensures that the named directory exists
|
2017-02-22 01:07:27 +01:00
|
|
|
func (b *Local) createState(name string) error {
|
2017-02-27 22:43:31 +01:00
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return nil
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
stateDir := filepath.Join(b.stateWorkspaceDir(), name)
|
2017-02-27 22:43:31 +01:00
|
|
|
s, err := os.Stat(stateDir)
|
|
|
|
if err == nil && s.IsDir() {
|
|
|
|
// no need to check for os.IsNotExist, since that is covered by os.MkdirAll
|
|
|
|
// which will catch the other possible errors as well.
|
|
|
|
return nil
|
2017-02-22 01:07:27 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:43:31 +01:00
|
|
|
err = os.MkdirAll(stateDir, 0755)
|
2017-02-22 01:07:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// stateWorkspaceDir returns the directory where state environments are stored.
|
|
|
|
func (b *Local) stateWorkspaceDir() string {
|
|
|
|
if b.StateWorkspaceDir != "" {
|
|
|
|
return b.StateWorkspaceDir
|
2017-03-09 11:47:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
return DefaultWorkspaceDir
|
2017-03-09 11:47:21 +01:00
|
|
|
}
|
2017-06-22 19:15:30 +02:00
|
|
|
|
|
|
|
func (b *Local) pluginInitRequired(providerErr *terraform.ResourceProviderError) {
|
|
|
|
b.CLI.Output(b.Colorize().Color(fmt.Sprintf(
|
|
|
|
strings.TrimSpace(errPluginInit)+"\n",
|
|
|
|
providerErr)))
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:38:55 +02:00
|
|
|
// this relies on multierror to format the plugin errors below the copy
|
2017-06-22 19:15:30 +02:00
|
|
|
const errPluginInit = `
|
|
|
|
[reset][bold][yellow]Plugin reinitialization required. Please run "terraform init".[reset]
|
2017-06-22 19:38:55 +02:00
|
|
|
[yellow]Reason: Could not satisfy plugin requirements.
|
2017-06-22 19:15:30 +02:00
|
|
|
|
|
|
|
Plugins are external binaries that Terraform uses to access and manipulate
|
2017-06-22 21:11:37 +02:00
|
|
|
resources. The configuration provided requires plugins which can't be located,
|
|
|
|
don't satisfy the version constraints, or are otherwise incompatible.
|
2017-06-22 19:15:30 +02:00
|
|
|
|
2017-06-22 19:38:55 +02:00
|
|
|
[reset][red]%s
|
2017-06-22 21:11:37 +02:00
|
|
|
|
|
|
|
[reset][yellow]Terraform automatically discovers provider requirements from your
|
|
|
|
configuration, including providers used in child modules. To see the
|
|
|
|
requirements and constraints from each module, run "terraform providers".
|
2017-06-22 19:15:30 +02:00
|
|
|
`
|