logging.PanicHandler can now be for internal use
Repurpose logging.PanicHandler to recover internal panics and print a message to users similar to panicwrap.
This commit is contained in:
parent
42742c173d
commit
3f31533f86
|
@ -2,14 +2,12 @@ package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hashicorp/go-hclog"
|
"github.com/hashicorp/go-hclog"
|
||||||
"github.com/mitchellh/panicwrap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// This output is shown if a panic happens.
|
// This output is shown if a panic happens.
|
||||||
|
@ -18,59 +16,33 @@ const panicOutput = `
|
||||||
!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
Terraform crashed! This is always indicative of a bug within Terraform.
|
Terraform crashed! This is always indicative of a bug within Terraform.
|
||||||
A crash log has been placed at %[1]q relative to your current
|
Please report the crash with Terraform[1] so that we can fix this.
|
||||||
working directory. It would be immensely helpful if you could please
|
|
||||||
report the crash with Terraform[1] so that we can fix this.
|
|
||||||
|
|
||||||
When reporting bugs, please include your terraform version. That
|
When reporting bugs, please include your terraform version, the stack trace
|
||||||
information is available on the first line of crash.log. You can also
|
shown below, and any additional information which may help replicate the issue.
|
||||||
get it by running 'terraform --version' on the command line.
|
|
||||||
|
|
||||||
SECURITY WARNING: the %[1]q file that was created may contain
|
|
||||||
sensitive information that must be redacted before it is safe to share
|
|
||||||
on the issue tracker.
|
|
||||||
|
|
||||||
[1]: https://github.com/hashicorp/terraform/issues
|
[1]: https://github.com/hashicorp/terraform/issues
|
||||||
|
|
||||||
!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
`
|
`
|
||||||
|
|
||||||
// panicHandler is what is called by panicwrap when a panic is encountered
|
// PanicHandler is called to recover from an internal panic in Terraform, and
|
||||||
// within Terraform. It is guaranteed to run after the resulting process has
|
// is intended to replace the standard stack trace with a more user friendly
|
||||||
// exited so we can take the log file, add in the panic, and store it
|
// error message.
|
||||||
// somewhere locally.
|
// PanicHandler must be called as a defered function.
|
||||||
func PanicHandler(tmpLogPath string) panicwrap.HandlerFunc {
|
func PanicHandler() {
|
||||||
return func(m string) {
|
recovered := recover()
|
||||||
// Create the crash log file where we'll write the logs
|
if recovered == nil {
|
||||||
f, err := ioutil.TempFile(".", "crash.*.log")
|
return
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Failed to create crash log file: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
tmpLog, err := os.Open(tmpLogPath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Failed to open log file %q: %v\n", tmpLogPath, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer tmpLog.Close()
|
|
||||||
|
|
||||||
// Copy the contents to the crash file. This will include
|
|
||||||
// the panic that just happened.
|
|
||||||
if _, err = io.Copy(f, tmpLog); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Failed to write crash log: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// add the trace back to the log
|
|
||||||
f.WriteString("\n" + m)
|
|
||||||
|
|
||||||
// Tell the user a crash occurred in some helpful way that
|
|
||||||
// they'll hopefully notice.
|
|
||||||
fmt.Printf("\n\n")
|
|
||||||
fmt.Printf(panicOutput, f.Name())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprint(os.Stderr, panicOutput)
|
||||||
|
fmt.Fprint(os.Stderr, recovered)
|
||||||
|
|
||||||
|
// When called from a deferred function, debug.PrintStack will include the
|
||||||
|
// full stack from the point of the pending panic.
|
||||||
|
debug.PrintStack()
|
||||||
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
const pluginPanicOutput = `
|
const pluginPanicOutput = `
|
||||||
|
@ -181,13 +153,5 @@ func (l *logPanicWrapper) Debug(msg string, args ...interface{}) {
|
||||||
l.panicRecorder(msg)
|
l.panicRecorder(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have logging turned on, we need to prevent panicwrap from seeing
|
|
||||||
// this as a core panic. This can be done by obfuscating the panic error
|
|
||||||
// line.
|
|
||||||
if panicPrefix {
|
|
||||||
colon := strings.Index(msg, ":")
|
|
||||||
msg = strings.ToUpper(msg[:colon]) + msg[colon:]
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Logger.Debug(msg, args...)
|
l.Logger.Debug(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/go-hclog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPanicRecorder(t *testing.T) {
|
func TestPanicRecorder(t *testing.T) {
|
||||||
|
@ -52,31 +49,3 @@ func TestPanicLimit(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLogPanicWrapper(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
||||||
Name: "test",
|
|
||||||
Level: hclog.Debug,
|
|
||||||
Output: &buf,
|
|
||||||
DisableTime: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
wrapped := (&logPanicWrapper{
|
|
||||||
Logger: logger,
|
|
||||||
}).Named("test")
|
|
||||||
|
|
||||||
wrapped.Debug("panic: invalid foo of bar")
|
|
||||||
wrapped.Debug("\tstack trace")
|
|
||||||
|
|
||||||
expected := `[DEBUG] test.test: PANIC: invalid foo of bar
|
|
||||||
[DEBUG] test.test: stack trace
|
|
||||||
`
|
|
||||||
|
|
||||||
got := buf.String()
|
|
||||||
|
|
||||||
if expected != got {
|
|
||||||
t.Fatalf("Expected:\n%q\nGot:\n%q", expected, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue