Skip to content

Commit

Permalink
Merge pull request #4850 from hashicorp/tmpkeyflag
Browse files Browse the repository at this point in the history
builder/amazon: set flag to delete temporary keypair
  • Loading branch information
mwhooker authored May 1, 2017
2 parents 1ec9525 + 80d6cd9 commit 98af553
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
16 changes: 6 additions & 10 deletions builder/amazon/common/step_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type StepKeyPair struct {
KeyPairName string
PrivateKeyFile string

keyName string
doCleanup bool
}

func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -67,11 +67,10 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}

// 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)
state.Put("keyPair", s.TemporaryKeyPairName)
state.Put("privateKey", *keyResp.KeyMaterial)

// If we're in debug mode, output the private key to the working
Expand Down Expand Up @@ -104,10 +103,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
}

Expand All @@ -116,10 +112,10 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) {

// Remove the keypair
ui.Say("Deleting temporary keypair...")
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.keyName})
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.TemporaryKeyPairName})
if err != nil {
ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))
}

// Also remove the physical key if we're debugging.
Expand Down
18 changes: 6 additions & 12 deletions builder/openstack/step_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type StepKeyPair struct {
KeyPairName string
PrivateKeyFile string

keyName string
doCleanup bool
}

func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -113,11 +113,11 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
}
}

// Set the keyname so we know to delete it later
s.keyName = s.TemporaryKeyPairName
// we created a temporary key, so remember to clean it up
s.doCleanup = true

// Set some state data for use in future steps
state.Put("keyPair", s.keyName)
state.Put("keyPair", s.TemporaryKeyPairName)
state.Put("privateKey", keypair.PrivateKey)

return multistep.ActionContinue
Expand Down Expand Up @@ -167,13 +167,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
}

Expand All @@ -189,7 +183,7 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
}

ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPairName))
err = keypairs.Delete(computeClient, s.keyName).ExtractErr()
err = keypairs.Delete(computeClient, s.TemporaryKeyPairName).ExtractErr()
if err != nil {
ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))
Expand Down

0 comments on commit 98af553

Please sign in to comment.