2014-05-24 21:27:58 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-07-13 18:34:35 +02:00
|
|
|
"bytes"
|
2017-01-19 05:50:45 +01:00
|
|
|
"context"
|
2014-06-19 01:42:13 +02:00
|
|
|
"fmt"
|
2014-06-19 06:36:44 +02:00
|
|
|
"os"
|
2014-07-13 18:34:35 +02:00
|
|
|
"sort"
|
2014-05-24 21:27:58 +02:00
|
|
|
"strings"
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
2015-10-15 22:48:58 +02:00
|
|
|
"github.com/hashicorp/go-getter"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2016-05-09 21:46:07 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2017-01-19 05:50:45 +01:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2014-06-19 01:42:13 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-05-24 21:27:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyCommand is a Command implementation that applies a Terraform
|
|
|
|
// configuration and actually builds or changes infrastructure.
|
|
|
|
type ApplyCommand struct {
|
2014-07-13 05:21:46 +02:00
|
|
|
Meta
|
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
// If true, then this apply command will become the "destroy"
|
|
|
|
// command. It is just like apply but only processes a destroy.
|
|
|
|
Destroy bool
|
2014-05-24 21:27:58 +02:00
|
|
|
}
|
|
|
|
|
2014-06-19 01:42:13 +02:00
|
|
|
func (c *ApplyCommand) Run(args []string) int {
|
2016-06-21 03:06:28 +02:00
|
|
|
var destroyForce, refresh, autoApprove bool
|
2017-03-08 05:09:48 +01:00
|
|
|
args, err := c.Meta.process(args, true)
|
|
|
|
if err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
cmdName := "apply"
|
|
|
|
if c.Destroy {
|
|
|
|
cmdName = "destroy"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdFlags := c.Meta.flagSet(cmdName)
|
2014-10-04 01:08:50 +02:00
|
|
|
if c.Destroy {
|
|
|
|
cmdFlags.BoolVar(&destroyForce, "force", false, "force")
|
|
|
|
}
|
2014-07-27 02:50:13 +02:00
|
|
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
2016-06-21 03:06:28 +02:00
|
|
|
if !c.Destroy {
|
2017-10-30 21:33:27 +01:00
|
|
|
cmdFlags.BoolVar(&autoApprove, "auto-approve", false, "skip interactive approval of plan before applying")
|
2016-06-21 03:06:28 +02:00
|
|
|
}
|
2015-10-05 22:06:08 +02:00
|
|
|
cmdFlags.IntVar(
|
|
|
|
&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
|
2017-01-19 05:50:45 +01:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
|
2014-10-12 03:05:23 +02:00
|
|
|
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
|
|
|
|
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
|
2017-02-06 16:07:32 +01:00
|
|
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
2017-04-01 22:19:59 +02:00
|
|
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
2014-06-19 01:42:13 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Get the args. The "maybeInit" flag tracks whether we may need to
|
|
|
|
// initialize the configuration from a remote path. This is true as long
|
|
|
|
// as we have an argument.
|
2014-06-19 01:42:13 +02:00
|
|
|
args = cmdFlags.Args()
|
2017-01-19 05:50:45 +01:00
|
|
|
maybeInit := len(args) == 1
|
|
|
|
configPath, err := ModulePath(args)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2014-06-19 01:42:13 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:26:12 +02:00
|
|
|
// Check for user-supplied plugin path
|
|
|
|
if c.pluginPath, err = c.loadPluginPath(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:22:20 +01:00
|
|
|
if !c.Destroy && maybeInit {
|
2017-01-19 05:50:45 +01:00
|
|
|
// We need the pwd for the getter operation below
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
// Do a detect to determine if we need to do an init + apply.
|
2015-10-15 22:48:58 +02:00
|
|
|
if detected, err := getter.Detect(configPath, pwd, getter.Detectors); err != nil {
|
2014-10-01 06:49:24 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Invalid path: %s", err))
|
|
|
|
return 1
|
|
|
|
} else if !strings.HasPrefix(detected, "file") {
|
|
|
|
// If this isn't a file URL then we're doing an init +
|
|
|
|
// apply.
|
|
|
|
var init InitCommand
|
|
|
|
init.Meta = c.Meta
|
|
|
|
if code := init.Run([]string{detected}); code != 0 {
|
|
|
|
return code
|
|
|
|
}
|
2014-09-30 00:55:28 +02:00
|
|
|
|
2014-10-01 06:49:24 +02:00
|
|
|
// Change the config path to be the cwd
|
|
|
|
configPath = pwd
|
|
|
|
}
|
2014-09-30 00:55:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Check if the path is a plan
|
|
|
|
plan, err := c.Plan(configPath)
|
2014-07-01 19:02:13 +02:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
2014-06-19 01:42:13 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
if c.Destroy && plan != nil {
|
2014-10-01 06:49:24 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Destroy can't be called with a plan file."))
|
|
|
|
return 1
|
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
if plan != nil {
|
|
|
|
// Reset the config path for backend loading
|
|
|
|
configPath = ""
|
|
|
|
}
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the module if we don't have one yet (not running from plan)
|
|
|
|
var mod *module.Tree
|
|
|
|
if plan == nil {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
var modDiags tfdiags.Diagnostics
|
|
|
|
mod, modDiags = c.Module(configPath)
|
|
|
|
diags = diags.Append(modDiags)
|
|
|
|
if modDiags.HasErrors() {
|
|
|
|
c.showDiagnostics(diags)
|
2017-01-19 05:50:45 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:47:53 +02:00
|
|
|
var conf *config.Config
|
|
|
|
if mod != nil {
|
|
|
|
conf = mod.Config()
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Load the backend
|
|
|
|
b, err := c.Backend(&BackendOpts{
|
2017-05-01 23:47:53 +02:00
|
|
|
Config: conf,
|
|
|
|
Plan: plan,
|
2017-01-19 05:50:45 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the operation
|
|
|
|
opReq := c.Operation()
|
|
|
|
opReq.Destroy = c.Destroy
|
|
|
|
opReq.Module = mod
|
|
|
|
opReq.Plan = plan
|
|
|
|
opReq.PlanRefresh = refresh
|
|
|
|
opReq.Type = backend.OperationTypeApply
|
2016-06-21 03:06:28 +02:00
|
|
|
opReq.AutoApprove = autoApprove
|
2017-06-13 03:30:57 +02:00
|
|
|
opReq.DestroyForce = destroyForce
|
2017-01-19 05:50:45 +01:00
|
|
|
|
|
|
|
// Perform the operation
|
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
defer ctxCancel()
|
2017-12-03 04:36:43 +01:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
op, err := b.Operation(ctx, opReq)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error starting operation: %s", err))
|
2015-01-16 19:46:38 +01:00
|
|
|
return 1
|
|
|
|
}
|
2014-06-19 01:42:13 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Wait for the operation to complete or an interrupt to occur
|
2014-07-03 02:01:02 +02:00
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
2017-01-19 05:50:45 +01:00
|
|
|
// Cancel our context so we can start gracefully exiting
|
|
|
|
ctxCancel()
|
2014-07-03 02:01:02 +02:00
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Notify the user
|
2017-04-25 17:43:59 +02:00
|
|
|
c.Ui.Output(outputInterrupt)
|
2014-07-03 02:01:02 +02:00
|
|
|
|
|
|
|
// Still get the result, since there is still one
|
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
c.Ui.Error(
|
|
|
|
"Two interrupts received. Exiting immediately. Note that data\n" +
|
|
|
|
"loss may have occurred.")
|
|
|
|
return 1
|
2017-01-19 05:50:45 +01:00
|
|
|
case <-op.Done():
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
2017-01-19 05:50:45 +01:00
|
|
|
case <-op.Done():
|
|
|
|
if err := op.Err; err != nil {
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
diags = diags.Append(err)
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
2014-06-19 01:42:13 +02:00
|
|
|
}
|
|
|
|
|
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-07 01:41:48 +01:00
|
|
|
c.showDiagnostics(diags)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-06-15 17:40:56 +02:00
|
|
|
if !c.Destroy {
|
2017-01-19 05:50:45 +01:00
|
|
|
// Get the right module that we used. If we ran a plan, then use
|
|
|
|
// that module.
|
|
|
|
if plan != nil {
|
|
|
|
mod = plan.Module
|
|
|
|
}
|
|
|
|
|
|
|
|
if outputs := outputsAsString(op.State, terraform.RootModulePath, mod.Config().Outputs, true); outputs != "" {
|
2015-06-15 17:40:56 +02:00
|
|
|
c.Ui.Output(c.Colorize().Color(outputs))
|
2014-07-13 18:34:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 21:27:58 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) Help() string {
|
2014-10-01 07:01:11 +02:00
|
|
|
if c.Destroy {
|
|
|
|
return c.helpDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.helpApply()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) Synopsis() string {
|
|
|
|
if c.Destroy {
|
|
|
|
return "Destroy Terraform-managed infrastructure"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Builds or changes infrastructure"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) helpApply() string {
|
2014-05-24 21:27:58 +02:00
|
|
|
helpText := `
|
2016-04-11 19:24:08 +02:00
|
|
|
Usage: terraform apply [options] [DIR-OR-PLAN]
|
2014-05-24 21:27:58 +02:00
|
|
|
|
2014-07-12 06:30:40 +02:00
|
|
|
Builds or changes infrastructure according to Terraform configuration
|
2014-09-30 00:57:35 +02:00
|
|
|
files in DIR.
|
|
|
|
|
2016-04-11 19:24:08 +02:00
|
|
|
By default, apply scans the current directory for the configuration
|
|
|
|
and applies the changes appropriately. However, a path to another
|
|
|
|
configuration or an execution plan can be provided. Execution plans can be
|
|
|
|
used to only execute a pre-determined set of actions.
|
|
|
|
|
2014-05-24 21:27:58 +02:00
|
|
|
Options:
|
|
|
|
|
2014-07-28 00:09:04 +02:00
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
2014-08-07 09:19:56 +02:00
|
|
|
".backup" extension. Set to "-" to disable backup.
|
2014-07-28 00:09:04 +02:00
|
|
|
|
2017-02-06 16:07:32 +01:00
|
|
|
-lock=true Lock the state file when locking is supported.
|
2017-02-03 20:15:08 +01:00
|
|
|
|
2017-04-01 22:19:59 +02:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2017-10-30 21:33:27 +01:00
|
|
|
-auto-approve Skip interactive approval of plan before applying.
|
2016-06-21 03:06:28 +02:00
|
|
|
|
2014-09-29 21:46:58 +02:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2017-02-08 20:49:08 +01:00
|
|
|
-parallelism=n Limit the number of parallel resource operations.
|
2015-10-05 22:06:08 +02:00
|
|
|
Defaults to 10.
|
2015-05-06 17:58:42 +02:00
|
|
|
|
2014-07-27 02:50:13 +02:00
|
|
|
-refresh=true Update state prior to checking for differences. This
|
|
|
|
has no effect if a plan file is given to apply.
|
|
|
|
|
2014-07-12 06:30:40 +02:00
|
|
|
-state=path Path to read and save state (unless state-out
|
|
|
|
is specified). Defaults to "terraform.tfstate".
|
2014-06-19 06:36:44 +02:00
|
|
|
|
2014-07-12 06:30:40 +02:00
|
|
|
-state-out=path Path to write state to that is different than
|
|
|
|
"-state". This can be used to preserve the old
|
|
|
|
state.
|
2014-05-24 21:27:58 +02:00
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
-target=resource Resource to target. Operation will be limited to this
|
|
|
|
resource and its dependencies. This flag can be used
|
|
|
|
multiple times.
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
|
|
|
flag can be set multiple times.
|
|
|
|
|
|
|
|
-var-file=foo Set variables in the Terraform configuration from
|
2017-06-22 03:22:07 +02:00
|
|
|
a file. If "terraform.tfvars" or any ".auto.tfvars"
|
|
|
|
files are present, they will be automatically loaded.
|
2014-07-18 20:37:27 +02:00
|
|
|
|
|
|
|
|
2014-05-24 21:27:58 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-10-01 07:01:11 +02:00
|
|
|
func (c *ApplyCommand) helpDestroy() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: terraform destroy [options] [DIR]
|
|
|
|
|
|
|
|
Destroy Terraform-managed infrastructure.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-backup=path Path to backup the existing state file before
|
|
|
|
modifying. Defaults to the "-state-out" path with
|
|
|
|
".backup" extension. Set to "-" to disable backup.
|
|
|
|
|
2014-10-04 01:08:50 +02:00
|
|
|
-force Don't ask for input for destroy confirmation.
|
2014-10-01 07:01:11 +02:00
|
|
|
|
2017-02-06 16:07:32 +01:00
|
|
|
-lock=true Lock the state file when locking is supported.
|
2017-02-03 20:15:08 +01:00
|
|
|
|
2017-04-01 22:19:59 +02:00
|
|
|
-lock-timeout=0s Duration to retry a state lock.
|
|
|
|
|
2014-10-01 07:01:11 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2015-10-06 00:18:03 +02:00
|
|
|
-parallelism=n Limit the number of concurrent operations.
|
|
|
|
Defaults to 10.
|
|
|
|
|
2014-10-01 07:01:11 +02:00
|
|
|
-refresh=true Update state prior to checking for differences. This
|
|
|
|
has no effect if a plan file is given to apply.
|
|
|
|
|
|
|
|
-state=path Path to read and save state (unless state-out
|
|
|
|
is specified). Defaults to "terraform.tfstate".
|
|
|
|
|
|
|
|
-state-out=path Path to write state to that is different than
|
|
|
|
"-state". This can be used to preserve the old
|
|
|
|
state.
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
-target=resource Resource to target. Operation will be limited to this
|
|
|
|
resource and its dependencies. This flag can be used
|
|
|
|
multiple times.
|
|
|
|
|
2014-10-01 07:01:11 +02:00
|
|
|
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
|
|
|
flag can be set multiple times.
|
|
|
|
|
|
|
|
-var-file=foo Set variables in the Terraform configuration from
|
2017-06-22 03:22:07 +02:00
|
|
|
a file. If "terraform.tfvars" or any ".auto.tfvars"
|
|
|
|
files are present, they will be automatically loaded.
|
2014-10-01 07:01:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
2014-06-27 23:43:23 +02:00
|
|
|
}
|
2015-06-15 17:40:56 +02:00
|
|
|
|
2016-07-29 21:13:50 +02:00
|
|
|
func outputsAsString(state *terraform.State, modPath []string, schema []*config.Output, includeHeader bool) string {
|
2015-06-15 17:40:56 +02:00
|
|
|
if state == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-11-03 06:33:34 +01:00
|
|
|
ms := state.ModuleByPath(modPath)
|
|
|
|
if ms == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs := ms.Outputs
|
2015-06-15 17:40:56 +02:00
|
|
|
outputBuf := new(bytes.Buffer)
|
|
|
|
if len(outputs) > 0 {
|
2016-05-09 21:46:07 +02:00
|
|
|
schemaMap := make(map[string]*config.Output)
|
2016-05-10 20:56:26 +02:00
|
|
|
if schema != nil {
|
|
|
|
for _, s := range schema {
|
|
|
|
schemaMap[s.Name] = s
|
|
|
|
}
|
2016-05-09 21:46:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 20:56:26 +02:00
|
|
|
if includeHeader {
|
|
|
|
outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n")
|
|
|
|
}
|
2015-06-15 17:40:56 +02:00
|
|
|
|
|
|
|
// Output the outputs in alphabetical order
|
|
|
|
keyLen := 0
|
2016-04-11 19:40:06 +02:00
|
|
|
ks := make([]string, 0, len(outputs))
|
2015-06-15 17:40:56 +02:00
|
|
|
for key, _ := range outputs {
|
2016-04-11 19:40:06 +02:00
|
|
|
ks = append(ks, key)
|
2015-06-15 17:40:56 +02:00
|
|
|
if len(key) > keyLen {
|
|
|
|
keyLen = len(key)
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 19:40:06 +02:00
|
|
|
sort.Strings(ks)
|
2015-06-15 17:40:56 +02:00
|
|
|
|
2016-04-11 19:40:06 +02:00
|
|
|
for _, k := range ks {
|
2016-05-10 20:56:26 +02:00
|
|
|
schema, ok := schemaMap[k]
|
|
|
|
if ok && schema.Sensitive {
|
2016-04-11 19:40:06 +02:00
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s = <sensitive>\n", k))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v := outputs[k]
|
2016-05-12 02:05:02 +02:00
|
|
|
switch typedV := v.Value.(type) {
|
2016-04-11 19:40:06 +02:00
|
|
|
case string:
|
|
|
|
outputBuf.WriteString(fmt.Sprintf("%s = %s\n", k, typedV))
|
|
|
|
case []interface{}:
|
|
|
|
outputBuf.WriteString(formatListOutput("", k, typedV))
|
|
|
|
outputBuf.WriteString("\n")
|
|
|
|
case map[string]interface{}:
|
|
|
|
outputBuf.WriteString(formatMapOutput("", k, typedV))
|
|
|
|
outputBuf.WriteString("\n")
|
2016-05-09 21:46:07 +02:00
|
|
|
}
|
2015-06-15 17:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(outputBuf.String())
|
|
|
|
}
|
2017-04-25 17:43:59 +02:00
|
|
|
|
|
|
|
const outputInterrupt = `Interrupt received.
|
|
|
|
Please wait for Terraform to exit or data loss may occur.
|
|
|
|
Gracefully shutting down...`
|