diff --git a/post-processor/vsphere/post-processor.go b/post-processor/vsphere/post-processor.go index 19f489928..c340662b1 100644 --- a/post-processor/vsphere/post-processor.go +++ b/post-processor/vsphere/post-processor.go @@ -1,10 +1,10 @@ package vsphere import ( - "bytes" "fmt" "log" "net/url" + "os" "os/exec" "strings" @@ -121,20 +121,49 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ovftool_uri += "/Resources/" + p.config.ResourcePool } - args := []string{ - fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure), - "--acceptAllEulas", - fmt.Sprintf("--name=\"%s\"", p.config.VMName), - fmt.Sprintf("--datastore=\"%s\"", p.config.Datastore), - fmt.Sprintf("--diskMode=\"%s\"", p.config.DiskMode), - fmt.Sprintf("--network=\"%s\"", p.config.VMNetwork), - fmt.Sprintf("--vmFolder=\"%s\"", p.config.VMFolder), - fmt.Sprintf("%s", source), - fmt.Sprintf("\"%s\"", ovftool_uri), + ui.Message(fmt.Sprintf("Uploading %s to vSphere", source)) + + args, err := p.BuildArgs(source, ovftool_uri) + if err != nil { + ui.Message(fmt.Sprintf("Failed: %s\n", err)) } ui.Message(fmt.Sprintf("Uploading %s to vSphere", source)) + log.Printf("Starting ovftool with parameters: %s", strings.Join(args, " ")) + cmd := exec.Command("ovftool", args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return nil, false, fmt.Errorf("Failed: %s\n", err) + } + + return artifact, false, nil +} + +func (p *PostProcessor) BuildArgs(source, ovftool_uri string) ([]string, error) { + args := []string{ + "--acceptAllEulas", + fmt.Sprintf(`--name=%s`, p.config.VMName), + fmt.Sprintf(`--datastore=%s`, p.config.Datastore), + } + + if p.config.Insecure { + args = append(args, fmt.Sprintf(`--noSSLVerify=%t`, p.config.Insecure)) + } + + if p.config.DiskMode != "" { + args = append(args, fmt.Sprintf(`--diskMode=%s`, p.config.DiskMode)) + } + + if p.config.VMFolder != "" { + args = append(args, fmt.Sprintf(`--vmFolder=%s`, p.config.VMFolder)) + } + + if p.config.VMNetwork != "" { + args = append(args, fmt.Sprintf(`--network=%s`, p.config.VMNetwork)) + } + if p.config.Overwrite == true { args = append(args, "--overwrite") } @@ -143,16 +172,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac args = append(args, p.config.Options...) } - ui.Message(fmt.Sprintf("Uploading %s to vSphere", source)) - var out bytes.Buffer - log.Printf("Starting ovftool with parameters: %s", strings.Join(args, " ")) - cmd := exec.Command("ovftool", args...) - cmd.Stdout = &out - if err := cmd.Run(); err != nil { - return nil, false, fmt.Errorf("Failed: %s\nStdout: %s", err, out.String()) - } + args = append(args, fmt.Sprintf(`%s`, source)) + args = append(args, fmt.Sprintf(`%s`, ovftool_uri)) - ui.Message(fmt.Sprintf("%s", out.String())) - - return artifact, false, nil + return args, nil } diff --git a/post-processor/vsphere/post-processor_test.go b/post-processor/vsphere/post-processor_test.go index 619049248..61b58789b 100644 --- a/post-processor/vsphere/post-processor_test.go +++ b/post-processor/vsphere/post-processor_test.go @@ -1 +1,42 @@ package vsphere + +import ( + "fmt" + "net/url" + "strings" + "testing" +) + +func TestArgs(t *testing.T) { + var p PostProcessor + + p.config.Username = "me" + p.config.Password = "notpassword" + p.config.Host = "myhost" + p.config.Datacenter = "mydc" + p.config.Cluster = "mycluster" + p.config.VMName = "my vm" + p.config.Datastore = "my datastore" + p.config.Insecure = true + p.config.DiskMode = "thin" + p.config.VMFolder = "my folder" + + source := "something.vmx" + ovftool_uri := fmt.Sprintf("vi://%s:%s@%s/%s/host/%s", + url.QueryEscape(p.config.Username), + url.QueryEscape(p.config.Password), + p.config.Host, + p.config.Datacenter, + p.config.Cluster) + + if p.config.ResourcePool != "" { + ovftool_uri += "/Resources/" + p.config.ResourcePool + } + + args, err := p.BuildArgs(source, ovftool_uri) + if err != nil { + t.Errorf("Error: %s", err) + } + + t.Logf("ovftool %s", strings.Join(args, " ")) +}