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-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(
|
|
|
|
errs, fmt.Errorf("Unknown configuration key: %s", unused))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
decoderConfig := &mapstructure.DecoderConfig{
|
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" {
|
2013-08-03 16:34:48 -04:00
|
|
|
// For Windows absolute file paths, remove leading / prior to processing
|
|
|
|
// since net/url turns "C:/" into "/C:/"
|
2013-08-02 17:06:06 -04:00
|
|
|
if runtime.GOOS == "windows" && url.Path[0] == '/' {
|
|
|
|
url.Path = url.Path[1:len(url.Path)]
|
2013-08-03 16:38:27 -04:00
|
|
|
|
|
|
|
// 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-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
|
|
|
}
|
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
|
|
|
|
}
|