use key pair name from config

This commit is contained in:
Adrien Delorme 2018-08-29 11:23:59 +02:00
parent 1e71712cc9
commit 0938b640cc
7 changed files with 19 additions and 28 deletions

View File

@ -35,7 +35,6 @@ func (s *stepConfigAlicloudKeyPair) Run(_ context.Context, state multistep.State
return multistep.ActionHalt
}
s.Comm.SSHKeyPairName = s.Comm.SSHKeyPairName
s.Comm.SSHPrivateKey = privateKeyBytes
return multistep.ActionContinue
@ -48,13 +47,12 @@ func (s *stepConfigAlicloudKeyPair) Run(_ context.Context, state multistep.State
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.Comm.SSHKeyPairName))
state.Put("keyPair", s.Comm.SSHKeyPairName)
return multistep.ActionContinue
}
if s.Comm.SSHTemporaryKeyPairName == "" {
ui.Say("Not using temporary keypair")
state.Put("keyPair", "")
s.Comm.SSHKeyPairName = ""
return multistep.ActionContinue
}

View File

@ -45,13 +45,12 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.Comm.SSHKeyPairName))
state.Put("keyPair", s.Comm.SSHKeyPairName)
return multistep.ActionContinue
}
if s.Comm.SSHTemporaryKeyPairName == "" {
ui.Say("Not using temporary keypair")
state.Put("keyPair", "")
s.Comm.SSHKeyPairName = ""
return multistep.ActionContinue
}

View File

@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
retry "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
@ -21,6 +22,7 @@ type StepRunSourceInstance struct {
AssociatePublicIpAddress bool
AvailabilityZone string
BlockDevices BlockDevices
Comm *communicator.Config
Ctx interpolate.Context
Debug bool
EbsOptimized bool
@ -42,10 +44,7 @@ type StepRunSourceInstance struct {
func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2)
var keyName string
if name, ok := state.GetOk("keyPair"); ok {
keyName = name.(string)
}
securityGroupIds := aws.StringSlice(state.Get("securityGroupIds").([]string))
ui := state.Get("ui").(packer.Ui)
@ -150,8 +149,8 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
volTags.Report(ui)
}
if keyName != "" {
runOpts.KeyName = &keyName
if s.Comm.SSHKeyPairName != "" {
runOpts.KeyName = &s.Comm.SSHKeyPairName
}
if s.SubnetId != "" && s.AssociatePublicIpAddress {

View File

@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
retry "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
@ -24,6 +25,7 @@ type StepRunSpotInstance struct {
AvailabilityZone string
BlockDevices BlockDevices
Debug bool
Comm *communicator.Config
EbsOptimized bool
ExpectedRootDevice string
IamInstanceProfile string
@ -46,10 +48,6 @@ type StepRunSpotInstance struct {
func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2)
var keyName string
if name, ok := state.GetOk("keyPair"); ok {
keyName = name.(string)
}
securityGroupIds := aws.StringSlice(state.Get("securityGroupIds").([]string))
ui := state.Get("ui").(packer.Ui)
@ -184,8 +182,8 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
runOpts.SecurityGroupIds = securityGroupIds
}
if keyName != "" {
runOpts.KeyName = &keyName
if s.Comm.SSHKeyPairName != "" {
runOpts.KeyName = &s.Comm.SSHKeyPairName
}
runSpotResp, err := ec2conn.RequestSpotInstances(&ec2.RequestSpotInstancesInput{

View File

@ -34,7 +34,6 @@ type Config struct {
Expunge bool `mapstructure:"expunge"`
Hypervisor string `mapstructure:"hypervisor"`
InstanceName string `mapstructure:"instance_name"`
Keypair string `mapstructure:"keypair"`
Network string `mapstructure:"network"`
Project string `mapstructure:"project"`
PublicIPAddress string `mapstructure:"public_ip_address"`
@ -126,7 +125,7 @@ func NewConfig(raws ...interface{}) (*Config, error) {
// If we are not given an explicit keypair, ssh_password or ssh_private_key_file,
// then create a temporary one, but only if the temporary_keypair_name has not
// been provided.
if c.Keypair == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
if c.Comm.SSHKeyPairName == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" {
c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
}

View File

@ -46,10 +46,9 @@ func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
p.SetName(config.InstanceName)
p.SetDisplayname("Created by Packer")
if keypair, ok := state.GetOk("keypair"); ok {
kp := keypair.(string)
ui.Message(fmt.Sprintf("Using keypair: %s", kp))
p.SetKeypair(kp)
if len(config.Comm.SSHKeyPairName) != 0 {
ui.Message(fmt.Sprintf("Using keypair: %s", config.Comm.SSHKeyPairName))
p.SetKeypair(config.Comm.SSHKeyPairName)
}
if securitygroups, ok := state.GetOk("security_groups"); ok {

View File

@ -42,13 +42,12 @@ func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", s.Comm.SSHKeyPairName))
state.Put("keypair", s.Comm.SSHKeyPairName)
return multistep.ActionContinue
}
if s.Comm.SSHTemporaryKeyPairName == "" {
ui.Say("Not using a keypair")
state.Put("keypair", "")
s.Comm.SSHKeyPairName = ""
return multistep.ActionContinue
}
@ -109,9 +108,9 @@ func (s *stepKeypair) Run(_ context.Context, state multistep.StateBag) multistep
}
}
// Set some state data for use in future steps
state.Put("keypair", s.Comm.SSHTemporaryKeyPairName)
state.Put("privateKey", keypair.Privatekey)
// Set some data for use in future steps
s.Comm.SSHKeyPairName = s.Comm.SSHTemporaryKeyPairName
s.Comm.SSHPrivateKey = []byte(keypair.Privatekey)
return multistep.ActionContinue
}