2014-07-13 05:21:46 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-07-18 20:37:27 +02:00
|
|
|
"flag"
|
2014-07-13 05:37:30 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
2014-07-13 05:21:46 +02:00
|
|
|
"github.com/mitchellh/colorstring"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Meta are the meta-options that are available on all or most commands.
|
|
|
|
type Meta struct {
|
2014-07-13 05:37:30 +02:00
|
|
|
Color bool
|
|
|
|
ContextOpts *terraform.ContextOpts
|
|
|
|
Ui cli.Ui
|
2014-07-28 00:09:04 +02:00
|
|
|
State *terraform.State
|
2014-07-13 05:59:16 +02:00
|
|
|
|
2014-07-18 00:14:26 +02:00
|
|
|
// This can be set by the command itself to provide extra hooks.
|
|
|
|
extraHooks []terraform.Hook
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
// Variables for the context (private)
|
|
|
|
variables map[string]string
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
color bool
|
2014-07-13 05:59:16 +02:00
|
|
|
oldUi cli.Ui
|
2014-07-13 05:21:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Colorize returns the colorization structure for a command.
|
|
|
|
func (m *Meta) Colorize() *colorstring.Colorize {
|
|
|
|
return &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
2014-07-17 18:34:32 +02:00
|
|
|
Disable: !m.color,
|
2014-07-13 05:21:46 +02:00
|
|
|
Reset: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:37:30 +02:00
|
|
|
// Context returns a Terraform Context taking into account the context
|
|
|
|
// options used to initialize this meta configuration.
|
2014-07-27 02:45:38 +02:00
|
|
|
func (m *Meta) Context(path, statePath string) (*terraform.Context, bool, error) {
|
2014-07-13 05:37:30 +02:00
|
|
|
opts := m.contextOpts()
|
|
|
|
|
|
|
|
// First try to just read the plan directly from the path given.
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err == nil {
|
|
|
|
plan, err := terraform.ReadPlan(f)
|
|
|
|
f.Close()
|
|
|
|
if err == nil {
|
2014-07-26 23:32:09 +02:00
|
|
|
if len(m.variables) > 0 {
|
2014-07-27 02:45:38 +02:00
|
|
|
return nil, false, fmt.Errorf(
|
2014-07-26 23:32:09 +02:00
|
|
|
"You can't set variables with the '-var' or '-var-file' flag\n" +
|
|
|
|
"when you're applying a plan file. The variables used when\n" +
|
|
|
|
"the plan was created will be used. If you wish to use different\n" +
|
|
|
|
"variable values, create a new plan file.")
|
|
|
|
}
|
|
|
|
|
2014-07-27 02:45:38 +02:00
|
|
|
return plan.Context(opts), true, nil
|
2014-07-13 05:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load up the state
|
|
|
|
var state *terraform.State
|
|
|
|
if statePath != "" {
|
|
|
|
f, err := os.Open(statePath)
|
2014-07-26 23:44:05 +02:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// If the state file doesn't exist, it is okay, since it
|
|
|
|
// is probably a new infrastructure.
|
|
|
|
err = nil
|
|
|
|
} else if err == nil {
|
2014-07-13 05:37:30 +02:00
|
|
|
state, err = terraform.ReadState(f)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2014-07-27 02:45:38 +02:00
|
|
|
return nil, false, fmt.Errorf("Error loading state: %s", err)
|
2014-07-13 05:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 00:09:04 +02:00
|
|
|
// Store the loaded state
|
|
|
|
m.State = state
|
|
|
|
|
2014-07-13 05:37:30 +02:00
|
|
|
config, err := config.LoadDir(path)
|
|
|
|
if err != nil {
|
2014-07-27 02:45:38 +02:00
|
|
|
return nil, false, fmt.Errorf("Error loading config: %s", err)
|
2014-07-13 05:37:30 +02:00
|
|
|
}
|
|
|
|
if err := config.Validate(); err != nil {
|
2014-07-27 02:45:38 +02:00
|
|
|
return nil, false, fmt.Errorf("Error validating config: %s", err)
|
2014-07-13 05:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.Config = config
|
|
|
|
opts.State = state
|
|
|
|
ctx := terraform.NewContext(opts)
|
2014-07-27 02:45:38 +02:00
|
|
|
return ctx, false, nil
|
2014-07-13 05:37:30 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// contextOpts returns the options to use to initialize a Terraform
|
|
|
|
// context with the settings from this Meta.
|
|
|
|
func (m *Meta) contextOpts() *terraform.ContextOpts {
|
|
|
|
var opts terraform.ContextOpts = *m.ContextOpts
|
2014-07-18 00:14:26 +02:00
|
|
|
opts.Hooks = make(
|
|
|
|
[]terraform.Hook,
|
|
|
|
len(m.ContextOpts.Hooks)+len(m.extraHooks)+1)
|
2014-07-13 05:37:30 +02:00
|
|
|
opts.Hooks[0] = m.uiHook()
|
|
|
|
copy(opts.Hooks[1:], m.ContextOpts.Hooks)
|
2014-07-18 00:14:26 +02:00
|
|
|
copy(opts.Hooks[len(m.ContextOpts.Hooks)+1:], m.extraHooks)
|
2014-07-18 20:37:27 +02:00
|
|
|
|
|
|
|
if len(m.variables) > 0 {
|
|
|
|
vs := make(map[string]string)
|
|
|
|
for k, v := range opts.Variables {
|
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range m.variables {
|
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
opts.Variables = vs
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:37:30 +02:00
|
|
|
return &opts
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
// flags adds the meta flags to the given FlagSet.
|
|
|
|
func (m *Meta) flagSet(n string) *flag.FlagSet {
|
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
|
|
|
f.Var((*FlagVar)(&m.variables), "var", "variables")
|
|
|
|
f.Var((*FlagVarFile)(&m.variables), "var-file", "variable file")
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// process will process the meta-parameters out of the arguments. This
|
|
|
|
// will potentially modify the args in-place. It will return the resulting
|
|
|
|
// slice.
|
|
|
|
func (m *Meta) process(args []string) []string {
|
2014-07-13 05:59:16 +02:00
|
|
|
// We do this so that we retain the ability to technically call
|
|
|
|
// process multiple times, even if we have no plans to do so
|
|
|
|
if m.oldUi != nil {
|
|
|
|
m.Ui = m.oldUi
|
|
|
|
}
|
2014-07-13 05:21:46 +02:00
|
|
|
|
2014-07-13 05:59:16 +02:00
|
|
|
// Set colorization
|
2014-07-17 18:34:32 +02:00
|
|
|
m.color = m.Color
|
2014-07-13 05:21:46 +02:00
|
|
|
for i, v := range args {
|
|
|
|
if v == "-no-color" {
|
|
|
|
m.Color = false
|
2014-07-13 05:59:16 +02:00
|
|
|
args = append(args[:i], args[i+1:]...)
|
|
|
|
break
|
2014-07-13 05:21:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:59:16 +02:00
|
|
|
// Set the UI
|
|
|
|
m.oldUi = m.Ui
|
|
|
|
m.Ui = &ColorizeUi{
|
|
|
|
Colorize: m.Colorize(),
|
|
|
|
ErrorColor: "[red]",
|
|
|
|
Ui: m.oldUi,
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
return args
|
|
|
|
}
|
2014-07-13 05:37:30 +02:00
|
|
|
|
|
|
|
// uiHook returns the UiHook to use with the context.
|
|
|
|
func (m *Meta) uiHook() *UiHook {
|
|
|
|
return &UiHook{
|
|
|
|
Colorize: m.Colorize(),
|
|
|
|
Ui: m.Ui,
|
|
|
|
}
|
|
|
|
}
|