allow ssh_host config option to override host logic for all builders

This commit is contained in:
Megan Marsh 2019-07-02 13:56:28 -07:00
parent ad8a88e366
commit eb8c74bf85
28 changed files with 185 additions and 79 deletions

View File

@ -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,

View File

@ -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) {

View File

@ -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),

View File

@ -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
}
}

View File

@ -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{},

View File

@ -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)
}
}

View File

@ -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,
},

View File

@ -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
}

View File

@ -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{},

View File

@ -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) {

View File

@ -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{},

View File

@ -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
}
}

View File

@ -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{},

View File

@ -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
}
}

View File

@ -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{},

View File

@ -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
}

View File

@ -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(),
},
&parallelscommon.StepUploadVersion{

View File

@ -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(),
},
&parallelscommon.StepUploadVersion{

View File

@ -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{},

View File

@ -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
}
}

View File

@ -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,

View File

@ -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) {

View File

@ -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),

View File

@ -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
}
}

View File

@ -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{},

View File

@ -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
}

View File

@ -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{},

View File

@ -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
}
}