From cb0011d665e2d5e81293023d3193627ce33814f3 Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Thu, 27 Dec 2018 02:33:58 -0600 Subject: [PATCH 1/4] Fix ssh_host bug in hyper-v builders. --- builder/hyperv/common/ssh.go | 34 ++++++++++++++++++++++------------ builder/hyperv/iso/builder.go | 2 +- builder/hyperv/vmcx/builder.go | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/builder/hyperv/common/ssh.go b/builder/hyperv/common/ssh.go index ab136f373..07141aab9 100644 --- a/builder/hyperv/common/ssh.go +++ b/builder/hyperv/common/ssh.go @@ -1,22 +1,32 @@ package common import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func CommHost(state multistep.StateBag) (string, error) { - vmName := state.Get("vmName").(string) - driver := state.Get("driver").(Driver) +func CommHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + + if host != "" { + log.Println("Using ssh_host value: %s", ipAddress) + return host, nil + } + + vmName := state.Get("vmName").(string) + driver := state.Get("driver").(Driver) - mac, err := driver.Mac(vmName) - if err != nil { - return "", err - } + mac, err := driver.Mac(vmName) + if err != nil { + return "", err + } - ip, err := driver.IpAddress(mac) - if err != nil { - return "", err - } + ip, err := driver.IpAddress(mac) + if err != nil { + return "", err + } - return ip, nil + return ip, nil + } } diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index b21f2f385..27d32ffa7 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -459,7 +459,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // configure the communicator ssh, winrm &communicator.StepConnect{ Config: &b.config.SSHConfig.Comm, - Host: hypervcommon.CommHost, + Host: hypervcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(), }, diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index e207c548f..d9da950a1 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -482,7 +482,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // configure the communicator ssh, winrm &communicator.StepConnect{ Config: &b.config.SSHConfig.Comm, - Host: hypervcommon.CommHost, + Host: hypervcommon.CommHost(b.config.SSHConfig.Comm.SSHHost), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(), }, From 35aaf44fa502b7aa71d93a53abad13fd08f1c106 Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Thu, 27 Dec 2018 02:55:14 -0600 Subject: [PATCH 2/4] A properly formatted hyperv ssh.go file. --- builder/hyperv/common/ssh.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/builder/hyperv/common/ssh.go b/builder/hyperv/common/ssh.go index 07141aab9..95d4eeaf8 100644 --- a/builder/hyperv/common/ssh.go +++ b/builder/hyperv/common/ssh.go @@ -1,32 +1,32 @@ package common import ( - "log" + "log" "github.com/hashicorp/packer/helper/multistep" ) func CommHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Println("Using ssh_host value: %s", ipAddress) - return host, nil - } + if host != "" { + log.Println("Using ssh_host value: %s", ipAddress) + return host, nil + } - vmName := state.Get("vmName").(string) - driver := state.Get("driver").(Driver) + vmName := state.Get("vmName").(string) + driver := state.Get("driver").(Driver) - mac, err := driver.Mac(vmName) - if err != nil { - return "", err - } + mac, err := driver.Mac(vmName) + if err != nil { + return "", err + } - ip, err := driver.IpAddress(mac) - if err != nil { - return "", err - } + ip, err := driver.IpAddress(mac) + if err != nil { + return "", err + } - return ip, nil - } + return ip, nil + } } From 8a9962882ccd1dda3cdb9708d3db3205d40deb4a Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Thu, 27 Dec 2018 03:15:44 -0600 Subject: [PATCH 3/4] Use Printf not Println. D'oh. --- builder/hyperv/common/ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/hyperv/common/ssh.go b/builder/hyperv/common/ssh.go index 95d4eeaf8..e02cf04a6 100644 --- a/builder/hyperv/common/ssh.go +++ b/builder/hyperv/common/ssh.go @@ -10,7 +10,7 @@ func CommHost(host string) func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { if host != "" { - log.Println("Using ssh_host value: %s", ipAddress) + log.Printf("Using ssh_host value: %s", host) return host, nil } From 217028b9025746f532c22484e1f933eb1027159a Mon Sep 17 00:00:00 2001 From: Ladar Levison Date: Thu, 27 Dec 2018 03:34:08 -0600 Subject: [PATCH 4/4] Added comment regarding use of host param. --- builder/hyperv/common/ssh.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builder/hyperv/common/ssh.go b/builder/hyperv/common/ssh.go index e02cf04a6..133f20a91 100644 --- a/builder/hyperv/common/ssh.go +++ b/builder/hyperv/common/ssh.go @@ -9,6 +9,7 @@ import ( func CommHost(host string) func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { + // Skip IP auto detection if the configuration has an ssh host configured. if host != "" { log.Printf("Using ssh_host value: %s", host) return host, nil