diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index f6e859c03..67ec74d79 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -88,7 +88,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { // if we are not given an explicit keypairname, create a temporary one if c.SSHKeyPairName == "" { - c.SSHKeyPairName = fmt.Sprintf( + c.TemporaryKeyPairName = fmt.Sprintf( "packer %s", uuid.TimeOrderedUUID()) } diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index c4e1fa110..8e9c4b6b9 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -142,12 +142,12 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { c := testConfig() - c.SSHKeyPairName = "" + c.TemporaryKeyPairName = "" if err := c.Prepare(nil); len(err) != 0 { t.Fatalf("err: %s", err) } - if c.SSHKeyPairName == "" { + if c.TemporaryKeyPairName == "" { t.Fatal("keypair empty") } } diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index db60e1e40..f6e6a0555 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -13,6 +13,7 @@ import ( type StepKeyPair struct { Debug bool DebugKeyPath string + TemporaryKeyPairName string KeyPairName string PrivateKeyFile string @@ -21,7 +22,9 @@ type StepKeyPair struct { func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { if s.PrivateKeyFile != "" { - s.keyName = s.KeyPairName // need to get from config + if s.KeyPairName != "" { + s.keyName = s.KeyPairName // need to get from config + } privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile) if err != nil { @@ -38,15 +41,15 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) ui := state.Get("ui").(packer.Ui) - ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.KeyPairName)) - keyResp, err := ec2conn.CreateKeyPair(s.KeyPairName) + ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName)) + keyResp, err := ec2conn.CreateKeyPair(s.TemporaryKeyPairName) if err != nil { state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err)) return multistep.ActionHalt } // Set the keyname so we know to delete it later - s.keyName = s.KeyPairName + s.keyName = s.TemporaryKeyPairName // Set some state data for use in future steps state.Put("keyPair", s.keyName) @@ -89,13 +92,13 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) { return } - //ec2conn := state.Get("ec2").(*ec2.EC2) + ec2conn := state.Get("ec2").(*ec2.EC2) ui := state.Get("ui").(packer.Ui) - ui.Say("DANGER: Deleting temporary keypair (not really)...") - //_, err := ec2conn.DeleteKeyPair(s.keyName) - //if err != nil { - //ui.Error(fmt.Sprintf( - //"Error cleaning up keypair. Please delete the key manually: %s", s.keyName)) - //} + ui.Say("DANGER: Deleting temporary keypair...") + _, err := ec2conn.DeleteKeyPair(s.keyName) + if err != nil { + ui.Error(fmt.Sprintf( + "Error cleaning up keypair. Please delete the key manually: %s", s.keyName)) + } } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 083507993..95e7ea016 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -89,10 +89,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnhancedNetworking: b.config.AMIEnhancedNetworking, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, - PrivateKeyFile: b.config.SSHPrivateKeyFile, + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + TemporaryKeyPairName: b.config.TemporaryKeyPairName, + KeyPairName: b.config.SSHKeyPairName, + PrivateKeyFile: b.config.SSHPrivateKeyFile, }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index ce582e039..b677f4da9 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -194,10 +194,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe EnhancedNetworking: b.config.AMIEnhancedNetworking, }, &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairName: b.config.SSHKeyPairName, - PrivateKeyFile: b.config.SSHPrivateKeyFile, + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + TemporaryKeyPairName: b.config.TemporaryKeyPairName, + KeyPairName: b.config.SSHKeyPairName, + PrivateKeyFile: b.config.SSHPrivateKeyFile, }, &awscommon.StepSecurityGroup{ SecurityGroupIds: b.config.SecurityGroupIds,