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"
|
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-08-28 09:30:44 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
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 {
|
2018-08-28 11:47:02 -04:00
|
|
|
Debug bool
|
|
|
|
Comm *communicator.Config
|
|
|
|
DebugKeyPath 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
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-10-02 16:20:36 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-08-28 09:30:44 -04:00
|
|
|
if s.Comm.SSHPrivateKeyFile != "" {
|
2016-10-23 23:26:14 -04:00
|
|
|
ui.Say("Using existing SSH private key")
|
2018-11-06 14:03:33 -05:00
|
|
|
privateKeyBytes, err := s.Comm.ReadSSHPrivateKeyFile()
|
2014-03-24 07:47:00 -04:00
|
|
|
if err != nil {
|
2018-11-06 14:03:33 -05:00
|
|
|
state.Put("error", err)
|
2014-03-24 07:47:00 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-08-28 09:30:44 -04:00
|
|
|
s.Comm.SSHPrivateKey = privateKeyBytes
|
2014-03-24 07:47:00 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:10:39 -04:00
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName == "" {
|
2016-10-23 23:26:14 -04:00
|
|
|
ui.Say("Using SSH Agent with key pair in Source AMI")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:10:39 -04:00
|
|
|
if s.Comm.SSHAgentAuth && s.Comm.SSHKeyPairName != "" {
|
|
|
|
ui.Say(fmt.Sprintf("Using SSH Agent for existing key pair %s", s.Comm.SSHKeyPairName))
|
2016-10-23 23:26:14 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:47:02 -04:00
|
|
|
if s.Comm.SSHTemporaryKeyPairName == "" {
|
2016-10-02 16:20:36 -04:00
|
|
|
ui.Say("Not using temporary keypair")
|
2018-08-29 05:23:59 -04:00
|
|
|
s.Comm.SSHKeyPairName = ""
|
2016-10-02 16:20:36 -04:00
|
|
|
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
|
|
|
|
2018-08-28 11:47:02 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.Comm.SSHTemporaryKeyPairName))
|
2015-06-19 00:21:20 -04:00
|
|
|
keyResp, err := ec2conn.CreateKeyPair(&ec2.CreateKeyPairInput{
|
2018-08-28 11:47:02 -04:00
|
|
|
KeyName: &s.Comm.SSHTemporaryKeyPairName})
|
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
|
|
|
|
2018-08-28 09:30:44 -04:00
|
|
|
// Set some data for use in future steps
|
2018-08-28 11:47:02 -04:00
|
|
|
s.Comm.SSHKeyPairName = s.Comm.SSHTemporaryKeyPairName
|
2018-08-28 09:30:44 -04:00
|
|
|
s.Comm.SSHPrivateKey = []byte(*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...")
|
2018-08-28 11:47:02 -04:00
|
|
|
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &s.Comm.SSHTemporaryKeyPairName})
|
2013-05-21 03:55:32 -04:00
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2018-08-28 11:47:02 -04:00
|
|
|
"Error cleaning up keypair. Please delete the key manually: %s", s.Comm.SSHTemporaryKeyPairName))
|
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
|
|
|
}
|