Merge pull request #7832 from hashicorp/fix_ssh_host

Fix ssh host
This commit is contained in:
Megan Marsh 2019-07-03 16:22:15 -07:00 committed by GitHub
commit aa66fe5d01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 121 additions and 128 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: communicator.CommHost(b.config.Comm.SSHHost, "ipaddress"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
SSHPort: commPort,
WinRMPort: commPort,

View File

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

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: communicator.CommHost(b.config.Comm.SSHHost, "droplet_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
new(common.StepProvision),

View File

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

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: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
WinRMConfig: winrmConfig,
},

View File

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

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: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&common.StepProvision{},

View File

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

View File

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

View File

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

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

View File

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

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: communicator.CommHost(b.config.Comm.SSHHost, "instance_ip"),
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: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
&common.StepProvision{},

View File

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

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: communicator.CommHost(b.config.Comm.SSHHost, "server_ip"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
},
new(common.StepProvision),

View File

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

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: communicator.CommHost(b.config.Communicator.SSHHost, "instance_ip"),
SSHConfig: b.config.Communicator.SSHConfigFunc(),
},
&common.StepProvision{},

View File

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

View File

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