2014-05-24 14:27:58 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-06-18 18:42:13 -05:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-06-18 23:36:44 -05:00
|
|
|
"os"
|
2014-05-24 14:27:58 -05:00
|
|
|
"strings"
|
|
|
|
|
2014-06-18 18:42:13 -05:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-05-24 14:27:58 -05: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-12 22:21:46 -05:00
|
|
|
Meta
|
|
|
|
|
2014-07-03 13:46:40 -05:00
|
|
|
ShutdownCh <-chan struct{}
|
|
|
|
ContextOpts *terraform.ContextOpts
|
|
|
|
Ui cli.Ui
|
2014-05-24 14:27:58 -05:00
|
|
|
}
|
|
|
|
|
2014-06-18 18:42:13 -05:00
|
|
|
func (c *ApplyCommand) Run(args []string) int {
|
2014-06-19 14:12:24 -05:00
|
|
|
var init bool
|
2014-07-11 23:30:40 -05:00
|
|
|
var statePath, stateOutPath string
|
2014-06-18 23:36:44 -05:00
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
args = c.Meta.process(args)
|
|
|
|
|
2014-06-18 18:42:13 -05:00
|
|
|
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
|
2014-06-19 14:12:24 -05:00
|
|
|
cmdFlags.BoolVar(&init, "init", false, "init")
|
2014-07-11 23:30:40 -05:00
|
|
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
|
|
|
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
2014-06-18 18:42:13 -05:00
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:32:34 -05:00
|
|
|
var configPath string
|
2014-06-18 18:42:13 -05:00
|
|
|
args = cmdFlags.Args()
|
2014-07-11 23:30:40 -05:00
|
|
|
if len(args) > 1 {
|
|
|
|
c.Ui.Error("The apply command expacts at most one argument.")
|
2014-06-18 18:42:13 -05:00
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
2014-07-11 23:32:34 -05:00
|
|
|
} else if len(args) == 1 {
|
|
|
|
configPath = args[0]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
configPath, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
|
|
|
|
}
|
2014-06-18 18:42:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
// If we don't specify an output path, default to out normal state
|
|
|
|
// path.
|
2014-06-18 23:36:44 -05:00
|
|
|
if stateOutPath == "" {
|
|
|
|
stateOutPath = statePath
|
|
|
|
}
|
2014-06-18 18:42:13 -05:00
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
// The state path to use to generate a plan. If we're initializing
|
|
|
|
// a new infrastructure, then we don't use a state path.
|
2014-07-01 12:02:13 -05:00
|
|
|
planStatePath := statePath
|
|
|
|
if init {
|
|
|
|
planStatePath = ""
|
2014-06-27 16:43:23 -05:00
|
|
|
}
|
2014-07-03 13:46:40 -05:00
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
// Build the context based on the arguments given
|
2014-07-03 13:46:40 -05:00
|
|
|
c.ContextOpts.Hooks = append(c.ContextOpts.Hooks, &UiHook{Ui: c.Ui})
|
|
|
|
ctx, err := ContextArg(configPath, planStatePath, c.ContextOpts)
|
2014-07-01 12:02:13 -05:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
2014-06-18 18:42:13 -05:00
|
|
|
}
|
2014-07-03 15:12:45 -05:00
|
|
|
if !validateContext(ctx, c.Ui) {
|
|
|
|
return 1
|
|
|
|
}
|
2014-06-18 18:42:13 -05:00
|
|
|
|
2014-07-12 19:03:55 -05:00
|
|
|
// Start the apply in a goroutine so that we can be interrupted.
|
2014-07-07 23:20:48 -05:00
|
|
|
var state *terraform.State
|
|
|
|
var applyErr error
|
|
|
|
doneCh := make(chan struct{})
|
2014-07-02 19:01:02 -05:00
|
|
|
go func() {
|
2014-07-07 23:20:48 -05:00
|
|
|
defer close(doneCh)
|
|
|
|
state, applyErr = ctx.Apply()
|
2014-07-02 19:01:02 -05:00
|
|
|
}()
|
|
|
|
|
2014-07-12 19:03:55 -05:00
|
|
|
// Wait for the apply to finish or for us to be interrupted so
|
|
|
|
// we can handle it properly.
|
2014-07-02 19:01:02 -05:00
|
|
|
err = nil
|
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
c.Ui.Output("Interrupt received. Gracefully shutting down...")
|
|
|
|
|
|
|
|
// Stop execution
|
2014-07-03 13:46:40 -05:00
|
|
|
ctx.Stop()
|
2014-07-02 19:01:02 -05: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-07 23:20:48 -05:00
|
|
|
case <-doneCh:
|
2014-07-02 19:01:02 -05:00
|
|
|
}
|
2014-07-07 23:20:48 -05:00
|
|
|
case <-doneCh:
|
2014-07-02 19:01:02 -05:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:20:48 -05: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-18 18:42:13 -05:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:20:48 -05:00
|
|
|
if applyErr != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error applying plan: %s", applyErr))
|
2014-06-18 23:36:44 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:42:13 -05:00
|
|
|
c.Ui.Output(strings.TrimSpace(state.String()))
|
|
|
|
|
2014-05-24 14:27:58 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) Help() string {
|
|
|
|
helpText := `
|
2014-07-11 23:30:40 -05:00
|
|
|
Usage: terraform apply [options] [dir]
|
2014-05-24 14:27:58 -05:00
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
Builds or changes infrastructure according to Terraform configuration
|
|
|
|
files .
|
2014-05-24 14:27:58 -05:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
-init If specified, new infrastructure can be built (no
|
|
|
|
previous state). This is just a safety switch
|
|
|
|
to prevent accidentally spinning up a new
|
|
|
|
infrastructure.
|
|
|
|
|
2014-07-12 22:21:46 -05:00
|
|
|
-no-color If specified, output won't contain any color.
|
|
|
|
|
2014-07-11 23:30:40 -05:00
|
|
|
-state=path Path to read and save state (unless state-out
|
|
|
|
is specified). Defaults to "terraform.tfstate".
|
2014-06-18 23:36:44 -05:00
|
|
|
|
2014-07-11 23:30:40 -05: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 14:27:58 -05:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ApplyCommand) Synopsis() string {
|
2014-06-27 16:43:23 -05:00
|
|
|
return "Builds or changes infrastructure"
|
|
|
|
}
|