From 57f185451a7a844c3a3bc59931e51dbf2c861c3f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 30 Aug 2013 14:48:50 -0700 Subject: [PATCH] builder/amazon: drop private key if debug mode [GH-373] --- CHANGELOG.md | 5 +++++ builder/amazon/common/step_key_pair.go | 21 +++++++++++++++++++++ builder/amazon/ebs/builder.go | 6 +++++- builder/amazon/instance/builder.go | 5 ++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e5b07ee..59413ef51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 0.3.6 (unreleased) +IMPROVEMENTS: + +* builder/amazon: In `-debug` mode, the keypair used will be saved to + the current directory so you can access the machine. [GH-373] + BUG FIXES: * core: Fix possible panic when ctrl-C during provisioner run. diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 3e49e3d32..2682f0e44 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -8,9 +8,13 @@ import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" "log" + "os" ) type StepKeyPair struct { + Debug bool + DebugKeyPath string + keyName string } @@ -34,6 +38,23 @@ func (s *StepKeyPair) Run(state map[string]interface{}) multistep.StepAction { state["keyPair"] = keyName state["privateKey"] = keyResp.KeyMaterial + // If we're in debug mode, output the private key to the working + // directory. + if s.Debug { + ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) + f, err := os.Create(s.DebugKeyPath) + if err != nil { + state["error"] = fmt.Errorf("Error saving debug key: %s", err) + return multistep.ActionHalt + } + defer f.Close() + + if _, err := f.Write([]byte(keyResp.KeyMaterial)); err != nil { + state["error"] = fmt.Errorf("Error saving debug key: %s", err) + return multistep.ActionHalt + } + } + return multistep.ActionContinue } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 409b14d72..996d51492 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -6,6 +6,7 @@ package ebs import ( + "fmt" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" awscommon "github.com/mitchellh/packer/builder/amazon/common" @@ -80,7 +81,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ - &awscommon.StepKeyPair{}, + &awscommon.StepKeyPair{ + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + }, &awscommon.StepSecurityGroup{ SecurityGroupId: b.config.SecurityGroupId, SSHPort: b.config.SSHPort, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index e288e0bea..2b1e236c7 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -184,7 +184,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ - &awscommon.StepKeyPair{}, + &awscommon.StepKeyPair{ + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + }, &awscommon.StepSecurityGroup{ SecurityGroupId: b.config.SecurityGroupId, SSHPort: b.config.SSHPort,