Commit ed7e0847 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon/all: TemporaryKeyPairName

/cc @whostolebenfrog - I actually simplified things quite a bit. I added
a "uuid" global template function so it just uses that now. I renamed it
so that it is clear it is a temporary keypair.
parent 615c6532
......@@ -21,8 +21,8 @@ type RunConfig struct {
SSHPort int `mapstructure:"ssh_port"`
SecurityGroupId string `mapstructure:"security_group_id"`
SubnetId string `mapstructure:"subnet_id"`
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
VpcId string `mapstructure:"vpc_id"`
SSHKeyPairPattern string `mapstructure:"ssh_keypair_pattern"`
// Unexported fields that are calculated from others
sshTimeout time.Duration
......@@ -46,8 +46,8 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
c.RawSSHTimeout = "1m"
}
if c.SSHKeyPairPattern == "" {
c.SSHKeyPairPattern = "packer %s"
if c.TemporaryKeyPairName == "" {
c.TemporaryKeyPairName = "packer {{uuid}}"
}
// Validation
......@@ -81,6 +81,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
"ssh_username": &c.SSHUsername,
"source_ami": &c.SourceAmi,
"subnet_id": &c.SubnetId,
"temporary_key_pair_name": &c.TemporaryKeyPairName,
"vpc_id": &c.VpcId,
}
......
......@@ -127,23 +127,14 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) {
}
}
func TestRunConfigPrepare_SSHKeyPairPattern(t *testing.T) {
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
c := testConfig()
c.SSHKeyPairPattern = ""
c.TemporaryKeyPairName = ""
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.SSHKeyPairPattern != "packer %s" {
t.Fatalf("invalid value: %s", c.SSHKeyPairPattern)
}
c.SSHKeyPairPattern = "valid-%s"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.SSHKeyPairPattern != "valid-%s" {
t.Fatalf("invalid value: %s", c.SSHKeyPairPattern)
if c.TemporaryKeyPairName == "" {
t.Fatal("keypair empty")
}
}
package common
import (
"cgl.tideland.biz/identifier"
"encoding/hex"
"fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"os"
"runtime"
)
......@@ -15,7 +12,7 @@ import (
type StepKeyPair struct {
Debug bool
DebugKeyPath string
KeyPairPattern string
KeyPairName string
keyName string
}
......@@ -24,20 +21,18 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating temporary keypair for this instance...")
keyName := fmt.Sprintf(s.KeyPairPattern, hex.EncodeToString(identifier.NewUUID().Raw()))
log.Printf("temporary keypair name: %s", keyName)
keyResp, err := ec2conn.CreateKeyPair(keyName)
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.KeyPairName))
keyResp, err := ec2conn.CreateKeyPair(s.KeyPairName)
if err != nil {
state.Put("error", fmt.Errorf("Error creating temporary keypair: %s", err))
return multistep.ActionHalt
}
// Set the keyname so we know to delete it later
s.keyName = keyName
s.keyName = s.KeyPairName
// Set some state data for use in future steps
state.Put("keyPair", keyName)
state.Put("keyPair", s.keyName)
state.Put("privateKey", keyResp.KeyMaterial)
// If we're in debug mode, output the private key to the working
......
......@@ -84,7 +84,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
KeyPairPattern: b.config.SSHKeyPairPattern,
KeyPairName: b.config.TemporaryKeyPairName,
},
&awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId,
......
......@@ -187,6 +187,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
KeyPairName: b.config.TemporaryKeyPairName,
},
&awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId,
......
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