builder.gcp: use c.Comm for ssh

This commit is contained in:
Adrien Delorme 2018-08-28 17:47:38 +02:00
parent e0e6b0b8f7
commit 4982627dd2
3 changed files with 14 additions and 9 deletions

View File

@ -51,9 +51,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps := []multistep.Step{
new(StepCheckExistingImage),
&StepCreateSSHKey{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
PrivateKeyFile: b.config.Comm.SSHPrivateKeyFile,
Debug: b.config.PackerDebug,
Comm: &b.config.Comm,
DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName),
},
&StepCreateInstance{
Debug: b.config.PackerDebug,

View File

@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"golang.org/x/crypto/ssh"
@ -17,9 +18,9 @@ import (
// StepCreateSSHKey represents a Packer build step that generates SSH key pairs.
type StepCreateSSHKey struct {
Debug bool
DebugKeyPath string
PrivateKeyFile string
Debug bool
Comm *communicator.Config
DebugKeyPath string
}
// Run executes the Packer build step that generates SSH key pairs.
@ -28,9 +29,9 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
if s.PrivateKeyFile != "" {
if s.Comm.SSHPrivateKeyFile != "" {
ui.Say("Using existing SSH private key")
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
privateKeyBytes, err := ioutil.ReadFile(s.Comm.SSHPrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf(
"Error loading configured private key file: %s", err))

View File

@ -3,6 +3,7 @@ package googlecompute
import (
"context"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"io/ioutil"
@ -17,7 +18,8 @@ func TestStepCreateSSHKey_impl(t *testing.T) {
func TestStepCreateSSHKey_privateKey(t *testing.T) {
state := testState(t)
step := new(StepCreateSSHKey)
step.PrivateKeyFile = "test-fixtures/fake-key"
step.Comm = new(communicator.Config)
step.Comm.SSHPrivateKeyFile = "test-fixtures/fake-key"
defer step.Cleanup(state)
// run the step
@ -35,6 +37,7 @@ func TestStepCreateSSHKey_privateKey(t *testing.T) {
func TestStepCreateSSHKey(t *testing.T) {
state := testState(t)
step := new(StepCreateSSHKey)
step.Comm = new(communicator.Config)
defer step.Cleanup(state)
// run the step
@ -62,6 +65,7 @@ func TestStepCreateSSHKey_debug(t *testing.T) {
state := testState(t)
step := new(StepCreateSSHKey)
step.Comm = new(communicator.Config)
step.Debug = true
step.DebugKeyPath = tf.Name()