GCP: put ssh public/private key in config

This commit is contained in:
Adrien Delorme 2018-08-23 17:27:31 +02:00
parent 51d2aac9f6
commit 663c8134ef
3 changed files with 12 additions and 11 deletions

View File

@ -76,7 +76,7 @@ func getImage(c *Config, d Driver) (*Image, error) {
func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("config").(*Config) c := state.Get("config").(*Config)
d := state.Get("driver").(Driver) d := state.Get("driver").(Driver)
sshPublicKey := state.Get("ssh_public_key").(string)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
sourceImage, err := getImage(c, d) sourceImage, err := getImage(c, d)
@ -98,7 +98,7 @@ func (s *StepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
var errCh <-chan error var errCh <-chan error
var metadata map[string]string 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{ errCh, err = d.RunInstance(&InstanceConfig{
AcceleratorType: c.AcceleratorType, AcceleratorType: c.AcceleratorType,
AcceleratorCount: c.AcceleratorCount, AcceleratorCount: c.AcceleratorCount,

View File

@ -23,10 +23,10 @@ type StepCreateSSHKey struct {
} }
// Run executes the Packer build step that generates SSH key pairs. // 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 // The key pairs are added to the ssh config
// "ssh_public_key".
func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
if s.PrivateKeyFile != "" { if s.PrivateKeyFile != "" {
ui.Say("Using existing SSH private key") ui.Say("Using existing SSH private key")
@ -37,8 +37,8 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("ssh_private_key", string(privateKeyBytes)) config.Comm.SSHPrivateKey = privateKeyBytes
state.Put("ssh_public_key", "") config.Comm.SSHPublicKey = nil
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -65,8 +65,8 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) config.Comm.SSHPrivateKey = pem.EncodeToMemory(&priv_blk)
state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) config.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub)
if s.Debug { if s.Debug {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))

View File

@ -26,6 +26,8 @@ type Config struct {
SSHPort int `mapstructure:"ssh_port"` SSHPort int `mapstructure:"ssh_port"`
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"` SSHPassword string `mapstructure:"ssh_password"`
SSHPublicKey []byte `mapstructure:"ssh_public_key"`
SSHPrivateKey []byte `mapstructure:"ssh_private_key"`
SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"` SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
SSHPty bool `mapstructure:"ssh_pty"` SSHPty bool `mapstructure:"ssh_pty"`
SSHTimeout time.Duration `mapstructure:"ssh_timeout"` 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))) privateKeys = append(privateKeys, []byte(iKey.(string)))
} }
// gcp key if len(c.SSHPrivateKey) != 0 {
if iKey, hasKey := state.GetOk("ssh_private_key"); hasKey { privateKeys = append(privateKeys, c.SSHPrivateKey)
privateKeys = append(privateKeys, []byte(iKey.(string)))
} }
//scaleway key //scaleway key