Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support archive priorities #59

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (cmd *cmdCut) Execute(args []string) error {
Suites: archiveInfo.Suites,
Components: archiveInfo.Components,
CacheDir: cache.DefaultDir("chisel"),
Priority: archiveInfo.Priority,
})
if err != nil {
return err
Expand Down
25 changes: 25 additions & 0 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ import (
"github.com/canonical/chisel/internal/deb"
)

type PackageInfo interface {
Name() string
Version() string
Arch() string
SHA256() string
}

type Archive interface {
Options() *Options
Fetch(pkg string) (io.ReadCloser, error)
Info(pkg string) PackageInfo
Exists(pkg string) bool
}

Expand All @@ -26,6 +34,7 @@ type Options struct {
Suites []string
Components []string
CacheDir string
Priority int32
}

func Open(options *Options) (Archive, error) {
Expand Down Expand Up @@ -86,6 +95,22 @@ func (a *ubuntuArchive) Exists(pkg string) bool {
return err == nil
}

type pkgInfo struct{ control.Section }

var _ PackageInfo = pkgInfo{}

func (info pkgInfo) Name() string { return info.Get("Package") }
func (info pkgInfo) Version() string { return info.Get("Version") }
func (info pkgInfo) Arch() string { return info.Get("Architecture") }
func (info pkgInfo) SHA256() string { return info.Get("SHA256") }

func (a *ubuntuArchive) Info(pkg string) PackageInfo {
if section, _, _ := a.selectPackage(pkg); section != nil {
return &pkgInfo{section}
}
return nil
}

func (a *ubuntuArchive) selectPackage(pkg string) (control.Section, *ubuntuIndex, error) {
var selectedVersion string
var selectedSection control.Section
Expand Down
33 changes: 33 additions & 0 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,39 @@ func (s *httpSuite) TestArchiveLabels(c *C) {
c.Assert(err, ErrorMatches, `.*\bno Ubuntu section`)
}

func (s *httpSuite) TestPackageInfo(c *C) {
s.prepareArchive("jammy", "22.04", "amd64", []string{"main", "universe"})

options := archive.Options{
Label: "ubuntu",
Version: "22.04",
Arch: "amd64",
Suites: []string{"jammy"},
Components: []string{"main", "universe"},
CacheDir: c.MkDir(),
}

archive, err := archive.Open(&options)
c.Assert(err, IsNil)

info1 := archive.Info("mypkg1")
c.Assert(info1, NotNil)
c.Assert(info1.Name(), Equals, "mypkg1")
c.Assert(info1.Version(), Equals, "1.1")
c.Assert(info1.Arch(), Equals, "amd64")
c.Assert(info1.SHA256(), Equals, "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05")

info3 := archive.Info("mypkg3")
c.Assert(info3, NotNil)
c.Assert(info3.Name(), Equals, "mypkg3")
c.Assert(info3.Version(), Equals, "1.3")
c.Assert(info3.Arch(), Equals, "amd64")
c.Assert(info3.SHA256(), Equals, "fe377bf13ba1a5cb287cb4e037e6e7321281c929405ae39a72358ef0f5d179aa")

info99 := archive.Info("mypkg99")
c.Assert(info99, IsNil)
}

func read(r io.Reader) string {
data, err := io.ReadAll(r)
if err != nil {
Expand Down
30 changes: 9 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 @@ -30,14 +29,14 @@ type Archive struct {
Version string
Suites []string
Components []string
Priority int32
}

// 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 +305,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 +322,7 @@ type yamlArchive struct {
Version string `yaml:"version"`
Suites []string `yaml:"suites"`
Components []string `yaml:"components"`
Default bool `yaml:"default"`
Priority int32 `yaml:"priority"`
}

type yamlPackage struct {
Expand Down Expand Up @@ -428,19 +424,12 @@ 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,
Suites: details.Suites,
Components: details.Components,
Priority: details.Priority,
}
}

Expand All @@ -464,7 +453,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
85 changes: 42 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,13 +760,36 @@ 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{},
},
},
},
}, {
summary: "Maximum priority",
input: map[string]string{
"chisel.yaml": `
format: chisel-v1
archives:
upper-limit:
version: 1
suites: [main]
components: [main]
priority: 2147483647
lower-limit:
version: 1
suites: [main]
components: [main]
priority: -2147483648
over-limit:
version: 1
suites: [main]
components: [main]
priority: 2147483648
`,
},
relerror: "(?s).*\\bcannot unmarshal !!int `2147483648` into int32\\b.*",
}}

const defaultChiselYaml = `
Expand Down
Loading
Loading