move panicwrap handler to logging package

This commit is contained in:
James Bardin 2020-10-21 09:45:00 -04:00
parent 2332a7ab47
commit 1d9d82973b
2 changed files with 9 additions and 12 deletions

View File

@ -1,8 +1,9 @@
package main
package logging
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
@ -15,7 +16,7 @@ const panicOutput = `
!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!
Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.log" relative to your current
A crash log has been placed at %[1]q relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.
@ -23,7 +24,7 @@ When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.
SECURITY WARNING: the "crash.log" file that was created may contain
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.
@ -36,14 +37,10 @@ on the issue tracker.
// within Terraform. It is guaranteed to run after the resulting process has
// exited so we can take the log file, add in the panic, and store it
// somewhere locally.
func panicHandler(logF *os.File) panicwrap.HandlerFunc {
func PanicHandler(logF *os.File) panicwrap.HandlerFunc {
return func(m string) {
// Right away just output this thing on stderr so that it gets
// shown in case anything below fails.
fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", m))
// Create the crash log file where we'll write the logs
f, err := os.Create("crash.log")
f, err := ioutil.TempFile(".", "crash.*.log")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create crash log file: %s", err)
return
@ -66,6 +63,6 @@ func panicHandler(logF *os.File) panicwrap.HandlerFunc {
// Tell the user a crash occurred in some helpful way that
// they'll hopefully notice.
fmt.Printf("\n\n")
fmt.Println(strings.TrimSpace(panicOutput))
fmt.Printf(strings.TrimSpace(panicOutput), f.Name())
}
}

View File

@ -19,6 +19,7 @@ import (
"github.com/hashicorp/terraform/command/cliconfig"
"github.com/hashicorp/terraform/command/format"
"github.com/hashicorp/terraform/httpclient"
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/version"
"github.com/mattn/go-colorable"
"github.com/mattn/go-shellwords"
@ -65,8 +66,7 @@ func realMain() int {
go copyOutput(outR, doneCh)
// Create the configuration for panicwrap and wrap our executable
wrapConfig.Handler = panicHandler(logTempFile)
wrapConfig.Writer = os.Stderr
wrapConfig.Handler = logging.PanicHandler(logTempFile)
wrapConfig.Stdout = outW
wrapConfig.IgnoreSignals = ignoreSignals
wrapConfig.ForwardSignals = forwardSignals