diff --git a/builder/googlecompute/builder.go b/builder/googlecompute/builder.go index 6ac294d6a..ee2431def 100644 --- a/builder/googlecompute/builder.go +++ b/builder/googlecompute/builder.go @@ -5,7 +5,7 @@ package googlecompute import ( "log" "time" - + "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/packer" @@ -48,21 +48,28 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state.Put("ui", ui) // Build the steps. - steps := []multistep.Step{ - new(StepCreateSSHKey), - new(StepCreateInstance), - new(StepInstanceInfo), - &common.StepConnectSSH{ - SSHAddress: sshAddress, - SSHConfig: sshConfig, - SSHWaitTimeout: 5 * time.Minute, - }, - new(common.StepProvision), - new(StepUpdateGsutil), - new(StepCreateImage), - new(StepUploadImage), - new(StepRegisterImage), - } + steps := []multistep.Step{ + &StepCreateSSHKey{ + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("gce_%s.pem", b.config.PackerBuildName), + }, + &StepCreateInstance{ + Debug: b.config.PackerDebug, + }, + &StepInstanceInfo{ + Debug: b.config.PackerDebug, + }, + &common.StepConnectSSH{ + SSHAddress: sshAddress, + SSHConfig: sshConfig, + SSHWaitTimeout: 5 * time.Minute, + }, + new(common.StepProvision), + new(StepUpdateGsutil), + new(StepCreateImage), + new(StepUploadImage), + new(StepRegisterImage), + } // Run the steps. if b.config.PackerDebug { diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index dcc8e88f7..cb7682a69 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -13,6 +13,7 @@ import ( // StepCreateInstance represents a Packer build step that creates GCE instances. type StepCreateInstance struct { instanceName string + Debug bool } // Run executes the Packer build step that creates a GCE instance. @@ -56,6 +57,12 @@ func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction ui.Message("Instance has been created!") + if s.Debug { + if name != "" { + ui.Message(fmt.Sprintf("Instance: %s started in %s", name, config.Zone)) + } + } + // Things succeeded, store the name so we can remove it later state.Put("instance_name", name) s.instanceName = name diff --git a/builder/googlecompute/step_create_ssh_key.go b/builder/googlecompute/step_create_ssh_key.go index 814d5bf04..74e05b74c 100644 --- a/builder/googlecompute/step_create_ssh_key.go +++ b/builder/googlecompute/step_create_ssh_key.go @@ -5,15 +5,19 @@ import ( "crypto/rsa" "crypto/x509" "encoding/pem" + "os" "fmt" - "code.google.com/p/go.crypto/ssh" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" ) // StepCreateSSHKey represents a Packer build step that generates SSH key pairs. -type StepCreateSSHKey int +type StepCreateSSHKey struct { + key int + Debug bool + DebugKeyPath string +} // Run executes the Packer build step that generates SSH key pairs. func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { @@ -41,9 +45,20 @@ func (s *StepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction { ui.Error(err.Error()) return multistep.ActionHalt } + state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) + state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) - state.Put("ssh_private_key", string(pem.EncodeToMemory(&priv_blk))) - state.Put("ssh_public_key", string(ssh.MarshalAuthorizedKey(pub))) + if s.Debug { + ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) + f, err := os.OpenFile(s.DebugKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + state.Put("error", fmt.Errorf("Error saving debug key: %s", err)) + return multistep.ActionHalt + } + // Write out the key + pem.Encode(f, &priv_blk) + f.Close() + } return multistep.ActionContinue } diff --git a/builder/googlecompute/step_instance_info.go b/builder/googlecompute/step_instance_info.go index 0f87077fd..470f40bc5 100644 --- a/builder/googlecompute/step_instance_info.go +++ b/builder/googlecompute/step_instance_info.go @@ -10,7 +10,10 @@ import ( ) // stepInstanceInfo represents a Packer build step that gathers GCE instance info. -type StepInstanceInfo int +type StepInstanceInfo struct { + info int + Debug bool +} // Run executes the Packer build step that gathers GCE instance info. func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction { @@ -44,6 +47,12 @@ func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } + if s.Debug { + if ip != "" { + ui.Message(fmt.Sprintf("Public IP: %s", ip)) + } + } + ui.Message(fmt.Sprintf("IP: %s", ip)) state.Put("instance_ip", ip) return multistep.ActionContinue