2014-07-13 05:21:46 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-09-09 05:56:18 +02:00
|
|
|
"bufio"
|
2017-02-28 19:13:03 +01:00
|
|
|
"bytes"
|
2017-03-29 22:45:25 +02:00
|
|
|
"errors"
|
2014-07-18 20:37:27 +02:00
|
|
|
"flag"
|
2014-07-13 05:37:30 +02:00
|
|
|
"fmt"
|
2014-09-09 05:56:18 +02:00
|
|
|
"io"
|
2016-11-22 00:05:49 +01:00
|
|
|
"io/ioutil"
|
2016-11-02 18:30:28 +01:00
|
|
|
"log"
|
2014-07-13 05:37:30 +02:00
|
|
|
"os"
|
2014-09-22 20:15:27 +02:00
|
|
|
"path/filepath"
|
2015-04-30 16:59:14 +02:00
|
|
|
"strconv"
|
2017-01-19 05:50:45 +01:00
|
|
|
"strings"
|
2016-11-22 00:05:49 +01:00
|
|
|
"time"
|
2014-07-13 05:37:30 +02:00
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
"github.com/hashicorp/go-getter"
|
2017-02-28 19:13:03 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/backend/local"
|
2016-10-26 21:46:22 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/experiment"
|
2016-12-10 20:30:40 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/variables"
|
2016-11-14 09:32:01 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/wrappedstreams"
|
2014-07-13 05:37:30 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
2014-07-13 05:21:46 +02:00
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Meta are the meta-options that are available on all or most commands.
|
|
|
|
type Meta struct {
|
2017-01-19 05:50:45 +01:00
|
|
|
// The exported fields below should be set by anyone using a
|
|
|
|
// command with a Meta field. These are expected to be set externally
|
|
|
|
// (not from within the command itself).
|
2014-07-28 07:58:35 +02:00
|
|
|
|
2017-04-14 03:05:58 +02:00
|
|
|
Color bool // True if output should be colored
|
|
|
|
GlobalPluginDirs []string // Additional paths to search for plugins
|
|
|
|
PluginOverrides *PluginOverrides // legacy overrides from .terraformrc file
|
|
|
|
Ui cli.Ui // Ui for output
|
2014-07-13 05:59:16 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// ExtraHooks are extra hooks to add to the context.
|
|
|
|
ExtraHooks []terraform.Hook
|
2014-07-18 00:14:26 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
//----------------------------------------------------------
|
|
|
|
// Protected: commands can set these
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
2017-06-14 21:14:26 +02:00
|
|
|
// Modify the data directory location. This should be accessed through the
|
|
|
|
// DataDir method.
|
2014-10-13 21:05:28 +02:00
|
|
|
dataDir string
|
|
|
|
|
2017-06-15 20:26:12 +02:00
|
|
|
// pluginPath is a user defined set of directories to look for plugins.
|
|
|
|
// This is set during init with the `-plugin-dir` flag, saved to a file in
|
|
|
|
// the data directory.
|
|
|
|
// This overrides all other search paths when discoverying plugins.
|
|
|
|
pluginPath []string
|
|
|
|
|
2017-04-14 03:05:58 +02:00
|
|
|
// Override certain behavior for tests within this package
|
|
|
|
testingOverrides *testingOverrides
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
//----------------------------------------------------------
|
|
|
|
// Private: do not set these
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
|
|
|
// backendState is the currently active backend state
|
|
|
|
backendState *terraform.BackendState
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
// Variables for the context (private)
|
2014-08-25 06:40:58 +02:00
|
|
|
autoKey string
|
core: Allow lists and maps as variable overrides
Terraform 0.7 introduces lists and maps as first-class values for
variables, in addition to string values which were previously available.
However, there was previously no way to override the default value of a
list or map, and the functionality for overriding specific map keys was
broken.
Using the environment variable method for setting variable values, there
was previously no way to give a variable a value of a list or map. These
now support HCL for individual values - specifying:
TF_VAR_test='["Hello", "World"]'
will set the variable `test` to a two-element list containing "Hello"
and "World". Specifying
TF_VAR_test_map='{"Hello = "World", "Foo" = "bar"}'
will set the variable `test_map` to a two-element map with keys "Hello"
and "Foo", and values "World" and "bar" respectively.
The same logic is applied to `-var` flags, and the file parsed by
`-var-files` ("autoVariables").
Note that care must be taken to not run into shell expansion for `-var-`
flags and environment variables.
We also merge map keys where appropriate. The override syntax has
changed (to be noted in CHANGELOG as a breaking change), so several
tests needed their syntax updating from the old `amis.us-east-1 =
"newValue"` style to `amis = "{ "us-east-1" = "newValue"}"` style as
defined in TF-002.
In order to continue supporting the `-var "foo=bar"` type of variable
flag (which is not valid HCL), a special case error is checked after HCL
parsing fails, and the old code path runs instead.
2016-07-21 03:38:26 +02:00
|
|
|
autoVariables map[string]interface{}
|
2014-09-29 20:11:35 +02:00
|
|
|
input bool
|
core: Allow lists and maps as variable overrides
Terraform 0.7 introduces lists and maps as first-class values for
variables, in addition to string values which were previously available.
However, there was previously no way to override the default value of a
list or map, and the functionality for overriding specific map keys was
broken.
Using the environment variable method for setting variable values, there
was previously no way to give a variable a value of a list or map. These
now support HCL for individual values - specifying:
TF_VAR_test='["Hello", "World"]'
will set the variable `test` to a two-element list containing "Hello"
and "World". Specifying
TF_VAR_test_map='{"Hello = "World", "Foo" = "bar"}'
will set the variable `test_map` to a two-element map with keys "Hello"
and "Foo", and values "World" and "bar" respectively.
The same logic is applied to `-var` flags, and the file parsed by
`-var-files` ("autoVariables").
Note that care must be taken to not run into shell expansion for `-var-`
flags and environment variables.
We also merge map keys where appropriate. The override syntax has
changed (to be noted in CHANGELOG as a breaking change), so several
tests needed their syntax updating from the old `amis.us-east-1 =
"newValue"` style to `amis = "{ "us-east-1" = "newValue"}"` style as
defined in TF-002.
In order to continue supporting the `-var "foo=bar"` type of variable
flag (which is not valid HCL), a special case error is checked after HCL
parsing fails, and the old code path runs instead.
2016-07-21 03:38:26 +02:00
|
|
|
variables map[string]interface{}
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
// Targets for this context (private)
|
|
|
|
targets []string
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Internal fields
|
2014-07-17 18:34:32 +02:00
|
|
|
color bool
|
2014-07-13 05:59:16 +02:00
|
|
|
oldUi cli.Ui
|
2014-10-12 03:05:23 +02:00
|
|
|
|
2015-02-22 03:17:40 +01:00
|
|
|
// The fields below are expected to be set by the command via
|
|
|
|
// command line flags. See the Apply command for an example.
|
|
|
|
//
|
2014-10-12 03:05:23 +02:00
|
|
|
// statePath is the path to the state file. If this is empty, then
|
|
|
|
// no state will be loaded. It is also okay for this to be a path to
|
|
|
|
// a file that doesn't exist; it is assumed that this means that there
|
|
|
|
// is simply no state.
|
2015-02-22 03:17:40 +01:00
|
|
|
//
|
2014-10-12 03:05:23 +02:00
|
|
|
// stateOutPath is used to override the output path for the state.
|
|
|
|
// If not provided, the StatePath is used causing the old state to
|
|
|
|
// be overriden.
|
2015-02-22 03:17:40 +01:00
|
|
|
//
|
2014-10-12 03:05:23 +02:00
|
|
|
// backupPath is used to backup the state file before writing a modified
|
2015-09-11 20:56:20 +02:00
|
|
|
// version. It defaults to stateOutPath + DefaultBackupExtension
|
2015-05-06 17:58:42 +02:00
|
|
|
//
|
|
|
|
// parallelism is used to control the number of concurrent operations
|
|
|
|
// allowed when walking the graph
|
2016-10-21 23:25:05 +02:00
|
|
|
//
|
|
|
|
// shadow is used to enable/disable the shadow graph
|
2016-11-23 10:44:52 +01:00
|
|
|
//
|
|
|
|
// provider is to specify specific resource providers
|
2017-02-02 00:16:16 +01:00
|
|
|
//
|
2017-03-21 20:05:51 +01:00
|
|
|
// stateLock is set to false to disable state locking
|
|
|
|
//
|
2017-04-01 21:42:13 +02:00
|
|
|
// stateLockTimeout is the optional duration to retry a state locks locks
|
|
|
|
// when it is already locked by another process.
|
|
|
|
//
|
2017-03-21 20:05:51 +01:00
|
|
|
// forceInitCopy suppresses confirmation for copying state data during
|
|
|
|
// init.
|
2017-04-20 23:26:50 +02:00
|
|
|
//
|
|
|
|
// reconfigure forces init to ignore any stored configuration.
|
2017-04-01 21:42:13 +02:00
|
|
|
statePath string
|
|
|
|
stateOutPath string
|
|
|
|
backupPath string
|
|
|
|
parallelism int
|
|
|
|
shadow bool
|
|
|
|
provider string
|
|
|
|
stateLock bool
|
|
|
|
stateLockTimeout time.Duration
|
|
|
|
forceInitCopy bool
|
2017-04-20 23:26:50 +02:00
|
|
|
reconfigure bool
|
2017-06-12 19:30:19 +02:00
|
|
|
|
|
|
|
// errWriter is the write side of a pipe for the FlagSet output. We need to
|
|
|
|
// keep track of this to close previous pipes between tests. Normal
|
|
|
|
// operation never needs to close this.
|
|
|
|
errWriter *io.PipeWriter
|
|
|
|
// done chan to wait for the scanner goroutine
|
|
|
|
errScannerDone chan struct{}
|
2014-10-12 03:05:23 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:05:58 +02:00
|
|
|
type PluginOverrides struct {
|
|
|
|
Providers map[string]string
|
|
|
|
Provisioners map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testingOverrides struct {
|
2017-04-22 03:06:30 +02:00
|
|
|
ProviderResolver terraform.ResourceProviderResolver
|
|
|
|
Provisioners map[string]terraform.ResourceProvisionerFactory
|
2017-04-14 03:05:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 03:05:23 +02:00
|
|
|
// initStatePaths is used to initialize the default values for
|
|
|
|
// statePath, stateOutPath, and backupPath
|
|
|
|
func (m *Meta) initStatePaths() {
|
|
|
|
if m.statePath == "" {
|
|
|
|
m.statePath = DefaultStateFilename
|
|
|
|
}
|
|
|
|
if m.stateOutPath == "" {
|
|
|
|
m.stateOutPath = m.statePath
|
|
|
|
}
|
|
|
|
if m.backupPath == "" {
|
2015-09-11 20:56:20 +02:00
|
|
|
m.backupPath = m.stateOutPath + DefaultBackupExtension
|
2014-10-12 03:05:23 +02:00
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 03:34:11 +02:00
|
|
|
// StateOutPath returns the true output path for the state file
|
|
|
|
func (m *Meta) StateOutPath() string {
|
|
|
|
return m.stateOutPath
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// Colorize returns the colorization structure for a command.
|
|
|
|
func (m *Meta) Colorize() *colorstring.Colorize {
|
|
|
|
return &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
2014-07-17 18:34:32 +02:00
|
|
|
Disable: !m.color,
|
2014-07-13 05:21:46 +02:00
|
|
|
Reset: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 05:52:06 +01:00
|
|
|
// DataDir returns the directory where local data will be stored.
|
2017-06-14 21:14:26 +02:00
|
|
|
// Defaults to DefaultsDataDir in the current working directory.
|
2015-03-05 05:52:06 +01:00
|
|
|
func (m *Meta) DataDir() string {
|
2016-07-21 00:55:05 +02:00
|
|
|
dataDir := DefaultDataDir
|
2015-03-05 05:52:06 +01:00
|
|
|
if m.dataDir != "" {
|
|
|
|
dataDir = m.dataDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataDir
|
|
|
|
}
|
|
|
|
|
2015-04-30 16:59:14 +02:00
|
|
|
const (
|
|
|
|
// InputModeEnvVar is the environment variable that, if set to "false" or
|
|
|
|
// "0", causes terraform commands to behave as if the `-input=false` flag was
|
|
|
|
// specified.
|
|
|
|
InputModeEnvVar = "TF_INPUT"
|
|
|
|
)
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
// InputMode returns the type of input we should ask for in the form of
|
|
|
|
// terraform.InputMode which is passed directly to Context.Input.
|
|
|
|
func (m *Meta) InputMode() terraform.InputMode {
|
|
|
|
if test || !m.input {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-04-30 16:59:14 +02:00
|
|
|
if envVar := os.Getenv(InputModeEnvVar); envVar != "" {
|
|
|
|
if v, err := strconv.ParseBool(envVar); err == nil {
|
|
|
|
if !v {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
var mode terraform.InputMode
|
|
|
|
mode |= terraform.InputModeProvider
|
2016-11-02 03:16:43 +01:00
|
|
|
mode |= terraform.InputModeVar
|
|
|
|
mode |= terraform.InputModeVarUnset
|
2014-10-08 19:29:54 +02:00
|
|
|
|
|
|
|
return mode
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 01:04:32 +01:00
|
|
|
// UIInput returns a UIInput object to be used for asking for input.
|
|
|
|
func (m *Meta) UIInput() terraform.UIInput {
|
|
|
|
return &UIInput{
|
|
|
|
Colorize: m.Colorize(),
|
2014-10-12 03:21:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 07:18:18 +01:00
|
|
|
// StdinPiped returns true if the input is piped.
|
|
|
|
func (m *Meta) StdinPiped() bool {
|
2016-11-14 09:32:01 +01:00
|
|
|
fi, err := wrappedstreams.Stdin().Stat()
|
2016-11-14 07:18:18 +01:00
|
|
|
if err != nil {
|
|
|
|
// If there is an error, let's just say its not piped
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.Mode()&os.ModeNamedPipe != 0
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:37:30 +02:00
|
|
|
// contextOpts returns the options to use to initialize a Terraform
|
|
|
|
// context with the settings from this Meta.
|
|
|
|
func (m *Meta) contextOpts() *terraform.ContextOpts {
|
2017-01-19 05:50:45 +01:00
|
|
|
var opts terraform.ContextOpts
|
2016-11-04 16:30:51 +01:00
|
|
|
opts.Hooks = []terraform.Hook{m.uiHook(), &terraform.DebugHook{}}
|
2017-01-19 05:50:45 +01:00
|
|
|
opts.Hooks = append(opts.Hooks, m.ExtraHooks...)
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2016-07-18 19:52:10 +02:00
|
|
|
vs := make(map[string]interface{})
|
2014-08-25 06:40:58 +02:00
|
|
|
for k, v := range opts.Variables {
|
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range m.autoVariables {
|
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range m.variables {
|
|
|
|
vs[k] = v
|
2014-07-18 20:37:27 +02:00
|
|
|
}
|
2014-08-25 06:40:58 +02:00
|
|
|
opts.Variables = vs
|
2017-03-14 00:25:27 +01:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
opts.Targets = m.targets
|
2014-10-01 07:01:11 +02:00
|
|
|
opts.UIInput = m.UIInput()
|
2017-01-19 05:50:45 +01:00
|
|
|
opts.Parallelism = m.parallelism
|
2016-10-21 23:25:05 +02:00
|
|
|
opts.Shadow = m.shadow
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2017-04-14 03:05:58 +02:00
|
|
|
// If testingOverrides are set, we'll skip the plugin discovery process
|
|
|
|
// and just work with what we've been given, thus allowing the tests
|
|
|
|
// to provide mock providers and provisioners.
|
|
|
|
if m.testingOverrides != nil {
|
2017-04-22 03:06:30 +02:00
|
|
|
opts.ProviderResolver = m.testingOverrides.ProviderResolver
|
2017-04-14 03:05:58 +02:00
|
|
|
opts.Provisioners = m.testingOverrides.Provisioners
|
|
|
|
} else {
|
2017-04-22 03:06:30 +02:00
|
|
|
opts.ProviderResolver = m.providerResolver()
|
2017-04-14 03:05:58 +02:00
|
|
|
opts.Provisioners = m.provisionerFactories()
|
|
|
|
}
|
|
|
|
|
2017-06-06 01:37:08 +02:00
|
|
|
opts.ProviderSHA256s = m.providerPluginsLock().Read()
|
2017-05-25 01:36:19 +02:00
|
|
|
|
2017-03-14 00:25:27 +01:00
|
|
|
opts.Meta = &terraform.ContextMeta{
|
2017-05-31 02:13:43 +02:00
|
|
|
Env: m.Workspace(),
|
2017-03-14 00:25:27 +01:00
|
|
|
}
|
|
|
|
|
2014-07-13 05:37:30 +02:00
|
|
|
return &opts
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
// flags adds the meta flags to the given FlagSet.
|
|
|
|
func (m *Meta) flagSet(n string) *flag.FlagSet {
|
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
2014-09-29 20:11:35 +02:00
|
|
|
f.BoolVar(&m.input, "input", true, "input")
|
2016-12-10 20:30:40 +01:00
|
|
|
f.Var((*variables.Flag)(&m.variables), "var", "variables")
|
|
|
|
f.Var((*variables.FlagFile)(&m.variables), "var-file", "variable file")
|
2015-03-24 17:18:15 +01:00
|
|
|
f.Var((*FlagStringSlice)(&m.targets), "target", "resource to target")
|
2014-08-25 06:40:58 +02:00
|
|
|
|
|
|
|
if m.autoKey != "" {
|
2016-12-10 20:30:40 +01:00
|
|
|
f.Var((*variables.FlagFile)(&m.autoVariables), m.autoKey, "variable file")
|
2014-08-25 06:40:58 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 23:25:05 +02:00
|
|
|
// Advanced (don't need documentation, or unlikely to be set)
|
|
|
|
f.BoolVar(&m.shadow, "shadow", true, "shadow graph")
|
|
|
|
|
2016-10-16 00:08:11 +02:00
|
|
|
// Experimental features
|
2016-10-26 21:46:22 +02:00
|
|
|
experiment.Flag(f)
|
2016-10-16 00:08:11 +02:00
|
|
|
|
2014-09-09 05:56:18 +02:00
|
|
|
// Create an io.Writer that writes to our Ui properly for errors.
|
|
|
|
// This is kind of a hack, but it does the job. Basically: create
|
|
|
|
// a pipe, use a scanner to break it into lines, and output each line
|
|
|
|
// to the UI. Do this forever.
|
2017-06-12 19:30:19 +02:00
|
|
|
|
|
|
|
// If a previous pipe exists, we need to close that first.
|
|
|
|
// This should only happen in testing.
|
|
|
|
if m.errWriter != nil {
|
|
|
|
m.errWriter.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.errScannerDone != nil {
|
|
|
|
<-m.errScannerDone
|
|
|
|
}
|
|
|
|
|
2014-09-09 05:56:18 +02:00
|
|
|
errR, errW := io.Pipe()
|
|
|
|
errScanner := bufio.NewScanner(errR)
|
2017-06-12 19:30:19 +02:00
|
|
|
m.errWriter = errW
|
|
|
|
m.errScannerDone = make(chan struct{})
|
2014-09-09 05:56:18 +02:00
|
|
|
go func() {
|
2017-06-12 19:30:19 +02:00
|
|
|
defer close(m.errScannerDone)
|
2014-09-09 05:56:18 +02:00
|
|
|
for errScanner.Scan() {
|
|
|
|
m.Ui.Error(errScanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetOutput(errW)
|
|
|
|
|
2016-03-22 18:41:02 +01:00
|
|
|
// Set the default Usage to empty
|
|
|
|
f.Usage = func() {}
|
|
|
|
|
2017-03-30 19:54:27 +02:00
|
|
|
// command that bypass locking will supply their own flag on this var, but
|
|
|
|
// set the initial meta value to true as a failsafe.
|
|
|
|
m.stateLock = true
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2014-09-22 20:15:27 +02:00
|
|
|
// moduleStorage returns the module.Storage implementation used to store
|
|
|
|
// modules for commands.
|
2015-10-15 22:48:58 +02:00
|
|
|
func (m *Meta) moduleStorage(root string) getter.Storage {
|
2014-09-22 20:15:27 +02:00
|
|
|
return &uiModuleStorage{
|
2015-10-15 22:48:58 +02:00
|
|
|
Storage: &getter.FolderStorage{
|
2014-09-22 20:15:27 +02:00
|
|
|
StorageDir: filepath.Join(root, "modules"),
|
|
|
|
},
|
|
|
|
Ui: m.Ui,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// process will process the meta-parameters out of the arguments. This
|
|
|
|
// will potentially modify the args in-place. It will return the resulting
|
|
|
|
// slice.
|
2014-10-01 17:37:57 +02:00
|
|
|
//
|
|
|
|
// vars says whether or not we support variables.
|
2014-08-05 18:32:01 +02:00
|
|
|
func (m *Meta) process(args []string, vars bool) []string {
|
2014-07-13 05:59:16 +02:00
|
|
|
// We do this so that we retain the ability to technically call
|
|
|
|
// process multiple times, even if we have no plans to do so
|
|
|
|
if m.oldUi != nil {
|
|
|
|
m.Ui = m.oldUi
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-07-13 05:59:16 +02:00
|
|
|
// Set colorization
|
2014-07-17 18:34:32 +02:00
|
|
|
m.color = m.Color
|
2014-07-13 05:21:46 +02:00
|
|
|
for i, v := range args {
|
|
|
|
if v == "-no-color" {
|
2014-09-09 05:41:10 +02:00
|
|
|
m.color = false
|
2015-06-21 22:51:40 +02:00
|
|
|
m.Color = false
|
2014-07-13 05:59:16 +02:00
|
|
|
args = append(args[:i], args[i+1:]...)
|
|
|
|
break
|
2014-07-13 05:21:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:59:16 +02:00
|
|
|
// Set the UI
|
|
|
|
m.oldUi = m.Ui
|
2014-08-19 19:22:26 +02:00
|
|
|
m.Ui = &cli.ConcurrentUi{
|
|
|
|
Ui: &ColorizeUi{
|
|
|
|
Colorize: m.Colorize(),
|
|
|
|
ErrorColor: "[red]",
|
2015-03-05 21:22:34 +01:00
|
|
|
WarnColor: "[yellow]",
|
2014-08-19 19:22:26 +02:00
|
|
|
Ui: m.oldUi,
|
|
|
|
},
|
2014-07-13 05:59:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
// If we support vars and the default var file exists, add it to
|
|
|
|
// the args...
|
2014-08-25 06:40:58 +02:00
|
|
|
m.autoKey = ""
|
2014-08-05 18:32:01 +02:00
|
|
|
if vars {
|
|
|
|
if _, err := os.Stat(DefaultVarsFilename); err == nil {
|
2014-09-09 05:57:08 +02:00
|
|
|
m.autoKey = "var-file-default"
|
2014-08-05 18:32:01 +02:00
|
|
|
args = append(args, "", "")
|
|
|
|
copy(args[2:], args[0:])
|
2014-08-25 06:40:58 +02:00
|
|
|
args[0] = "-" + m.autoKey
|
2014-08-05 18:32:01 +02:00
|
|
|
args[1] = DefaultVarsFilename
|
|
|
|
}
|
2015-03-02 18:21:45 +01:00
|
|
|
|
|
|
|
if _, err := os.Stat(DefaultVarsFilename + ".json"); err == nil {
|
|
|
|
m.autoKey = "var-file-default"
|
|
|
|
args = append(args, "", "")
|
|
|
|
copy(args[2:], args[0:])
|
|
|
|
args[0] = "-" + m.autoKey
|
|
|
|
args[1] = DefaultVarsFilename + ".json"
|
|
|
|
}
|
2014-08-05 18:32:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
return args
|
|
|
|
}
|
2014-07-13 05:37:30 +02:00
|
|
|
|
|
|
|
// uiHook returns the UiHook to use with the context.
|
|
|
|
func (m *Meta) uiHook() *UiHook {
|
|
|
|
return &UiHook{
|
|
|
|
Colorize: m.Colorize(),
|
|
|
|
Ui: m.Ui,
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 19:56:50 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// confirm asks a yes/no confirmation.
|
|
|
|
func (m *Meta) confirm(opts *terraform.InputOpts) (bool, error) {
|
2017-06-24 01:11:30 +02:00
|
|
|
if !m.Input() {
|
|
|
|
return false, errors.New("input is disabled")
|
2017-03-29 22:45:25 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
for {
|
|
|
|
v, err := m.UIInput().Input(opts)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf(
|
|
|
|
"Error asking for confirmation: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(v) {
|
|
|
|
case "no":
|
|
|
|
return false, nil
|
|
|
|
case "yes":
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 21:05:39 +02:00
|
|
|
const (
|
2016-01-20 19:50:33 +01:00
|
|
|
// ModuleDepthDefault is the default value for
|
|
|
|
// module depth, which can be overridden by flag
|
|
|
|
// or env var
|
|
|
|
ModuleDepthDefault = -1
|
|
|
|
|
|
|
|
// ModuleDepthEnvVar is the name of the environment variable that can be used to set module depth.
|
2015-04-30 21:05:39 +02:00
|
|
|
ModuleDepthEnvVar = "TF_MODULE_DEPTH"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Meta) addModuleDepthFlag(flags *flag.FlagSet, moduleDepth *int) {
|
2016-01-20 19:50:33 +01:00
|
|
|
flags.IntVar(moduleDepth, "module-depth", ModuleDepthDefault, "module-depth")
|
2015-04-30 21:05:39 +02:00
|
|
|
if envVar := os.Getenv(ModuleDepthEnvVar); envVar != "" {
|
|
|
|
if md, err := strconv.Atoi(envVar); err == nil {
|
|
|
|
*moduleDepth = md
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:46:26 +01:00
|
|
|
// outputShadowError outputs the error from ctx.ShadowError. If the
|
|
|
|
// error is nil then nothing happens. If output is false then it isn't
|
|
|
|
// outputted to the user (you can define logic to guard against outputting).
|
|
|
|
func (m *Meta) outputShadowError(err error, output bool) bool {
|
|
|
|
// Do nothing if no error
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not outputting, do nothing
|
|
|
|
if !output {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-22 00:05:49 +01:00
|
|
|
// Write the shadow error output to a file
|
|
|
|
path := fmt.Sprintf("terraform-error-%d.log", time.Now().UTC().Unix())
|
|
|
|
if err := ioutil.WriteFile(path, []byte(err.Error()), 0644); err != nil {
|
|
|
|
// If there is an error writing it, just let it go
|
|
|
|
log.Printf("[ERROR] Error writing shadow error: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:46:26 +01:00
|
|
|
// Output!
|
|
|
|
m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold][yellow]\nExperimental feature failure! Please report a bug.\n\n"+
|
|
|
|
"This is not an error. Your Terraform operation completed successfully.\n"+
|
|
|
|
"Your real infrastructure is unaffected by this message.\n\n"+
|
|
|
|
"[reset][yellow]While running, Terraform sometimes tests experimental features in the\n"+
|
|
|
|
"background. These features cannot affect real state and never touch\n"+
|
|
|
|
"real infrastructure. If the features work properly, you see nothing.\n"+
|
|
|
|
"If the features fail, this message appears.\n\n"+
|
|
|
|
"You can report an issue at: https://github.com/hashicorp/terraform/issues\n\n"+
|
2016-11-22 00:05:49 +01:00
|
|
|
"The failure was written to %q. Please\n"+
|
|
|
|
"double check this file contains no sensitive information and report\n"+
|
|
|
|
"it with your issue.\n\n"+
|
2016-11-03 22:46:26 +01:00
|
|
|
"This is not an error. Your terraform operation completed successfully\n"+
|
|
|
|
"and your real infrastructure is unaffected by this message.",
|
2016-11-22 00:05:49 +01:00
|
|
|
path,
|
2016-11-03 22:46:26 +01:00
|
|
|
)))
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-02-28 19:13:03 +01:00
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// WorkspaceNameEnvVar is the name of the environment variable that can be used
|
|
|
|
// to set the name of the Terraform workspace, overriding the workspace chosen
|
|
|
|
// by `terraform workspace select`.
|
|
|
|
//
|
|
|
|
// Note that this environment variable is ignored by `terraform workspace new`
|
|
|
|
// and `terraform workspace delete`.
|
|
|
|
const WorkspaceNameEnvVar = "TF_WORKSPACE"
|
2017-05-12 22:53:29 +02:00
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// Workspace returns the name of the currently configured workspace, corresponding
|
2017-02-28 19:13:03 +01:00
|
|
|
// to the desired named state.
|
2017-05-31 02:13:43 +02:00
|
|
|
func (m *Meta) Workspace() string {
|
|
|
|
current, _ := m.WorkspaceOverridden()
|
2017-05-12 22:53:29 +02:00
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// WorkspaceOverridden returns the name of the currently configured workspace,
|
2017-05-12 22:53:29 +02:00
|
|
|
// corresponding to the desired named state, as well as a bool saying whether
|
2017-05-31 00:06:13 +02:00
|
|
|
// this was set via the TF_WORKSPACE environment variable.
|
2017-05-31 02:13:43 +02:00
|
|
|
func (m *Meta) WorkspaceOverridden() (string, bool) {
|
|
|
|
if envVar := os.Getenv(WorkspaceNameEnvVar); envVar != "" {
|
2017-05-12 22:53:29 +02:00
|
|
|
return envVar, true
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:14:26 +02:00
|
|
|
envData, err := ioutil.ReadFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile))
|
2017-02-28 19:13:03 +01:00
|
|
|
current := string(bytes.TrimSpace(envData))
|
|
|
|
if current == "" {
|
|
|
|
current = backend.DefaultStateName
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2017-05-31 00:06:13 +02:00
|
|
|
// always return the default if we can't get a workspace name
|
|
|
|
log.Printf("[ERROR] failed to read current workspace: %s", err)
|
2017-02-28 19:13:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-12 22:53:29 +02:00
|
|
|
return current, false
|
2017-02-28 19:13:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:13:43 +02:00
|
|
|
// SetWorkspace saves the given name as the current workspace in the local
|
2017-05-31 00:06:13 +02:00
|
|
|
// filesystem.
|
2017-05-31 02:13:43 +02:00
|
|
|
func (m *Meta) SetWorkspace(name string) error {
|
2017-06-14 21:14:26 +02:00
|
|
|
err := os.MkdirAll(m.DataDir(), 0755)
|
2017-02-28 19:13:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-14 21:14:26 +02:00
|
|
|
err = ioutil.WriteFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile), []byte(name), 0644)
|
2017-02-28 19:13:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|