diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 29972c9bc..a94adb01a 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -19,7 +19,8 @@ type StepKeyPair struct { KeyPairName string PrivateKeyFile string - keyName string + keyName string + doCleanup bool } func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { @@ -69,6 +70,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { // Set the keyname so we know to delete it later s.keyName = s.TemporaryKeyPairName + s.doCleanup = true // Set some state data for use in future steps state.Put("keyPair", s.keyName) @@ -104,10 +106,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { } func (s *StepKeyPair) Cleanup(state multistep.StateBag) { - // If no key name is set, then we never created it, so just return - // If we used an SSH private key file, do not go about deleting - // keypairs - if s.PrivateKeyFile != "" || (s.KeyPairName == "" && s.keyName == "") { + if !s.doCleanup { return } diff --git a/builder/openstack/step_key_pair.go b/builder/openstack/step_key_pair.go index 5125c6c8f..b59052b6f 100644 --- a/builder/openstack/step_key_pair.go +++ b/builder/openstack/step_key_pair.go @@ -22,7 +22,8 @@ type StepKeyPair struct { KeyPairName string PrivateKeyFile string - keyName string + keyName string + doCleanup bool } func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { @@ -84,6 +85,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { } ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.TemporaryKeyPairName)) + s.doCleanup = true keypair.PrivateKey = berToDer(keypair.PrivateKey, ui) @@ -167,13 +169,7 @@ func berToDer(ber string, ui packer.Ui) string { } func (s *StepKeyPair) Cleanup(state multistep.StateBag) { - // If we used an SSH private key file, do not go about deleting - // keypairs - if s.PrivateKeyFile != "" || (s.KeyPairName == "" && s.keyName == "") { - return - } - // If no key name is set, then we never created it, so just return - if s.TemporaryKeyPairName == "" { + if !s.doCleanup { return }