builder/amazon: drop private key if debug mode [GH-373]

This commit is contained in:
Mitchell Hashimoto 2013-08-30 14:48:50 -07:00
parent 404ae53a4a
commit 57f185451a
4 changed files with 35 additions and 2 deletions

View File

@ -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.

View File

@ -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
}

View File

@ -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,

View File

@ -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,