From 1d22902eb0216e1c181fabb9fc445c98afecb3d1 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sat, 13 May 2023 11:04:35 +0000 Subject: [PATCH] feat: download debian cache automatically --- cache/cache.go | 23 +++++++++++++++++++++++ distro/debian/debian.go | 7 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cache/cache.go b/cache/cache.go index 33aab4d..21dad0a 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "os/exec" + "path/filepath" "github.com/cavaliergopher/grab/v3" "github.com/rs/zerolog/log" @@ -62,3 +63,25 @@ func DownloadQemuImage(path, file string) (err error) { err = unpackTar(resp.Filename, path) return } + +func DownloadDebianCache(cachePath string) (err error) { + tmp, err := ioutil.TempDir(config.Dir("tmp"), "out-of-tree_") + if err != nil { + return + } + defer os.RemoveAll(tmp) + + file := filepath.Base(cachePath) + + fileurl, err := url.JoinPath(URL, file) + if err != nil { + return + } + + resp, err := grab.Get(tmp, fileurl) + if err != nil { + return + } + + return os.Rename(filepath.Join(tmp, resp.Filename), cachePath) +} diff --git a/distro/debian/debian.go b/distro/debian/debian.go index 7019bda..fa18ee4 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -8,6 +8,7 @@ import ( "github.com/rs/zerolog/log" + "code.dumpstack.io/tools/out-of-tree/cache" "code.dumpstack.io/tools/out-of-tree/config" ) @@ -121,7 +122,6 @@ func kernelRelease(deb string) (r Release, err error) { } var ( - CacheURL string CachePath string RefetchDays int = 7 ) @@ -130,6 +130,11 @@ func MatchImagePkg(km config.KernelMask) (pkgs []string, err error) { if CachePath == "" { CachePath = config.File("debian.cache") log.Debug().Msgf("Use default kernels cache path: %s", CachePath) + + err = cache.DownloadDebianCache(CachePath) + if err != nil { + log.Debug().Msg("No remote cache, will take some time") + } } else { log.Debug().Msgf("Debian kernels cache path: %s", CachePath) }