2015-05-27 13:44:10 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-05-27 14:34:47 -04:00
|
|
|
"fmt"
|
2015-05-27 13:44:10 -04:00
|
|
|
"reflect"
|
2015-05-27 14:34:47 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-05-27 13:44:10 -04:00
|
|
|
|
2015-05-27 14:34:47 -04:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-05-27 13:44:10 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DecodeOpts are the options for decoding configuration.
|
|
|
|
type DecodeOpts struct {
|
2015-05-27 15:55:41 -04:00
|
|
|
// Metadata, if non-nil, will be set to the metadata post-decode
|
|
|
|
Metadata *mapstructure.Metadata
|
|
|
|
|
2015-05-27 13:44:10 -04:00
|
|
|
// Interpolate, if true, will automatically interpolate the
|
|
|
|
// configuration with the given InterpolateContext. User variables
|
|
|
|
// will be automatically detected and added in-place to the given
|
|
|
|
// context.
|
|
|
|
Interpolate bool
|
|
|
|
InterpolateContext *interpolate.Context
|
|
|
|
InterpolateFilter *interpolate.RenderFilter
|
2019-05-23 08:10:41 -04:00
|
|
|
|
|
|
|
DecodeHooks []mapstructure.DecodeHookFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultDecodeHookFuncs = []mapstructure.DecodeHookFunc{
|
|
|
|
uint8ToStringHook,
|
|
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
2015-05-27 13:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes the configuration into the target and optionally
|
|
|
|
// automatically interpolates all the configuration as it goes.
|
|
|
|
func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error {
|
|
|
|
if config == nil {
|
|
|
|
config = &DecodeOpts{Interpolate: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate first
|
|
|
|
if config.Interpolate {
|
|
|
|
// Detect user variables from the raws and merge them into our context
|
|
|
|
ctx, err := DetectContext(raws...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if config.InterpolateContext == nil {
|
|
|
|
config.InterpolateContext = ctx
|
|
|
|
} else {
|
2015-06-30 13:42:55 -04:00
|
|
|
config.InterpolateContext.BuildName = ctx.BuildName
|
|
|
|
config.InterpolateContext.BuildType = ctx.BuildType
|
2015-06-14 01:56:36 -04:00
|
|
|
config.InterpolateContext.TemplatePath = ctx.TemplatePath
|
2015-05-27 13:44:10 -04:00
|
|
|
config.InterpolateContext.UserVariables = ctx.UserVariables
|
|
|
|
}
|
|
|
|
ctx = config.InterpolateContext
|
|
|
|
|
|
|
|
// Render everything
|
|
|
|
for i, raw := range raws {
|
|
|
|
m, err := interpolate.RenderMap(raw, ctx, config.InterpolateFilter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
raws[i] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:10:41 -04:00
|
|
|
decodeHookFuncs := DefaultDecodeHookFuncs
|
|
|
|
if len(config.DecodeHooks) != 0 {
|
|
|
|
decodeHookFuncs = config.DecodeHooks
|
|
|
|
}
|
|
|
|
|
2015-05-27 13:44:10 -04:00
|
|
|
// Build our decoder
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
Result: target,
|
|
|
|
Metadata: &md,
|
|
|
|
WeaklyTypedInput: true,
|
2019-05-23 08:10:41 -04:00
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(decodeHookFuncs...),
|
2015-05-27 13:44:10 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, raw := range raws {
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
2015-05-27 14:34:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 15:55:41 -04:00
|
|
|
// Set the metadata if it is set
|
|
|
|
if config.Metadata != nil {
|
|
|
|
*config.Metadata = md
|
|
|
|
}
|
|
|
|
|
2015-05-27 14:34:47 -04:00
|
|
|
// If we have unused keys, it is an error
|
|
|
|
if len(md.Unused) > 0 {
|
|
|
|
var err error
|
|
|
|
sort.Strings(md.Unused)
|
|
|
|
for _, unused := range md.Unused {
|
|
|
|
if unused != "type" && !strings.HasPrefix(unused, "packer_") {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"unknown configuration key: %q", unused))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-05-27 13:44:10 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DetectContext builds a base interpolate.Context, automatically
|
|
|
|
// detecting things like user variables from the raw configuration params.
|
|
|
|
func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
|
|
|
|
var s struct {
|
2018-08-10 17:25:14 -04:00
|
|
|
BuildName string `mapstructure:"packer_build_name"`
|
|
|
|
BuildType string `mapstructure:"packer_builder_type"`
|
|
|
|
TemplatePath string `mapstructure:"packer_template_path"`
|
|
|
|
Vars map[string]string `mapstructure:"packer_user_variables"`
|
|
|
|
SensitiveVars []string `mapstructure:"packer_sensitive_variables"`
|
2015-05-27 13:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range raws {
|
|
|
|
if err := mapstructure.Decode(r, &s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &interpolate.Context{
|
2018-08-10 17:25:14 -04:00
|
|
|
BuildName: s.BuildName,
|
|
|
|
BuildType: s.BuildType,
|
|
|
|
TemplatePath: s.TemplatePath,
|
|
|
|
UserVariables: s.Vars,
|
|
|
|
SensitiveVariables: s.SensitiveVars,
|
2015-05-27 13:44:10 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uint8ToStringHook(f reflect.Kind, t reflect.Kind, v interface{}) (interface{}, error) {
|
|
|
|
// We need to convert []uint8 to string. We have to do this
|
|
|
|
// because internally Packer uses MsgPack for RPC and the MsgPack
|
|
|
|
// codec turns strings into []uint8
|
|
|
|
if f == reflect.Slice && t == reflect.String {
|
|
|
|
dataVal := reflect.ValueOf(v)
|
|
|
|
dataType := dataVal.Type()
|
|
|
|
elemKind := dataType.Elem().Kind()
|
|
|
|
if elemKind == reflect.Uint8 {
|
|
|
|
v = string(dataVal.Interface().([]uint8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|