builder/amazon: set flag to delete temporary keypair

This commit is contained in:
Matthew Hooker 2017-04-30 14:24:22 -07:00
parent 1ec9525204
commit 602fd64c5d
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 8 additions and 13 deletions

View File

@ -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
}

View File

@ -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
}