terraform/command/apply.go

154 lines
3.6 KiB
Go
Raw Normal View History

2014-05-24 21:27:58 +02:00
package command
import (
2014-06-19 01:42:13 +02:00
"flag"
"fmt"
2014-06-19 06:36:44 +02:00
"os"
2014-05-24 21:27:58 +02:00
"strings"
2014-06-19 01:42:13 +02:00
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
2014-05-24 21:27:58 +02:00
"github.com/mitchellh/cli"
)
// ApplyCommand is a Command implementation that applies a Terraform
// configuration and actually builds or changes infrastructure.
type ApplyCommand struct {
2014-06-19 01:42:13 +02:00
TFConfig *terraform.Config
Ui cli.Ui
2014-05-24 21:27:58 +02:00
}
2014-06-19 01:42:13 +02:00
func (c *ApplyCommand) Run(args []string) int {
2014-06-19 21:12:24 +02:00
var init bool
2014-06-19 06:36:44 +02:00
var statePath, stateOutPath string
2014-06-19 01:42:13 +02:00
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
2014-06-19 21:12:24 +02:00
cmdFlags.BoolVar(&init, "init", false, "init")
2014-06-19 06:36:44 +02:00
cmdFlags.StringVar(&statePath, "state", "terraform.tfstate", "path")
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
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
}
args = cmdFlags.Args()
if len(args) != 1 {
c.Ui.Error(
"The apply command expects only one argument with the path\n" +
"to a Terraform configuration.\n")
cmdFlags.Usage()
return 1
}
2014-06-19 06:36:44 +02:00
if statePath == "" {
c.Ui.Error("-state cannot be blank")
return 1
}
if stateOutPath == "" {
stateOutPath = statePath
}
if !init {
if _, err := os.Stat(statePath); err != nil {
c.Ui.Error(fmt.Sprintf(
"There was an error reading the state file. The path\n"+
"and error are shown below. If you're trying to build a\n"+
"brand new infrastructure, explicitly pass the '-init'\n"+
"flag to Terraform to tell it it is okay to build new\n"+
"state.\n\n"+
"Path: %s\n"+
"Error: %s",
statePath,
err))
return 1
}
}
2014-06-19 06:36:44 +02:00
2014-06-19 21:12:24 +02:00
// Load up the state
var state *terraform.State
if !init {
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-06-19 01:42:13 +02:00
b, err := config.Load(args[0])
if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
return 1
}
tfconfig := c.TFConfig
tfconfig.Config = b
tf, err := terraform.New(tfconfig)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
return 1
}
plan, err := tf.Plan(state)
2014-06-19 01:42:13 +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-19 01:42:13 +02:00
return 1
}
state, err = tf.Apply(plan)
2014-06-19 01:42:13 +02:00
if err != nil {
2014-06-20 20:47:02 +02:00
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", err))
2014-06-19 01:42:13 +02:00
return 1
}
2014-06-19 06:36:44 +02:00
// Write state out to the file
f, err := os.Create(stateOutPath)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
return 1
}
defer f.Close()
if err := terraform.WriteState(state, f); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
return 1
}
2014-06-19 01:42:13 +02:00
c.Ui.Output(strings.TrimSpace(state.String()))
2014-05-24 21:27:58 +02:00
return 0
}
func (c *ApplyCommand) Help() string {
helpText := `
Usage: terraform apply [terraform.tf]
Builds or changes infrastructure according to the Terraform configuration
file.
Options:
2014-06-19 06:36:44 +02:00
-init If specified, it is okay to build brand new
infrastructure (with no state file specified).
-state=terraform.tfstate Path to the state file to build off of. This file
will also be written to with updated state unless
-state-out is specified.
-state-out=file.tfstate Path to save the new state. If not specified, the
-state value will be used.
2014-05-24 21:27:58 +02:00
`
return strings.TrimSpace(helpText)
}
func (c *ApplyCommand) Synopsis() string {
return "Builds or changes infrastructure according to Terrafiles"
}