1
0

Fetch debian kernels function

This commit is contained in:
dump_stack() 2023-05-12 15:00:50 +00:00
parent fa579e5170
commit b5d4bdd5f4
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 59 additions and 46 deletions

View File

@ -1,12 +1,9 @@
package main
import (
"time"
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/distro/debian"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
)
type DebianCmd struct {
@ -26,52 +23,14 @@ func (cmd *DebianCacheCmd) Run() (err error) {
}
defer c.Close()
versions, err := snapshot.SourcePackageVersions("linux")
log.Info().Msg("Fetching kernels...")
_, err = debian.GetKernels(c, cmd.Refetch)
if err != nil {
log.Error().Err(err).Msg("get source package versions")
log.Error().Err(err).Msg("")
return
}
for i, version := range versions {
slog := log.With().Str("version", version).Logger()
slog.Info().Msgf("%03d/%03d", i, len(versions))
var dk debian.DebianKernel
dk, err = c.Get(version)
if err == nil && !dk.Internal.Invalid {
slog.Info().Msgf("found in cache")
continue
}
if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, cmd.Refetch)
if refetch.After(time.Now()) {
slog.Info().Msgf("refetch at %v", refetch)
continue
}
}
dk, err = debian.GetDebianKernel(version)
if err != nil {
if err == debian.ErrNoBinaryPackages {
slog.Warn().Err(err).Msg("")
} else {
slog.Error().Err(err).Msg("get debian kernel")
}
dk.Internal.Invalid = true
dk.Internal.LastFetch = time.Now()
}
err = c.Put(dk)
if err != nil {
slog.Error().Err(err).Msg("put to cache")
return
}
slog.Info().Msgf("%s cached", version)
}
log.Info().Msg("Success")
return
}

View File

@ -6,6 +6,8 @@ import (
"strings"
"time"
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
)
@ -100,3 +102,55 @@ func GetDebianKernel(version string) (dk DebianKernel, err error) {
return
}
func GetKernels(c *Cache, refetchDays int) (kernels []DebianKernel, err error) {
versions, err := snapshot.SourcePackageVersions("linux")
if err != nil {
log.Error().Err(err).Msg("get source package versions")
return
}
for i, version := range versions {
slog := log.With().Str("version", version).Logger()
slog.Debug().Msgf("%03d/%03d", i, len(versions))
var dk DebianKernel
dk, err = c.Get(version)
if err == nil && !dk.Internal.Invalid {
slog.Debug().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.Debug().Msgf("refetch at %v", refetchDays)
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
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
}