First attempt for re-using a named EC2 keypair

Adds a 'ssh_keypair_name' option to the configuration
for AWS, along with some munging to create the
temporarily keypair if one isn't specific.

NOT YET WORKING.

From a 'make' I get the following errors:

builder/amazon/ebs/builder.go:94: b.config.SSHKeyPairName undefined
(type config has no field or method SSHKeyPairName)

builder/amazon/instance/builder.go:199: b.config.SSHKeyPairName
undefined (type Config has no field or method SSHKeyPairName)
This commit is contained in:
Lesko, Matthew (NIH/NLM/NCBI) [C] 2015-01-13 12:20:31 -05:00
parent 8fea991516
commit 8c87b1cc00
5 changed files with 21 additions and 16 deletions

View File

@ -24,6 +24,7 @@ type RunConfig struct {
RawSSHTimeout string `mapstructure:"ssh_timeout"` RawSSHTimeout string `mapstructure:"ssh_timeout"`
SSHUsername string `mapstructure:"ssh_username"` SSHUsername string `mapstructure:"ssh_username"`
SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"` SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
SSHKeyPairName string `mapstructure:"ssh_keypair_name"`
SSHPrivateIp bool `mapstructure:"ssh_private_ip"` SSHPrivateIp bool `mapstructure:"ssh_private_ip"`
SSHPort int `mapstructure:"ssh_port"` SSHPort int `mapstructure:"ssh_port"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
@ -55,6 +56,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
"ssh_timeout": &c.RawSSHTimeout, "ssh_timeout": &c.RawSSHTimeout,
"ssh_username": &c.SSHUsername, "ssh_username": &c.SSHUsername,
"ssh_private_key_file": &c.SSHPrivateKeyFile, "ssh_private_key_file": &c.SSHPrivateKeyFile,
"ssh_keypair_name": &c.SSHKeyPairName,
"source_ami": &c.SourceAmi, "source_ami": &c.SourceAmi,
"subnet_id": &c.SubnetId, "subnet_id": &c.SubnetId,
"temporary_key_pair_name": &c.TemporaryKeyPairName, "temporary_key_pair_name": &c.TemporaryKeyPairName,
@ -84,8 +86,9 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
c.RawSSHTimeout = "5m" c.RawSSHTimeout = "5m"
} }
if c.TemporaryKeyPairName == "" { // if we are not given an explicit keypairname, create a temporary one
c.TemporaryKeyPairName = fmt.Sprintf( if c.SSHKeyPairName == "" {
c.SSHKeyPairName = fmt.Sprintf(
"packer %s", uuid.TimeOrderedUUID()) "packer %s", uuid.TimeOrderedUUID())
} }

View File

@ -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.TemporaryKeyPairName = "" c.SSHKeyPairName = ""
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.TemporaryKeyPairName == "" { if c.SSHKeyPairName == "" {
t.Fatal("keypair empty") t.Fatal("keypair empty")
} }
} }

View File

@ -21,7 +21,7 @@ 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 != "" {
s.keyName = "" 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 {
@ -29,7 +29,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
state.Put("keyPair", "") state.Put("keyPair", s.keyName)
state.Put("privateKey", string(privateKeyBytes)) state.Put("privateKey", string(privateKeyBytes))
return multistep.ActionContinue return multistep.ActionContinue
@ -83,17 +83,19 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
func (s *StepKeyPair) Cleanup(state multistep.StateBag) { func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// If no key name is set, then we never created it, so just return // If no key name is set, then we never created it, so just return
if s.keyName == "" { // If we used an SSH private key file, do not go about deleting
// keypairs
if s.PrivateKeyFile != "" {
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("Deleting temporary keypair...") ui.Say("DANGER: Deleting temporary keypair (not really)...")
_, 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))
} //}
} }

View File

@ -91,7 +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),
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.SSHKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile, PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{

View File

@ -196,7 +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),
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.SSHKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile, PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{