Skip to content

Commit

Permalink
Add vbox.log to crashreport
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
  • Loading branch information
jeanlaurent committed Dec 18, 2015
1 parent 32795c9 commit fd25762
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
25 changes: 23 additions & 2 deletions libmachine/crashreport/crash_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

"errors"

"io/ioutil"

"github.com/bugsnag/bugsnag-go"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine/log"
Expand All @@ -35,8 +37,7 @@ func Configure(key string) {
}
}

// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
func Send(err error, context string, driverName string, command string) error {
func SendWithFile(err error, context string, driverName string, command string, path string) error {
if noReportFileExist() || apiKey == noreportAPIKey {
log.Debug("Opting out of crash reporting.")
return nil
Expand Down Expand Up @@ -67,6 +68,7 @@ func Send(err error, context string, driverName string, command string) error {
detectRunningShell(&metaData)
detectUname(&metaData)
detectOSVersion(&metaData)
addFile(path, &metaData)

var buffer bytes.Buffer
for _, message := range log.History() {
Expand All @@ -76,6 +78,25 @@ func Send(err error, context string, driverName string, command string) error {
return bugsnag.Notify(err, metaData, bugsnag.SeverityError, bugsnag.Context{String: context}, bugsnag.ErrorClass{Name: fmt.Sprintf("%s/%s", driverName, command)})
}

// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
func Send(err error, context string, driverName string, command string) error {
return SendWithFile(err, context, driverName, command, "")
}

func addFile(path string, metaData *bugsnag.MetaData) {
file, err := os.Open(path)
if err != nil {
log.Debug(err)
return
}
data, err := ioutil.ReadAll(file)
if err != nil {
log.Debug(err)
return
}
metaData.Add("logfile", filepath.Base(path), string(data))
}

func noReportFileExist() bool {
optOutFilePath := filepath.Join(mcndirs.GetBaseDir(), "no-error-report")
if _, err := os.Stat(optOutFilePath); os.IsNotExist(err) {
Expand Down
39 changes: 39 additions & 0 deletions libmachine/crashreport/crash_report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package crashreport

import (
"testing"

"io/ioutil"

"os"
"path/filepath"

"github.com/bugsnag/bugsnag-go"
"github.com/stretchr/testify/assert"
)

func TestFileIsNotReadWhenNotExisting(t *testing.T) {
metaData := bugsnag.MetaData{}
addFile("not existing", &metaData)
assert.Empty(t, metaData)
}

func TestRead(t *testing.T) {
metaData := bugsnag.MetaData{}
content := "foo\nbar\nqix\n"
fileName := createTempFile(t, content)
defer os.Remove(fileName)
addFile(fileName, &metaData)
assert.Equal(t, "foo\nbar\nqix\n", metaData["logfile"][filepath.Base(fileName)])
}

func createTempFile(t *testing.T, content string) string {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(file.Name(), []byte(content), 0644); err != nil {
t.Fatal(err)
}
return file.Name()
}
11 changes: 10 additions & 1 deletion libmachine/libmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Creating machine...")

if err := api.performCreate(h); err != nil {
crashreport.Send(err, "api.performCreate", h.DriverName, "Create")
sendCrashReport(err, api, h)
return err
}

Expand Down Expand Up @@ -152,3 +152,12 @@ func (api *Client) performCreate(h *host.Host) error {
return nil

}

func sendCrashReport(err error, api *Client, host *host.Host) {
if host.DriverName == "virtualbox" {
vboxlogPath := filepath.Join(api.GetMachinesDir(), host.Name, host.Name, "Logs", "VBox.log")
crashreport.SendWithFile(err, "api.performCreate", host.DriverName, "Create", vboxlogPath)
} else {
crashreport.Send(err, "api.performCreate", host.DriverName, "Create")
}
}

0 comments on commit fd25762

Please sign in to comment.