Commit b52132c6 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

post-processor/vsphere: cleanups

/cc @mheidenr - Some basic cleanups. :)
parent 7d9b48e7
...@@ -17,13 +17,12 @@ type Config struct { ...@@ -17,13 +17,12 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
Insecure bool `mapstructure:"insecure"` Insecure bool `mapstructure:"insecure"`
Datacenter string `mapstructure:"datacenter"` Datacenter string `mapstructure:"datacenter"`
Datastore string `mapstructure:"datastore"` Datastore string `mapstructure:"datastore"`
Host string `mapstructure:"host"` Host string `mapstructure:"host"`
VMNetwork string `mapstructure:"vm_network"` VMNetwork string `mapstructure:"vm_network"`
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
PathToResoucePool string `mapstructure:"path_to_resouce_pool"` PathToResourcePool string `mapstructure:"path_to_resource_pool"`
Username string `mapstructure:"username"` Username string `mapstructure:"username"`
VMFolder string `mapstructure:"vm_folder"` VMFolder string `mapstructure:"vm_folder"`
VMName string `mapstructure:"vm_name"` VMName string `mapstructure:"vm_name"`
...@@ -48,11 +47,9 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -48,11 +47,9 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
// Accumulate any errors // Accumulate any errors
errs := new(packer.MultiError) errs := new(packer.MultiError)
program := "ovftool" if _, err := exec.LookPath("ovftool"); err != nil {
_, erro := exec.LookPath(program)
if erro != nil {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error : %s not set", erro)) errs, fmt.Errorf("ovftool not found: %s", err))
} }
validates := map[string]*string{ validates := map[string]*string{
...@@ -61,7 +58,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -61,7 +58,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
"host": &p.config.Host, "host": &p.config.Host,
"vm_network": &p.config.VMNetwork, "vm_network": &p.config.VMNetwork,
"password": &p.config.Password, "password": &p.config.Password,
"path_to_resouce_pool": &p.config.PathToResoucePool, "path_to_resource_pool": &p.config.PathToResourcePool,
"username": &p.config.Username, "username": &p.config.Username,
"vm_folder": &p.config.VMFolder, "vm_folder": &p.config.VMFolder,
"vm_name": &p.config.VMName, "vm_name": &p.config.VMName,
...@@ -70,7 +67,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -70,7 +67,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
for n := range validates { for n := range validates {
if *validates[n] == "" { if *validates[n] == "" {
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Argument %s: not set", n)) errs, fmt.Errorf("%s must be set", n))
} }
} }
...@@ -82,8 +79,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { ...@@ -82,8 +79,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
} }
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
_, ok := builtins[artifact.BuilderId()] if _, ok := builtins[artifact.BuilderId()]; !ok {
if !ok {
return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId()) return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
} }
...@@ -91,6 +87,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -91,6 +87,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
for _, path := range artifact.Files() { for _, path := range artifact.Files() {
if strings.HasSuffix(path, ".vmx") { if strings.HasSuffix(path, ".vmx") {
vmx = path vmx = path
break
} }
} }
...@@ -98,24 +95,28 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ...@@ -98,24 +95,28 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
return nil, false, fmt.Errorf("VMX file not found") return nil, false, fmt.Errorf("VMX file not found")
} }
ui.Message(fmt.Sprintf("uploading %s to vSphere", vmx)) ui.Message(fmt.Sprintf("Uploading %s to vSphere", vmx))
program := "ovftool" args := []string{
nossl := fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure) fmt.Sprintf("--noSSLVerify=%t", p.config.Insecure),
accepteulas := "--acceptAllEulas" "--acceptAllEulas",
name := "--name=" + p.config.VMName fmt.Sprintf("--name=%s", p.config.VMName),
datastore := "--datastore=" + p.config.Datastore fmt.Sprintf("--datastore=%s", p.config.Datastore),
network := "--network=" + p.config.VMNetwork fmt.Sprintf("--network=%s", p.config.VMNetwork),
vm_folder := "--vmFolder=" + p.config.VMFolder fmt.Sprintf("--vmFolder=%s", p.config.VMFolder),
url := "vi://" + p.config.Username + ":" + p.config.Password + "@" + p.config.Host + "/" + p.config.Datacenter + "/" + p.config.PathToResoucePool fmt.Sprintf("vi://%s:%s@%s/%s/%s",
p.config.Username,
cmd := exec.Command(program, nossl, accepteulas, name, datastore, network, vm_folder, vmx, url) p.config.Password,
p.config.Host,
p.config.Datacenter,
p.config.PathToResourcePool),
}
var out bytes.Buffer var out bytes.Buffer
cmd := exec.Command("ovftool", args...)
cmd.Stdout = &out cmd.Stdout = &out
err_run := cmd.Run() if err := cmd.Run(); err != nil {
if err_run != nil { return nil, false, fmt.Errorf("Failed: %s\nStdout: %s", err, out.String())
return nil, false, fmt.Errorf("%s", out.String())
} }
ui.Message(fmt.Sprintf("%s", out.String())) ui.Message(fmt.Sprintf("%s", out.String()))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment