From 0938b640cc548e9cece5c37b116defa049362709 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 29 Aug 2018 11:23:59 +0200 Subject: [PATCH] use key pair name from config --- builder/alicloud/ecs/step_config_key_pair.go | 4 +--- builder/amazon/common/step_key_pair.go | 3 +-- builder/amazon/common/step_run_source_instance.go | 11 +++++------ builder/amazon/common/step_run_spot_instance.go | 10 ++++------ builder/cloudstack/config.go | 3 +-- builder/cloudstack/step_create_instance.go | 7 +++---- builder/cloudstack/step_keypair.go | 9 ++++----- 7 files changed, 19 insertions(+), 28 deletions(-) diff --git a/builder/alicloud/ecs/step_config_key_pair.go b/builder/alicloud/ecs/step_config_key_pair.go index 14941666e..9d502ea68 100644 --- a/builder/alicloud/ecs/step_config_key_pair.go +++ b/builder/alicloud/ecs/step_config_key_pair.go @@ -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 } diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 93651a21c..9b34b64af 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -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 } diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 7c31fa1f8..a63ca1a47 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -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 { diff --git a/builder/amazon/common/step_run_spot_instance.go b/builder/amazon/common/step_run_spot_instance.go index 09d456496..e2f7822d7 100644 --- a/builder/amazon/common/step_run_spot_instance.go +++ b/builder/amazon/common/step_run_spot_instance.go @@ -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{ diff --git a/builder/cloudstack/config.go b/builder/cloudstack/config.go index 82a28bf24..f74a89502 100644 --- a/builder/cloudstack/config.go +++ b/builder/cloudstack/config.go @@ -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()) } diff --git a/builder/cloudstack/step_create_instance.go b/builder/cloudstack/step_create_instance.go index 81f0c0699..11303f46e 100644 --- a/builder/cloudstack/step_create_instance.go +++ b/builder/cloudstack/step_create_instance.go @@ -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 { diff --git a/builder/cloudstack/step_keypair.go b/builder/cloudstack/step_keypair.go index f51963059..f08bd706d 100644 --- a/builder/cloudstack/step_keypair.go +++ b/builder/cloudstack/step_keypair.go @@ -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 }