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.
This commit is contained in:
parent
615c65320a
commit
ed7e0847fd
|
@ -11,18 +11,18 @@ import (
|
||||||
// RunConfig contains configuration for running an instance from a source
|
// RunConfig contains configuration for running an instance from a source
|
||||||
// AMI and details on how to access that launched image.
|
// AMI and details on how to access that launched image.
|
||||||
type RunConfig struct {
|
type RunConfig struct {
|
||||||
SourceAmi string `mapstructure:"source_ami"`
|
SourceAmi string `mapstructure:"source_ami"`
|
||||||
IamInstanceProfile string `mapstructure:"iam_instance_profile"`
|
IamInstanceProfile string `mapstructure:"iam_instance_profile"`
|
||||||
InstanceType string `mapstructure:"instance_type"`
|
InstanceType string `mapstructure:"instance_type"`
|
||||||
UserData string `mapstructure:"user_data"`
|
UserData string `mapstructure:"user_data"`
|
||||||
UserDataFile string `mapstructure:"user_data_file"`
|
UserDataFile string `mapstructure:"user_data_file"`
|
||||||
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
RawSSHTimeout string `mapstructure:"ssh_timeout"`
|
||||||
SSHUsername string `mapstructure:"ssh_username"`
|
SSHUsername string `mapstructure:"ssh_username"`
|
||||||
SSHPort int `mapstructure:"ssh_port"`
|
SSHPort int `mapstructure:"ssh_port"`
|
||||||
SecurityGroupId string `mapstructure:"security_group_id"`
|
SecurityGroupId string `mapstructure:"security_group_id"`
|
||||||
SubnetId string `mapstructure:"subnet_id"`
|
SubnetId string `mapstructure:"subnet_id"`
|
||||||
VpcId string `mapstructure:"vpc_id"`
|
TemporaryKeyPairName string `mapstructure:"temporary_key_pair_name"`
|
||||||
SSHKeyPairPattern string `mapstructure:"ssh_keypair_pattern"`
|
VpcId string `mapstructure:"vpc_id"`
|
||||||
|
|
||||||
// Unexported fields that are calculated from others
|
// Unexported fields that are calculated from others
|
||||||
sshTimeout time.Duration
|
sshTimeout time.Duration
|
||||||
|
@ -46,8 +46,8 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
|
||||||
c.RawSSHTimeout = "1m"
|
c.RawSSHTimeout = "1m"
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.SSHKeyPairPattern == "" {
|
if c.TemporaryKeyPairName == "" {
|
||||||
c.SSHKeyPairPattern = "packer %s"
|
c.TemporaryKeyPairName = "packer {{uuid}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
|
@ -74,14 +74,15 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
|
||||||
}
|
}
|
||||||
|
|
||||||
templates := map[string]*string{
|
templates := map[string]*string{
|
||||||
"iam_instance_profile": &c.IamInstanceProfile,
|
"iam_instance_profile": &c.IamInstanceProfile,
|
||||||
"instance_type": &c.InstanceType,
|
"instance_type": &c.InstanceType,
|
||||||
"ssh_timeout": &c.RawSSHTimeout,
|
"ssh_timeout": &c.RawSSHTimeout,
|
||||||
"security_group_id": &c.SecurityGroupId,
|
"security_group_id": &c.SecurityGroupId,
|
||||||
"ssh_username": &c.SSHUsername,
|
"ssh_username": &c.SSHUsername,
|
||||||
"source_ami": &c.SourceAmi,
|
"source_ami": &c.SourceAmi,
|
||||||
"subnet_id": &c.SubnetId,
|
"subnet_id": &c.SubnetId,
|
||||||
"vpc_id": &c.VpcId,
|
"temporary_key_pair_name": &c.TemporaryKeyPairName,
|
||||||
|
"vpc_id": &c.VpcId,
|
||||||
}
|
}
|
||||||
|
|
||||||
for n, ptr := range templates {
|
for n, ptr := range templates {
|
||||||
|
|
|
@ -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 := testConfig()
|
||||||
c.SSHKeyPairPattern = ""
|
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.SSHKeyPairPattern != "packer %s" {
|
if c.TemporaryKeyPairName == "" {
|
||||||
t.Fatalf("invalid value: %s", c.SSHKeyPairPattern)
|
t.Fatal("keypair empty")
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,18 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cgl.tideland.biz/identifier"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepKeyPair struct {
|
type StepKeyPair struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
DebugKeyPath string
|
DebugKeyPath string
|
||||||
KeyPairPattern string
|
KeyPairName string
|
||||||
|
|
||||||
keyName string
|
keyName string
|
||||||
}
|
}
|
||||||
|
@ -24,20 +21,18 @@ 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("Creating temporary keypair for this instance...")
|
ui.Say(fmt.Sprintf("Creating temporary keypair: %s", s.KeyPairName))
|
||||||
keyName := fmt.Sprintf(s.KeyPairPattern, hex.EncodeToString(identifier.NewUUID().Raw()))
|
keyResp, err := ec2conn.CreateKeyPair(s.KeyPairName)
|
||||||
log.Printf("temporary keypair name: %s", keyName)
|
|
||||||
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
|
||||||
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 = keyName
|
s.keyName = s.KeyPairName
|
||||||
|
|
||||||
// Set some state data for use in future steps
|
// Set some state data for use in future steps
|
||||||
state.Put("keyPair", keyName)
|
state.Put("keyPair", s.keyName)
|
||||||
state.Put("privateKey", keyResp.KeyMaterial)
|
state.Put("privateKey", keyResp.KeyMaterial)
|
||||||
|
|
||||||
// If we're in debug mode, output the private key to the working
|
// If we're in debug mode, output the private key to the working
|
||||||
|
|
|
@ -82,9 +82,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&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),
|
||||||
KeyPairPattern: b.config.SSHKeyPairPattern,
|
KeyPairName: b.config.TemporaryKeyPairName,
|
||||||
},
|
},
|
||||||
&awscommon.StepSecurityGroup{
|
&awscommon.StepSecurityGroup{
|
||||||
SecurityGroupId: b.config.SecurityGroupId,
|
SecurityGroupId: b.config.SecurityGroupId,
|
||||||
|
|
|
@ -187,6 +187,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),
|
||||||
|
KeyPairName: b.config.TemporaryKeyPairName,
|
||||||
},
|
},
|
||||||
&awscommon.StepSecurityGroup{
|
&awscommon.StepSecurityGroup{
|
||||||
SecurityGroupId: b.config.SecurityGroupId,
|
SecurityGroupId: b.config.SecurityGroupId,
|
||||||
|
|
Loading…
Reference in New Issue