From 8bdd3b45c7065db6b12862b3801409784d4a8805 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 31 Jan 2018 13:19:31 -0800 Subject: [PATCH] use helper functions for reading vmx files --- builder/vmware/common/ssh.go | 13 ++----------- builder/vmware/common/step_configure_vmx.go | 5 +---- builder/vmware/common/step_configure_vnc.go | 15 ++------------- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/builder/vmware/common/ssh.go b/builder/vmware/common/ssh.go index 871ba8df9..4e339edb9 100644 --- a/builder/vmware/common/ssh.go +++ b/builder/vmware/common/ssh.go @@ -3,9 +3,7 @@ package common import ( "errors" "fmt" - "io/ioutil" "log" - "os" commonssh "github.com/hashicorp/packer/common/ssh" "github.com/hashicorp/packer/communicator/ssh" @@ -23,18 +21,11 @@ func CommHost(config *SSHConfig) func(multistep.StateBag) (string, error) { } log.Println("Lookup up IP information...") - f, err := os.Open(vmxPath) + + vmxData, err := ReadVMX(vmxPath) if err != nil { return "", err } - defer f.Close() - - vmxBytes, err := ioutil.ReadAll(f) - if err != nil { - return "", err - } - - vmxData := ParseVMX(string(vmxBytes)) var ok bool macAddress := "" diff --git a/builder/vmware/common/step_configure_vmx.go b/builder/vmware/common/step_configure_vmx.go index 155234ac4..102ac52ee 100644 --- a/builder/vmware/common/step_configure_vmx.go +++ b/builder/vmware/common/step_configure_vmx.go @@ -3,7 +3,6 @@ package common import ( "context" "fmt" - "io/ioutil" "log" "regexp" "strings" @@ -26,7 +25,7 @@ func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) mult ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) - vmxContents, err := ioutil.ReadFile(vmxPath) + vmxData, err := ReadVMX(vmxPath) if err != nil { err := fmt.Errorf("Error reading VMX file: %s", err) state.Put("error", err) @@ -34,8 +33,6 @@ func (s *StepConfigureVMX) Run(_ context.Context, state multistep.StateBag) mult return multistep.ActionHalt } - vmxData := ParseVMX(string(vmxContents)) - // Set this so that no dialogs ever appear from Packer. vmxData["msg.autoanswer"] = "true" diff --git a/builder/vmware/common/step_configure_vnc.go b/builder/vmware/common/step_configure_vnc.go index 90aa4a998..54f5ac182 100644 --- a/builder/vmware/common/step_configure_vnc.go +++ b/builder/vmware/common/step_configure_vnc.go @@ -3,11 +3,9 @@ package common import ( "context" "fmt" - "io/ioutil" "log" "math/rand" "net" - "os" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -87,17 +85,9 @@ func (s *StepConfigureVNC) Run(_ context.Context, state multistep.StateBag) mult ui := state.Get("ui").(packer.Ui) vmxPath := state.Get("vmx_path").(string) - f, err := os.Open(vmxPath) + vmxData, err := ReadVMX(vmxPath) if err != nil { - err := fmt.Errorf("Error reading VMX data: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - - vmxBytes, err := ioutil.ReadAll(f) - if err != nil { - err := fmt.Errorf("Error reading VMX data: %s", err) + err := fmt.Errorf("Error reading VMX file: %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt @@ -121,7 +111,6 @@ func (s *StepConfigureVNC) Run(_ context.Context, state multistep.StateBag) mult log.Printf("Found available VNC port: %d", vncPort) - vmxData := ParseVMX(string(vmxBytes)) vncFinder.UpdateVMX(vncBindAddress, vncPassword, vncPort, vmxData) if err := WriteVMX(vmxPath, vmxData); err != nil {