2013-07-19 14:59:04 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-19 15:09:13 -04:00
|
|
|
"fmt"
|
2013-07-19 14:59:04 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-07-19 15:09:13 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-29 02:51:21 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-07-29 03:09:48 -04:00
|
|
|
"path/filepath"
|
2013-12-16 20:57:07 -05:00
|
|
|
"reflect"
|
2013-08-02 17:06:06 -04:00
|
|
|
"runtime"
|
2013-07-19 15:09:13 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2013-07-19 14:59:04 -04:00
|
|
|
)
|
|
|
|
|
2013-10-11 06:50:08 -04:00
|
|
|
// ScrubConfig is a helper that returns a string representation of
|
|
|
|
// any struct with the given values stripped out.
|
|
|
|
func ScrubConfig(target interface{}, values ...string) string {
|
|
|
|
conf := fmt.Sprintf("Config: %+v", target)
|
|
|
|
for _, value := range values {
|
|
|
|
conf = strings.Replace(conf, value, "<Filtered>", -1)
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:09:13 -04:00
|
|
|
// CheckUnusedConfig is a helper that makes sure that the there are no
|
|
|
|
// unused configuration keys, properly ignoring keys that don't matter.
|
2013-07-19 19:08:25 -04:00
|
|
|
func CheckUnusedConfig(md *mapstructure.Metadata) *packer.MultiError {
|
2013-07-19 15:09:13 -04:00
|
|
|
errs := make([]error, 0)
|
|
|
|
|
|
|
|
if md.Unused != nil && len(md.Unused) > 0 {
|
|
|
|
sort.Strings(md.Unused)
|
|
|
|
for _, unused := range md.Unused {
|
|
|
|
if unused != "type" && !strings.HasPrefix(unused, "packer_") {
|
|
|
|
errs = append(
|
2015-03-04 13:57:03 -05:00
|
|
|
errs, fmt.Errorf("Unknown configuration key: %q", unused))
|
2013-07-19 15:09:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:30:49 -04:00
|
|
|
// ChooseString returns the first non-empty value.
|
|
|
|
func ChooseString(vals ...string) string {
|
2014-04-26 14:12:43 -04:00
|
|
|
for _, el := range vals {
|
|
|
|
if el != "" {
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 00:30:49 -04:00
|
|
|
|
2014-04-26 14:12:43 -04:00
|
|
|
return ""
|
2014-04-22 00:30:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-19 14:59:04 -04:00
|
|
|
// DecodeConfig is a helper that handles decoding raw configuration using
|
|
|
|
// mapstructure. It returns the metadata and any errors that may happen.
|
|
|
|
// If you need extra configuration for mapstructure, you should configure
|
|
|
|
// it manually and not use this helper function.
|
|
|
|
func DecodeConfig(target interface{}, raws ...interface{}) (*mapstructure.Metadata, error) {
|
2013-12-16 20:57:07 -05:00
|
|
|
decodeHook, err := decodeConfigHook(raws)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-07-19 14:59:04 -04:00
|
|
|
var md mapstructure.Metadata
|
|
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
2014-05-01 17:24:19 -04:00
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
2014-04-27 17:47:16 -04:00
|
|
|
decodeHook,
|
|
|
|
mapstructure.StringToSliceHookFunc(","),
|
|
|
|
),
|
2013-09-18 19:18:39 -04:00
|
|
|
Metadata: &md,
|
|
|
|
Result: target,
|
|
|
|
WeaklyTypedInput: true,
|
2013-07-19 14:59:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, raw := range raws {
|
|
|
|
err := decoder.Decode(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &md, nil
|
|
|
|
}
|
2013-07-29 02:51:21 -04:00
|
|
|
|
|
|
|
// DownloadableURL processes a URL that may also be a file path and returns
|
|
|
|
// a completely valid URL. For example, the original URL might be "local/file.iso"
|
|
|
|
// which isn't a valid URL. DownloadableURL will return "file:///local/file.iso"
|
|
|
|
func DownloadableURL(original string) (string, error) {
|
2013-08-15 23:16:05 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// If the distance to the first ":" is just one character, assume
|
|
|
|
// we're dealing with a drive letter and thus a file path.
|
|
|
|
idx := strings.Index(original, ":")
|
|
|
|
if idx == 1 {
|
|
|
|
original = "file:///" + original
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:51:21 -04:00
|
|
|
url, err := url.Parse(original)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Scheme == "" {
|
|
|
|
url.Scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Scheme == "file" {
|
2014-04-22 00:28:47 -04:00
|
|
|
// Windows file handling is all sorts of tricky...
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// If the path is using Windows-style slashes, URL parses
|
|
|
|
// it into the host field.
|
|
|
|
if url.Path == "" && strings.Contains(url.Host, `\`) {
|
|
|
|
url.Path = url.Host
|
|
|
|
url.Host = ""
|
|
|
|
}
|
2013-08-03 16:38:27 -04:00
|
|
|
|
2014-04-22 00:28:47 -04:00
|
|
|
// For Windows absolute file paths, remove leading / prior to processing
|
|
|
|
// since net/url turns "C:/" into "/C:/"
|
|
|
|
if len(url.Path) > 0 && url.Path[0] == '/' {
|
|
|
|
url.Path = url.Path[1:len(url.Path)]
|
|
|
|
}
|
2013-08-02 17:06:06 -04:00
|
|
|
}
|
2013-08-02 16:59:19 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
// Only do the filepath transformations if the file appears
|
|
|
|
// to actually exist.
|
|
|
|
if _, err := os.Stat(url.Path); err == nil {
|
|
|
|
url.Path, err = filepath.Abs(url.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-07-29 03:06:21 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
url.Path, err = filepath.EvalSymlinks(url.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-07-29 03:06:21 -04:00
|
|
|
|
2013-12-06 21:31:45 -05:00
|
|
|
url.Path = filepath.Clean(url.Path)
|
2013-07-29 03:06:21 -04:00
|
|
|
}
|
2014-04-22 00:28:47 -04:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Also replace all backslashes with forwardslashes since Windows
|
|
|
|
// users are likely to do this but the URL should actually only
|
|
|
|
// contain forward slashes.
|
|
|
|
url.Path = strings.Replace(url.Path, `\`, `/`, -1)
|
|
|
|
}
|
2013-07-29 02:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is lowercased
|
|
|
|
url.Scheme = strings.ToLower(url.Scheme)
|
|
|
|
|
|
|
|
// This is to work around issue #5927. This can safely be removed once
|
|
|
|
// we distribute with a version of Go that fixes that bug.
|
|
|
|
//
|
|
|
|
// See: https://code.google.com/p/go/issues/detail?id=5927
|
2013-07-29 03:09:48 -04:00
|
|
|
if url.Path != "" && url.Path[0] != '/' {
|
2013-07-29 02:51:21 -04:00
|
|
|
url.Path = "/" + url.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the scheme is something we support in our common downloader.
|
|
|
|
supported := []string{"file", "http", "https"}
|
|
|
|
found := false
|
|
|
|
for _, s := range supported {
|
|
|
|
if url.Scheme == s {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return "", fmt.Errorf("Unsupported URL scheme: %s", url.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.String(), nil
|
|
|
|
}
|
2013-12-16 20:57:07 -05:00
|
|
|
|
|
|
|
// This returns a mapstructure.DecodeHookFunc that automatically template
|
|
|
|
// processes any configuration values that aren't strings but have been
|
|
|
|
// provided as strings.
|
|
|
|
//
|
|
|
|
// For example: "image_id" wants an int and the user uses a string with
|
|
|
|
// a user variable like "{{user `image_id`}}". This decode hook makes that
|
|
|
|
// work.
|
|
|
|
func decodeConfigHook(raws []interface{}) (mapstructure.DecodeHookFunc, error) {
|
|
|
|
// First thing we do is decode PackerConfig so that we can have access
|
|
|
|
// to the user variables so that we can process some templates.
|
|
|
|
var pc PackerConfig
|
2014-04-26 17:23:22 -04:00
|
|
|
|
|
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
|
|
|
Result: &pc,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
}
|
|
|
|
decoder, err := mapstructure.NewDecoder(decoderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-16 20:57:07 -05:00
|
|
|
for _, raw := range raws {
|
2014-04-26 17:23:22 -04:00
|
|
|
if err := decoder.Decode(raw); err != nil {
|
2013-12-16 20:57:07 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tpl.UserVars = pc.PackerUserVars
|
|
|
|
|
|
|
|
return func(f reflect.Kind, t reflect.Kind, v interface{}) (interface{}, error) {
|
|
|
|
if t != reflect.String {
|
2014-04-28 19:19:35 -04:00
|
|
|
// 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
|
2014-04-28 19:18:11 -04:00
|
|
|
if f == reflect.Slice {
|
2014-05-01 17:24:19 -04:00
|
|
|
dataVal := reflect.ValueOf(v)
|
2014-04-28 19:18:11 -04:00
|
|
|
dataType := dataVal.Type()
|
|
|
|
elemKind := dataType.Elem().Kind()
|
|
|
|
if elemKind == reflect.Uint8 {
|
|
|
|
v = string(dataVal.Interface().([]uint8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 20:57:07 -05:00
|
|
|
if sv, ok := v.(string); ok {
|
|
|
|
var err error
|
|
|
|
v, err = tpl.Process(sv, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}, nil
|
|
|
|
}
|