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

View File

@ -142,12 +142,12 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) {
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
c := testConfig()
c.TemporaryKeyPairName = ""
c.SSHKeyPairName = ""
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.TemporaryKeyPairName == "" {
if c.SSHKeyPairName == "" {
t.Fatal("keypair empty")
}
}

View File

@ -21,7 +21,7 @@ type StepKeyPair struct {
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
if s.PrivateKeyFile != "" {
s.keyName = ""
s.keyName = s.KeyPairName // need to get from config
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
if err != nil {
@ -29,7 +29,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}
state.Put("keyPair", "")
state.Put("keyPair", s.keyName)
state.Put("privateKey", string(privateKeyBytes))
return multistep.ActionContinue
@ -83,17 +83,19 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
func (s *StepKeyPair) Cleanup(state multistep.StateBag) {
// 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
}
ec2conn := state.Get("ec2").(*ec2.EC2)
//ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting temporary keypair...")
_, err := ec2conn.DeleteKeyPair(s.keyName)
if err != nil {
ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.keyName))
}
ui.Say("DANGER: Deleting temporary keypair (not really)...")
//_, err := ec2conn.DeleteKeyPair(s.keyName)
//if err != nil {
//ui.Error(fmt.Sprintf(
//"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{
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
KeyPairName: b.config.TemporaryKeyPairName,
KeyPairName: b.config.SSHKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile,
},
&awscommon.StepSecurityGroup{

View File

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