2014-06-09 20:53:41 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-27 07:23:51 +02:00
|
|
|
"log"
|
2014-06-19 22:51:05 +02:00
|
|
|
"os"
|
2014-06-09 20:53:41 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
// PlanCommand is a Command implementation that compares a Terraform
|
2014-06-09 20:53:41 +02:00
|
|
|
// configuration to an actual infrastructure and shows the differences.
|
2014-06-20 20:47:02 +02:00
|
|
|
type PlanCommand struct {
|
2014-07-13 05:21:46 +02:00
|
|
|
Meta
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Run(args []string) int {
|
2015-04-01 18:38:19 +02:00
|
|
|
var destroy, refresh, detailed bool
|
2014-10-12 03:47:52 +02:00
|
|
|
var outPath string
|
2014-09-25 07:54:51 +02:00
|
|
|
var moduleDepth int
|
2014-06-19 22:51:05 +02:00
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
args = c.Meta.process(args, true)
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
cmdFlags := c.Meta.flagSet("plan")
|
2014-07-01 18:12:05 +02:00
|
|
|
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
2014-06-26 18:56:29 +02:00
|
|
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
2015-04-30 21:05:39 +02:00
|
|
|
c.addModuleDepthFlag(cmdFlags, &moduleDepth)
|
2014-06-27 07:23:51 +02:00
|
|
|
cmdFlags.StringVar(&outPath, "out", "", "path")
|
2015-10-05 22:06:08 +02:00
|
|
|
cmdFlags.IntVar(
|
|
|
|
&c.Meta.parallelism, "parallelism", DefaultParallelism, "parallelism")
|
2014-10-12 03:47:52 +02:00
|
|
|
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
|
2015-04-01 18:38:19 +02:00
|
|
|
cmdFlags.BoolVar(&detailed, "detailed-exitcode", false, "detailed-exitcode")
|
2014-06-09 20:53:41 +02:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
var path string
|
2014-06-09 20:53:41 +02:00
|
|
|
args = cmdFlags.Args()
|
2014-07-12 05:51:26 +02:00
|
|
|
if len(args) > 1 {
|
2014-06-09 20:53:41 +02:00
|
|
|
c.Ui.Error(
|
2014-07-12 05:51:26 +02:00
|
|
|
"The plan command expects at most one argument with the path\n" +
|
2014-06-09 20:53:41 +02:00
|
|
|
"to a Terraform configuration.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
2014-07-12 05:51:26 +02:00
|
|
|
} else if len(args) == 1 {
|
|
|
|
path = args[0]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
path, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
}
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-24 12:32:15 +02:00
|
|
|
countHook := new(CountHook)
|
|
|
|
c.Meta.extraHooks = []terraform.Hook{countHook}
|
|
|
|
|
2014-09-22 19:56:50 +02:00
|
|
|
ctx, _, err := c.Context(contextOpts{
|
2015-05-07 17:21:00 +02:00
|
|
|
Destroy: destroy,
|
|
|
|
Path: path,
|
|
|
|
StatePath: c.Meta.statePath,
|
|
|
|
Parallelism: c.Meta.parallelism,
|
2014-09-22 19:56:50 +02:00
|
|
|
})
|
2014-06-09 20:53:41 +02:00
|
|
|
if err != nil {
|
2014-07-13 05:37:30 +02:00
|
|
|
c.Ui.Error(err.Error())
|
2014-06-09 20:53:41 +02:00
|
|
|
return 1
|
|
|
|
}
|
2015-11-10 19:31:15 +01:00
|
|
|
|
2014-12-09 03:32:03 +01:00
|
|
|
if err := ctx.Input(c.InputMode()); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error configuring: %s", err))
|
2014-07-03 22:12:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2015-11-10 19:31:15 +01:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:56:29 +02:00
|
|
|
if refresh {
|
2016-05-25 00:51:18 +02:00
|
|
|
c.Ui.Output("Refreshing Terraform state in-memory prior to plan...")
|
|
|
|
c.Ui.Output("The refreshed state will be used to calculate this plan, but")
|
|
|
|
c.Ui.Output("will not be persisted to local or remote state storage.\n")
|
2016-05-21 02:53:20 +02:00
|
|
|
_, err := ctx.Refresh()
|
2014-10-12 03:47:52 +02:00
|
|
|
if err != nil {
|
2014-06-26 18:56:29 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-13 02:17:03 +02:00
|
|
|
c.Ui.Output("")
|
2014-06-26 18:56:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-24 17:18:15 +01:00
|
|
|
plan, err := ctx.Plan()
|
2014-06-10 20:34:08 +02:00
|
|
|
if err != nil {
|
2014-06-20 20:47:02 +02:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
|
2014-06-10 20:34:08 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-27 07:23:51 +02:00
|
|
|
if outPath != "" {
|
|
|
|
log.Printf("[INFO] Writing plan output to: %s", outPath)
|
|
|
|
f, err := os.Create(outPath)
|
|
|
|
if err == nil {
|
|
|
|
defer f.Close()
|
|
|
|
err = terraform.WritePlan(plan, f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error writing plan file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-19 23:58:30 +02:00
|
|
|
}
|
2014-06-10 20:34:08 +02:00
|
|
|
|
2016-01-20 23:00:20 +01:00
|
|
|
if plan.Diff.Empty() {
|
|
|
|
c.Ui.Output(
|
|
|
|
"No changes. Infrastructure is up-to-date. This means that Terraform\n" +
|
|
|
|
"could not detect any differences between your configuration and\n" +
|
|
|
|
"the real physical resources that exist. As a result, Terraform\n" +
|
|
|
|
"doesn't need to do anything.")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-07-13 01:32:48 +02:00
|
|
|
if outPath == "" {
|
2014-07-13 01:35:59 +02:00
|
|
|
c.Ui.Output(strings.TrimSpace(planHeaderNoOutput) + "\n")
|
2014-07-13 01:32:48 +02:00
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
strings.TrimSpace(planHeaderYesOutput)+"\n",
|
|
|
|
outPath))
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
c.Ui.Output(FormatPlan(&FormatPlanOpts{
|
|
|
|
Plan: plan,
|
|
|
|
Color: c.Colorize(),
|
|
|
|
ModuleDepth: moduleDepth,
|
|
|
|
}))
|
2014-07-13 01:32:48 +02:00
|
|
|
|
2015-06-24 12:32:15 +02:00
|
|
|
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold]Plan:[reset] "+
|
|
|
|
"%d to add, %d to change, %d to destroy.",
|
2015-09-04 17:07:16 +02:00
|
|
|
countHook.ToAdd+countHook.ToRemoveAndAdd,
|
|
|
|
countHook.ToChange,
|
|
|
|
countHook.ToRemove+countHook.ToRemoveAndAdd)))
|
2015-06-24 12:32:15 +02:00
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
if detailed {
|
|
|
|
return 2
|
|
|
|
}
|
2014-06-09 20:53:41 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Help() string {
|
2014-06-09 20:53:41 +02:00
|
|
|
helpText := `
|
2014-07-12 05:51:26 +02:00
|
|
|
Usage: terraform plan [options] [dir]
|
|
|
|
|
|
|
|
Generates an execution plan for Terraform.
|
2014-06-09 20:53:41 +02:00
|
|
|
|
2014-07-12 05:51:26 +02:00
|
|
|
This execution plan can be reviewed prior to running apply to get a
|
|
|
|
sense for what Terraform will do. Optionally, the plan can be saved to
|
|
|
|
a Terraform plan file, and apply can take this plan file to execute
|
|
|
|
this plan exactly.
|
2014-06-09 20:53:41 +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
|
|
|
|
2014-07-01 18:12:35 +02:00
|
|
|
-destroy If set, a plan will be generated to destroy all resources
|
|
|
|
managed by the given configuration and state.
|
|
|
|
|
2015-04-01 18:38:19 +02:00
|
|
|
-detailed-exitcode Return detailed exit codes when the command exits. This
|
|
|
|
will change the meaning of exit codes to:
|
|
|
|
0 - Succeeded, diff is empty (no changes)
|
|
|
|
1 - Errored
|
|
|
|
2 - Succeeded, there is a diff
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
-input=true Ask for input for variables if not directly set.
|
|
|
|
|
2014-09-25 07:54:51 +02:00
|
|
|
-module-depth=n Specifies the depth of modules to show in the output.
|
|
|
|
This does not affect the plan itself, only the output
|
2016-01-20 19:50:33 +01:00
|
|
|
shown. By default, this is -1, which will expand all.
|
2014-09-25 07:54:51 +02:00
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-06-27 07:23:51 +02:00
|
|
|
-out=path Write a plan file to the given path. This can be used as
|
|
|
|
input to the "apply" command.
|
|
|
|
|
2015-10-06 00:18:03 +02:00
|
|
|
-parallelism=n Limit the number of concurrent operations. Defaults to 10.
|
2015-10-05 22:06:08 +02:00
|
|
|
|
2014-06-26 18:56:29 +02:00
|
|
|
-refresh=true Update state prior to checking for differences.
|
|
|
|
|
2014-06-09 20:53:41 +02:00
|
|
|
-state=statefile Path to a Terraform state file to use to look
|
2014-07-12 05:51:26 +02:00
|
|
|
up Terraform-managed resources. By default it will
|
|
|
|
use the state "terraform.tfstate" if it exists.
|
2014-06-09 20:53:41 +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
|
|
|
|
a file. If "terraform.tfvars" is present, it will be
|
|
|
|
automatically loaded if this flag is not specified.
|
2014-06-09 20:53:41 +02:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:47:02 +02:00
|
|
|
func (c *PlanCommand) Synopsis() string {
|
2014-07-13 04:28:38 +02:00
|
|
|
return "Generate and show an execution plan"
|
2014-06-09 20:53:41 +02:00
|
|
|
}
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
const planHeaderNoOutput = `
|
|
|
|
The Terraform execution plan has been generated and is shown below.
|
|
|
|
Resources are shown in alphabetical order for quick scanning. Green resources
|
|
|
|
will be created (or destroyed and then created if an existing resource
|
|
|
|
exists), yellow resources are being changed in-place, and red resources
|
2016-05-08 09:16:35 +02:00
|
|
|
will be destroyed. Cyan entries are data sources to be read.
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
Note: You didn't specify an "-out" parameter to save this plan, so when
|
|
|
|
"apply" is called, Terraform can't guarantee this is what will execute.
|
|
|
|
`
|
|
|
|
|
|
|
|
const planHeaderYesOutput = `
|
|
|
|
The Terraform execution plan has been generated and is shown below.
|
|
|
|
Resources are shown in alphabetical order for quick scanning. Green resources
|
|
|
|
will be created (or destroyed and then created if an existing resource
|
|
|
|
exists), yellow resources are being changed in-place, and red resources
|
2016-05-08 09:16:35 +02:00
|
|
|
will be destroyed. Cyan entries are data sources to be read.
|
2014-07-13 01:32:48 +02:00
|
|
|
|
|
|
|
Your plan was also saved to the path below. Call the "apply" subcommand
|
|
|
|
with this plan file and Terraform will exactly execute this execution
|
|
|
|
plan.
|
|
|
|
|
|
|
|
Path: %s
|
|
|
|
`
|