From 6371b70609b0b85fc9580a0e35853c81fef5d807 Mon Sep 17 00:00:00 2001 From: Florian Noeding Date: Mon, 24 Mar 2014 12:47:00 +0100 Subject: [PATCH] optionally use existing ssh key for amazon builders --- builder/amazon/common/run_config.go | 2 ++ builder/amazon/common/step_key_pair.go | 23 ++++++++++++++++--- builder/amazon/ebs/builder.go | 7 +++--- builder/amazon/instance/builder.go | 7 +++--- .../docs/builders/amazon-ebs.html.markdown | 3 +++ .../builders/amazon-instance.html.markdown | 3 +++ 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 893586044..c50c22f7e 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -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, diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 93ef6625d..3a7eb9f35 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -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) diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index f17dad4d4..2e898671e 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -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, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 9b0c09a75..81be72f72 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -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, diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index 190e5fa35..73686901c 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -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. diff --git a/website/source/docs/builders/amazon-instance.html.markdown b/website/source/docs/builders/amazon-instance.html.markdown index a712b9618..f3086c4bd 100644 --- a/website/source/docs/builders/amazon-instance.html.markdown +++ b/website/source/docs/builders/amazon-instance.html.markdown @@ -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.