Merge pull request #971 from fnoeding/amazon-sshkey

optionally use existing ssh key for amazon builders
This commit is contained in:
Ross Smith II 2014-04-21 08:10:05 -07:00
commit ef1898083a
6 changed files with 36 additions and 9 deletions

View File

@ -19,6 +19,7 @@ type RunConfig struct {
SourceAmi string `mapstructure:"source_ami"`
RawSSHTimeout string `mapstructure:"ssh_timeout"`
SSHUsername string `mapstructure:"ssh_username"`
SSHPrivateKeyFile string `mapstructure:"ssh_private_key_file"`
SSHPort int `mapstructure:"ssh_port"`
SecurityGroupId string `mapstructure:"security_group_id"`
SecurityGroupIds []string `mapstructure:"security_group_ids"`
@ -91,6 +92,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
"instance_type": &c.InstanceType,
"ssh_timeout": &c.RawSSHTimeout,
"ssh_username": &c.SSHUsername,
"ssh_private_key_file": &c.SSHPrivateKeyFile,
"source_ami": &c.SourceAmi,
"subnet_id": &c.SubnetId,
"temporary_key_pair_name": &c.TemporaryKeyPairName,

View File

@ -5,19 +5,36 @@ import (
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"runtime"
)
type StepKeyPair struct {
Debug bool
DebugKeyPath string
KeyPairName string
Debug bool
DebugKeyPath string
KeyPairName string
PrivateKeyFile string
keyName string
}
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction {
if s.PrivateKeyFile != "" {
s.keyName = ""
privateKeyBytes, err := ioutil.ReadFile(s.PrivateKeyFile)
if err != nil {
state.Put("error", fmt.Errorf("Error loading configured private key file: %s", err))
return multistep.ActionHalt
}
state.Put("keyPair", "")
state.Put("privateKey", string(privateKeyBytes))
return multistep.ActionContinue
}
ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui)

View File

@ -83,9 +83,10 @@ 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),
KeyPairName: b.config.TemporaryKeyPairName,
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
KeyPairName: b.config.TemporaryKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile,
},
&awscommon.StepSecurityGroup{
SecurityGroupIds: b.config.SecurityGroupIds,

View File

@ -187,9 +187,10 @@ 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),
KeyPairName: b.config.TemporaryKeyPairName,
Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
KeyPairName: b.config.TemporaryKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile,
},
&awscommon.StepSecurityGroup{
SecurityGroupIds: b.config.SecurityGroupIds,

View File

@ -108,6 +108,9 @@ Optional:
* `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22.
* `ssh_private_key_file` - Use this ssh private key file instead of a generated
ssh key pair for connecting to the instance.
* `ssh_timeout` (string) - The time to wait for SSH to become available
before timing out. The format of this value is a duration such as "5s"
or "5m". The default SSH timeout is "5m", or five minutes.

View File

@ -147,6 +147,9 @@ Optional:
* `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22.
* `ssh_private_key_file` - Use this ssh private key file instead of a generated
ssh key pair for connecting to the instance.
* `ssh_timeout` (string) - The time to wait for SSH to become available
before timing out. The format of this value is a duration such as "5s"
or "5m". The default SSH timeout is "5m", or five minutes.