2013-03-25 19:29:26 -04:00
|
|
|
package packer
|
|
|
|
|
2013-04-21 15:36:55 -04:00
|
|
|
import (
|
2013-08-13 12:52:32 -04:00
|
|
|
"bytes"
|
2013-04-21 15:36:55 -04:00
|
|
|
"fmt"
|
2013-06-07 13:23:15 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-08-09 19:50:24 -04:00
|
|
|
jsonutil "github.com/mitchellh/packer/common/json"
|
2013-08-13 12:52:32 -04:00
|
|
|
"io"
|
2013-08-09 17:43:34 -04:00
|
|
|
"io/ioutil"
|
2013-08-13 12:52:32 -04:00
|
|
|
"os"
|
2013-07-13 21:29:14 -04:00
|
|
|
"sort"
|
2013-04-21 15:36:55 -04:00
|
|
|
)
|
2013-03-25 19:29:26 -04:00
|
|
|
|
|
|
|
// The rawTemplate struct represents the structure of a template read
|
|
|
|
// directly from a file. The builders and other components map just to
|
|
|
|
// "interface{}" pointers since we actually don't know what their contents
|
|
|
|
// are until we read the "type" field.
|
|
|
|
type rawTemplate struct {
|
2013-08-31 20:33:17 -04:00
|
|
|
Variables map[string]interface{}
|
2013-06-17 17:54:04 -04:00
|
|
|
Builders []map[string]interface{}
|
|
|
|
Hooks map[string][]string
|
|
|
|
Provisioners []map[string]interface{}
|
2013-07-13 21:29:14 -04:00
|
|
|
PostProcessors []interface{} `mapstructure:"post-processors"`
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 20:33:27 -04:00
|
|
|
// The Template struct represents a parsed template, parsed into the most
|
|
|
|
// completed form it can be without additional processing by the caller.
|
2013-03-25 19:29:26 -04:00
|
|
|
type Template struct {
|
2013-08-31 20:33:17 -04:00
|
|
|
Variables map[string]RawVariable
|
2013-08-13 12:10:49 -04:00
|
|
|
Builders map[string]RawBuilderConfig
|
2013-06-18 12:27:08 -04:00
|
|
|
Hooks map[string][]string
|
2013-08-13 12:10:49 -04:00
|
|
|
PostProcessors [][]RawPostProcessorConfig
|
|
|
|
Provisioners []RawProvisionerConfig
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:10:49 -04:00
|
|
|
// The RawBuilderConfig struct represents a raw, unprocessed builder
|
2013-03-25 19:29:26 -04:00
|
|
|
// configuration. It contains the name of the builder as well as the
|
|
|
|
// raw configuration. If requested, this is used to compile into a full
|
|
|
|
// builder configuration at some point.
|
2013-08-13 12:10:49 -04:00
|
|
|
type RawBuilderConfig struct {
|
2013-06-07 13:23:15 -04:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
RawConfig interface{}
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:10:49 -04:00
|
|
|
// RawPostProcessorConfig represents a raw, unprocessed post-processor
|
2013-06-18 12:27:08 -04:00
|
|
|
// configuration. It contains the type of the post processor as well as the
|
|
|
|
// raw configuration that is handed to the post-processor for it to process.
|
2013-08-13 12:10:49 -04:00
|
|
|
type RawPostProcessorConfig struct {
|
2013-09-20 14:13:43 -04:00
|
|
|
TemplateOnlyExcept `mapstructure:",squash"`
|
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
Type string
|
|
|
|
KeepInputArtifact bool `mapstructure:"keep_input_artifact"`
|
2013-08-19 19:00:13 -04:00
|
|
|
RawConfig map[string]interface{}
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:10:49 -04:00
|
|
|
// RawProvisionerConfig represents a raw, unprocessed provisioner configuration.
|
2013-05-22 18:12:36 -04:00
|
|
|
// It contains the type of the provisioner as well as the raw configuration
|
|
|
|
// that is handed to the provisioner for it to process.
|
2013-08-13 12:10:49 -04:00
|
|
|
type RawProvisionerConfig struct {
|
2013-09-20 14:13:43 -04:00
|
|
|
TemplateOnlyExcept `mapstructure:",squash"`
|
|
|
|
|
2013-06-07 13:23:15 -04:00
|
|
|
Type string
|
|
|
|
Override map[string]interface{}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
RawConfig interface{}
|
2013-05-22 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 20:33:17 -04:00
|
|
|
// RawVariable represents a variable configuration within a template.
|
|
|
|
type RawVariable struct {
|
|
|
|
Default string
|
|
|
|
Required bool
|
|
|
|
}
|
|
|
|
|
2013-04-20 20:33:27 -04:00
|
|
|
// ParseTemplate takes a byte slice and parses a Template from it, returning
|
2013-05-22 17:36:21 -04:00
|
|
|
// the template and possibly errors while loading the template. The error
|
|
|
|
// could potentially be a MultiError, representing multiple errors. Knowing
|
|
|
|
// and checking for this can be useful, if you wish to format it in a certain
|
|
|
|
// way.
|
2013-04-15 18:48:42 -04:00
|
|
|
func ParseTemplate(data []byte) (t *Template, err error) {
|
2013-07-13 21:29:14 -04:00
|
|
|
var rawTplInterface interface{}
|
2013-08-09 19:50:24 -04:00
|
|
|
err = jsonutil.Unmarshal(data, &rawTplInterface)
|
2013-03-25 19:29:26 -04:00
|
|
|
if err != nil {
|
2013-04-15 18:48:42 -04:00
|
|
|
return
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:29:14 -04:00
|
|
|
// Decode the raw template interface into the actual rawTemplate
|
|
|
|
// structure, checking for any extranneous keys along the way.
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
var rawTpl rawTemplate
|
|
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
|
|
|
Metadata: &md,
|
|
|
|
Result: &rawTpl,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = decoder.Decode(rawTplInterface)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errors := make([]error, 0)
|
|
|
|
|
|
|
|
if len(md.Unused) > 0 {
|
|
|
|
sort.Strings(md.Unused)
|
|
|
|
for _, unused := range md.Unused {
|
|
|
|
errors = append(
|
|
|
|
errors, fmt.Errorf("Unknown root level key in template: '%s'", unused))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:04:19 -04:00
|
|
|
t = &Template{}
|
2013-08-31 20:33:17 -04:00
|
|
|
t.Variables = make(map[string]RawVariable)
|
2013-08-13 12:10:49 -04:00
|
|
|
t.Builders = make(map[string]RawBuilderConfig)
|
2013-05-11 01:47:20 -04:00
|
|
|
t.Hooks = rawTpl.Hooks
|
2013-08-13 12:10:49 -04:00
|
|
|
t.PostProcessors = make([][]RawPostProcessorConfig, len(rawTpl.PostProcessors))
|
|
|
|
t.Provisioners = make([]RawProvisionerConfig, len(rawTpl.Provisioners))
|
2013-04-15 20:04:19 -04:00
|
|
|
|
2013-08-09 16:29:38 -04:00
|
|
|
// Gather all the variables
|
|
|
|
for k, v := range rawTpl.Variables {
|
2013-08-31 20:33:17 -04:00
|
|
|
var variable RawVariable
|
|
|
|
variable.Default = ""
|
|
|
|
variable.Required = v == nil
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
def, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("variable '%s': default value must be string or null", k))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
variable.Default = def
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Variables[k] = variable
|
2013-08-09 16:29:38 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 17:36:21 -04:00
|
|
|
// Gather all the builders
|
2013-05-22 17:11:34 -04:00
|
|
|
for i, v := range rawTpl.Builders {
|
2013-08-13 12:10:49 -04:00
|
|
|
var raw RawBuilderConfig
|
2013-06-07 13:23:15 -04:00
|
|
|
if err := mapstructure.Decode(v, &raw); err != nil {
|
|
|
|
if merr, ok := err.(*mapstructure.Error); ok {
|
|
|
|
for _, err := range merr.Errors {
|
|
|
|
errors = append(errors, fmt.Errorf("builder %d: %s", i+1, err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errors = append(errors, fmt.Errorf("builder %d: %s", i+1, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw.Type == "" {
|
2013-05-22 17:36:33 -04:00
|
|
|
errors = append(errors, fmt.Errorf("builder %d: missing 'type'", i+1))
|
2013-05-22 17:11:34 -04:00
|
|
|
continue
|
2013-04-15 20:04:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to get the name of the builder. If the "name" key
|
|
|
|
// missing, use the "type" field, which is guaranteed to exist
|
|
|
|
// at this point.
|
2013-06-07 13:23:15 -04:00
|
|
|
if raw.Name == "" {
|
|
|
|
raw.Name = raw.Type
|
2013-05-22 17:14:40 -04:00
|
|
|
}
|
2013-04-15 20:04:19 -04:00
|
|
|
|
2013-05-22 17:11:34 -04:00
|
|
|
// Check if we already have a builder with this name and error if so
|
2013-06-07 13:23:15 -04:00
|
|
|
if _, ok := t.Builders[raw.Name]; ok {
|
|
|
|
errors = append(errors, fmt.Errorf("builder with name '%s' already exists", raw.Name))
|
2013-05-22 17:11:34 -04:00
|
|
|
continue
|
2013-04-15 20:04:19 -04:00
|
|
|
}
|
|
|
|
|
2013-07-19 10:36:13 -04:00
|
|
|
// Now that we have the name, remove it from the config - as the builder
|
|
|
|
// itself doesn't know about, and it will cause a validation error.
|
|
|
|
delete(v, "name")
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
raw.RawConfig = v
|
2013-06-07 13:23:15 -04:00
|
|
|
|
|
|
|
t.Builders[raw.Name] = raw
|
2013-04-15 18:48:42 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:27:08 -04:00
|
|
|
// Gather all the post-processors. This is a complicated process since there
|
|
|
|
// are actually three different formats that the user can use to define
|
|
|
|
// a post-processor.
|
|
|
|
for i, rawV := range rawTpl.PostProcessors {
|
2013-08-19 18:55:30 -04:00
|
|
|
rawPP, err := parsePostProcessor(i, rawV)
|
2013-06-18 12:27:08 -04:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
configs := make([]RawPostProcessorConfig, 0, len(rawPP))
|
2013-06-18 12:27:08 -04:00
|
|
|
for j, pp := range rawPP {
|
2013-09-20 14:13:43 -04:00
|
|
|
var config RawPostProcessorConfig
|
|
|
|
if err := mapstructure.Decode(pp, &config); err != nil {
|
2013-06-18 12:27:08 -04:00
|
|
|
if merr, ok := err.(*mapstructure.Error); ok {
|
|
|
|
for _, err := range merr.Errors {
|
2013-09-20 14:13:43 -04:00
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("Post-processor #%d.%d: %s", i+1, j+1, err))
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-20 14:13:43 -04:00
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("Post-processor %d.%d: %s", i+1, j+1, err))
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Type == "" {
|
2013-09-20 14:13:43 -04:00
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("Post-processor %d.%d: missing 'type'", i+1, j+1))
|
2013-06-18 12:27:08 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:00:13 -04:00
|
|
|
// Remove the input keep_input_artifact option
|
2013-09-20 14:13:43 -04:00
|
|
|
config.TemplateOnlyExcept.Prune(pp)
|
2013-08-19 19:00:13 -04:00
|
|
|
delete(pp, "keep_input_artifact")
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
// Verify that the only settings are good
|
|
|
|
if errs := config.TemplateOnlyExcept.Validate(t.Builders); len(errs) > 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("Post-processor %d.%d: %s", i+1, j+1, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
config.RawConfig = pp
|
2013-09-20 14:13:43 -04:00
|
|
|
|
|
|
|
// Add it to the list of configs
|
|
|
|
configs = append(configs, config)
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
2013-09-20 14:13:43 -04:00
|
|
|
|
|
|
|
t.PostProcessors[i] = configs
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
// Gather all the provisioners
|
|
|
|
for i, v := range rawTpl.Provisioners {
|
2013-06-07 13:23:15 -04:00
|
|
|
raw := &t.Provisioners[i]
|
|
|
|
if err := mapstructure.Decode(v, raw); err != nil {
|
|
|
|
if merr, ok := err.(*mapstructure.Error); ok {
|
|
|
|
for _, err := range merr.Errors {
|
|
|
|
errors = append(errors, fmt.Errorf("provisioner %d: %s", i+1, err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errors = append(errors, fmt.Errorf("provisioner %d: %s", i+1, err))
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:12:36 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:23:15 -04:00
|
|
|
if raw.Type == "" {
|
|
|
|
errors = append(errors, fmt.Errorf("provisioner %d: missing 'type'", i+1))
|
2013-05-22 18:12:36 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:49:35 -04:00
|
|
|
// Delete the keys that we used
|
2013-09-20 14:13:43 -04:00
|
|
|
raw.TemplateOnlyExcept.Prune(v)
|
2013-07-14 17:07:11 -04:00
|
|
|
delete(v, "override")
|
|
|
|
|
2013-08-28 00:34:55 -04:00
|
|
|
// Verify that the override keys exist...
|
|
|
|
for name, _ := range raw.Override {
|
|
|
|
if _, ok := t.Builders[name]; !ok {
|
|
|
|
errors = append(
|
|
|
|
errors, fmt.Errorf("provisioner %d: build '%s' not found for override", i+1, name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:49:35 -04:00
|
|
|
// Verify that the only settings are good
|
2013-09-20 14:13:43 -04:00
|
|
|
if errs := raw.TemplateOnlyExcept.Validate(t.Builders); len(errs) > 0 {
|
|
|
|
errors = append(errors, errs...)
|
2013-09-20 13:49:35 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:11:49 -04:00
|
|
|
raw.RawConfig = v
|
2013-05-22 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
2013-06-29 17:02:20 -04:00
|
|
|
if len(t.Builders) == 0 {
|
|
|
|
errors = append(errors, fmt.Errorf("No builders are defined in the template."))
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:36:21 -04:00
|
|
|
// If there were errors, we put it into a MultiError and return
|
2013-05-22 17:11:34 -04:00
|
|
|
if len(errors) > 0 {
|
|
|
|
err = &MultiError{errors}
|
2013-07-13 21:29:14 -04:00
|
|
|
t = nil
|
2013-05-22 17:11:34 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-04-15 18:48:42 -04:00
|
|
|
return
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-08-09 17:43:34 -04:00
|
|
|
// ParseTemplateFile takes the given template file and parses it into
|
|
|
|
// a single template.
|
|
|
|
func ParseTemplateFile(path string) (*Template, error) {
|
2013-08-13 12:52:32 -04:00
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if path == "-" {
|
|
|
|
// Read from stdin...
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err := io.Copy(buf, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data = buf.Bytes()
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
data, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-09 17:43:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParseTemplate(data)
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:55:30 -04:00
|
|
|
func parsePostProcessor(i int, rawV interface{}) (result []map[string]interface{}, errors []error) {
|
2013-06-18 12:27:08 -04:00
|
|
|
switch v := rawV.(type) {
|
|
|
|
case string:
|
|
|
|
result = []map[string]interface{}{
|
|
|
|
{"type": v},
|
|
|
|
}
|
|
|
|
case map[string]interface{}:
|
|
|
|
result = []map[string]interface{}{v}
|
|
|
|
case []interface{}:
|
|
|
|
result = make([]map[string]interface{}, len(v))
|
|
|
|
errors = make([]error, 0)
|
|
|
|
for j, innerRawV := range v {
|
|
|
|
switch innerV := innerRawV.(type) {
|
|
|
|
case string:
|
|
|
|
result[j] = map[string]interface{}{"type": innerV}
|
|
|
|
case map[string]interface{}:
|
|
|
|
result[j] = innerV
|
|
|
|
case []interface{}:
|
|
|
|
errors = append(
|
|
|
|
errors,
|
|
|
|
fmt.Errorf("Post-processor %d.%d: sequences not allowed to be nested in sequences", i+1, j+1))
|
2013-07-01 18:07:09 -04:00
|
|
|
default:
|
|
|
|
errors = append(errors, fmt.Errorf("Post-processor %d.%d is in a bad format.", i+1, j+1))
|
2013-07-01 17:46:32 -04:00
|
|
|
}
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
2013-07-01 17:46:32 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
if len(errors) == 0 {
|
|
|
|
errors = nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
result = nil
|
|
|
|
errors = []error{fmt.Errorf("Post-processor %d is in a bad format.", i+1)}
|
2013-06-18 12:27:08 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
return
|
|
|
|
}
|
2013-06-18 12:27:08 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// BuildNames returns a slice of the available names of builds that
|
|
|
|
// this template represents.
|
|
|
|
func (t *Template) BuildNames() []string {
|
|
|
|
names := make([]string, 0, len(t.Builders))
|
|
|
|
for name, _ := range t.Builders {
|
|
|
|
names = append(names, name)
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
return names
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// Build returns a Build for the given name.
|
|
|
|
//
|
|
|
|
// If the build does not exist as part of this template, an error is
|
|
|
|
// returned.
|
|
|
|
func (t *Template) Build(name string, components *ComponentFinder) (b Build, err error) {
|
|
|
|
// Setup the Builder
|
|
|
|
builderConfig, ok := t.Builders[name]
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("No such build found in template: %s", name)
|
|
|
|
return
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// We panic if there is no builder function because this is really
|
|
|
|
// an internal bug that always needs to be fixed, not an error.
|
|
|
|
if components.Builder == nil {
|
|
|
|
panic("no builder function")
|
|
|
|
}
|
2013-05-22 20:02:34 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// Panic if there are provisioners on the template but no provisioner
|
|
|
|
// component finder. This is always an internal error, so we panic.
|
|
|
|
if len(t.Provisioners) > 0 && components.Provisioner == nil {
|
|
|
|
panic("no provisioner function")
|
|
|
|
}
|
2013-05-22 20:05:36 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
builder, err := components.Builder(builderConfig.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-07 23:42:49 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
if builder == nil {
|
|
|
|
err = fmt.Errorf("Builder type not found: %s", builderConfig.Type)
|
|
|
|
return
|
|
|
|
}
|
2013-04-21 15:36:55 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// Gather the Hooks
|
|
|
|
hooks := make(map[string][]Hook)
|
|
|
|
for tplEvent, tplHooks := range t.Hooks {
|
|
|
|
curHooks := make([]Hook, 0, len(tplHooks))
|
2013-05-11 13:00:37 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
for _, hookName := range tplHooks {
|
|
|
|
var hook Hook
|
|
|
|
hook, err = components.Hook(hookName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-11 13:00:37 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
if hook == nil {
|
|
|
|
err = fmt.Errorf("Hook not found: %s", hookName)
|
|
|
|
return
|
2013-05-11 13:00:37 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
curHooks = append(curHooks, hook)
|
2013-05-11 13:00:37 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
hooks[tplEvent] = curHooks
|
|
|
|
}
|
2013-06-18 12:58:39 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// Prepare the post-processors
|
|
|
|
postProcessors := make([][]coreBuildPostProcessor, 0, len(t.PostProcessors))
|
|
|
|
for _, rawPPs := range t.PostProcessors {
|
2013-09-20 14:13:43 -04:00
|
|
|
current := make([]coreBuildPostProcessor, 0, len(rawPPs))
|
|
|
|
for _, rawPP := range rawPPs {
|
|
|
|
if rawPP.TemplateOnlyExcept.Skip(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
pp, err := components.PostProcessor(rawPP.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-06-18 12:58:39 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
if pp == nil {
|
|
|
|
return nil, fmt.Errorf("PostProcessor type not found: %s", rawPP.Type)
|
2013-07-01 17:46:32 -04:00
|
|
|
}
|
2013-05-22 19:15:57 -04:00
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
current = append(current, coreBuildPostProcessor{
|
2013-07-01 18:07:09 -04:00
|
|
|
processor: pp,
|
|
|
|
processorType: rawPP.Type,
|
2013-08-13 12:11:49 -04:00
|
|
|
config: rawPP.RawConfig,
|
2013-07-01 18:07:09 -04:00
|
|
|
keepInputArtifact: rawPP.KeepInputArtifact,
|
2013-09-20 14:13:43 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no post-processors in this chain, just continue.
|
|
|
|
// This can happen if the post-processors skip certain builds.
|
|
|
|
if len(current) == 0 {
|
|
|
|
continue
|
2013-07-01 18:07:09 -04:00
|
|
|
}
|
2013-05-22 19:15:57 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
postProcessors = append(postProcessors, current)
|
|
|
|
}
|
2013-06-07 13:35:26 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
// Prepare the provisioners
|
|
|
|
provisioners := make([]coreBuildProvisioner, 0, len(t.Provisioners))
|
|
|
|
for _, rawProvisioner := range t.Provisioners {
|
2013-09-20 14:13:43 -04:00
|
|
|
if rawProvisioner.TemplateOnlyExcept.Skip(name) {
|
|
|
|
continue
|
2013-09-20 13:49:35 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
var provisioner Provisioner
|
|
|
|
provisioner, err = components.Provisioner(rawProvisioner.Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-01 17:46:32 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
if provisioner == nil {
|
|
|
|
err = fmt.Errorf("Provisioner type not found: %s", rawProvisioner.Type)
|
|
|
|
return
|
2013-06-07 13:35:26 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
configs := make([]interface{}, 1, 2)
|
2013-08-13 12:11:49 -04:00
|
|
|
configs[0] = rawProvisioner.RawConfig
|
2013-07-01 18:07:09 -04:00
|
|
|
|
|
|
|
if rawProvisioner.Override != nil {
|
|
|
|
if override, ok := rawProvisioner.Override[name]; ok {
|
|
|
|
configs = append(configs, override)
|
|
|
|
}
|
2013-07-01 17:46:32 -04:00
|
|
|
}
|
2013-05-22 19:15:57 -04:00
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
coreProv := coreBuildProvisioner{provisioner, configs}
|
|
|
|
provisioners = append(provisioners, coreProv)
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:33:17 -04:00
|
|
|
// Prepare the variables
|
|
|
|
variables := make(map[string]coreBuildVariable)
|
|
|
|
for k, v := range t.Variables {
|
|
|
|
variables[k] = coreBuildVariable{
|
|
|
|
Default: v.Default,
|
|
|
|
Required: v.Required,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 18:07:09 -04:00
|
|
|
b = &coreBuild{
|
|
|
|
name: name,
|
|
|
|
builder: builder,
|
2013-08-13 12:11:49 -04:00
|
|
|
builderConfig: builderConfig.RawConfig,
|
2013-07-01 18:07:09 -04:00
|
|
|
builderType: builderConfig.Type,
|
|
|
|
hooks: hooks,
|
|
|
|
postProcessors: postProcessors,
|
|
|
|
provisioners: provisioners,
|
2013-08-31 20:33:17 -04:00
|
|
|
variables: variables,
|
2013-04-21 15:36:55 -04:00
|
|
|
}
|
2013-07-01 18:07:09 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2013-09-20 14:13:43 -04:00
|
|
|
|
|
|
|
// TemplateOnlyExcept contains the logic required for "only" and "except"
|
|
|
|
// meta-parameters.
|
|
|
|
type TemplateOnlyExcept struct {
|
|
|
|
Only []string
|
|
|
|
Except []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune will prune out the used values from the raw map.
|
|
|
|
func (t *TemplateOnlyExcept) Prune(raw map[string]interface{}) {
|
|
|
|
delete(raw, "except")
|
|
|
|
delete(raw, "only")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip tests if we should skip putting this item onto a build.
|
|
|
|
func (t *TemplateOnlyExcept) Skip(name string) bool {
|
|
|
|
if len(t.Only) > 0 {
|
|
|
|
onlyFound := false
|
|
|
|
for _, n := range t.Only {
|
|
|
|
if n == name {
|
|
|
|
onlyFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !onlyFound {
|
|
|
|
// Skip this provisioner
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:16:33 -04:00
|
|
|
// If the name is in the except list, then skip that
|
|
|
|
for _, n := range t.Except {
|
|
|
|
if n == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validates the only/except parameters.
|
|
|
|
func (t *TemplateOnlyExcept) Validate(b map[string]RawBuilderConfig) (e []error) {
|
2013-09-20 14:20:05 -04:00
|
|
|
if len(t.Only) > 0 && len(t.Except) > 0 {
|
|
|
|
e = append(e,
|
|
|
|
fmt.Errorf("Only one of 'only' or 'except' may be specified."))
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
if len(t.Only) > 0 {
|
|
|
|
for _, n := range t.Only {
|
|
|
|
if _, ok := b[n]; !ok {
|
|
|
|
e = append(e,
|
|
|
|
fmt.Errorf("'only' specified builder '%s' not found", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:18:00 -04:00
|
|
|
for _, n := range t.Except {
|
|
|
|
if _, ok := b[n]; !ok {
|
|
|
|
e = append(e,
|
|
|
|
fmt.Errorf("'except' specified builder '%s' not found", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 14:13:43 -04:00
|
|
|
return
|
|
|
|
}
|