diff --git a/client/driver/executor/checks.go b/client/driver/executor/checks.go index 876e4108c..fe65ebe56 100644 --- a/client/driver/executor/checks.go +++ b/client/driver/executor/checks.go @@ -1,8 +1,11 @@ package executor import ( + "fmt" "log" + "os/exec" "sync" + "syscall" "time" "github.com/armon/circbuf" @@ -149,6 +152,51 @@ func (e *ExecScriptCheck) Interval() time.Duration { return e.interval } +// Run runs an exec script check +func (e *ExecScriptCheck) Run() *cstructs.CheckResult { + buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) + cmd := exec.Command(e.cmd, e.args...) + cmd.Stdout = buf + cmd.Stderr = buf + e.setChroot(cmd) + ts := time.Now() + if err := cmd.Start(); err != nil { + return &cstructs.CheckResult{Err: err} + } + errCh := make(chan error, 2) + go func() { + errCh <- cmd.Wait() + }() + for { + select { + case err := <-errCh: + endTime := time.Now() + if err == nil { + return &cstructs.CheckResult{ + ExitCode: 0, + Output: string(buf.Bytes()), + Timestamp: ts, + } + } + exitCode := 1 + if exitErr, ok := err.(*exec.ExitError); ok { + if status, ok := exitErr.Sys().(syscall.WaitStatus); ok { + exitCode = status.ExitStatus() + } + } + return &cstructs.CheckResult{ + ExitCode: exitCode, + Output: string(buf.Bytes()), + Timestamp: ts, + Duration: endTime.Sub(ts), + } + case <-time.After(e.Timeout()): + errCh <- fmt.Errorf("timed out after waiting 30s") + } + } + return nil +} + // Timeout returns the duration after which a check is timed out. func (e *ExecScriptCheck) Timeout() time.Duration { if e.timeout == 0 { diff --git a/client/driver/executor/checks_unix.go b/client/driver/executor/checks_unix.go index 0b1223d13..b18812dd8 100644 --- a/client/driver/executor/checks_unix.go +++ b/client/driver/executor/checks_unix.go @@ -3,15 +3,8 @@ package executor import ( - "fmt" "os/exec" "syscall" - "time" - - "golang.org/x/sys/unix" - - "github.com/armon/circbuf" - cstructs "github.com/hashicorp/nomad/client/driver/structs" ) func (e *ExecScriptCheck) setChroot(cmd *exec.Cmd) { @@ -23,48 +16,3 @@ func (e *ExecScriptCheck) setChroot(cmd *exec.Cmd) { } cmd.Dir = "/" } - -// Run runs an exec script check -func (e *ExecScriptCheck) Run() *cstructs.CheckResult { - buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) - cmd := exec.Command(e.cmd, e.args...) - cmd.Stdout = buf - cmd.Stderr = buf - e.setChroot(cmd) - ts := time.Now() - if err := cmd.Start(); err != nil { - return &cstructs.CheckResult{Err: err} - } - errCh := make(chan error, 2) - go func() { - errCh <- cmd.Wait() - }() - for { - select { - case err := <-errCh: - endTime := time.Now() - if err == nil { - return &cstructs.CheckResult{ - ExitCode: 0, - Output: string(buf.Bytes()), - Timestamp: ts, - } - } - exitCode := 1 - if exitErr, ok := err.(*exec.ExitError); ok { - if status, ok := exitErr.Sys().(unix.WaitStatus); ok { - exitCode = status.ExitStatus() - } - } - return &cstructs.CheckResult{ - ExitCode: exitCode, - Output: string(buf.Bytes()), - Timestamp: ts, - Duration: endTime.Sub(ts), - } - case <-time.After(e.Timeout()): - errCh <- fmt.Errorf("timed out after waiting 30s") - } - } - return nil -} diff --git a/client/driver/executor/checks_windows.go b/client/driver/executor/checks_windows.go index f88446ad8..a35c2722d 100644 --- a/client/driver/executor/checks_windows.go +++ b/client/driver/executor/checks_windows.go @@ -2,62 +2,7 @@ package executor -import ( - "fmt" - "os/exec" - "time" - - "golang.org/x/sys/windows" - - "github.com/armon/circbuf" - - cstructs "github.com/hashicorp/nomad/client/driver/structs" -) +import "os/exec" func (e *ExecScriptCheck) setChroot(cmd *exec.Cmd) { } - -// Run runs an exec script check -func (e *ExecScriptCheck) Run() *cstructs.CheckResult { - buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) - cmd := exec.Command(e.cmd, e.args...) - cmd.Stdout = buf - cmd.Stderr = buf - e.setChroot(cmd) - ts := time.Now() - if err := cmd.Start(); err != nil { - return &cstructs.CheckResult{Err: err} - } - errCh := make(chan error, 2) - go func() { - errCh <- cmd.Wait() - }() - for { - select { - case err := <-errCh: - endTime := time.Now() - if err == nil { - return &cstructs.CheckResult{ - ExitCode: 0, - Output: string(buf.Bytes()), - Timestamp: ts, - } - } - exitCode := 1 - if exitErr, ok := err.(*exec.ExitError); ok { - if status, ok := exitErr.Sys().(windows.WaitStatus); ok { - exitCode = status.ExitStatus() - } - } - return &cstructs.CheckResult{ - ExitCode: exitCode, - Output: string(buf.Bytes()), - Timestamp: ts, - Duration: endTime.Sub(ts), - } - case <-time.After(e.Timeout()): - errCh <- fmt.Errorf("timed out after waiting 30s") - } - } - return nil -}