From ad8a88e366b5cf7ecfc298083d3529dc806b53bd Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 2 Jul 2019 13:00:55 -0700 Subject: [PATCH 1/6] allow ssh_host to override the host ip gathered from the instance for GCP builder --- builder/googlecompute/ssh.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/builder/googlecompute/ssh.go b/builder/googlecompute/ssh.go index e498ed43b..3de529eeb 100644 --- a/builder/googlecompute/ssh.go +++ b/builder/googlecompute/ssh.go @@ -5,6 +5,10 @@ import ( ) func commHost(state multistep.StateBag) (string, error) { + config := state.Get("config").(*Config) + if config.Comm.SSHHost != "" { + return config.Comm.SSHHost, nil + } ipAddress := state.Get("instance_ip").(string) return ipAddress, nil } From eb8c74bf85b5a55612dd5cdd359a9eb206a8be07 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 2 Jul 2019 13:56:28 -0700 Subject: [PATCH 2/6] allow ssh_host config option to override host logic for all builders --- builder/cloudstack/builder.go | 2 +- builder/cloudstack/ssh.go | 19 ++++++++++++------ builder/digitalocean/builder.go | 2 +- builder/digitalocean/ssh.go | 14 +++++++++++--- builder/docker/builder.go | 2 +- builder/docker/comm.go | 16 ++++++++++++---- builder/googlecompute/builder.go | 2 +- builder/googlecompute/ssh.go | 16 ++++++++++------ builder/linode/builder.go | 2 +- builder/linode/ssh.go | 18 ++++++++++++----- builder/oneandone/builder.go | 2 +- builder/oneandone/ssh.go | 15 ++++++++++++--- builder/oracle/classic/builder.go | 6 +++--- builder/oracle/common/ssh.go | 15 ++++++++++++--- builder/oracle/oci/builder.go | 2 +- builder/parallels/common/ssh.go | 32 +++++++++++++++++++------------ builder/parallels/iso/builder.go | 2 +- builder/parallels/pvm/builder.go | 2 +- builder/profitbricks/builder.go | 2 +- builder/profitbricks/ssh.go | 15 ++++++++++++--- builder/qemu/builder.go | 2 +- builder/qemu/ssh.go | 13 +++++++++++-- builder/scaleway/builder.go | 2 +- builder/scaleway/ssh.go | 15 ++++++++++++--- builder/triton/builder.go | 2 +- builder/triton/ssh.go | 25 ++++++++++++++++-------- builder/yandex/builder.go | 2 +- builder/yandex/ssh.go | 15 ++++++++++++--- 28 files changed, 184 insertions(+), 78 deletions(-) diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index c40b112a3..325b2ea86 100644 --- a/builder/cloudstack/builder.go +++ b/builder/cloudstack/builder.go @@ -77,7 +77,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepSetupNetworking{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), SSHPort: commPort, WinRMPort: commPort, diff --git a/builder/cloudstack/ssh.go b/builder/cloudstack/ssh.go index ec9fbe123..a3b81ca64 100644 --- a/builder/cloudstack/ssh.go +++ b/builder/cloudstack/ssh.go @@ -2,17 +2,24 @@ package cloudstack import ( "fmt" + "log" "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ip, hasIP := state.Get("ipaddress").(string) - if !hasIP { - return "", fmt.Errorf("Failed to retrieve IP address") - } +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + ip, hasIP := state.Get("ipaddress").(string) + if !hasIP { + return "", fmt.Errorf("Failed to retrieve IP address") + } - return ip, nil + return ip, nil + } } func commPort(state multistep.StateBag) (int, error) { diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 4b5f6e36d..7b10c90cd 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -86,7 +86,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepDropletInfo), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, new(common.StepProvision), diff --git a/builder/digitalocean/ssh.go b/builder/digitalocean/ssh.go index b47628ba8..e0567cd27 100644 --- a/builder/digitalocean/ssh.go +++ b/builder/digitalocean/ssh.go @@ -1,10 +1,18 @@ package digitalocean import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("droplet_ip").(string) - return ipAddress, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + ipAddress := state.Get("droplet_ip").(string) + return ipAddress, nil + } } diff --git a/builder/docker/builder.go b/builder/docker/builder.go index 493e9b6de..63cb8f55d 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -48,7 +48,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &StepRun{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), CustomConnect: map[string]multistep.Step{ "docker": &StepConnectDocker{}, diff --git a/builder/docker/comm.go b/builder/docker/comm.go index e11a0eaf2..aa21405c5 100644 --- a/builder/docker/comm.go +++ b/builder/docker/comm.go @@ -1,11 +1,19 @@ package docker import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - containerId := state.Get("container_id").(string) - driver := state.Get("driver").(Driver) - return driver.IPAddress(containerId) +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + containerId := state.Get("container_id").(string) + driver := state.Get("driver").(Driver) + return driver.IPAddress(containerId) + } } diff --git a/builder/googlecompute/builder.go b/builder/googlecompute/builder.go index 9a2b4fecc..34aa97832 100644 --- a/builder/googlecompute/builder.go +++ b/builder/googlecompute/builder.go @@ -67,7 +67,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), WinRMConfig: winrmConfig, }, diff --git a/builder/googlecompute/ssh.go b/builder/googlecompute/ssh.go index 3de529eeb..4638d8242 100644 --- a/builder/googlecompute/ssh.go +++ b/builder/googlecompute/ssh.go @@ -1,14 +1,18 @@ package googlecompute import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - config := state.Get("config").(*Config) - if config.Comm.SSHHost != "" { - return config.Comm.SSHHost, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + ipAddress := state.Get("instance_ip").(string) + return ipAddress, nil } - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil } diff --git a/builder/linode/builder.go b/builder/linode/builder.go index 2837b1e7b..def4b1f2e 100644 --- a/builder/linode/builder.go +++ b/builder/linode/builder.go @@ -56,7 +56,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (ret &stepCreateLinode{client}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/linode/ssh.go b/builder/linode/ssh.go index 6380ecb0e..8f291c450 100644 --- a/builder/linode/ssh.go +++ b/builder/linode/ssh.go @@ -2,18 +2,26 @@ package linode import ( "fmt" + "log" "github.com/hashicorp/packer/helper/multistep" "github.com/linode/linodego" "golang.org/x/crypto/ssh" ) -func commHost(state multistep.StateBag) (string, error) { - instance := state.Get("instance").(*linodego.Instance) - if len(instance.IPv4) == 0 { - return "", fmt.Errorf("Linode instance %d has no IPv4 addresses!", instance.ID) +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + instance := state.Get("instance").(*linodego.Instance) + if len(instance.IPv4) == 0 { + return "", fmt.Errorf("Linode instance %d has no IPv4 addresses!", instance.ID) + } + return instance.IPv4[0].String(), nil } - return instance.IPv4[0].String(), nil } func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) { diff --git a/builder/oneandone/builder.go b/builder/oneandone/builder.go index a67329c6d..d09bbac64 100644 --- a/builder/oneandone/builder.go +++ b/builder/oneandone/builder.go @@ -44,7 +44,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepCreateServer), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/oneandone/ssh.go b/builder/oneandone/ssh.go index 4068b87af..9a3271f07 100644 --- a/builder/oneandone/ssh.go +++ b/builder/oneandone/ssh.go @@ -1,10 +1,19 @@ package oneandone import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + ipAddress := state.Get("server_ip").(string) + return ipAddress, nil + } } diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index eb0ce97d8..f6440ee03 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -99,7 +99,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost, + Host: ocommon.CommHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, @@ -129,7 +129,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack KeyName: fmt.Sprintf("packer-generated-key_%s", runID), StepConnectSSH: &communicator.StepConnectSSH{ Config: &b.config.BuilderComm, - Host: ocommon.CommHost, + Host: ocommon.CommHost(b.config.Comm.SSHHost), SSHConfig: b.config.BuilderComm.SSHConfigFunc(), }, }, @@ -162,7 +162,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepCreateInstance{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost, + Host: ocommon.CommHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/oracle/common/ssh.go b/builder/oracle/common/ssh.go index 53b5d349a..461c977b1 100644 --- a/builder/oracle/common/ssh.go +++ b/builder/oracle/common/ssh.go @@ -1,10 +1,19 @@ package common import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func CommHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil +func CommHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + ipAddress := state.Get("instance_ip").(string) + return ipAddress, nil + } } diff --git a/builder/oracle/oci/builder.go b/builder/oracle/oci/builder.go index bf34eafc5..f4a60d3e1 100644 --- a/builder/oracle/oci/builder.go +++ b/builder/oracle/oci/builder.go @@ -65,7 +65,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost, + Host: ocommon.CommHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/parallels/common/ssh.go b/builder/parallels/common/ssh.go index 7c94006eb..df82c8082 100644 --- a/builder/parallels/common/ssh.go +++ b/builder/parallels/common/ssh.go @@ -1,23 +1,31 @@ package common import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) // CommHost returns the VM's IP address which should be used to access it by SSH. -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.Printf("Using ssh_host value: %s", host) + 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/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 287a47361..688d063fc 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -198,7 +198,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.SSHConfig.Comm, - Host: parallelscommon.CommHost, + Host: parallelscommon.CommHost(b.config.SSHConfig.Comm.SSHHost), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(), }, ¶llelscommon.StepUploadVersion{ diff --git a/builder/parallels/pvm/builder.go b/builder/parallels/pvm/builder.go index a19e6b833..d62415a3d 100644 --- a/builder/parallels/pvm/builder.go +++ b/builder/parallels/pvm/builder.go @@ -85,7 +85,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.SSHConfig.Comm, - Host: parallelscommon.CommHost, + Host: parallelscommon.CommHost(b.config.SSHConfig.Comm.SSHHost), SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(), }, ¶llelscommon.StepUploadVersion{ diff --git a/builder/profitbricks/builder.go b/builder/profitbricks/builder.go index dd678c35e..0cd2e0003 100644 --- a/builder/profitbricks/builder.go +++ b/builder/profitbricks/builder.go @@ -41,7 +41,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepCreateServer), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/profitbricks/ssh.go b/builder/profitbricks/ssh.go index c860bc841..5ae5f22d8 100644 --- a/builder/profitbricks/ssh.go +++ b/builder/profitbricks/ssh.go @@ -1,10 +1,19 @@ package profitbricks import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + ipAddress := state.Get("server_ip").(string) + return ipAddress, nil + } } diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 90969f38c..b9ea394db 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -432,7 +432,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack steps = append(steps, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), SSHPort: commPort, WinRMPort: commPort, diff --git a/builder/qemu/ssh.go b/builder/qemu/ssh.go index 1fe4537bd..a444dc0c4 100644 --- a/builder/qemu/ssh.go +++ b/builder/qemu/ssh.go @@ -1,11 +1,20 @@ package qemu import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - return "127.0.0.1", nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + return "127.0.0.1", nil + } } func commPort(state multistep.StateBag) (int, error) { diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 418f945fb..3b194ed14 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -56,7 +56,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepServerInfo), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, new(common.StepProvision), diff --git a/builder/scaleway/ssh.go b/builder/scaleway/ssh.go index e291151ab..0c2c3f9b7 100644 --- a/builder/scaleway/ssh.go +++ b/builder/scaleway/ssh.go @@ -1,10 +1,19 @@ package scaleway import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + ipAddress := state.Get("server_ip").(string) + return ipAddress, nil + } } diff --git a/builder/triton/builder.go b/builder/triton/builder.go index 4e40116c5..ba6e2baa8 100644 --- a/builder/triton/builder.go +++ b/builder/triton/builder.go @@ -64,7 +64,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &StepCreateSourceMachine{}, &communicator.StepConnect{ Config: &config.Comm, - Host: commHost, + Host: commHost(b.config.Comm.SSHHost), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/triton/ssh.go b/builder/triton/ssh.go index 0508bfed9..70d52d5e8 100644 --- a/builder/triton/ssh.go +++ b/builder/triton/ssh.go @@ -1,17 +1,26 @@ package triton import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - driver := state.Get("driver").(Driver) - machineID := state.Get("machine").(string) +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } - machine, err := driver.GetMachineIP(machineID) - if err != nil { - return "", err - } + driver := state.Get("driver").(Driver) + machineID := state.Get("machine").(string) - return machine, nil + machine, err := driver.GetMachineIP(machineID) + if err != nil { + return "", err + } + + return machine, nil + } } diff --git a/builder/yandex/builder.go b/builder/yandex/builder.go index 38ea7524d..8fff5b4ba 100644 --- a/builder/yandex/builder.go +++ b/builder/yandex/builder.go @@ -61,7 +61,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepInstanceInfo{}, &communicator.StepConnect{ Config: &b.config.Communicator, - Host: commHost, + Host: commHost(b.config.Communicator.SSHHost), SSHConfig: b.config.Communicator.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/yandex/ssh.go b/builder/yandex/ssh.go index c32f16b53..4a18a0d79 100644 --- a/builder/yandex/ssh.go +++ b/builder/yandex/ssh.go @@ -1,10 +1,19 @@ package yandex import ( + "log" + "github.com/hashicorp/packer/helper/multistep" ) -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil +func commHost(host string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + + ipAddress := state.Get("instance_ip").(string) + return ipAddress, nil + } } From 0cfe8e7070b30175903d1db6bb5a6f3ebe966147 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 2 Jul 2019 14:00:51 -0700 Subject: [PATCH 3/6] allow ssh_host override for OpenStack --- builder/openstack/builder.go | 1 + builder/openstack/ssh.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 661913dd0..9822626aa 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -133,6 +133,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &communicator.StepConnect{ Config: &b.config.RunConfig.Comm, Host: CommHost( + b.config.RunConfig.Comm.SSHHost, computeClient, b.config.Comm.SSHInterface, b.config.Comm.SSHIPVersion), diff --git a/builder/openstack/ssh.go b/builder/openstack/ssh.go index 0fd9f794b..1f93363c4 100644 --- a/builder/openstack/ssh.go +++ b/builder/openstack/ssh.go @@ -14,10 +14,16 @@ import ( // CommHost looks up the host for the communicator. func CommHost( + host string, client *gophercloud.ServiceClient, sshinterface string, sshipversion string) func(multistep.StateBag) (string, error) { return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + s := state.Get("server").(*servers.Server) // If we have a specific interface, try that From c03c3f4410eeaf55c59cedf565685f787a4e78ec Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 3 Jul 2019 13:30:29 -0700 Subject: [PATCH 4/6] replace duplicate commHost functions with a single communicator helper that checks for ssh_host and does a statebag lookup for a given key --- builder/cloudstack/builder.go | 2 +- builder/cloudstack/ssh.go | 15 --------------- builder/digitalocean/builder.go | 2 +- builder/digitalocean/ssh.go | 18 ------------------ builder/googlecompute/builder.go | 2 +- builder/googlecompute/ssh.go | 18 ------------------ builder/oneandone/builder.go | 2 +- builder/oneandone/ssh.go | 19 ------------------- builder/oracle/classic/builder.go | 2 +- builder/oracle/common/ssh.go | 19 ------------------- builder/oracle/oci/builder.go | 2 +- builder/profitbricks/builder.go | 2 +- builder/profitbricks/ssh.go | 19 ------------------- builder/scaleway/builder.go | 2 +- builder/scaleway/ssh.go | 19 ------------------- builder/yandex/builder.go | 2 +- builder/yandex/ssh.go | 19 ------------------- 17 files changed, 9 insertions(+), 155 deletions(-) delete mode 100644 builder/digitalocean/ssh.go delete mode 100644 builder/googlecompute/ssh.go delete mode 100644 builder/oneandone/ssh.go delete mode 100644 builder/oracle/common/ssh.go delete mode 100644 builder/profitbricks/ssh.go delete mode 100644 builder/scaleway/ssh.go delete mode 100644 builder/yandex/ssh.go diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index 325b2ea86..fe376125f 100644 --- a/builder/cloudstack/builder.go +++ b/builder/cloudstack/builder.go @@ -77,7 +77,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepSetupNetworking{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "ipaddress"), SSHConfig: b.config.Comm.SSHConfigFunc(), SSHPort: commPort, WinRMPort: commPort, diff --git a/builder/cloudstack/ssh.go b/builder/cloudstack/ssh.go index a3b81ca64..518747ceb 100644 --- a/builder/cloudstack/ssh.go +++ b/builder/cloudstack/ssh.go @@ -7,21 +7,6 @@ import ( "github.com/hashicorp/packer/helper/multistep" ) -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - ip, hasIP := state.Get("ipaddress").(string) - if !hasIP { - return "", fmt.Errorf("Failed to retrieve IP address") - } - - return ip, nil - } -} - func commPort(state multistep.StateBag) (int, error) { commPort, hasPort := state.Get("commPort").(int) if !hasPort { diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 7b10c90cd..6519d30f5 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -86,7 +86,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepDropletInfo), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "droplet_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, new(common.StepProvision), diff --git a/builder/digitalocean/ssh.go b/builder/digitalocean/ssh.go deleted file mode 100644 index e0567cd27..000000000 --- a/builder/digitalocean/ssh.go +++ /dev/null @@ -1,18 +0,0 @@ -package digitalocean - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - ipAddress := state.Get("droplet_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/googlecompute/builder.go b/builder/googlecompute/builder.go index 34aa97832..1cd8692b0 100644 --- a/builder/googlecompute/builder.go +++ b/builder/googlecompute/builder.go @@ -67,7 +67,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), WinRMConfig: winrmConfig, }, diff --git a/builder/googlecompute/ssh.go b/builder/googlecompute/ssh.go deleted file mode 100644 index 4638d8242..000000000 --- a/builder/googlecompute/ssh.go +++ /dev/null @@ -1,18 +0,0 @@ -package googlecompute - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/oneandone/builder.go b/builder/oneandone/builder.go index d09bbac64..5ea7674bc 100644 --- a/builder/oneandone/builder.go +++ b/builder/oneandone/builder.go @@ -44,7 +44,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepCreateServer), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/oneandone/ssh.go b/builder/oneandone/ssh.go deleted file mode 100644 index 9a3271f07..000000000 --- a/builder/oneandone/ssh.go +++ /dev/null @@ -1,19 +0,0 @@ -package oneandone - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index f6440ee03..d8e169146 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -129,7 +129,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack KeyName: fmt.Sprintf("packer-generated-key_%s", runID), StepConnectSSH: &communicator.StepConnectSSH{ Config: &b.config.BuilderComm, - Host: ocommon.CommHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.BuilderComm.SSHConfigFunc(), }, }, diff --git a/builder/oracle/common/ssh.go b/builder/oracle/common/ssh.go deleted file mode 100644 index 461c977b1..000000000 --- a/builder/oracle/common/ssh.go +++ /dev/null @@ -1,19 +0,0 @@ -package common - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func CommHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/oracle/oci/builder.go b/builder/oracle/oci/builder.go index f4a60d3e1..7822b144f 100644 --- a/builder/oracle/oci/builder.go +++ b/builder/oracle/oci/builder.go @@ -65,7 +65,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/profitbricks/builder.go b/builder/profitbricks/builder.go index 0cd2e0003..3ab9d18fe 100644 --- a/builder/profitbricks/builder.go +++ b/builder/profitbricks/builder.go @@ -41,7 +41,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepCreateServer), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/profitbricks/ssh.go b/builder/profitbricks/ssh.go deleted file mode 100644 index 5ae5f22d8..000000000 --- a/builder/profitbricks/ssh.go +++ /dev/null @@ -1,19 +0,0 @@ -package profitbricks - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 3b194ed14..83812c4a3 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -56,7 +56,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack new(stepServerInfo), &communicator.StepConnect{ Config: &b.config.Comm, - Host: commHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, new(common.StepProvision), diff --git a/builder/scaleway/ssh.go b/builder/scaleway/ssh.go deleted file mode 100644 index 0c2c3f9b7..000000000 --- a/builder/scaleway/ssh.go +++ /dev/null @@ -1,19 +0,0 @@ -package scaleway - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil - } -} diff --git a/builder/yandex/builder.go b/builder/yandex/builder.go index 8fff5b4ba..5020fec2b 100644 --- a/builder/yandex/builder.go +++ b/builder/yandex/builder.go @@ -61,7 +61,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepInstanceInfo{}, &communicator.StepConnect{ Config: &b.config.Communicator, - Host: commHost(b.config.Communicator.SSHHost), + Host: communicator.CommHost(b.config.Communicator.SSHHost, "instance_ip"), SSHConfig: b.config.Communicator.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/yandex/ssh.go b/builder/yandex/ssh.go deleted file mode 100644 index 4a18a0d79..000000000 --- a/builder/yandex/ssh.go +++ /dev/null @@ -1,19 +0,0 @@ -package yandex - -import ( - "log" - - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(host string) func(multistep.StateBag) (string, error) { - return func(state multistep.StateBag) (string, error) { - if host != "" { - log.Printf("Using ssh_host value: %s", host) - return host, nil - } - - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil - } -} From 8718e98efef7d0134ba5f64e5ae720fb32109b15 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 3 Jul 2019 13:30:50 -0700 Subject: [PATCH 5/6] add common commHost function --- helper/communicator/comm_host.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 helper/communicator/comm_host.go diff --git a/helper/communicator/comm_host.go b/helper/communicator/comm_host.go new file mode 100644 index 000000000..b2ffde9d3 --- /dev/null +++ b/helper/communicator/comm_host.go @@ -0,0 +1,22 @@ +package communicator + +import ( + "log" + + "github.com/hashicorp/packer/helper/multistep" +) + +// Generic commHost function that should work for most cloud builders. +func CommHost(host string, statebagKey string) func(multistep.StateBag) (string, error) { + return func(state multistep.StateBag) (string, error) { + if host != "" { + log.Printf("Using ssh_host value: %s", host) + return host, nil + } + ipAddress, hasIP := state.Get(statebagKey).(string) + if !hasIP { + return "", fmt.Errorf("Failed to retrieve IP address.") + } + return ipAddress, nil + } +} From 242f5b1c9f69730defc881e90f50e7e3574cadb6 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 3 Jul 2019 13:34:23 -0700 Subject: [PATCH 6/6] typos --- builder/cloudstack/ssh.go | 1 - builder/oracle/classic/builder.go | 4 ++-- helper/communicator/comm_host.go | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/builder/cloudstack/ssh.go b/builder/cloudstack/ssh.go index 518747ceb..269c2a583 100644 --- a/builder/cloudstack/ssh.go +++ b/builder/cloudstack/ssh.go @@ -2,7 +2,6 @@ package cloudstack import ( "fmt" - "log" "github.com/hashicorp/packer/helper/multistep" ) diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index d8e169146..d97b3be10 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -99,7 +99,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack }, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, @@ -162,7 +162,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &stepCreateInstance{}, &communicator.StepConnect{ Config: &b.config.Comm, - Host: ocommon.CommHost(b.config.Comm.SSHHost), + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/helper/communicator/comm_host.go b/helper/communicator/comm_host.go index b2ffde9d3..adc49b257 100644 --- a/helper/communicator/comm_host.go +++ b/helper/communicator/comm_host.go @@ -1,6 +1,7 @@ package communicator import ( + "fmt" "log" "github.com/hashicorp/packer/helper/multistep"