2013-07-20 22:40:45 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-06-04 13:00:06 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 03:55:32 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-03-24 07:47:00 -04:00
|
|
|
"io/ioutil"
|
2013-08-30 17:48:50 -04:00
|
|
|
"os"
|
2013-08-30 18:04:23 -04:00
|
|
|
"runtime"
|
2013-05-21 03:55:32 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:40:45 -04:00
|
|
|
type StepKeyPair struct {
|
2014-03-24 07:47:00 -04:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
KeyPairName string
|
|
|
|
PrivateKeyFile string
|
2013-08-30 17:48:50 -04:00
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
keyName string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
|
2014-03-24 07:47:00 -04:00
|
|
|
if s.PrivateKeyFile != "" {
|
|
|
|
s.keyName = ""
|
|
|
|
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error loading configured private key file: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("keyPair", "")
|
|
|
|
state.Put("privateKey", string(privateKeyBytes))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-09-05 15:23:08 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.KeyPairName))
|
|
|
|
keyResp, err := ec2conn.CreateKeyPair(s.KeyPairName)
|
2013-05-21 03:55:32 -04:00
|
|
|
if err != nil {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the keyname so we know to delete it later
|
2013-09-05 15:23:08 -04:00
|
|
|
s.keyName = s.KeyPairName
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
2013-09-05 15:23:08 -04:00
|
|
|
state.Put("keyPair", s.keyName)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("privateKey", keyResp.KeyMaterial)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-08-30 17:48:50 -04:00
|
|
|
// If we're in debug mode, output the private key to the working
|
|
|
|
// directory.
|
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
|
|
|
|
f, err := os.Create(s.DebugKeyPath)
|
|
|
|
if err != nil {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
2013-08-30 17:48:50 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2013-08-30 18:03:29 -04:00
|
|
|
// Write the key out
|
2013-08-30 17:48:50 -04:00
|
|
|
if _, err := f.Write([]byte(keyResp.KeyMaterial)); err != nil {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
|
2013-08-30 17:48:50 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-08-30 18:03:29 -04:00
|
|
|
|
|
|
|
// Chmod it so that it is SSH ready
|
2013-08-30 18:04:23 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err := f.Chmod(0600); err != nil {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error setting permissions of debug key: %s", err))
|
2013-08-30 18:04:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-08-30 18:03:29 -04:00
|
|
|
}
|
2013-08-30 17:48:50 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
|
2013-05-21 03:55:32 -04:00
|
|
|
// If no key name is set, then we never created it, so just return
|
|
|
|
if s.keyName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary keypair...")
|
|
|
|
_, err := ec2conn.DeleteKeyPair(s.keyName)
|
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
}
|