1
0

Use go timers for kill docker by timeout, fixes #12

This commit is contained in:
dump_stack() 2018-12-08 02:53:29 +00:00
parent 49b567cd4b
commit 880af47cc5
4 changed files with 31 additions and 20 deletions

View File

@ -14,6 +14,7 @@ import (
"os/user" "os/user"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/jollheef/out-of-tree/config" "github.com/jollheef/out-of-tree/config"
"github.com/naoina/toml" "github.com/naoina/toml"
@ -33,8 +34,7 @@ func matchDebianHeadersPkg(container, mask string, generic bool) (
pkgs []string, err error) { pkgs []string, err error) {
cmd := "apt-cache search linux-headers | cut -d ' ' -f 1" cmd := "apt-cache search linux-headers | cut -d ' ' -f 1"
c := dockerCommand(container, "/tmp", "1m", cmd) output, err := dockerRun(time.Minute, container, "/tmp", cmd)
rawOutput, err := c.CombinedOutput()
if err != nil { if err != nil {
return return
} }
@ -44,7 +44,7 @@ func matchDebianHeadersPkg(container, mask string, generic bool) (
return return
} }
kernels := r.FindAll(rawOutput, -1) kernels := r.FindAll([]byte(output), -1)
for _, k := range kernels { for _, k := range kernels {
pkg := string(k) pkg := string(k)

View File

@ -124,7 +124,7 @@ func main() {
"Create directories && download images") "Create directories && download images")
// Check for required commands // Check for required commands
for _, cmd := range []string{"timeout", "docker", "qemu-system-x86_64"} { for _, cmd := range []string{"docker", "qemu-system-x86_64"} {
_, err := exec.Command("which", cmd).CombinedOutput() _, err := exec.Command("which", cmd).CombinedOutput()
if err != nil { if err != nil {
log.Fatalln("Command not found:", cmd) log.Fatalln("Command not found:", cmd)

29
pew.go
View File

@ -26,10 +26,24 @@ import (
var somethingFailed = false var somethingFailed = false
func dockerCommand(container, workdir, timeout, command string) *exec.Cmd { func dockerRun(timeout time.Duration, container, workdir, command string) (
return exec.Command("timeout", "-k", timeout, timeout, "docker", "run", output string, err error) {
"-v", workdir+":/work", container,
"bash", "-c", "cd /work && "+command) cmd := exec.Command("docker", "run", "-v", workdir+":/work",
container, "bash", "-c", "cd /work && "+command)
timer := time.AfterFunc(timeout, func() {
cmd.Process.Kill()
})
defer timer.Stop()
raw, err := cmd.CombinedOutput()
if err != nil {
return
}
output = string(raw)
return
} }
func build(tmp string, ka config.Artifact, ki config.KernelInfo, func build(tmp string, ka config.Artifact, ki config.KernelInfo,
@ -51,11 +65,8 @@ func build(tmp string, ka config.Artifact, ki config.KernelInfo,
kernel := "/lib/modules/" + ki.KernelRelease + "/build" kernel := "/lib/modules/" + ki.KernelRelease + "/build"
seconds := fmt.Sprintf("%ds", dockerTimeout/time.Second) output, err = dockerRun(dockerTimeout, ki.ContainerName,
cmd := dockerCommand(ki.ContainerName, tmpSourcePath, seconds, tmpSourcePath, "make KERNEL="+kernel+" TARGET="+target)
"make KERNEL="+kernel+" TARGET="+target)
rawOutput, err := cmd.CombinedOutput()
output = string(rawOutput)
if err != nil { if err != nil {
err = errors.New("make execution error") err = errors.New("make execution error")
return return

View File

@ -13,7 +13,7 @@ import (
"time" "time"
) )
func TestDockerCommand(t *testing.T) { func TestDockerRun(t *testing.T) {
tmp, err := ioutil.TempDir("/tmp/", "out-of-tree_test_") tmp, err := ioutil.TempDir("/tmp/", "out-of-tree_test_")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -22,8 +22,9 @@ func TestDockerCommand(t *testing.T) {
start := time.Now() start := time.Now()
c := dockerCommand("ubuntu", tmp, "1s", "sleep 5s") timeout := time.Second
_, err = c.CombinedOutput()
_, err = dockerRun(timeout, "ubuntu", tmp, "sleep 5s")
if err == nil { if err == nil {
t.Fatal("docker is not killed by timeout") t.Fatal("docker is not killed by timeout")
} }
@ -32,13 +33,12 @@ func TestDockerCommand(t *testing.T) {
time.Since(start), time.Second)) time.Since(start), time.Second))
} }
c = dockerCommand("ubuntu", tmp, "1m", "echo hello") output, err := dockerRun(time.Minute, "ubuntu", tmp, "echo hello")
rawOutput, err := c.CombinedOutput()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !strings.Contains(string(rawOutput), "hello") { if !strings.Contains(output, "hello") {
t.Fatal("wrong output (" + string(rawOutput) + ")") t.Fatal("wrong output (" + output + ")")
} }
} }