Skip to content

Commit

Permalink
Select latest package from multiple archives
Browse files Browse the repository at this point in the history
In the future, we'd like to support different kinds of archives which
have a non-empty intersection of packages. In other words, we'd like to
support archives that partly override the Ubuntu archive.

Currently, each sliced package is tied to an archive via the "archive"
property that defaults to the default archive. Drop this link from the
sliced packages to the archives and select the latest available version
of the package from all configured archives.

This commit, in effect, doesn't change the current behavior because we
don't allow archives to define their URLs. It's only preparation for
future commits that will add support for different types of archives.
  • Loading branch information
woky committed Oct 12, 2023
1 parent 19c9af6 commit c551dc3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 74 deletions.
27 changes: 6 additions & 21 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
// Release is a collection of package slices targeting a particular
// distribution version.
type Release struct {
Path string
Packages map[string]*Package
Archives map[string]*Archive
DefaultArchive string
Path string
Packages map[string]*Package
Archives map[string]*Archive
}

// Archive is the location from which binary packages are obtained.
Expand All @@ -34,10 +33,9 @@ type Archive struct {

// Package holds a collection of slices that represent parts of themselves.
type Package struct {
Name string
Path string
Archive string
Slices map[string]*Slice
Name string
Path string
Slices map[string]*Slice
}

// Slice holds the details about a package slice.
Expand Down Expand Up @@ -306,9 +304,6 @@ func readSlices(release *Release, baseDir, dirName string) error {
if err != nil {
return err
}
if pkg.Archive == "" {
pkg.Archive = release.DefaultArchive
}

release.Packages[pkg.Name] = pkg
}
Expand All @@ -326,7 +321,6 @@ type yamlArchive struct {
Version string `yaml:"version"`
Suites []string `yaml:"suites"`
Components []string `yaml:"components"`
Default bool `yaml:"default"`
}

type yamlPackage struct {
Expand Down Expand Up @@ -428,14 +422,6 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) {
if len(details.Components) == 0 {
return nil, fmt.Errorf("%s: archive %q missing components field", fileName, archiveName)
}
if len(yamlVar.Archives) == 1 {
details.Default = true
} else if details.Default && release.DefaultArchive != "" {
return nil, fmt.Errorf("%s: more than one default archive: %s, %s", fileName, release.DefaultArchive, archiveName)
}
if details.Default {
release.DefaultArchive = archiveName
}
release.Archives[archiveName] = &Archive{
Name: archiveName,
Version: details.Version,
Expand Down Expand Up @@ -464,7 +450,6 @@ func parsePackage(baseDir, pkgName, pkgPath string, data []byte) (*Package, erro
if yamlPkg.Name != pkg.Name {
return nil, fmt.Errorf("%s: filename and 'package' field (%q) disagree", pkgPath, yamlPkg.Name)
}
pkg.Archive = yamlPkg.Archive

zeroPath := yamlPath{}
for sliceName, yamlSlice := range yamlPkg.Slices {
Expand Down
61 changes: 18 additions & 43 deletions internal/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -72,10 +70,9 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{},
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{},
},
},
},
Expand Down Expand Up @@ -103,8 +100,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -115,9 +110,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice1": {
Package: "mypkg",
Expand Down Expand Up @@ -164,8 +158,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -176,9 +168,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice1": {
Package: "mypkg",
Expand Down Expand Up @@ -424,8 +415,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -436,9 +425,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice1": {
Package: "mypkg",
Expand Down Expand Up @@ -638,8 +626,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -650,9 +636,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice": {
Package: "mypkg",
Expand All @@ -677,8 +662,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -689,9 +672,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice": {
Package: "mypkg",
Expand All @@ -717,8 +699,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand All @@ -729,9 +709,8 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "ubuntu",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{
"myslice": {
Package: "mypkg",
Expand All @@ -755,7 +734,6 @@ var setupTests = []setupTest{{
version: 22.04
components: [main, universe]
suites: [jammy]
default: true
bar:
version: 22.04
components: [universe]
Expand All @@ -766,8 +744,6 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
DefaultArchive: "foo",

Archives: map[string]*setup.Archive{
"foo": {
Name: "foo",
Expand All @@ -784,10 +760,9 @@ var setupTests = []setupTest{{
},
Packages: map[string]*setup.Package{
"mypkg": {
Archive: "foo",
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{},
Name: "mypkg",
Path: "slices/mydir/mypkg.yaml",
Slices: map[string]*setup.Slice{},
},
},
},
Expand Down
21 changes: 14 additions & 7 deletions internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func Run(options *RunOptions) error {
syscall.Umask(oldUmask)
}()

release := options.Selection.Release
targetDir := filepath.Clean(options.TargetDir)
targetDirAbs := targetDir
if !filepath.IsAbs(targetDirAbs) {
Expand All @@ -74,15 +73,23 @@ func Run(options *RunOptions) error {
for _, slice := range options.Selection.Slices {
extractPackage := extract[slice.Package]
if extractPackage == nil {
archiveName := release.Packages[slice.Package].Archive
archive := options.Archives[archiveName]
if archive == nil {
return fmt.Errorf("archive %q not defined", archiveName)
var selectedVersion string
var selectedArchive archive.Archive
for _, currentArchive := range options.Archives {
pkgInfo := currentArchive.Info(slice.Package)
if pkgInfo == nil {
continue
}
currentVersion := pkgInfo.Version()
if selectedVersion == "" || deb.CompareVersions(selectedVersion, currentVersion) < 0 {
selectedVersion = currentVersion
selectedArchive = currentArchive
}
}
if !archive.Exists(slice.Package) {
if selectedVersion == "" {
return fmt.Errorf("slice package %q missing from archive", slice.Package)
}
archives[slice.Package] = archive
archives[slice.Package] = selectedArchive
extractPackage = make(map[string][]deb.ExtractInfo)
extract[slice.Package] = extractPackage
}
Expand Down
Loading

0 comments on commit c551dc3

Please sign in to comment.