2014-06-09 13:53:41 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-06-27 00:23:51 -05:00
|
|
|
"log"
|
2014-06-19 15:51:05 -05:00
|
|
|
"os"
|
2014-06-09 13:53:41 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
// PlanCommand is a Command implementation that compares a Terraform
|
2014-06-09 13:53:41 -05:00
|
|
|
// configuration to an actual infrastructure and shows the differences.
|
2014-06-20 13:47:02 -05:00
|
|
|
type PlanCommand struct {
|
2014-07-12 22:21:46 -05:00
|
|
|
Meta
|
|
|
|
|
2014-07-03 13:46:40 -05:00
|
|
|
ContextOpts *terraform.ContextOpts
|
|
|
|
Ui cli.Ui
|
2014-06-09 13:53:41 -05:00
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
func (c *PlanCommand) Run(args []string) int {
|
2014-07-01 11:12:05 -05:00
|
|
|
var destroy, refresh bool
|
2014-06-27 00:23:51 -05:00
|
|
|
var outPath, statePath string
|
2014-06-19 15:51:05 -05:00
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
args = c.Meta.process(args)
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError)
|
2014-07-01 11:12:05 -05:00
|
|
|
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
2014-06-26 11:56:29 -05:00
|
|
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
2014-06-27 00:23:51 -05:00
|
|
|
cmdFlags.StringVar(&outPath, "out", "", "path")
|
2014-07-11 23:03:56 -05:00
|
|
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
2014-06-09 13:53:41 -05:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:51:26 -05:00
|
|
|
var path string
|
2014-06-09 13:53:41 -05:00
|
|
|
args = cmdFlags.Args()
|
2014-07-11 22:51:26 -05:00
|
|
|
if len(args) > 1 {
|
2014-06-09 13:53:41 -05:00
|
|
|
c.Ui.Error(
|
2014-07-11 22:51:26 -05:00
|
|
|
"The plan command expects at most one argument with the path\n" +
|
2014-06-09 13:53:41 -05:00
|
|
|
"to a Terraform configuration.\n")
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
2014-07-11 22:51:26 -05: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 13:53:41 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:03:56 -05:00
|
|
|
// If the default state path doesn't exist, ignore it.
|
|
|
|
if statePath != "" {
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
if os.IsNotExist(err) && statePath == DefaultStateFilename {
|
|
|
|
statePath = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 15:51:05 -05:00
|
|
|
// Load up the state
|
|
|
|
var state *terraform.State
|
|
|
|
if statePath != "" {
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:51:26 -05:00
|
|
|
b, err := config.LoadDir(path)
|
2014-06-09 13:53:41 -05:00
|
|
|
if err != nil {
|
2014-07-11 23:03:56 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
|
2014-06-09 13:53:41 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:46:40 -05:00
|
|
|
c.ContextOpts.Config = b
|
|
|
|
c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui})
|
|
|
|
c.ContextOpts.State = state
|
|
|
|
ctx := terraform.NewContext(c.ContextOpts)
|
2014-07-03 15:12:45 -05:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-09 13:53:41 -05:00
|
|
|
|
2014-06-26 11:56:29 -05:00
|
|
|
if refresh {
|
2014-07-12 19:17:03 -05:00
|
|
|
c.Ui.Output("Refreshing Terraform state prior to plan...\n")
|
2014-07-03 13:46:40 -05:00
|
|
|
if _, err := ctx.Refresh(); err != nil {
|
2014-06-26 11:56:29 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2014-07-12 19:17:03 -05:00
|
|
|
c.Ui.Output("")
|
2014-06-26 11:56:29 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 13:46:40 -05:00
|
|
|
plan, err := ctx.Plan(&terraform.PlanOpts{Destroy: destroy})
|
2014-06-10 13:34:08 -05:00
|
|
|
if err != nil {
|
2014-06-20 13:47:02 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
|
2014-06-10 13:34:08 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
if plan.Diff.Empty() {
|
2014-07-12 18:35:59 -05:00
|
|
|
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.")
|
2014-06-27 00:23:51 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:58:30 -05:00
|
|
|
}
|
2014-06-10 13:34:08 -05:00
|
|
|
|
2014-07-12 18:32:48 -05:00
|
|
|
if outPath == "" {
|
2014-07-12 18:35:59 -05:00
|
|
|
c.Ui.Output(strings.TrimSpace(planHeaderNoOutput) + "\n")
|
2014-07-12 18:32:48 -05:00
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
strings.TrimSpace(planHeaderYesOutput)+"\n",
|
|
|
|
outPath))
|
|
|
|
}
|
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
c.Ui.Output(FormatPlan(plan, c.Colorize()))
|
2014-07-12 18:32:48 -05:00
|
|
|
|
2014-06-09 13:53:41 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
func (c *PlanCommand) Help() string {
|
2014-06-09 13:53:41 -05:00
|
|
|
helpText := `
|
2014-07-11 22:51:26 -05:00
|
|
|
Usage: terraform plan [options] [dir]
|
|
|
|
|
|
|
|
Generates an execution plan for Terraform.
|
2014-06-09 13:53:41 -05:00
|
|
|
|
2014-07-11 22:51:26 -05: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 13:53:41 -05:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-07-01 11:12:35 -05:00
|
|
|
-destroy If set, a plan will be generated to destroy all resources
|
|
|
|
managed by the given configuration and state.
|
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-06-27 00:23:51 -05:00
|
|
|
-out=path Write a plan file to the given path. This can be used as
|
|
|
|
input to the "apply" command.
|
|
|
|
|
2014-06-26 11:56:29 -05:00
|
|
|
-refresh=true Update state prior to checking for differences.
|
|
|
|
|
2014-06-09 13:53:41 -05:00
|
|
|
-state=statefile Path to a Terraform state file to use to look
|
2014-07-11 22:51:26 -05:00
|
|
|
up Terraform-managed resources. By default it will
|
|
|
|
use the state "terraform.tfstate" if it exists.
|
2014-06-09 13:53:41 -05:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2014-06-20 13:47:02 -05:00
|
|
|
func (c *PlanCommand) Synopsis() string {
|
2014-07-12 21:28:38 -05:00
|
|
|
return "Generate and show an execution plan"
|
2014-06-09 13:53:41 -05:00
|
|
|
}
|
2014-07-12 18:32:48 -05: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
|
|
|
|
will be destroyed.
|
|
|
|
|
|
|
|
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
|
|
|
|
will be destroyed.
|
|
|
|
|
|
|
|
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
|
|
|
|
`
|