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

View File

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

View File

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

View File

@ -34,7 +34,6 @@ type Config struct {
Expunge bool `mapstructure:"expunge"` Expunge bool `mapstructure:"expunge"`
Hypervisor string `mapstructure:"hypervisor"` Hypervisor string `mapstructure:"hypervisor"`
InstanceName string `mapstructure:"instance_name"` InstanceName string `mapstructure:"instance_name"`
Keypair string `mapstructure:"keypair"`
Network string `mapstructure:"network"` Network string `mapstructure:"network"`
Project string `mapstructure:"project"` Project string `mapstructure:"project"`
PublicIPAddress string `mapstructure:"public_ip_address"` 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, // 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 // then create a temporary one, but only if the temporary_keypair_name has not
// been provided. // been provided.
if c.Keypair == "" && c.Comm.SSHTemporaryKeyPairName == "" && if c.Comm.SSHKeyPairName == "" && c.Comm.SSHTemporaryKeyPairName == "" &&
c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" { c.Comm.SSHPrivateKeyFile == "" && c.Comm.SSHPassword == "" {
c.Comm.SSHTemporaryKeyPairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) 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.SetName(config.InstanceName)
p.SetDisplayname("Created by Packer") p.SetDisplayname("Created by Packer")
if keypair, ok := state.GetOk("keypair"); ok { if len(config.Comm.SSHKeyPairName) != 0 {
kp := keypair.(string) ui.Message(fmt.Sprintf("Using keypair: %s", config.Comm.SSHKeyPairName))
ui.Message(fmt.Sprintf("Using keypair: %s", kp)) p.SetKeypair(config.Comm.SSHKeyPairName)
p.SetKeypair(kp)
} }
if securitygroups, ok := state.GetOk("security_groups"); ok { 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 != "" { if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
ui.Say(fmt.Sprintf("Using SSH Agent for existing keypair %s", 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 return multistep.ActionContinue
} }
if s.Comm.SSHTemporaryKeyPairName == "" { if s.Comm.SSHTemporaryKeyPairName == "" {
ui.Say("Not using a keypair") ui.Say("Not using a keypair")
state.Put("keypair", "") s.Comm.SSHKeyPairName = ""
return multistep.ActionContinue 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 // Set some data for use in future steps
state.Put("keypair", s.Comm.SSHTemporaryKeyPairName) s.Comm.SSHKeyPairName = s.Comm.SSHTemporaryKeyPairName
state.Put("privateKey", keypair.Privatekey) s.Comm.SSHPrivateKey = []byte(keypair.Privatekey)
return multistep.ActionContinue return multistep.ActionContinue
} }