1
0

Implements setting docker and qemu timeout

This commit is contained in:
dump_stack() 2018-10-27 19:06:24 +00:00
parent 053678fee6
commit 305c27eb7d

37
main.go
View File

@ -140,7 +140,9 @@ func dockerCommand(container, workdir, timeout, command string) *exec.Cmd {
"bash", "-c", "cd /work && "+command) "bash", "-c", "cd /work && "+command)
} }
func build(tmp string, ka artifact, ki kernelInfo) (outPath, output string, err error) { func build(tmp string, ka artifact, ki kernelInfo,
dockerTimeout time.Duration) (outPath, output string, err error) {
target := fmt.Sprintf("%d_%s", rand.Int(), ki.KernelRelease) target := fmt.Sprintf("%d_%s", rand.Int(), ki.KernelRelease)
tmpSourcePath := tmp + "/source" tmpSourcePath := tmp + "/source"
@ -157,7 +159,8 @@ func build(tmp string, ka artifact, ki kernelInfo) (outPath, output string, err
kernel := "/lib/modules/" + ki.KernelRelease + "/build" kernel := "/lib/modules/" + ki.KernelRelease + "/build"
cmd := dockerCommand(ki.ContainerName, tmpSourcePath, "1m", // TODO CFG seconds := fmt.Sprintf("%ds", dockerTimeout/time.Second)
cmd := dockerCommand(ki.ContainerName, tmpSourcePath, seconds,
"make KERNEL="+kernel+" TARGET="+target) "make KERNEL="+kernel+" TARGET="+target)
rawOutput, err := cmd.CombinedOutput() rawOutput, err := cmd.CombinedOutput()
output = string(rawOutput) output = string(rawOutput)
@ -254,7 +257,9 @@ func dumpResult(q *qemu.QemuSystem, ka artifact, ki kernelInfo, build_ok, run_ok
} }
} }
func whatever(swg *sizedwaitgroup.SizedWaitGroup, ka artifact, ki kernelInfo) { func whatever(swg *sizedwaitgroup.SizedWaitGroup, ka artifact, ki kernelInfo,
qemuTimeout, dockerTimeout time.Duration) {
defer swg.Done() defer swg.Done()
kernel := qemu.Kernel{KernelPath: ki.KernelPath, InitrdPath: ki.InitrdPath} kernel := qemu.Kernel{KernelPath: ki.KernelPath, InitrdPath: ki.InitrdPath}
@ -262,7 +267,7 @@ func whatever(swg *sizedwaitgroup.SizedWaitGroup, ka artifact, ki kernelInfo) {
if err != nil { if err != nil {
return return
} }
q.Timeout = time.Minute q.Timeout = qemuTimeout
err = q.Start() err = q.Start()
if err != nil { if err != nil {
@ -282,7 +287,7 @@ func whatever(swg *sizedwaitgroup.SizedWaitGroup, ka artifact, ki kernelInfo) {
defer dumpResult(q, ka, ki, &build_ok, &run_ok, &test_ok) defer dumpResult(q, ka, ki, &build_ok, &run_ok, &test_ok)
// TODO Write build log to file or database // TODO Write build log to file or database
outFile, output, err := build(tmp, ka, ki) outFile, output, err := build(tmp, ka, ki, dockerTimeout)
if err != nil { if err != nil {
log.Println(output) log.Println(output)
return return
@ -387,7 +392,9 @@ func readArtifactConfig(path string) (artifactCfg artifact, err error) {
return return
} }
func performCI(ka artifact, kcfg kernelConfig) (err error) { func performCI(ka artifact, kcfg kernelConfig,
qemuTimeout, dockerTimeout time.Duration) (err error) {
swg := sizedwaitgroup.New(runtime.NumCPU()) swg := sizedwaitgroup.New(runtime.NumCPU())
for _, kernel := range kcfg.Kernels { for _, kernel := range kcfg.Kernels {
var supported bool var supported bool
@ -398,7 +405,8 @@ func performCI(ka artifact, kcfg kernelConfig) (err error) {
if supported { if supported {
swg.Add() swg.Add()
go whatever(&swg, ka, kernel) go whatever(&swg, ka, kernel, qemuTimeout,
dockerTimeout)
} }
} }
swg.Wait() swg.Wait()
@ -412,7 +420,9 @@ func exists(path string) bool {
return true return true
} }
func pewHandler(workPath, kcfgPath, ovrrdKrnl string, guess bool) (err error) { func pewHandler(workPath, kcfgPath, ovrrdKrnl string, guess bool,
qemuTimeout, dockerTimeout time.Duration) (err error) {
ka, err := readArtifactConfig(workPath + "/.out-of-tree.toml") ka, err := readArtifactConfig(workPath + "/.out-of-tree.toml")
if err != nil { if err != nil {
return return
@ -457,7 +467,7 @@ func pewHandler(workPath, kcfgPath, ovrrdKrnl string, guess bool) (err error) {
return return
} }
err = performCI(ka, kcfg) err = performCI(ka, kcfg, qemuTimeout, dockerTimeout)
if err != nil { if err != nil {
return return
} }
@ -480,6 +490,12 @@ func main() {
kcfgFlag := app.Flag("kernels", "Path to kernels config") kcfgFlag := app.Flag("kernels", "Path to kernels config")
kcfg := kcfgFlag.Envar("OUT_OF_TREE_KCFG").Required().ExistingFile() kcfg := kcfgFlag.Envar("OUT_OF_TREE_KCFG").Required().ExistingFile()
qemuTimeoutFlag := app.Flag("qemu-timeout", "Timeout for qemu")
qemuTimeout := qemuTimeoutFlag.Default("1m").Duration()
dockerTimeoutFlag := app.Flag("docker-timeout", "Timeout for docker")
dockerTimeout := dockerTimeoutFlag.Default("1m").Duration()
pewCommand := app.Command("pew", "Build, run and test module/exploit") pewCommand := app.Command("pew", "Build, run and test module/exploit")
pewKernelFlag := pewCommand.Flag("kernel", "Override kernel regex") pewKernelFlag := pewCommand.Flag("kernel", "Override kernel regex")
pewKernel := pewKernelFlag.String() pewKernel := pewKernelFlag.String()
@ -490,7 +506,8 @@ func main() {
var err error var err error
switch kingpin.MustParse(app.Parse(os.Args[1:])) { switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case pewCommand.FullCommand(): case pewCommand.FullCommand():
err = pewHandler(*path, *kcfg, *pewKernel, *pewGuess) err = pewHandler(*path, *kcfg, *pewKernel, *pewGuess,
*qemuTimeout, *dockerTimeout)
} }
if err != nil { if err != nil {