feat: add limit amount of kernels to fetch
This commit is contained in:
		
							
								
								
									
										2
									
								
								.github/workflows/debian-cache.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/debian-cache.yml
									
									
									
									
										vendored
									
									
								
							| @@ -25,7 +25,7 @@ jobs: | |||||||
|       run: go build |       run: go build | ||||||
|  |  | ||||||
|     - name: Cache |     - name: Cache | ||||||
|       run: ./out-of-tree --log-level=trace distro debian cache --refetch=0 |       run: ./out-of-tree --log-level=trace distro debian cache --refetch=0 --limit=64 | ||||||
|  |  | ||||||
|     - name: Install s3cmd |     - name: Install s3cmd | ||||||
|       run: sudo apt install s3cmd |       run: sudo apt install s3cmd | ||||||
|   | |||||||
							
								
								
									
										14
									
								
								distro.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								distro.go
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ package main | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"math" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| @@ -30,6 +31,7 @@ type DebianCmd struct { | |||||||
| 	Cache DebianCacheCmd `cmd:"" help:"populate cache"` | 	Cache DebianCacheCmd `cmd:"" help:"populate cache"` | ||||||
| 	Fetch DebianFetchCmd `cmd:"" help:"download deb packages"` | 	Fetch DebianFetchCmd `cmd:"" help:"download deb packages"` | ||||||
|  |  | ||||||
|  | 	Limit int    `help:"limit amount of kernels to fetch"` | ||||||
| 	Regex string `help:"match deb pkg names by regex" default:".*"` | 	Regex string `help:"match deb pkg names by regex" default:".*"` | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -47,7 +49,11 @@ func (cmd *DebianCacheCmd) Run(dcmd *DebianCmd) (err error) { | |||||||
|  |  | ||||||
| 	log.Info().Msg("Fetching kernels...") | 	log.Info().Msg("Fetching kernels...") | ||||||
|  |  | ||||||
| 	kernels, err := debian.GetKernels() | 	if dcmd.Limit == 0 { | ||||||
|  | 		dcmd.Limit = math.MaxInt32 | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	kernels, err := debian.GetKernelsWithLimit(dcmd.Limit) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Error().Err(err).Msg("") | 		log.Error().Err(err).Msg("") | ||||||
| 		return | 		return | ||||||
| @@ -152,7 +158,11 @@ func (cmd *DebianFetchCmd) Run(dcmd *DebianCmd) (err error) { | |||||||
| 	log.Info().Msg("will not download packages that exist on the mirror") | 	log.Info().Msg("will not download packages that exist on the mirror") | ||||||
| 	log.Info().Msg("use --ignore-mirror if you really need it") | 	log.Info().Msg("use --ignore-mirror if you really need it") | ||||||
|  |  | ||||||
| 	kernels, err := debian.GetKernels() | 	if dcmd.Limit == 0 { | ||||||
|  | 		dcmd.Limit = math.MaxInt32 | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	kernels, err := debian.GetKernelsWithLimit(dcmd.Limit) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Error().Err(err).Msg("") | 		log.Error().Err(err).Msg("") | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ package debian | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"math" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| @@ -260,7 +261,7 @@ func findKbuild(versions []string, kpkgver string) ( | |||||||
| } | } | ||||||
|  |  | ||||||
| func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string, | func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string, | ||||||
| 	version string) (kernels []DebianKernel) { | 	version string) (kernels []DebianKernel, fromcache bool) { | ||||||
|  |  | ||||||
| 	var dk DebianKernel | 	var dk DebianKernel | ||||||
| 	dks, err := c.Get(version) | 	dks, err := c.Get(version) | ||||||
| @@ -269,6 +270,7 @@ func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string, | |||||||
| 		if !dk.Internal.Invalid { | 		if !dk.Internal.Invalid { | ||||||
| 			slog.Trace().Msgf("found in cache") | 			slog.Trace().Msgf("found in cache") | ||||||
| 			kernels = append(kernels, dk) | 			kernels = append(kernels, dk) | ||||||
|  | 			fromcache = true | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -336,7 +338,9 @@ var ( | |||||||
| 	RefetchDays int = 14 | 	RefetchDays int = 14 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func GetKernels() (kernels []DebianKernel, err error) { | // GetKernelsWithLimit is workaround for testing and building the | ||||||
|  | // first cache, which is heavily rate limited by snapshot.debian.org | ||||||
|  | func GetKernelsWithLimit(limit int) (kernels []DebianKernel, err error) { | ||||||
| 	if CachePath == "" { | 	if CachePath == "" { | ||||||
| 		CachePath = config.File("debian.cache") | 		CachePath = config.File("debian.cache") | ||||||
| 		log.Debug().Msgf("Use default kernels cache path: %s", CachePath) | 		log.Debug().Msgf("Use default kernels cache path: %s", CachePath) | ||||||
| @@ -381,10 +385,19 @@ func GetKernels() (kernels []DebianKernel, err error) { | |||||||
| 	for i, version := range versions { | 	for i, version := range versions { | ||||||
| 		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) | 		vkernels, fromcache := getKernelsByVersion(slog, c, toolsVersions, version) | ||||||
| 		kernels = append(kernels, vkernels...) | 		kernels = append(kernels, vkernels...) | ||||||
|  | 		if !fromcache { | ||||||
|  | 			limit-- | ||||||
|  | 		} | ||||||
|  | 		if limit <= 0 { | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func GetKernels() (kernels []DebianKernel, err error) { | ||||||
|  | 	return GetKernelsWithLimit(math.MaxInt32) | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user