Refactor
This commit is contained in:
parent
5ad41bc1c8
commit
e0c0d3a072
@ -14,18 +14,21 @@ import (
|
|||||||
"github.com/naoina/toml"
|
"github.com/naoina/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// KernelMask defines the kernel
|
||||||
type KernelMask struct {
|
type KernelMask struct {
|
||||||
DistroType DistroType
|
DistroType DistroType
|
||||||
DistroRelease string // 18.04/7.4.1708/9.1
|
DistroRelease string // 18.04/7.4.1708/9.1
|
||||||
ReleaseMask string
|
ReleaseMask string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DockerName is returns stable name for docker container
|
||||||
func (km KernelMask) DockerName() string {
|
func (km KernelMask) DockerName() string {
|
||||||
distro := strings.ToLower(km.DistroType.String())
|
distro := strings.ToLower(km.DistroType.String())
|
||||||
release := strings.Replace(km.DistroRelease, ".", "__", -1)
|
release := strings.Replace(km.DistroRelease, ".", "__", -1)
|
||||||
return fmt.Sprintf("out_of_tree_%s_%s", distro, release)
|
return fmt.Sprintf("out_of_tree_%s_%s", distro, release)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ArtifactType is the kernel module or exploit
|
||||||
type ArtifactType int
|
type ArtifactType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -37,6 +40,7 @@ func (at ArtifactType) String() string {
|
|||||||
return [...]string{"module", "exploit"}[at]
|
return [...]string{"module", "exploit"}[at]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalTOML is for support github.com/naoina/toml
|
||||||
func (at *ArtifactType) UnmarshalTOML(data []byte) (err error) {
|
func (at *ArtifactType) UnmarshalTOML(data []byte) (err error) {
|
||||||
stype := strings.Trim(string(data), `"`)
|
stype := strings.Trim(string(data), `"`)
|
||||||
stypelower := strings.ToLower(stype)
|
stypelower := strings.ToLower(stype)
|
||||||
@ -50,6 +54,7 @@ func (at *ArtifactType) UnmarshalTOML(data []byte) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalTOML is for support github.com/naoina/toml
|
||||||
func (at ArtifactType) MarshalTOML() (data []byte, err error) {
|
func (at ArtifactType) MarshalTOML() (data []byte, err error) {
|
||||||
s := ""
|
s := ""
|
||||||
switch at {
|
switch at {
|
||||||
@ -64,6 +69,7 @@ func (at ArtifactType) MarshalTOML() (data []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Artifact is for .out-of-tree.toml
|
||||||
type Artifact struct {
|
type Artifact struct {
|
||||||
Name string
|
Name string
|
||||||
Type ArtifactType
|
Type ArtifactType
|
||||||
@ -89,6 +95,7 @@ func (ka Artifact) checkSupport(ki KernelInfo, km KernelMask) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Supported returns true if given kernel is supported by artifact
|
||||||
func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) {
|
func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) {
|
||||||
for _, km := range ka.SupportedKernels {
|
for _, km := range ka.SupportedKernels {
|
||||||
supported, err = ka.checkSupport(ki, km)
|
supported, err = ka.checkSupport(ki, km)
|
||||||
@ -100,6 +107,7 @@ func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DistroType is enum with all supported distros
|
||||||
type DistroType int
|
type DistroType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -110,6 +118,7 @@ const (
|
|||||||
|
|
||||||
var DistroTypeStrings = [...]string{"Ubuntu", "CentOS", "Debian"}
|
var DistroTypeStrings = [...]string{"Ubuntu", "CentOS", "Debian"}
|
||||||
|
|
||||||
|
// NewDistroType is create new Distro object
|
||||||
func NewDistroType(dType string) (dt DistroType, err error) {
|
func NewDistroType(dType string) (dt DistroType, err error) {
|
||||||
err = dt.UnmarshalTOML([]byte(dType))
|
err = dt.UnmarshalTOML([]byte(dType))
|
||||||
return
|
return
|
||||||
@ -119,6 +128,7 @@ func (dt DistroType) String() string {
|
|||||||
return DistroTypeStrings[dt]
|
return DistroTypeStrings[dt]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalTOML is for support github.com/naoina/toml
|
||||||
func (dt *DistroType) UnmarshalTOML(data []byte) (err error) {
|
func (dt *DistroType) UnmarshalTOML(data []byte) (err error) {
|
||||||
sDistro := strings.Trim(string(data), `"`)
|
sDistro := strings.Trim(string(data), `"`)
|
||||||
if strings.EqualFold(sDistro, "Ubuntu") {
|
if strings.EqualFold(sDistro, "Ubuntu") {
|
||||||
@ -133,6 +143,7 @@ func (dt *DistroType) UnmarshalTOML(data []byte) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalTOML is for support github.com/naoina/toml
|
||||||
func (dt DistroType) MarshalTOML() (data []byte, err error) {
|
func (dt DistroType) MarshalTOML() (data []byte, err error) {
|
||||||
s := ""
|
s := ""
|
||||||
switch dt {
|
switch dt {
|
||||||
@ -175,6 +186,7 @@ type KernelInfo struct {
|
|||||||
VmlinuxPath string
|
VmlinuxPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KernelConfig is the ~/.out-of-tree/kernels.toml configuration description
|
||||||
type KernelConfig struct {
|
type KernelConfig struct {
|
||||||
Kernels []KernelInfo
|
Kernels []KernelInfo
|
||||||
}
|
}
|
||||||
@ -190,6 +202,7 @@ func readFileAll(path string) (buf []byte, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadKernelConfig is for read kernels.toml
|
||||||
func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) {
|
func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) {
|
||||||
buf, err := readFileAll(path)
|
buf, err := readFileAll(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -204,6 +217,7 @@ func ReadKernelConfig(path string) (kernelCfg KernelConfig, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadArtifactConfig is for read .out-of-tree.toml
|
||||||
func ReadArtifactConfig(path string) (artifactCfg Artifact, err error) {
|
func ReadArtifactConfig(path string) (artifactCfg Artifact, err error) {
|
||||||
buf, err := readFileAll(path)
|
buf, err := readFileAll(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
"code.dumpstack.io/tools/out-of-tree/config"
|
"code.dumpstack.io/tools/out-of-tree/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var KERNELS_ALL int64 = math.MaxInt64
|
const KERNELS_ALL int64 = math.MaxInt64
|
||||||
|
|
||||||
func kernelListHandler(kcfg config.KernelConfig) (err error) {
|
func kernelListHandler(kcfg config.KernelConfig) (err error) {
|
||||||
if len(kcfg.Kernels) == 0 {
|
if len(kcfg.Kernels) == 0 {
|
||||||
|
16
log.go
16
log.go
@ -88,7 +88,7 @@ func logHandler(db *sql.DB, path, tag string, num int, rate bool) (err error) {
|
|||||||
success := 0
|
success := 0
|
||||||
for _, l := range les {
|
for _, l := range les {
|
||||||
if l.Test.Ok {
|
if l.Test.Ok {
|
||||||
success += 1
|
success++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,21 +179,21 @@ func getStats(db *sql.DB, path, tag string) (
|
|||||||
|
|
||||||
rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease]
|
rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease]
|
||||||
|
|
||||||
rs.All += 1
|
rs.All++
|
||||||
if l.Build.Ok {
|
if l.Build.Ok {
|
||||||
rs.BuildOK += 1
|
rs.BuildOK++
|
||||||
}
|
}
|
||||||
if l.Run.Ok {
|
if l.Run.Ok {
|
||||||
rs.RunOK += 1
|
rs.RunOK++
|
||||||
}
|
}
|
||||||
if l.Test.Ok {
|
if l.Test.Ok {
|
||||||
rs.TestOK += 1
|
rs.TestOK++
|
||||||
}
|
}
|
||||||
if l.KernelPanic {
|
if l.KernelPanic {
|
||||||
rs.Panic += 1
|
rs.Panic++
|
||||||
}
|
}
|
||||||
if l.KilledByTimeout {
|
if l.KilledByTimeout {
|
||||||
rs.Timeout += 1
|
rs.Timeout++
|
||||||
}
|
}
|
||||||
|
|
||||||
distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs
|
distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs
|
||||||
@ -202,7 +202,7 @@ func getStats(db *sql.DB, path, tag string) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func logJsonHandler(db *sql.DB, path, tag string) (err error) {
|
func logJSONHandler(db *sql.DB, path, tag string) (err error) {
|
||||||
distros, err := getStats(db, path, tag)
|
distros, err := getStats(db, path, tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
8
main.go
8
main.go
@ -195,8 +195,8 @@ func main() {
|
|||||||
"Show all info for log entry with ID")
|
"Show all info for log entry with ID")
|
||||||
logDumpID := logDumpCommand.Arg("ID", "").Required().Int()
|
logDumpID := logDumpCommand.Arg("ID", "").Required().Int()
|
||||||
|
|
||||||
logJsonCommand := logCommand.Command("json", "Generate json statistics")
|
logJSONCommand := logCommand.Command("json", "Generate json statistics")
|
||||||
logJsonTag := logJsonCommand.Flag("tag", "Filter tag").Required().String()
|
logJSONTag := logJSONCommand.Flag("tag", "Filter tag").Required().String()
|
||||||
|
|
||||||
logMarkdownCommand := logCommand.Command("markdown", "Generate markdown statistics")
|
logMarkdownCommand := logCommand.Command("markdown", "Generate markdown statistics")
|
||||||
logMarkdownTag := logMarkdownCommand.Flag("tag", "Filter tag").Required().String()
|
logMarkdownTag := logMarkdownCommand.Flag("tag", "Filter tag").Required().String()
|
||||||
@ -283,8 +283,8 @@ func main() {
|
|||||||
err = logHandler(db, *path, *logTag, *logNum, *logRate)
|
err = logHandler(db, *path, *logTag, *logNum, *logRate)
|
||||||
case logDumpCommand.FullCommand():
|
case logDumpCommand.FullCommand():
|
||||||
err = logDumpHandler(db, *logDumpID)
|
err = logDumpHandler(db, *logDumpID)
|
||||||
case logJsonCommand.FullCommand():
|
case logJSONCommand.FullCommand():
|
||||||
err = logJsonHandler(db, *path, *logJsonTag)
|
err = logJSONHandler(db, *path, *logJSONTag)
|
||||||
case logMarkdownCommand.FullCommand():
|
case logMarkdownCommand.FullCommand():
|
||||||
err = logMarkdownHandler(db, *path, *logMarkdownTag)
|
err = logMarkdownHandler(db, *path, *logMarkdownTag)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user