diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index c40b112a3..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, + 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 ec9fbe123..269c2a583 100644 --- a/builder/cloudstack/ssh.go +++ b/builder/cloudstack/ssh.go @@ -6,15 +6,6 @@ import ( "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") - } - - 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 4b5f6e36d..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, + 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 b47628ba8..000000000 --- a/builder/digitalocean/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package digitalocean - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - 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..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, + 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 e498ed43b..000000000 --- a/builder/googlecompute/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package googlecompute - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - 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..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, + 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 4068b87af..000000000 --- a/builder/oneandone/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package oneandone - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("server_ip").(string) - return ipAddress, nil -} 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 diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index eb0ce97d8..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, + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), 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: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), 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: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), SSHConfig: b.config.Comm.SSHConfigFunc(), }, &common.StepProvision{}, diff --git a/builder/oracle/common/ssh.go b/builder/oracle/common/ssh.go deleted file mode 100644 index 53b5d349a..000000000 --- a/builder/oracle/common/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package common - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func CommHost(state multistep.StateBag) (string, error) { - 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..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, + Host: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"), 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 + } + + return ip, nil } - - ip, err := driver.IPAddress(mac) - if err != nil { - return "", err - } - - 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..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, + 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 c860bc841..000000000 --- a/builder/profitbricks/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package profitbricks - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - 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..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, + 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 e291151ab..000000000 --- a/builder/scaleway/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package scaleway - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - 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) + + machine, err := driver.GetMachineIP(machineID) + if err != nil { + return "", err + } + + return machine, nil } - - return machine, nil } diff --git a/builder/yandex/builder.go b/builder/yandex/builder.go index 38ea7524d..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, + 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 c32f16b53..000000000 --- a/builder/yandex/ssh.go +++ /dev/null @@ -1,10 +0,0 @@ -package yandex - -import ( - "github.com/hashicorp/packer/helper/multistep" -) - -func commHost(state multistep.StateBag) (string, error) { - ipAddress := state.Get("instance_ip").(string) - return ipAddress, nil -} diff --git a/helper/communicator/comm_host.go b/helper/communicator/comm_host.go new file mode 100644 index 000000000..adc49b257 --- /dev/null +++ b/helper/communicator/comm_host.go @@ -0,0 +1,23 @@ +package communicator + +import ( + "fmt" + "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 + } +}