github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/step_key_pair.go (about) 1 package common 2 3 import ( 4 "cgl.tideland.biz/identifier" 5 "encoding/hex" 6 "fmt" 7 "github.com/mitchellh/goamz/ec2" 8 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/packer" 10 "log" 11 ) 12 13 type StepKeyPair struct { 14 keyName string 15 } 16 17 func (s *StepKeyPair) Run(state map[string]interface{}) multistep.StepAction { 18 ec2conn := state["ec2"].(*ec2.EC2) 19 ui := state["ui"].(packer.Ui) 20 21 ui.Say("Creating temporary keypair for this instance...") 22 keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw())) 23 log.Printf("temporary keypair name: %s", keyName) 24 keyResp, err := ec2conn.CreateKeyPair(keyName) 25 if err != nil { 26 state["error"] = fmt.Errorf("Error creating temporary keypair: %s", err) 27 return multistep.ActionHalt 28 } 29 30 // Set the keyname so we know to delete it later 31 s.keyName = keyName 32 33 // Set some state data for use in future steps 34 state["keyPair"] = keyName 35 state["privateKey"] = keyResp.KeyMaterial 36 37 return multistep.ActionContinue 38 } 39 40 func (s *StepKeyPair) Cleanup(state map[string]interface{}) { 41 // If no key name is set, then we never created it, so just return 42 if s.keyName == "" { 43 return 44 } 45 46 ec2conn := state["ec2"].(*ec2.EC2) 47 ui := state["ui"].(packer.Ui) 48 49 ui.Say("Deleting temporary keypair...") 50 _, err := ec2conn.DeleteKeyPair(s.keyName) 51 if err != nil { 52 ui.Error(fmt.Sprintf( 53 "Error cleaning up keypair. Please delete the key manually: %s", s.keyName)) 54 } 55 }