Merge pull request #4850 from hashicorp/tmpkeyflag

builder/amazon: set flag to delete temporary keypair
This commit is contained in:
Matthew Hooker 2017-05-01 10:44:33 -07:00 committed by GitHub
commit 98af553685
2 changed files with 12 additions and 22 deletions

View File

@ -19,7 +19,7 @@ type StepKeyPair struct {
KeyPairName string KeyPairName string
PrivateKeyFile string PrivateKeyFile string
keyName string doCleanup bool
} }
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
@ -67,11 +67,10 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
// Set the keyname so we know to delete it later s.doCleanup = true
s.keyName = s.TemporaryKeyPairName
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("keyPair", s.keyName) state.Put("keyPair", s.TemporaryKeyPairName)
state.Put("privateKey", *keyResp.KeyMaterial) state.Put("privateKey", *keyResp.KeyMaterial)
// If we're in debug mode, output the private key to the working // If we're in debug mode, output the private key to the working
@ -104,10 +103,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
} }
func (s *StepKeyPair) Cleanup(state multistep.StateBag) { func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// If no key name is set, then we never created it, so just return if !s.doCleanup {
// If we used an SSH private key file, do not go about deleting
// keypairs
if s.PrivateKeyFile != "" || (s.KeyPairName == "" && s.keyName == "") {
return return
} }
@ -116,10 +112,10 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// Remove the keypair // Remove the keypair
ui.Say("Deleting temporary 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 { if err != nil {
ui.Error(fmt.Sprintf( 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. // Also remove the physical key if we're debugging.

View File

@ -22,7 +22,7 @@ type StepKeyPair struct {
KeyPairName string KeyPairName string
PrivateKeyFile string PrivateKeyFile string
keyName string doCleanup bool
} }
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
@ -113,11 +113,11 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
} }
} }
// Set the keyname so we know to delete it later // we created a temporary key, so remember to clean it up
s.keyName = s.TemporaryKeyPairName s.doCleanup = true
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("keyPair", s.keyName) state.Put("keyPair", s.TemporaryKeyPairName)
state.Put("privateKey", keypair.PrivateKey) state.Put("privateKey", keypair.PrivateKey)
return multistep.ActionContinue return multistep.ActionContinue
@ -167,13 +167,7 @@ func berToDer(ber string, ui packer.Ui) string {
} }
func (s *StepKeyPair) Cleanup(state multistep.StateBag) { func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// If we used an SSH private key file, do not go about deleting if !s.doCleanup {
// 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 == "" {
return return
} }
@ -189,7 +183,7 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
} }
ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPairName)) 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 { if err != nil {
ui.Error(fmt.Sprintf( ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName)) "Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))