From ed7e0847fd9018e13ce34caa977997020f89a4e7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Sep 2013 12:23:08 -0700 Subject: [PATCH] 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. --- builder/amazon/common/run_config.go | 45 ++++++++++++------------ builder/amazon/common/run_config_test.go | 17 +++------ builder/amazon/common/step_key_pair.go | 19 ++++------ builder/amazon/ebs/builder.go | 6 ++-- builder/amazon/instance/builder.go | 1 + 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 6c162d052..b21999260 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -11,18 +11,18 @@ import ( // RunConfig contains configuration for running an instance from a source // AMI and details on how to access that launched image. type RunConfig struct { - SourceAmi string `mapstructure:"source_ami"` - IamInstanceProfile string `mapstructure:"iam_instance_profile"` - InstanceType string `mapstructure:"instance_type"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` - RawSSHTimeout string `mapstructure:"ssh_timeout"` - SSHUsername string `mapstructure:"ssh_username"` - SSHPort int `mapstructure:"ssh_port"` - SecurityGroupId string `mapstructure:"security_group_id"` - SubnetId string `mapstructure:"subnet_id"` - VpcId string `mapstructure:"vpc_id"` - SSHKeyPairPattern string `mapstructure:"ssh_keypair_pattern"` + SourceAmi string `mapstructure:"source_ami"` + IamInstanceProfile string `mapstructure:"iam_instance_profile"` + InstanceType string `mapstructure:"instance_type"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + RawSSHTimeout string `mapstructure:"ssh_timeout"` + SSHUsername string `mapstructure:"ssh_username"` + 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"` // 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 @@ -74,14 +74,15 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { } templates := map[string]*string{ - "iam_instance_profile": &c.IamInstanceProfile, - "instance_type": &c.InstanceType, - "ssh_timeout": &c.RawSSHTimeout, - "security_group_id": &c.SecurityGroupId, - "ssh_username": &c.SSHUsername, - "source_ami": &c.SourceAmi, - "subnet_id": &c.SubnetId, - "vpc_id": &c.VpcId, + "iam_instance_profile": &c.IamInstanceProfile, + "instance_type": &c.InstanceType, + "ssh_timeout": &c.RawSSHTimeout, + "security_group_id": &c.SecurityGroupId, + "ssh_username": &c.SSHUsername, + "source_ami": &c.SourceAmi, + "subnet_id": &c.SubnetId, + "temporary_key_pair_name": &c.TemporaryKeyPairName, + "vpc_id": &c.VpcId, } for n, ptr := range templates { diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index a9f054fc2..1d376b1dd 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -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") } } diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 772e390c5..93ef6625d 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -1,21 +1,18 @@ 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" ) type StepKeyPair struct { - Debug bool - DebugKeyPath string - KeyPairPattern string + Debug bool + DebugKeyPath 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 diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 59de87b33..b89a9b71c 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -82,9 +82,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), - KeyPairPattern: b.config.SSHKeyPairPattern, + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + KeyPairName: b.config.TemporaryKeyPairName, }, &awscommon.StepSecurityGroup{ SecurityGroupId: b.config.SecurityGroupId, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 2fd5b0664..a507d71ef 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -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,