Permit Temp keys and named SSH keypairs

These changes permit the use of pre-created SSH keypairs with AWS. If
so, the configuration for the builder needs to include an
ssh_keypair_name option and a ssh_private_key_file.

If ssh_private_key_file is *not* defined, it'll go through the
rigamarole of creating a temporary keypair. The ssh_keypair_name option
by itself won't make that change, because it doesn't make sense to
specify a keypair but not tell packer where the private key is, but it
does happen that you could have a private key and the public-key is
"baked in", and not part of your EC2 account.
parent 8c87b1cc
...@@ -88,7 +88,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { ...@@ -88,7 +88,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
// if we are not given an explicit keypairname, create a temporary one // if we are not given an explicit keypairname, create a temporary one
if c.SSHKeyPairName == "" { if c.SSHKeyPairName == "" {
c.SSHKeyPairName = fmt.Sprintf( c.TemporaryKeyPairName = fmt.Sprintf(
"packer %s", uuid.TimeOrderedUUID()) "packer %s", uuid.TimeOrderedUUID())
} }
......
...@@ -142,12 +142,12 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { ...@@ -142,12 +142,12 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) {
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
c := testConfig() c := testConfig()
c.SSHKeyPairName = "" c.TemporaryKeyPairName = ""
if err := c.Prepare(nil); len(err) != 0 { if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if c.SSHKeyPairName == "" { if c.TemporaryKeyPairName == "" {
t.Fatal("keypair empty") t.Fatal("keypair empty")
} }
} }
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
type StepKeyPair struct { type StepKeyPair struct {
Debug bool Debug bool
DebugKeyPath string DebugKeyPath string
TemporaryKeyPairName string
KeyPairName string KeyPairName string
PrivateKeyFile string PrivateKeyFile string
...@@ -21,7 +22,9 @@ type StepKeyPair struct { ...@@ -21,7 +22,9 @@ type StepKeyPair struct {
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
if s.PrivateKeyFile != "" { if s.PrivateKeyFile != "" {
if s.KeyPairName != "" {
s.keyName = s.KeyPairName // need to get from config s.keyName = s.KeyPairName // need to get from config
}
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile) privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
if err != nil { if err != nil {
...@@ -38,15 +41,15 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { ...@@ -38,15 +41,15 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.KeyPairName)) ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.TemporaryKeyPairName))
keyResp, err := ec2conn.CreateKeyPair(s.KeyPairName) keyResp, err := ec2conn.CreateKeyPair(s.TemporaryKeyPairName)
if err != nil { if err != nil {
state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err)) state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
} }
// Set the keyname so we know to delete it later // Set the keyname so we know to delete it later
s.keyName = s.KeyPairName s.keyName = s.TemporaryKeyPairName
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("keyPair", s.keyName) state.Put("keyPair", s.keyName)
...@@ -89,13 +92,13 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) { ...@@ -89,13 +92,13 @@ func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
return return
} }
//ec2conn := state.Get("ec2").(*ec2.EC2) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("DANGER: Deleting temporary keypair (not really)...") ui.Say("DANGER: Deleting temporary keypair...")
//_, err := ec2conn.DeleteKeyPair(s.keyName) _, err := ec2conn.DeleteKeyPair(s.keyName)
//if err != nil { if err != nil {
//ui.Error(fmt.Sprintf( ui.Error(fmt.Sprintf(
//"Error cleaning up keypair. Please delete the key manually: %s", s.keyName)) "Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
//} }
} }
...@@ -91,6 +91,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -91,6 +91,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{ &awscommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
TemporaryKeyPairName: b.config.TemporaryKeyPairName,
KeyPairName: b.config.SSHKeyPairName, KeyPairName: b.config.SSHKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile, PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
......
...@@ -196,6 +196,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -196,6 +196,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{ &awscommon.StepKeyPair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
TemporaryKeyPairName: b.config.TemporaryKeyPairName,
KeyPairName: b.config.SSHKeyPairName, KeyPairName: b.config.SSHKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile, PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment