replace duplicate commHost functions with a single communicator helper that checks for ssh_host and does a statebag lookup for a given key

This commit is contained in:
Megan Marsh 2019-07-03 13:30:29 -07:00
parent 0cfe8e7070
commit c03c3f4410
17 changed files with 9 additions and 155 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(b.config.Comm.SSHHost),
Host: communicator.CommHost(b.config.Comm.SSHHost, "ipaddress"),
SSHConfig: b.config.Comm.SSHConfigFunc(),
SSHPort: commPort,
WinRMPort: commPort,

View File

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

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

View File

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

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

View File

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

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

View File

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

View File

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

View File

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

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

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

View File

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

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

View File

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

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

View File

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