2015-02-05 00:44:23 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2015-02-14 01:27:23 +01:00
|
|
|
"fmt"
|
2015-02-17 04:04:05 +01:00
|
|
|
"log"
|
2015-05-14 05:09:05 +02:00
|
|
|
"os"
|
2015-02-14 02:29:38 +01:00
|
|
|
"sort"
|
2015-05-14 05:09:05 +02:00
|
|
|
"strings"
|
2015-02-05 00:44:23 +01:00
|
|
|
"sync"
|
|
|
|
|
2015-02-07 18:53:46 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-02-14 02:29:38 +01:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2015-02-05 00:44:23 +01:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
|
|
|
)
|
|
|
|
|
2015-02-14 02:29:38 +01:00
|
|
|
// InputMode defines what sort of input will be asked for when Input
|
|
|
|
// is called on Context.
|
|
|
|
type InputMode byte
|
|
|
|
|
|
|
|
const (
|
2015-03-07 00:04:12 +01:00
|
|
|
// InputModeVar asks for all variables
|
2015-02-14 02:29:38 +01:00
|
|
|
InputModeVar InputMode = 1 << iota
|
|
|
|
|
2015-03-07 00:04:12 +01:00
|
|
|
// InputModeVarUnset asks for variables which are not set yet
|
|
|
|
InputModeVarUnset
|
|
|
|
|
2015-02-14 02:29:38 +01:00
|
|
|
// InputModeProvider asks for provider variables
|
|
|
|
InputModeProvider
|
|
|
|
|
|
|
|
// InputModeStd is the standard operating mode and asks for both variables
|
|
|
|
// and providers.
|
|
|
|
InputModeStd = InputModeVar | InputModeProvider
|
|
|
|
)
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
// ContextOpts are the user-configurable options to create a context with
|
|
|
|
// NewContext.
|
|
|
|
type ContextOpts struct {
|
2015-03-24 17:18:15 +01:00
|
|
|
Destroy bool
|
2015-02-05 00:44:23 +01:00
|
|
|
Diff *Diff
|
|
|
|
Hooks []Hook
|
|
|
|
Module *module.Tree
|
|
|
|
Parallelism int
|
|
|
|
State *State
|
|
|
|
Providers map[string]ResourceProviderFactory
|
|
|
|
Provisioners map[string]ResourceProvisionerFactory
|
2015-03-24 17:18:15 +01:00
|
|
|
Targets []string
|
2015-02-05 00:44:23 +01:00
|
|
|
Variables map[string]string
|
|
|
|
|
|
|
|
UIInput UIInput
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context represents all the context that Terraform needs in order to
|
|
|
|
// perform operations on infrastructure. This structure is built using
|
|
|
|
// NewContext. See the documentation for that.
|
2015-02-14 03:15:36 +01:00
|
|
|
type Context struct {
|
2015-03-24 17:18:15 +01:00
|
|
|
destroy bool
|
2015-02-12 00:22:03 +01:00
|
|
|
diff *Diff
|
2015-02-13 04:34:21 +01:00
|
|
|
diffLock sync.RWMutex
|
2015-02-11 22:43:07 +01:00
|
|
|
hooks []Hook
|
2015-02-09 20:15:54 +01:00
|
|
|
module *module.Tree
|
|
|
|
providers map[string]ResourceProviderFactory
|
|
|
|
provisioners map[string]ResourceProvisionerFactory
|
2015-02-13 18:05:09 +01:00
|
|
|
sh *stopHook
|
2015-02-09 20:15:54 +01:00
|
|
|
state *State
|
|
|
|
stateLock sync.RWMutex
|
2015-03-24 17:18:15 +01:00
|
|
|
targets []string
|
2015-02-14 02:29:38 +01:00
|
|
|
uiInput UIInput
|
2015-02-09 20:15:54 +01:00
|
|
|
variables map[string]string
|
2015-02-13 18:05:09 +01:00
|
|
|
|
2015-02-14 02:59:54 +01:00
|
|
|
l sync.Mutex // Lock acquired during any task
|
2015-02-17 05:27:29 +01:00
|
|
|
parallelSem Semaphore
|
2015-02-14 02:59:54 +01:00
|
|
|
providerInputConfig map[string]map[string]interface{}
|
|
|
|
runCh <-chan struct{}
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new Context structure.
|
|
|
|
//
|
|
|
|
// Once a Context is creator, the pointer values within ContextOpts
|
|
|
|
// should not be mutated in any way, since the pointers are copied, not
|
|
|
|
// the values themselves.
|
2015-02-14 03:15:36 +01:00
|
|
|
func NewContext(opts *ContextOpts) *Context {
|
2015-02-13 18:05:09 +01:00
|
|
|
// Copy all the hooks and add our stop hook. We don't append directly
|
|
|
|
// to the Config so that we're not modifying that in-place.
|
|
|
|
sh := new(stopHook)
|
|
|
|
hooks := make([]Hook, len(opts.Hooks)+1)
|
|
|
|
copy(hooks, opts.Hooks)
|
|
|
|
hooks[len(opts.Hooks)] = sh
|
|
|
|
|
2015-02-12 00:22:03 +01:00
|
|
|
state := opts.State
|
|
|
|
if state == nil {
|
|
|
|
state = new(State)
|
|
|
|
state.init()
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:27:29 +01:00
|
|
|
// Determine parallelism, default to 10. We do this both to limit
|
|
|
|
// CPU pressure but also to have an extra guard against rate throttling
|
|
|
|
// from providers.
|
|
|
|
par := opts.Parallelism
|
|
|
|
if par == 0 {
|
|
|
|
par = 10
|
|
|
|
}
|
|
|
|
|
2015-05-14 05:09:05 +02:00
|
|
|
// Setup the variables. We first take the variables given to us.
|
|
|
|
// We then merge in the variables set in the environment.
|
|
|
|
variables := make(map[string]string)
|
|
|
|
for _, v := range os.Environ() {
|
|
|
|
if !strings.HasPrefix(v, VarEnvPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off the prefix and get the value after the first "="
|
|
|
|
idx := strings.Index(v, "=")
|
|
|
|
k := v[len(VarEnvPrefix):idx]
|
|
|
|
v = v[idx+1:]
|
|
|
|
|
|
|
|
// Override the command-line set variable
|
|
|
|
variables[k] = v
|
|
|
|
}
|
2015-05-14 18:58:30 +02:00
|
|
|
for k, v := range opts.Variables {
|
|
|
|
variables[k] = v
|
|
|
|
}
|
2015-05-14 05:09:05 +02:00
|
|
|
|
2015-02-14 03:15:36 +01:00
|
|
|
return &Context{
|
2015-03-24 17:18:15 +01:00
|
|
|
destroy: opts.Destroy,
|
2015-02-17 05:27:29 +01:00
|
|
|
diff: opts.Diff,
|
|
|
|
hooks: hooks,
|
|
|
|
module: opts.Module,
|
|
|
|
providers: opts.Providers,
|
|
|
|
provisioners: opts.Provisioners,
|
|
|
|
state: state,
|
2015-03-24 17:18:15 +01:00
|
|
|
targets: opts.Targets,
|
2015-02-17 05:27:29 +01:00
|
|
|
uiInput: opts.UIInput,
|
2015-05-14 05:09:05 +02:00
|
|
|
variables: variables,
|
2015-02-17 05:27:29 +01:00
|
|
|
|
|
|
|
parallelSem: NewSemaphore(par),
|
2015-02-14 02:59:54 +01:00
|
|
|
providerInputConfig: make(map[string]map[string]interface{}),
|
|
|
|
sh: sh,
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:52:31 +02:00
|
|
|
type ContextGraphOpts struct {
|
|
|
|
Validate bool
|
|
|
|
Verbose bool
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:22:08 +01:00
|
|
|
// Graph returns the graph for this config.
|
2015-04-23 17:52:31 +02:00
|
|
|
func (c *Context) Graph(g *ContextGraphOpts) (*Graph, error) {
|
|
|
|
return c.graphBuilder(g).Build(RootModulePath)
|
2015-02-14 03:22:08 +01:00
|
|
|
}
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
// GraphBuilder returns the GraphBuilder that will be used to create
|
|
|
|
// the graphs for this context.
|
2015-04-23 17:52:31 +02:00
|
|
|
func (c *Context) graphBuilder(g *ContextGraphOpts) GraphBuilder {
|
2015-02-05 00:44:23 +01:00
|
|
|
// TODO test
|
|
|
|
providers := make([]string, 0, len(c.providers))
|
|
|
|
for k, _ := range c.providers {
|
|
|
|
providers = append(providers, k)
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
provisioners := make([]string, 0, len(c.provisioners))
|
|
|
|
for k, _ := range c.provisioners {
|
|
|
|
provisioners = append(provisioners, k)
|
|
|
|
}
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
return &BuiltinGraphBuilder{
|
2015-02-09 20:15:54 +01:00
|
|
|
Root: c.module,
|
2015-02-13 21:05:34 +01:00
|
|
|
Diff: c.diff,
|
2015-02-09 20:15:54 +01:00
|
|
|
Providers: providers,
|
|
|
|
Provisioners: provisioners,
|
|
|
|
State: c.state,
|
2015-03-24 17:18:15 +01:00
|
|
|
Targets: c.targets,
|
|
|
|
Destroy: c.destroy,
|
2015-04-23 17:52:31 +02:00
|
|
|
Validate: g.Validate,
|
|
|
|
Verbose: g.Verbose,
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:29:38 +01:00
|
|
|
// Input asks for input to fill variables and provider configurations.
|
|
|
|
// This modifies the configuration in-place, so asking for Input twice
|
|
|
|
// may result in different UI output showing different current values.
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) Input(mode InputMode) error {
|
2015-02-14 02:29:38 +01:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
|
|
|
if mode&InputModeVar != 0 {
|
|
|
|
// Walk the variables first for the root module. We walk them in
|
|
|
|
// alphabetical order for UX reasons.
|
|
|
|
rootConf := c.module.Config()
|
|
|
|
names := make([]string, len(rootConf.Variables))
|
|
|
|
m := make(map[string]*config.Variable)
|
|
|
|
for i, v := range rootConf.Variables {
|
|
|
|
names[i] = v.Name
|
|
|
|
m[v.Name] = v
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
for _, n := range names {
|
2015-07-02 17:22:11 +02:00
|
|
|
// If we only care about unset variables, then if the variable
|
2015-03-07 00:04:12 +01:00
|
|
|
// is set, continue on.
|
|
|
|
if mode&InputModeVarUnset != 0 {
|
|
|
|
if _, ok := c.variables[n]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:29:38 +01:00
|
|
|
v := m[n]
|
|
|
|
switch v.Type() {
|
2015-04-19 01:31:21 +02:00
|
|
|
case config.VariableTypeUnknown:
|
|
|
|
continue
|
2015-02-14 02:29:38 +01:00
|
|
|
case config.VariableTypeMap:
|
|
|
|
continue
|
|
|
|
case config.VariableTypeString:
|
|
|
|
// Good!
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown variable type: %#v", v.Type()))
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:22:11 +02:00
|
|
|
// If the variable is not already set, and the variable defines a
|
|
|
|
// default, use that for the value.
|
|
|
|
if _, ok := c.variables[n]; !ok {
|
|
|
|
if v.Default != nil {
|
|
|
|
c.variables[n] = v.Default.(string)
|
|
|
|
continue
|
|
|
|
}
|
2015-02-14 02:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the user for a value for this variable
|
|
|
|
var value string
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
value, err = c.uiInput.Input(&InputOpts{
|
|
|
|
Id: fmt.Sprintf("var.%s", n),
|
|
|
|
Query: fmt.Sprintf("var.%s", n),
|
|
|
|
Description: v.Description,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error asking for %s: %s", n, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" && v.Required() {
|
|
|
|
// Redo if it is required.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
// No value, just exit the loop. With no value, we just
|
|
|
|
// use whatever is currently set in variables.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != "" {
|
|
|
|
c.variables[n] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:59:54 +01:00
|
|
|
if mode&InputModeProvider != 0 {
|
2015-04-23 17:52:31 +02:00
|
|
|
// Build the graph
|
|
|
|
graph, err := c.Graph(&ContextGraphOpts{Validate: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:59:54 +01:00
|
|
|
// Do the walk
|
2015-04-23 17:52:31 +02:00
|
|
|
if _, err := c.walk(graph, walkInput); err != nil {
|
2015-02-14 02:59:54 +01:00
|
|
|
return err
|
2015-02-14 02:29:38 +01:00
|
|
|
}
|
2015-02-14 02:59:54 +01:00
|
|
|
}
|
2015-02-14 02:29:38 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:46:22 +01:00
|
|
|
// Apply applies the changes represented by this context and returns
|
|
|
|
// the resulting state.
|
|
|
|
//
|
|
|
|
// In addition to returning the resulting state, this context is updated
|
|
|
|
// with the latest state.
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) Apply() (*State, error) {
|
2015-02-13 18:05:09 +01:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2015-02-12 23:46:22 +01:00
|
|
|
// Copy our own state
|
2015-02-24 06:32:27 +01:00
|
|
|
c.state = c.state.DeepCopy()
|
2015-02-12 23:46:22 +01:00
|
|
|
|
2015-04-23 17:52:31 +02:00
|
|
|
// Build the graph
|
|
|
|
graph, err := c.Graph(&ContextGraphOpts{Validate: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-12 23:46:22 +01:00
|
|
|
// Do the walk
|
2015-10-02 20:18:33 +02:00
|
|
|
if c.destroy {
|
|
|
|
_, err = c.walk(graph, walkDestroy)
|
|
|
|
} else {
|
|
|
|
_, err = c.walk(graph, walkApply)
|
|
|
|
}
|
2015-02-12 23:46:22 +01:00
|
|
|
|
|
|
|
// Clean out any unused things
|
|
|
|
c.state.prune()
|
|
|
|
|
|
|
|
return c.state, err
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:22:03 +01:00
|
|
|
// Plan generates an execution plan for the given context.
|
|
|
|
//
|
|
|
|
// The execution plan encapsulates the context and can be stored
|
|
|
|
// in order to reinstantiate a context later for Apply.
|
|
|
|
//
|
|
|
|
// Plan also updates the diff of this context to be the diff generated
|
|
|
|
// by the plan, so Apply can be called after.
|
2015-03-24 17:18:15 +01:00
|
|
|
func (c *Context) Plan() (*Plan, error) {
|
2015-02-13 18:05:09 +01:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2015-02-12 00:22:03 +01:00
|
|
|
p := &Plan{
|
|
|
|
Module: c.module,
|
|
|
|
Vars: c.variables,
|
|
|
|
State: c.state,
|
|
|
|
}
|
|
|
|
|
|
|
|
var operation walkOperation
|
2015-03-24 17:18:15 +01:00
|
|
|
if c.destroy {
|
2015-02-12 00:22:03 +01:00
|
|
|
operation = walkPlanDestroy
|
|
|
|
} else {
|
|
|
|
// Set our state to be something temporary. We do this so that
|
|
|
|
// the plan can update a fake state so that variables work, then
|
|
|
|
// we replace it back with our old state.
|
|
|
|
old := c.state
|
|
|
|
if old == nil {
|
|
|
|
c.state = &State{}
|
|
|
|
c.state.init()
|
|
|
|
} else {
|
2015-02-24 06:32:27 +01:00
|
|
|
c.state = old.DeepCopy()
|
2015-02-12 00:22:03 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
c.state = old
|
|
|
|
}()
|
|
|
|
|
|
|
|
operation = walkPlan
|
|
|
|
}
|
|
|
|
|
2015-02-13 04:34:21 +01:00
|
|
|
// Setup our diff
|
|
|
|
c.diffLock.Lock()
|
|
|
|
c.diff = new(Diff)
|
|
|
|
c.diff.init()
|
|
|
|
c.diffLock.Unlock()
|
|
|
|
|
2015-04-23 17:52:31 +02:00
|
|
|
// Build the graph
|
|
|
|
graph, err := c.Graph(&ContextGraphOpts{Validate: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-12 00:22:03 +01:00
|
|
|
// Do the walk
|
2015-04-23 17:52:31 +02:00
|
|
|
if _, err := c.walk(graph, operation); err != nil {
|
2015-02-12 02:01:08 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 04:34:21 +01:00
|
|
|
p.Diff = c.diff
|
2015-02-12 00:22:03 +01:00
|
|
|
|
2015-05-06 00:24:44 +02:00
|
|
|
// Now that we have a diff, we can build the exact graph that Apply will use
|
|
|
|
// and catch any possible cycles during the Plan phase.
|
|
|
|
if _, err := c.Graph(&ContextGraphOpts{Validate: true}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-13 04:34:21 +01:00
|
|
|
return p, nil
|
2015-02-12 00:22:03 +01:00
|
|
|
}
|
|
|
|
|
2015-02-10 17:58:14 +01:00
|
|
|
// Refresh goes through all the resources in the state and refreshes them
|
|
|
|
// to their latest state. This will update the state that this context
|
|
|
|
// works with, along with returning it.
|
|
|
|
//
|
|
|
|
// Even in the case an error is returned, the state will be returned and
|
|
|
|
// will potentially be partially updated.
|
2015-02-14 03:19:41 +01:00
|
|
|
func (c *Context) Refresh() (*State, error) {
|
2015-02-13 18:05:09 +01:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2015-02-11 22:52:13 +01:00
|
|
|
// Copy our own state
|
2015-02-24 06:32:27 +01:00
|
|
|
c.state = c.state.DeepCopy()
|
2015-02-11 22:52:13 +01:00
|
|
|
|
2015-04-23 17:52:31 +02:00
|
|
|
// Build the graph
|
|
|
|
graph, err := c.Graph(&ContextGraphOpts{Validate: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-02-11 22:52:13 +01:00
|
|
|
// Do the walk
|
2015-04-23 17:52:31 +02:00
|
|
|
if _, err := c.walk(graph, walkRefresh); err != nil {
|
2015-02-14 03:19:41 +01:00
|
|
|
return nil, err
|
2015-02-10 17:58:14 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 17:51:17 +01:00
|
|
|
// Clean out any unused things
|
|
|
|
c.state.prune()
|
|
|
|
|
2015-02-11 17:48:45 +01:00
|
|
|
return c.state, nil
|
2015-02-10 17:58:14 +01:00
|
|
|
}
|
|
|
|
|
2015-02-13 18:05:09 +01:00
|
|
|
// Stop stops the running task.
|
|
|
|
//
|
|
|
|
// Stop will block until the task completes.
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) Stop() {
|
2015-02-13 18:05:09 +01:00
|
|
|
c.l.Lock()
|
|
|
|
ch := c.runCh
|
|
|
|
|
|
|
|
// If we aren't running, then just return
|
|
|
|
if ch == nil {
|
|
|
|
c.l.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the hook we want to stop
|
|
|
|
c.sh.Stop()
|
|
|
|
|
|
|
|
// Wait for us to stop
|
|
|
|
c.l.Unlock()
|
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
|
2015-02-05 00:44:23 +01:00
|
|
|
// Validate validates the configuration and returns any warnings or errors.
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) Validate() ([]string, []error) {
|
2015-02-13 18:05:09 +01:00
|
|
|
v := c.acquireRun()
|
|
|
|
defer c.releaseRun(v)
|
|
|
|
|
2015-02-07 18:53:46 +01:00
|
|
|
var errs error
|
2015-02-05 02:02:18 +01:00
|
|
|
|
|
|
|
// Validate the configuration itself
|
|
|
|
if err := c.module.Validate(); err != nil {
|
2015-02-07 18:53:46 +01:00
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This only needs to be done for the root module, since inter-module
|
|
|
|
// variables are validated in the module tree.
|
|
|
|
if config := c.module.Config(); config != nil {
|
|
|
|
// Validate the user variables
|
|
|
|
if err := smcUserVariables(config, c.variables); len(err) > 0 {
|
|
|
|
errs = multierror.Append(errs, err...)
|
|
|
|
}
|
2015-02-05 02:02:18 +01:00
|
|
|
}
|
|
|
|
|
2015-06-25 07:43:55 +02:00
|
|
|
// If we have errors at this point, the graphing has no chance,
|
|
|
|
// so just bail early.
|
|
|
|
if errs != nil {
|
|
|
|
return nil, []error{errs}
|
|
|
|
}
|
|
|
|
|
2015-05-06 00:24:44 +02:00
|
|
|
// Build the graph so we can walk it and run Validate on nodes.
|
|
|
|
// We also validate the graph generated here, but this graph doesn't
|
|
|
|
// necessarily match the graph that Plan will generate, so we'll validate the
|
|
|
|
// graph again later after Planning.
|
|
|
|
graph, err := c.Graph(&ContextGraphOpts{Validate: true})
|
2015-04-23 17:52:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:58:14 +01:00
|
|
|
// Walk
|
2015-04-23 17:52:31 +02:00
|
|
|
walker, err := c.walk(graph, walkValidate)
|
2015-02-05 00:44:23 +01:00
|
|
|
if err != nil {
|
2015-02-07 18:55:11 +01:00
|
|
|
return nil, multierror.Append(errs, err).Errors
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
|
|
|
|
2015-02-07 22:29:55 +01:00
|
|
|
// Return the result
|
|
|
|
rerrs := multierror.Append(errs, walker.ValidationErrors...)
|
|
|
|
return walker.ValidationWarnings, rerrs.Errors
|
2015-02-05 00:44:23 +01:00
|
|
|
}
|
2015-02-10 17:58:14 +01:00
|
|
|
|
2015-03-07 00:12:39 +01:00
|
|
|
// Module returns the module tree associated with this context.
|
|
|
|
func (c *Context) Module() *module.Tree {
|
|
|
|
return c.module
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:04:12 +01:00
|
|
|
// Variables will return the mapping of variables that were defined
|
|
|
|
// for this Context. If Input was called, this mapping may be different
|
|
|
|
// than what was given.
|
|
|
|
func (c *Context) Variables() map[string]string {
|
|
|
|
return c.variables
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVariable sets a variable after a context has already been built.
|
|
|
|
func (c *Context) SetVariable(k, v string) {
|
|
|
|
c.variables[k] = v
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) acquireRun() chan<- struct{} {
|
2015-02-13 18:05:09 +01:00
|
|
|
c.l.Lock()
|
|
|
|
defer c.l.Unlock()
|
|
|
|
|
|
|
|
// Wait for no channel to exist
|
|
|
|
for c.runCh != nil {
|
|
|
|
c.l.Unlock()
|
|
|
|
ch := c.runCh
|
|
|
|
<-ch
|
|
|
|
c.l.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
c.runCh = ch
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:15:36 +01:00
|
|
|
func (c *Context) releaseRun(ch chan<- struct{}) {
|
2015-02-13 18:05:09 +01:00
|
|
|
c.l.Lock()
|
|
|
|
defer c.l.Unlock()
|
|
|
|
|
|
|
|
close(ch)
|
|
|
|
c.runCh = nil
|
|
|
|
c.sh.Reset()
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:52:31 +02:00
|
|
|
func (c *Context) walk(
|
|
|
|
graph *Graph, operation walkOperation) (*ContextGraphWalker, error) {
|
2015-02-10 17:58:14 +01:00
|
|
|
// Walk the graph
|
2015-10-11 19:45:33 +02:00
|
|
|
log.Printf("[DEBUG] Starting graph walk: %s", operation.String())
|
2015-02-10 17:58:14 +01:00
|
|
|
walker := &ContextGraphWalker{Context: c, Operation: operation}
|
|
|
|
return walker, graph.Walk(walker)
|
|
|
|
}
|