2023-05-11 20:08:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-15 11:35:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/cavaliergopher/grab/v3"
|
2023-05-11 20:08:08 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-05-15 13:12:56 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/cache"
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-11 20:08:08 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/debian"
|
2023-05-15 11:35:15 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
|
2023-05-15 11:48:06 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2023-05-11 20:08:08 +00:00
|
|
|
)
|
|
|
|
|
2023-05-15 10:35:07 +00:00
|
|
|
type DistroCmd struct {
|
|
|
|
Debian DebianCmd `cmd:""`
|
|
|
|
}
|
|
|
|
|
2023-05-11 20:08:08 +00:00
|
|
|
type DebianCmd struct {
|
2023-05-15 11:35:15 +00:00
|
|
|
Cache DebianCacheCmd `cmd:"" help:"populate cache"`
|
|
|
|
GetDeb DebianGetDebCmd `cmd:"" help:"download deb packages"`
|
2023-05-11 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DebianCacheCmd struct {
|
2023-05-15 10:35:07 +00:00
|
|
|
Path string `help:"path to cache"`
|
2023-05-12 00:07:51 +00:00
|
|
|
Refetch int `help:"days before refetch versions without deb package" default:"7"`
|
2023-05-11 20:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd *DebianCacheCmd) Run() (err error) {
|
2023-05-15 10:35:07 +00:00
|
|
|
if cmd.Path != "" {
|
|
|
|
debian.CachePath = cmd.Path
|
|
|
|
}
|
2023-05-14 12:37:45 +00:00
|
|
|
debian.RefetchDays = cmd.Refetch
|
2023-05-11 20:08:08 +00:00
|
|
|
|
2023-05-12 15:00:50 +00:00
|
|
|
log.Info().Msg("Fetching kernels...")
|
|
|
|
|
2023-05-14 12:37:45 +00:00
|
|
|
_, err = debian.GetKernels()
|
2023-05-11 20:08:08 +00:00
|
|
|
if err != nil {
|
2023-05-12 15:00:50 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-05-11 20:08:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:00:50 +00:00
|
|
|
log.Info().Msg("Success")
|
2023-05-11 20:08:08 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-15 11:35:15 +00:00
|
|
|
|
|
|
|
type DebianGetDebCmd struct {
|
|
|
|
Path string `help:"path to download directory" type:"existingdir" default:"./"`
|
|
|
|
Regexp string `help:"match deb pkg names by regexp" default:".*"`
|
2023-05-15 13:12:56 +00:00
|
|
|
|
|
|
|
IgnoreCached bool `help:"ignore packages found on remote mirror"`
|
2023-05-15 13:41:37 +00:00
|
|
|
|
|
|
|
Max int `help:"do not download more than X" default:"100500"`
|
2023-05-15 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd DebianGetDebCmd) Run() (err error) {
|
2023-05-15 11:50:54 +00:00
|
|
|
re, err := regexp.Compile(cmd.Regexp)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("regexp")
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:35:15 +00:00
|
|
|
kernels, err := debian.GetKernels()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var packages []snapshot.Package
|
|
|
|
for _, kernel := range kernels {
|
|
|
|
for _, pkg := range kernel.Packages() {
|
|
|
|
if !re.MatchString(pkg.Deb.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-15 13:12:56 +00:00
|
|
|
|
2023-05-15 11:35:15 +00:00
|
|
|
packages = append(packages, pkg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:48:06 +00:00
|
|
|
tmp, err := os.MkdirTemp(cmd.Path, "tmp-")
|
2023-05-15 11:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2023-05-15 13:41:04 +00:00
|
|
|
hasresults := false
|
|
|
|
|
2023-05-15 11:35:15 +00:00
|
|
|
for _, pkg := range packages {
|
2023-05-15 13:41:37 +00:00
|
|
|
if cmd.Max <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:12:56 +00:00
|
|
|
if cmd.IgnoreCached {
|
|
|
|
log.Debug().Msgf("check cache for %s", pkg.Deb.Name)
|
|
|
|
found, _ := cache.PackageURL(config.Debian, pkg.Deb.URL)
|
|
|
|
if found {
|
|
|
|
log.Debug().Msgf("%s already cached", pkg.Deb.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:48:06 +00:00
|
|
|
target := filepath.Join(cmd.Path, filepath.Base(pkg.Deb.URL))
|
|
|
|
|
|
|
|
if fs.PathExists(target) {
|
|
|
|
log.Info().Msgf("%s already exists", pkg.Deb.URL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:35:15 +00:00
|
|
|
log.Info().Msgf("downloading %s", pkg.Deb.URL)
|
|
|
|
|
|
|
|
resp, err := grab.Get(tmp, pkg.Deb.URL)
|
|
|
|
if err != nil {
|
2023-05-15 14:11:08 +00:00
|
|
|
err = nil
|
2023-05-15 11:35:15 +00:00
|
|
|
log.Warn().Err(err).Msg("download")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-15 11:48:06 +00:00
|
|
|
err = os.Rename(resp.Filename, target)
|
2023-05-15 11:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("mv")
|
|
|
|
}
|
2023-05-15 13:41:04 +00:00
|
|
|
|
|
|
|
hasresults = true
|
2023-05-15 13:41:37 +00:00
|
|
|
cmd.Max--
|
2023-05-15 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 13:41:04 +00:00
|
|
|
if !hasresults {
|
2023-05-15 14:26:38 +00:00
|
|
|
log.Fatal().Msg("no packages found to download")
|
2023-05-15 13:41:04 +00:00
|
|
|
}
|
2023-05-15 11:35:15 +00:00
|
|
|
return
|
|
|
|
}
|