1
0

refactor: move getting kernel by version to function

This commit is contained in:
dump_stack() 2023-05-15 17:57:44 +00:00
parent 9b33140cc8
commit 4ea7fbfbf9
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/cache" "code.dumpstack.io/tools/out-of-tree/cache"
@ -220,6 +221,74 @@ func findKbuild(versions []string, kpkgver string) (
return return
} }
func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string,
version string) (kernels []DebianKernel) {
dk, err := c.Get(version)
if err == nil && !dk.Internal.Invalid {
slog.Trace().Msgf("found in cache")
kernels = append(kernels, dk)
return
}
if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
if refetch.After(time.Now()) {
slog.Trace().Msgf("refetch at %v", refetch)
return
}
}
dk, err = getDebianKernel(version)
if err != nil {
if err == ErrNoBinaryPackages {
slog.Warn().Err(err).Msg("")
} else {
slog.Error().Err(err).Msg("get debian kernel")
}
dk.Internal.Invalid = true
}
if !dk.HasDependency("kbuild") {
if !kver(dk.Version.Package).LessThan(kver("4.5-rc0")) {
dk.Internal.Invalid = true
} else {
// Debian kernels prior to the 4.5 package
// version did not have a kbuild built from
// the linux source itself, but used the
// linux-tools source package.
kbuildpkg, err := findKbuild(
toolsVersions,
dk.Version.Package,
)
if err != nil {
dk.Internal.Invalid = true
} else {
dk.Dependencies = append(
dk.Dependencies,
kbuildpkg,
)
}
}
}
dk.Internal.LastFetch = time.Now()
if !dk.Internal.Invalid {
kernels = append(kernels, dk)
}
err = c.Put(dk)
if err != nil {
slog.Error().Err(err).Msg("put to cache")
return
}
slog.Debug().Msgf("%s cached", version)
return
}
var ( var (
CachePath string CachePath string
RefetchDays int = 14 RefetchDays int = 14
@ -249,7 +318,7 @@ func GetKernels() (kernels []DebianKernel, err error) {
} }
defer c.Close() defer c.Close()
linuxToolsVersions, err := snapshot.SourcePackageVersions("linux-tools") toolsVersions, err := snapshot.SourcePackageVersions("linux-tools")
if err != nil { if err != nil {
log.Error().Err(err).Msg("get linux-tools source pkg versions") log.Error().Err(err).Msg("get linux-tools source pkg versions")
return return
@ -268,70 +337,11 @@ func GetKernels() (kernels []DebianKernel, err error) {
} }
for i, version := range versions { for i, version := range versions {
// TODO move this scope to function
slog := log.With().Str("version", version).Logger() slog := log.With().Str("version", version).Logger()
slog.Trace().Msgf("%03d/%03d", i, len(versions)) slog.Trace().Msgf("%03d/%03d", i, len(versions))
vkernels := getKernelsByVersion(slog, c, toolsVersions, version)
kernels = append(kernels, vkernels...)
var dk DebianKernel
dk, err = c.Get(version)
if err == nil && !dk.Internal.Invalid {
slog.Trace().Msgf("found in cache")
kernels = append(kernels, dk)
continue
}
if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
if refetch.After(time.Now()) {
slog.Trace().Msgf("refetch at %v", refetch)
continue
}
}
dk, err = getDebianKernel(version)
if err != nil {
if err == ErrNoBinaryPackages {
slog.Warn().Err(err).Msg("")
} else {
slog.Error().Err(err).Msg("get debian kernel")
}
dk.Internal.Invalid = true
}
if !dk.HasDependency("kbuild") {
if !kver(dk.Version.Package).LessThan(kver("4.5-rc0")) {
dk.Internal.Invalid = true
} else {
// Debian kernels prior to the 4.5 package
// version did not have a kbuild built from
// the linux source itself, but used the
// linux-tools source package.
kbuildpkg, err := findKbuild(
linuxToolsVersions,
dk.Version.Package,
)
if err != nil {
dk.Internal.Invalid = true
} else {
dk.Dependencies = append(
dk.Dependencies,
kbuildpkg,
)
}
}
}
dk.Internal.LastFetch = time.Now()
err = c.Put(dk)
if err != nil {
slog.Error().Err(err).Msg("put to cache")
return
}
slog.Debug().Msgf("%s cached", version)
} }
return return