Simplify usage of os/exec

For now, keep the finishRunning() wrapper but use a straight cmd.Run()
call instead of the convoluted goroutine trying to catch signals.

It turns out that Unix process group handling is enough to interrupt
pending processes when stopping the run with something like a Ctrl+C
which should be enough.

Tested:
- Full e2e run with hack/e2e-test.sh, two tests failed but looks like
  they've been failing before this change.
- Started a hack/e2e.go -v -build and interrupted it with Ctrl+C,
  confirmed that build-release.sh was killed in the process.
This commit is contained in:
Filipe Brandenburger 2015-02-13 13:53:27 -08:00
parent c8b6c62698
commit 70d37800b7

View File

@ -27,7 +27,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -63,7 +62,6 @@ const (
) )
var ( var (
signals = make(chan os.Signal, 100)
// Root directory of the specified cluster version, rather than of where // Root directory of the specified cluster version, rather than of where
// this script is being run from. // this script is being run from.
versionRoot = *root versionRoot = *root
@ -86,7 +84,6 @@ type ResultsByTest map[string]TestResult
func main() { func main() {
flag.Parse() flag.Parse()
signal.Notify(signals, os.Interrupt)
if *isup { if *isup {
status := 1 status := 1
@ -295,40 +292,17 @@ func runBashUntil(stepName string, cmd *exec.Cmd) func() {
} }
} }
func finishRunningWithOutputs(stepName string, cmd *exec.Cmd) (bool, string, string) { func finishRunning(stepName string, cmd *exec.Cmd) bool {
log.Printf("Running: %v", stepName)
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
if *verbose { if *verbose {
cmd.Stdout = io.MultiWriter(os.Stdout, stdout) cmd.Stdout = os.Stdout
cmd.Stderr = io.MultiWriter(os.Stderr, stderr) cmd.Stderr = os.Stderr
} else {
cmd.Stdout = stdout
cmd.Stderr = stderr
} }
log.Printf("Running: %v", stepName)
done := make(chan struct{})
defer close(done)
go func() {
for {
select {
case <-done:
return
case s := <-signals:
cmd.Process.Signal(s)
}
}
}()
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
log.Printf("Error running %v: %v", stepName, err) log.Printf("Error running %v: %v", stepName, err)
return false, string(stdout.Bytes()), string(stderr.Bytes()) return false
} }
return true, string(stdout.Bytes()), string(stderr.Bytes()) return true
}
func finishRunning(stepName string, cmd *exec.Cmd) bool {
result, _, _ := finishRunningWithOutputs(stepName, cmd)
return result
} }
func printBashOutputs(headerprefix, lineprefix, output string, escape bool) { func printBashOutputs(headerprefix, lineprefix, output string, escape bool) {