2013-07-20 22:40:45 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-05-21 03:55:32 -04:00
|
|
|
"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"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/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
|
2016-10-23 22:43:47 -04:00
|
|
|
SSHAgentAuth bool
|
2015-01-13 22:58:41 -05:00
|
|
|
DebugKeyPath string
|
|
|
|
TemporaryKeyPairName string
|
|
|
|
KeyPairName string
|
|
|
|
PrivateKeyFile string
|
2013-08-30 17:48:50 -04:00
|
|
|
|
2017-04-30 17:24:22 -04:00
|
|
|
doCleanup bool
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-10-02 16:20:36 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2014-03-24 07:47:00 -04:00
|
|
|
if s.PrivateKeyFile != "" {
|
2016-10-23 23:26:14 -04:00
|
|
|
ui.Say("Using existing SSH private key")
|
2014-03-24 07:47:00 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:26:14 -04:00
|
|
|
if s.SSHAgentAuth && s.KeyPairName == "" {
|
|
|
|
ui.Say("Using SSH Agent with key pair in Source AMI")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.SSHAgentAuth && s.KeyPairName != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.KeyPairName))
|
|
|
|
state.Put("keyPair", s.KeyPairName)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:20:36 -04:00
|
|
|
if s.TemporaryKeyPairName == "" {
|
|
|
|
ui.Say("Not using temporary keypair")
|
|
|
|
state.Put("keyPair", "")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
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
|
|
|
}
|
|
|
|
|
2017-04-30 17:24:22 -04:00
|
|
|
s.doCleanup = true
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
2017-05-01 13:17:21 -04:00
|
|
|
state.Put("keyPair", s.TemporaryKeyPairName)
|
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) {
|
2017-04-30 17:24:22 -04:00
|
|
|
if !s.doCleanup {
|
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...")
|
2017-05-01 13:17:21 -04:00
|
|
|
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.TemporaryKeyPairName})
|
2013-05-21 03:55:32 -04:00
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2017-05-01 13:17:21 -04:00
|
|
|
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))
|
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
|
|
|
}
|