From eb8c74bf85b5a55612dd5cdd359a9eb206a8be07 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 2 Jul 2019 13:56:28 -0700 Subject: [PATCH] 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 + } }