Skip to content

Commit

Permalink
Merge branch 'hashicorpGH-2377' into release-1.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
arizvisa committed Dec 21, 2017
2 parents 5b4cf8d + 058b399 commit 14ec85c
Show file tree
Hide file tree
Showing 34 changed files with 4,077 additions and 173 deletions.
11 changes: 0 additions & 11 deletions builder/virtualbox/ovf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@ func TestNewConfig_sourcePath(t *testing.T) {
t.Fatalf("Nonexistant file should throw a validation error!")
}

// Bad
c = testConfig(t)
c["source_path"] = "ftp://i/dont/exist"
_, warns, err = NewConfig(c)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should error")
}

// Good
tf := getTempFile(t)
defer os.Remove(tf.Name())
Expand Down
5 changes: 5 additions & 0 deletions builder/vmware/iso/step_create_vmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction {
isoPath := state.Get("iso_path").(string)
ui := state.Get("ui").(packer.Ui)

// Convert the iso_path into a path relative to the .vmx file if possible
if relativeIsoPath, err := filepath.Rel(config.VMXTemplatePath, filepath.FromSlash(isoPath)); err == nil {
isoPath = relativeIsoPath
}

ui.Say("Building and writing VMX file")

vmxTemplate := DefaultVMXTemplate
Expand Down
96 changes: 33 additions & 63 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
Expand Down Expand Up @@ -47,81 +46,52 @@ func ChooseString(vals ...string) string {
// 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) {
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
}
}

url, err := url.Parse(original)
if err != nil {
return "", err
// Verify that the scheme is something we support in our common downloader.
supported := []string{"file", "http", "https", "ftp", "smb"}
found := false
for _, s := range supported {
if strings.HasPrefix(strings.ToLower(original), s+"://") {
found = true
break
}
}

if url.Scheme == "" {
url.Scheme = "file"
}
// If it's properly prefixed with something we support, then we don't need
// to make it a uri.
if found {
original = filepath.ToSlash(original)

if url.Scheme == "file" {
// 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 = ""
}

// 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)]
}
// make sure that it can be parsed though..
uri, err := url.Parse(original)
if err != nil {
return "", err
}

// 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
}
uri.Scheme = strings.ToLower(uri.Scheme)

url.Path, err = filepath.EvalSymlinks(url.Path)
if err != nil {
return "", err
}
return uri.String(), nil
}

url.Path = filepath.Clean(url.Path)
// If the file exists, then make it an absolute path
_, err := os.Stat(original)
if err == nil {
original, err = filepath.Abs(filepath.FromSlash(original))
if err != nil {
return "", err
}

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)
original, err = filepath.EvalSymlinks(original)
if err != nil {
return "", err
}
}

// Make sure it is lowercased
url.Scheme = strings.ToLower(url.Scheme)

// 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
}
original = filepath.Clean(original)
original = filepath.ToSlash(original)
}

if !found {
return "", fmt.Errorf("Unsupported URL scheme: %s", url.Scheme)
}
// Since it wasn't properly prefixed, let's make it into a well-formed
// file:// uri.

return url.String(), nil
return "file://" + original, nil
}
17 changes: 2 additions & 15 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -41,13 +40,7 @@ func TestDownloadableURL(t *testing.T) {
// Invalid URL: has hex code in host
_, err := DownloadableURL("http://what%20.com")
if err == nil {
t.Fatal("expected err")
}

// Invalid: unsupported scheme
_, err = DownloadableURL("ftp://host.com/path")
if err == nil {
t.Fatal("expected err")
t.Fatalf("expected err : %s", err)
}

// Valid: http
Expand Down Expand Up @@ -85,11 +78,7 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
}

tfPath = filepath.Clean(tfPath)

filePrefix := "file://"
if runtime.GOOS == "windows" {
filePrefix += "/"
}

// Relative filepath. We run this test in a func so that
// the defers run right away.
Expand All @@ -111,9 +100,7 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
t.Fatalf("err: %s", err)
}

expected := fmt.Sprintf("%s%s",
filePrefix,
strings.Replace(tfPath, `\`, `/`, -1))
expected := "file://" + strings.Replace(tfPath, `\`, `/`, -1)
if u != expected {
t.Fatalf("unexpected: %#v != %#v", u, expected)
}
Expand Down
Loading

0 comments on commit 14ec85c

Please sign in to comment.