Commit ef189808 authored by Ross Smith II's avatar Ross Smith II

Merge pull request #971 from fnoeding/amazon-sshkey

optionally use existing ssh key for amazon builders
parents e94a7e7f 6371b706
...@@ -19,6 +19,7 @@ type RunConfig struct { ...@@ -19,6 +19,7 @@ type RunConfig struct {
SourceAmi string `mapstructure:"source_ami"` SourceAmi string `mapstructure:"source_ami"`
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"`
SSHPort int `mapstructure:"ssh_port"` SSHPort int `mapstructure:"ssh_port"`
SecurityGroupId string `mapstructure:"security_group_id"` SecurityGroupId string `mapstructure:"security_group_id"`
SecurityGroupIds []string `mapstructure:"security_group_ids"` SecurityGroupIds []string `mapstructure:"security_group_ids"`
...@@ -91,6 +92,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { ...@@ -91,6 +92,7 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
"instance_type": &c.InstanceType, "instance_type": &c.InstanceType,
"ssh_timeout": &c.RawSSHTimeout, "ssh_timeout": &c.RawSSHTimeout,
"ssh_username": &c.SSHUsername, "ssh_username": &c.SSHUsername,
"ssh_private_key_file": &c.SSHPrivateKeyFile,
"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,
......
...@@ -5,19 +5,36 @@ import ( ...@@ -5,19 +5,36 @@ import (
"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"
"io/ioutil"
"os" "os"
"runtime" "runtime"
) )
type StepKeyPair struct { type StepKeyPair struct {
Debug bool Debug bool
DebugKeyPath string DebugKeyPath string
KeyPairName string KeyPairName string
PrivateKeyFile string
keyName string keyName string
} }
func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { 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) ec2conn := state.Get("ec2").(*ec2.EC2)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
......
...@@ -83,9 +83,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -83,9 +83,10 @@ 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),
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.TemporaryKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
......
...@@ -187,9 +187,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -187,9 +187,10 @@ 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),
KeyPairName: b.config.TemporaryKeyPairName, KeyPairName: b.config.TemporaryKeyPairName,
PrivateKeyFile: b.config.SSHPrivateKeyFile,
}, },
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupIds: b.config.SecurityGroupIds, SecurityGroupIds: b.config.SecurityGroupIds,
......
...@@ -108,6 +108,9 @@ Optional: ...@@ -108,6 +108,9 @@ Optional:
* `ssh_port` (int) - The port that SSH will be available on. This defaults * `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22. 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 * `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" 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. or "5m". The default SSH timeout is "5m", or five minutes.
......
...@@ -147,6 +147,9 @@ Optional: ...@@ -147,6 +147,9 @@ Optional:
* `ssh_port` (int) - The port that SSH will be available on. This defaults * `ssh_port` (int) - The port that SSH will be available on. This defaults
to port 22. 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 * `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" 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. or "5m". The default SSH timeout is "5m", or five minutes.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment