2013-07-20 22:40:45 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-05 17:58:48 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-21 03:55:32 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:40:45 -04:00
|
|
|
type StepKeyPair struct {
|
2015-01-13 22:58:41 -05:00
|
|
|
Debug bool
|
|
|
|
DebugKeyPath string
|
|
|
|
TemporaryKeyPairName 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 != "" {
|
|
|
|
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
|
|
|
|
if err != nil {
|
2015-06-19 00:21:20 -04:00
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"Error loading configured private key file: %s", err))
|
2014-03-24 07:47:00 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:21:20 -04:00
|
|
|
state.Put("keyPair", s.KeyPairName)
|
2014-03-24 07:47:00 -04:00
|
|
|
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
|
|
|
|
2015-01-13 16:27:33 -05:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName))
|
2015-06-19 00:21:20 -04:00
|
|
|
keyResp, err := ec2conn.CreateKeyPair(&ec2.CreateKeyPairInput{
|
|
|
|
KeyName: &s.TemporaryKeyPairName})
|
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
|
2015-01-13 16:27:33 -05:00
|
|
|
s.keyName = s.TemporaryKeyPairName
|
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)
|
2015-04-05 17:58:48 -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
|
2015-04-05 17:58:48 -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
|
2015-01-13 22:58:41 -05:00
|
|
|
// If we used an SSH private key file, do not go about deleting
|
|
|
|
// keypairs
|
2015-01-13 12:20:31 -05:00
|
|
|
if s.PrivateKeyFile != "" {
|
2013-05-21 03:55:32 -04:00
|
|
|
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
|
|
|
|
2015-05-29 20:10:14 -04:00
|
|
|
// Remove the keypair
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Say("Deleting temporary keypair...")
|
2015-04-05 17:58:48 -04:00
|
|
|
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.keyName})
|
2013-05-21 03:55:32 -04:00
|
|
|
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
|
|
|
}
|
2015-05-29 20:10:14 -04:00
|
|
|
|
|
|
|
// Also remove the physical key if we're debugging.
|
|
|
|
if s.Debug {
|
|
|
|
if err := os.Remove(s.DebugKeyPath); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error removing debug key '%s': %s", s.DebugKeyPath, err))
|
|
|
|
}
|
|
|
|
}
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|