2019-08-13 23:33:52 +00:00
|
|
|
// Copyright 2019 Mikhail Klementev. All rights reserved.
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-08-16 19:31:58 +00:00
|
|
|
"encoding/json"
|
2019-08-13 23:33:52 +00:00
|
|
|
"fmt"
|
2019-08-16 19:31:58 +00:00
|
|
|
"log"
|
2019-08-13 23:33:52 +00:00
|
|
|
"math"
|
2019-08-16 20:11:35 +00:00
|
|
|
"os"
|
2019-08-13 23:33:52 +00:00
|
|
|
|
2019-08-16 20:11:35 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-08-13 23:33:52 +00:00
|
|
|
"gopkg.in/logrusorgru/aurora.v1"
|
2019-08-16 20:11:35 +00:00
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2019-08-13 23:33:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func logLogEntry(l logEntry) {
|
|
|
|
distroInfo := fmt.Sprintf("%s-%s {%s}", l.DistroType,
|
|
|
|
l.DistroRelease, l.KernelRelease)
|
|
|
|
|
|
|
|
artifactInfo := fmt.Sprintf("{[%s] %s}", l.Type, l.Name)
|
|
|
|
|
|
|
|
colored := ""
|
|
|
|
if l.Type == config.KernelExploit {
|
2019-08-16 18:30:46 +00:00
|
|
|
colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s",
|
|
|
|
l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
|
2019-08-13 23:33:52 +00:00
|
|
|
genOkFail("BUILD", l.Build.Ok),
|
|
|
|
genOkFail("LPE", l.Test.Ok))
|
|
|
|
} else {
|
2019-08-16 18:30:46 +00:00
|
|
|
colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s %s",
|
|
|
|
l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
|
2019-08-13 23:33:52 +00:00
|
|
|
genOkFail("BUILD", l.Build.Ok),
|
|
|
|
genOkFail("INSMOD", l.Run.Ok),
|
|
|
|
genOkFail("TEST", l.Test.Ok))
|
|
|
|
}
|
|
|
|
|
|
|
|
additional := ""
|
|
|
|
if l.KernelPanic {
|
|
|
|
additional = "(panic)"
|
|
|
|
} else if l.KilledByTimeout {
|
|
|
|
additional = "(timeout)"
|
|
|
|
}
|
|
|
|
|
|
|
|
if additional != "" {
|
|
|
|
fmt.Println(colored, additional)
|
|
|
|
} else {
|
|
|
|
fmt.Println(colored)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 18:30:46 +00:00
|
|
|
func logHandler(db *sql.DB, path, tag string, num int, rate bool) (err error) {
|
2019-08-13 23:33:52 +00:00
|
|
|
var les []logEntry
|
|
|
|
|
|
|
|
ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
|
|
|
|
if kaErr == nil {
|
2019-08-16 19:31:58 +00:00
|
|
|
log.Println(".out-of-tree.toml found, filter by artifact name")
|
2019-08-16 18:30:46 +00:00
|
|
|
les, err = getAllArtifactLogs(db, tag, num, ka)
|
2019-08-13 23:33:52 +00:00
|
|
|
} else {
|
2019-08-16 18:30:46 +00:00
|
|
|
les, err = getAllLogs(db, tag, num)
|
2019-08-13 23:33:52 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := "\nS"
|
|
|
|
if rate {
|
|
|
|
if kaErr != nil {
|
|
|
|
err = kaErr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = fmt.Sprintf("{[%s] %s} Overall s", ka.Type, ka.Name)
|
|
|
|
|
2019-08-16 18:30:46 +00:00
|
|
|
les, err = getAllArtifactLogs(db, tag, math.MaxInt64, ka)
|
2019-08-13 23:33:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, l := range les {
|
|
|
|
logLogEntry(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
success := 0
|
|
|
|
for _, l := range les {
|
|
|
|
if l.Test.Ok {
|
|
|
|
success += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
overall := float64(success) / float64(len(les))
|
|
|
|
fmt.Printf("%success rate: %.04f (~%.0f%%)\n",
|
|
|
|
s, overall, overall*100)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-08-13 23:54:45 +00:00
|
|
|
|
|
|
|
func logDumpHandler(db *sql.DB, id int) (err error) {
|
|
|
|
l, err := getLogByID(db, id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("ID:", l.ID)
|
|
|
|
fmt.Println("Date:", l.Timestamp)
|
2019-08-16 18:30:46 +00:00
|
|
|
fmt.Println("Tag:", l.Tag)
|
2019-08-13 23:54:45 +00:00
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Println("Type:", l.Type.String())
|
|
|
|
fmt.Println("Name:", l.Name)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Println("Distro:", l.DistroType.String(), l.DistroRelease)
|
|
|
|
fmt.Println("Kernel:", l.KernelRelease)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Println("Build ok:", l.Build.Ok)
|
|
|
|
if l.Type == config.KernelModule {
|
|
|
|
fmt.Println("Insmod ok:", l.Run.Ok)
|
|
|
|
}
|
|
|
|
fmt.Println("Test ok:", l.Test.Ok)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("Build output:\n%s\n", l.Build.Output)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if l.Type == config.KernelModule {
|
|
|
|
fmt.Printf("Insmod output:\n%s\n", l.Run.Output)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Test output:\n%s\n", l.Test.Output)
|
|
|
|
fmt.Println()
|
|
|
|
|
2019-08-14 22:59:34 +00:00
|
|
|
fmt.Printf("Qemu stdout:\n%s\n", l.Stdout)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("Qemu stderr:\n%s\n", l.Stderr)
|
|
|
|
fmt.Println()
|
|
|
|
|
2019-08-13 23:54:45 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-16 19:31:58 +00:00
|
|
|
|
|
|
|
type runstat struct {
|
|
|
|
All, BuildOK, RunOK, TestOK, Timeout, Panic int
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:11:35 +00:00
|
|
|
func getStats(db *sql.DB, path, tag string) (
|
|
|
|
distros map[string]map[string]map[string]runstat, err error) {
|
|
|
|
|
2019-08-16 19:31:58 +00:00
|
|
|
var les []logEntry
|
|
|
|
|
|
|
|
ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
|
|
|
|
if kaErr == nil {
|
|
|
|
les, err = getAllArtifactLogs(db, tag, -1, ka)
|
|
|
|
} else {
|
|
|
|
les, err = getAllLogs(db, tag, -1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:11:35 +00:00
|
|
|
distros = make(map[string]map[string]map[string]runstat)
|
2019-08-16 19:31:58 +00:00
|
|
|
|
|
|
|
for _, l := range les {
|
|
|
|
_, ok := distros[l.DistroType.String()]
|
|
|
|
if !ok {
|
|
|
|
distros[l.DistroType.String()] = make(map[string]map[string]runstat)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = distros[l.DistroType.String()][l.DistroRelease]
|
|
|
|
if !ok {
|
|
|
|
distros[l.DistroType.String()][l.DistroRelease] = make(map[string]runstat)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease]
|
|
|
|
|
|
|
|
rs.All += 1
|
|
|
|
if l.Build.Ok {
|
|
|
|
rs.BuildOK += 1
|
|
|
|
}
|
|
|
|
if l.Run.Ok {
|
|
|
|
rs.RunOK += 1
|
|
|
|
}
|
|
|
|
if l.Test.Ok {
|
|
|
|
rs.TestOK += 1
|
|
|
|
}
|
|
|
|
if l.KernelPanic {
|
|
|
|
rs.Panic += 1
|
|
|
|
}
|
|
|
|
if l.KilledByTimeout {
|
|
|
|
rs.Timeout += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:11:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func logJsonHandler(db *sql.DB, path, tag string) (err error) {
|
|
|
|
distros, err := getStats(db, path, tag)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:31:58 +00:00
|
|
|
bytes, err := json.Marshal(&distros)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(bytes))
|
2019-08-16 20:11:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func logMarkdownHandler(db *sql.DB, path, tag string) (err error) {
|
|
|
|
distros, err := getStats(db, path, tag)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
table.SetHeader([]string{"Distro", "Release", "Kernel", "Reliability"})
|
|
|
|
table.SetBorders(tablewriter.Border{
|
|
|
|
Left: true, Top: false, Right: true, Bottom: false})
|
|
|
|
table.SetCenterSeparator("|")
|
|
|
|
|
|
|
|
for distro, releases := range distros {
|
|
|
|
for release, kernels := range releases {
|
|
|
|
for kernel, stats := range kernels {
|
|
|
|
all := float64(stats.All)
|
|
|
|
ok := float64(stats.TestOK)
|
|
|
|
r := fmt.Sprintf("%6.02f%%", (ok/all)*100)
|
|
|
|
table.Append([]string{distro, release, kernel, r})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 19:31:58 +00:00
|
|
|
|
2019-08-16 20:11:35 +00:00
|
|
|
table.Render()
|
2019-08-16 19:31:58 +00:00
|
|
|
return
|
|
|
|
}
|