terraform/command/apply.go

395 lines
12 KiB
Go
Raw Normal View History

2014-05-24 21:27:58 +02:00
package command
import (
2014-07-13 18:34:35 +02:00
"bytes"
2014-06-19 01:42:13 +02:00
"fmt"
2014-07-13 18:34:35 +02:00
"sort"
2014-05-24 21:27:58 +02:00
"strings"
2017-01-19 05:50:45 +01:00
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/plans/planfile"
"github.com/hashicorp/terraform/repl"
"github.com/hashicorp/terraform/states"
terraform: ugly huge change to weave in new HCL2-oriented types Due to how deeply the configuration types go into Terraform Core, there isn't a great way to switch out to HCL2 gradually. As a consequence, this huge commit gets us from the old state to a _compilable_ new state, but does not yet attempt to fix any tests and has a number of known missing parts and bugs. We will continue to iterate on this in forthcoming commits, heading back towards passing tests and making Terraform fully-functional again. The three main goals here are: - Use the configuration models from the "configs" package instead of the older models in the "config" package, which is now deprecated and preserved only to help us write our migration tool. - Do expression inspection and evaluation using the functionality of the new "lang" package, instead of the Interpolator type and related functionality in the main "terraform" package. - Represent addresses of various objects using types in the addrs package, rather than hand-constructed strings. This is not critical to support the above, but was a big help during the implementation of these other points since it made it much more explicit what kind of address is expected in each context. Since our new packages are built to accommodate some future planned features that are not yet implemented (e.g. the "for_each" argument on resources, "count"/"for_each" on modules), and since there's still a fair amount of functionality still using old-style APIs, there is a moderate amount of shimming here to connect new assumptions with old, hopefully in a way that makes it easier to find and eliminate these shims later. I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
"github.com/hashicorp/terraform/tfdiags"
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 {
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 {
var refresh, autoApprove bool
args = c.Meta.process(args)
2014-10-01 06:49:24 +02:00
cmdName := "apply"
if c.Destroy {
cmdName = "destroy"
}
cmdFlags := c.Meta.extendedFlagSet(cmdName)
cmdFlags.BoolVar(&autoApprove, "auto-approve", false, "skip interactive approval of plan before applying")
2014-07-27 02:50:13 +02:00
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
2017-01-19 05:50:45 +01:00
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
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")
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
}
var diags tfdiags.Diagnostics
2014-06-19 01:42:13 +02:00
args = cmdFlags.Args()
var planPath string
if len(args) > 0 {
planPath = args[0]
args = args[1:]
}
2017-01-19 05:50:45 +01:00
configPath, err := ModulePath(args)
if err != nil {
c.Ui.Error(err.Error())
2014-06-19 01:42:13 +02:00
return 1
}
// 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
}
// Try to load plan if path is specified
var planFile *planfile.Reader
if planPath != "" {
planFile, err = c.PlanFile(planPath)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
2014-06-19 01:42:13 +02:00
}
if c.Destroy && planFile != nil {
2020-12-01 16:42:07 +01:00
c.Ui.Error("Destroy can't be called with a plan file.")
2014-10-01 06:49:24 +02:00
return 1
}
if planFile != nil {
2017-01-19 05:50:45 +01:00
// Reset the config path for backend loading
configPath = ""
if !c.variableArgs.Empty() {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Can't set variables when applying a saved plan",
"The -var and -var-file options cannot be used when applying a saved plan file, because a saved plan includes the variable values that were set when it was created.",
))
c.showDiagnostics(diags)
return 1
}
}
// Set up our count hook that keeps track of resource changes
countHook := new(CountHook)
c.ExtraHooks = append(c.ExtraHooks, countHook)
// Load the backend
var be backend.Enhanced
var beDiags tfdiags.Diagnostics
if planFile == nil {
backendConfig, configDiags := c.loadBackendConfig(configPath)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
c.showDiagnostics(diags)
2017-01-19 05:50:45 +01:00
return 1
}
be, beDiags = c.Backend(&BackendOpts{
Config: backendConfig,
})
} else {
plan, err := planFile.ReadPlan()
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to read plan from plan file",
fmt.Sprintf("Cannot read the plan from the given plan file: %s.", err),
))
c.showDiagnostics(diags)
return 1
}
if plan.Backend.Config == nil {
// Should never happen; always indicates a bug in the creation of the plan file
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to read plan from plan file",
2020-12-01 16:42:07 +01:00
"The given plan file does not have a valid backend configuration. This is a bug in the Terraform command that generated this plan file.",
))
c.showDiagnostics(diags)
return 1
}
be, beDiags = c.BackendForPlan(plan.Backend)
}
diags = diags.Append(beDiags)
if beDiags.HasErrors() {
c.showDiagnostics(diags)
2017-01-19 05:50:45 +01:00
return 1
}
// Applying changes with dev overrides in effect could make it impossible
// to switch back to a release version if the schema isn't compatible,
// so we'll warn about it.
diags = diags.Append(c.providerDevOverrideRuntimeWarnings())
// Before we delegate to the backend, we'll print any warning diagnostics
// we've accumulated here, since the backend will start fresh with its own
// diagnostics.
c.showDiagnostics(diags)
diags = nil
2017-01-19 05:50:45 +01:00
// Build the operation
opReq := c.Operation(be)
opReq.AutoApprove = autoApprove
opReq.ConfigDir = configPath
opReq.Destroy = c.Destroy
opReq.PlanFile = planFile
2017-01-19 05:50:45 +01:00
opReq.PlanRefresh = refresh
opReq.Type = backend.OperationTypeApply
opReq.ConfigLoader, err = c.initConfigLoader()
2017-01-19 05:50:45 +01:00
if err != nil {
c.showDiagnostics(err)
return 1
2014-06-19 01:42:13 +02:00
}
{
var moreDiags tfdiags.Diagnostics
opReq.Variables, moreDiags = c.collectVariableValues()
diags = diags.Append(moreDiags)
if moreDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
}
2014-06-19 01:42:13 +02:00
op, err := c.RunOperation(be, opReq)
if err != nil {
c.showDiagnostics(err)
return 1
}
if op.Result != backend.OperationSuccess {
return op.Result.ExitStatus()
}
// Show the count results from the operation
if c.Destroy {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset][bold][green]\n"+
"Destroy complete! Resources: %d destroyed.",
countHook.Removed)))
} else {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset][bold][green]\n"+
"Apply complete! Resources: %d added, %d changed, %d destroyed.",
countHook.Added,
countHook.Changed,
countHook.Removed)))
}
// only show the state file help message if the state is local.
if (countHook.Added > 0 || countHook.Changed > 0) && c.Meta.stateOutPath != "" {
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
"[reset]\n"+
"The state of your infrastructure has been saved to the path\n"+
"below. This state is required to modify and destroy your\n"+
"infrastructure, so keep it safe. To inspect the complete state\n"+
"use the `terraform show` command.\n\n"+
"State path: %s",
c.Meta.stateOutPath)))
}
if !c.Destroy {
if outputs := outputsAsString(op.State, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
2014-07-13 18:34:35 +02:00
}
return op.Result.ExitStatus()
2014-05-24 21:27:58 +02:00
}
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 previously-created infrastructure"
2014-10-01 07:01:11 +02:00
}
return "Create or update infrastructure"
2014-10-01 07:01:11 +02:00
}
func (c *ApplyCommand) helpApply() string {
2014-05-24 21:27:58 +02:00
helpText := `
Usage: terraform apply [options] [PLAN]
2014-05-24 21:27:58 +02:00
Creates or updates infrastructure according to Terraform configuration
files in the current directory.
2014-09-30 00:57:35 +02:00
By default, Terraform will generate a new plan and present it for your
approval before taking any action. You can optionally provide a plan
file created by a previous call to "terraform plan", in which case
Terraform will take the actions described in that plan without any
confirmation prompt.
2014-05-24 21:27:58 +02:00
Options:
-auto-approve Skip interactive approval of plan before applying.
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
".backup" extension. Set to "-" to disable backup.
2014-07-28 00:09:04 +02:00
-compact-warnings If Terraform produces any warnings that are not
accompanied by errors, show them in a more compact
form that includes only the summary messages.
2017-02-06 16:07:32 +01:00
-lock=true Lock the state file when locking is supported.
-lock-timeout=0s Duration to retry a state lock.
-input=true Ask for input for variables if not directly set.
-no-color If specified, output won't contain any color.
-parallelism=n Limit the number of parallel resource operations.
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.
-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
-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
-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]
2014-10-01 07:01:11 +02:00
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.
-auto-approve Skip interactive approval before destroying.
2017-02-06 16:07:32 +01:00
-lock=true Lock the state file when locking is supported.
-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.
-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.
-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)
}
func outputsAsString(state *states.State, includeHeader bool) string {
if state == nil {
return ""
}
ms := state.RootModule()
outputs := ms.OutputValues
outputBuf := new(bytes.Buffer)
if len(outputs) > 0 {
if includeHeader {
outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n")
}
// Output the outputs in alphabetical order
keyLen := 0
ks := make([]string, 0, len(outputs))
2020-12-01 16:42:07 +01:00
for key := range outputs {
ks = append(ks, key)
if len(key) > keyLen {
keyLen = len(key)
}
}
sort.Strings(ks)
for _, k := range ks {
v := outputs[k]
if v.Sensitive {
outputBuf.WriteString(fmt.Sprintf("%s = <sensitive>\n", k))
continue
}
result := repl.FormatValue(v.Value, 0)
outputBuf.WriteString(fmt.Sprintf("%s = %s\n", k, result))
}
}
return strings.TrimSpace(outputBuf.String())
}
const outputInterrupt = `Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...`