From 663c8134efd9efde111e83859a946eeae2008d77 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 23 Aug 2018 17:27:31 +0200 Subject: [PATCH] GCP: put ssh public/private key in config --- builder/googlecompute/step_create_instance.go | 4 ++-- builder/googlecompute/step_create_ssh_key.go | 12 ++++++------ helper/communicator/config.go | 7 ++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index de3c7b2ac..5e0592894 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -76,7 +76,7 @@ func getImage(c *Config, d Driver) (*Image, error) { func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { c := state.Get("config").(*Config) d := state.Get("driver").(Driver) - sshPublicKey := state.Get("ssh_public_key").(string) + ui := state.Get("ui").(packer.Ui) sourceImage, err := getImage(c, d) @@ -98,7 +98,7 @@ func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu var errCh <-chan error var metadata map[string]string - metadata, err = c.createInstanceMetadata(sourceImage, sshPublicKey) + metadata, err = c.createInstanceMetadata(sourceImage, string(c.Comm.SSHPublicKey)) errCh, err = d.RunInstance(&InstanceConfig{ AcceleratorType: c.AcceleratorType, AcceleratorCount: c.AcceleratorCount, diff --git a/builder/googlecompute/step_create_ssh_key.go b/builder/googlecompute/step_create_ssh_key.go index 7d5a24555..476ea454c 100644 --- a/builder/googlecompute/step_create_ssh_key.go +++ b/builder/googlecompute/step_create_ssh_key.go @@ -23,10 +23,10 @@ type StepCreateSSHKey struct { } // Run executes the Packer build step that generates SSH key pairs. -// The key pairs are added to the multistep state as "ssh_private_key" and -// "ssh_public_key". +// The key pairs are added to the ssh config func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) + config := state.Get("config").(*Config) if s.PrivateKeyFile != "" { ui.Say("Using existing SSH private key") @@ -37,8 +37,8 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult return multistep.ActionHalt } - state.Put("ssh_private_key", string(privateKeyBytes)) - state.Put("ssh_public_key", "") + config.Comm.SSHPrivateKey = privateKeyBytes + config.Comm.SSHPublicKey = nil return multistep.ActionContinue } @@ -65,8 +65,8 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult ui.Error(err.Error()) return multistep.ActionHalt } - state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) - state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) + config.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk) + config.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub) if s.Debug { ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) diff --git a/helper/communicator/config.go b/helper/communicator/config.go index a0db7b6eb..3d25f210c 100644 --- a/helper/communicator/config.go +++ b/helper/communicator/config.go @@ -26,6 +26,8 @@ type Config struct { SSHPort int `mapstructure:"ssh_port"` SSHUsername string `mapstructure:"ssh_username"` SSHPassword string `mapstructure:"ssh_password"` + SSHPublicKey []byte `mapstructure:"ssh_public_key"` + SSHPrivateKey []byte `mapstructure:"ssh_private_key"` SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"` SSHPty bool `mapstructure:"ssh_pty"` SSHTimeout time.Duration `mapstructure:"ssh_timeout"` @@ -97,9 +99,8 @@ func (c *Config) SSHConfigFunc() func(multistep.StateBag) (*ssh.ClientConfig, er privateKeys = append(privateKeys, []byte(iKey.(string))) } - // gcp key - if iKey, hasKey := state.GetOk("ssh_private_key"); hasKey { - privateKeys = append(privateKeys, []byte(iKey.(string))) + if len(c.SSHPrivateKey) != 0 { + privateKeys = append(privateKeys, c.SSHPrivateKey) } //scaleway key