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/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-07-03 20:46:40 +02:00
|
|
|
ShutdownCh <-chan struct{}
|
|
|
|
ContextOpts *terraform.ContextOpts
|
|
|
|
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-27 23:43:23 +02:00
|
|
|
var stateOutPath string
|
2014-06-19 06:36:44 +02:00
|
|
|
|
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-27 23:43:23 +02:00
|
|
|
cmdFlags.StringVar(&stateOutPath, "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()
|
2014-06-27 23:43:23 +02:00
|
|
|
if len(args) != 2 {
|
|
|
|
c.Ui.Error("The apply command expects two arguments.\n")
|
2014-06-19 01:42:13 +02:00
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
statePath := args[0]
|
|
|
|
configPath := args[1]
|
|
|
|
|
2014-06-19 06:36:44 +02:00
|
|
|
if stateOutPath == "" {
|
|
|
|
stateOutPath = statePath
|
|
|
|
}
|
2014-06-19 01:42:13 +02:00
|
|
|
|
2014-07-01 19:02:13 +02:00
|
|
|
planStatePath := statePath
|
|
|
|
if init {
|
|
|
|
planStatePath = ""
|
2014-06-27 23:43:23 +02:00
|
|
|
}
|
2014-07-03 20:46:40 +02:00
|
|
|
|
|
|
|
// Initialize Terraform right away
|
|
|
|
c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui})
|
|
|
|
ctx, err := ContextArg(configPath, planStatePath, c.ContextOpts)
|
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
|
|
|
}
|
2014-07-03 22:12:45 +02:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-19 01:42:13 +02:00
|
|
|
|
2014-07-08 06:20:48 +02:00
|
|
|
var state *terraform.State
|
|
|
|
var applyErr error
|
|
|
|
doneCh := make(chan struct{})
|
2014-07-03 02:01:02 +02:00
|
|
|
go func() {
|
2014-07-08 06:20:48 +02:00
|
|
|
defer close(doneCh)
|
|
|
|
state, applyErr = ctx.Apply()
|
2014-07-03 02:01:02 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
err = nil
|
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
c.Ui.Output("Interrupt received. Gracefully shutting down...")
|
|
|
|
|
|
|
|
// Stop execution
|
2014-07-03 20:46:40 +02:00
|
|
|
ctx.Stop()
|
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
|
2014-07-08 06:20:48 +02:00
|
|
|
case <-doneCh:
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
2014-07-08 06:20:48 +02:00
|
|
|
case <-doneCh:
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:20:48 +02:00
|
|
|
if state != nil {
|
|
|
|
// Write state out to the file
|
|
|
|
f, err := os.Create(stateOutPath)
|
|
|
|
if err == nil {
|
|
|
|
err = terraform.WriteState(state, f)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to save state: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-19 01:42:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:20:48 +02:00
|
|
|
if applyErr != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", applyErr))
|
2014-06-19 06:36:44 +02:00
|
|
|
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 := `
|
2014-06-27 23:43:23 +02:00
|
|
|
Usage: terraform apply [options] STATE PATH
|
2014-05-24 21:27:58 +02:00
|
|
|
|
|
|
|
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).
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
-out=file.tfstate Path to save the new state. If not specified, the
|
|
|
|
state path argument will be used.
|
2014-05-24 21:27:58 +02:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) Synopsis() string {
|
2014-06-27 23:43:23 +02:00
|
|
|
return "Builds or changes infrastructure"
|
|
|
|
}
|