Skip to content

Commit

Permalink
Merge branch 'hashicorpGH-2377' into release-0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arizvisa committed Apr 5, 2016
2 parents 5740df3 + 2b848bc commit 23bf0e3
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 146 deletions.
6 changes: 5 additions & 1 deletion builder/vmware/iso/driver_esx5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iso

import (
"fmt"
"github.com/mitchellh/multistep"
vmwcommon "github.com/mitchellh/packer/builder/vmware/common"
"net"
"testing"
Expand All @@ -22,14 +23,17 @@ func TestESX5Driver_implRemoteDriver(t *testing.T) {
func TestESX5Driver_HostIP(t *testing.T) {
expected_host := "127.0.0.1"

state := new(multistep.BasicStateBag)

//create mock SSH server
listen, _ := net.Listen("tcp", fmt.Sprintf("%s:0", expected_host))
port := listen.Addr().(*net.TCPAddr).Port
defer listen.Close()

driver := ESX5Driver{Host: "localhost", Port: uint(port)}
state.Put("driver", driver)

if host, _ := driver.HostIP(); host != expected_host {
if host, _ := driver.HostIP(state); host != expected_host {
t.Error(fmt.Sprintf("Expected string, %s but got %s", expected_host, host))
}
}
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 @@ -174,6 +174,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
104 changes: 30 additions & 74 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package common

import (
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"net/url"
)

// ScrubConfig is a helper that returns a string representation of
Expand Down Expand Up @@ -37,89 +36,46 @@ 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
}

if url.Scheme == "" {
url.Scheme = "file"
}

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)]
}
// 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
}
}

// 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
}
// 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)

url.Path, err = filepath.EvalSymlinks(url.Path)
if err != nil {
return "", err
}
// make sure that it can be parsed though..
uri,err := url.Parse(original)
if err != nil { return "", err }

url.Path = filepath.Clean(url.Path)
}
uri.Scheme = strings.ToLower(uri.Scheme)

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)
}
return uri.String(), nil
}

// Make sure it is lowercased
url.Scheme = strings.ToLower(url.Scheme)
// 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 }

// 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
if url.Path != "" && url.Path[0] != '/' {
url.Path = "/" + url.Path
}
original, err = filepath.EvalSymlinks(original)
if err != nil { return "", err }

// 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 23bf0e3

Please sign in to comment.