Skip to content

Commit

Permalink
Support archive priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
woky committed May 5, 2023
1 parent f065e27 commit 08ce9bd
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 27 deletions.
1 change: 1 addition & 0 deletions cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (cmd *cmdCut) Execute(args []string) error {
Components: archiveInfo.Components,
CacheDir: cache.DefaultDir("chisel"),
Pro: archiveInfo.Pro,
Priority: archiveInfo.Priority,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Options struct {
Components []string
CacheDir string
Pro string
Priority int
}

func Open(options *Options) (Archive, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Archive struct {
Suites []string
Components []string
Pro string
Priority int
}

// Package holds a collection of slices that represent parts of themselves.
Expand Down Expand Up @@ -323,6 +324,7 @@ type yamlArchive struct {
Suites []string `yaml:"suites"`
Components []string `yaml:"components"`
Pro string `yaml:"pro"`
Priority int `yaml:"priority"`
}

type yamlPackage struct {
Expand Down Expand Up @@ -435,6 +437,7 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) {
Suites: details.Suites,
Components: details.Components,
Pro: details.Pro,
Priority: details.Priority,
}
}

Expand Down
22 changes: 11 additions & 11 deletions internal/setup/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-security"}, []string{"main", "other"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-security"}, []string{"main", "other"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -95,7 +95,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -146,7 +146,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -396,7 +396,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -600,7 +600,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -629,7 +629,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -659,7 +659,7 @@ var setupTests = []setupTest{{
`,
},
release: &setup.Release{
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""}},
Archives: map[string]*setup.Archive{"ubuntu": {"ubuntu", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0}},
Packages: map[string]*setup.Package{
"mypkg": {
Name: "mypkg",
Expand Down Expand Up @@ -698,8 +698,8 @@ var setupTests = []setupTest{{
},
release: &setup.Release{
Archives: map[string]*setup.Archive{
"foo": {"foo", "22.04", []string{"jammy"}, []string{"main", "universe"}, ""},
"bar": {"bar", "22.04", []string{"jammy-updates"}, []string{"universe"}, ""},
"foo": {"foo", "22.04", []string{"jammy"}, []string{"main", "universe"}, "", 0},
"bar": {"bar", "22.04", []string{"jammy-updates"}, []string{"universe"}, "", 0},
},
Packages: map[string]*setup.Package{
"mypkg": {
Expand Down Expand Up @@ -731,8 +731,8 @@ var setupTests = []setupTest{{
},
release: &setup.Release{
Archives: map[string]*setup.Archive{
"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-updates", "jammy-security"}, []string{"main", "universe"}, ""},
"ubuntu-fips": {"ubuntu-fips", "22.04", []string{"jammy"}, []string{"main"}, "fips"},
"ubuntu": {"ubuntu", "22.04", []string{"jammy", "jammy-updates", "jammy-security"}, []string{"main", "universe"}, "", 0},
"ubuntu-fips": {"ubuntu-fips", "22.04", []string{"jammy"}, []string{"main"}, "fips", 0},
},
Packages: map[string]*setup.Package{
"mypkg": {
Expand Down
21 changes: 20 additions & 1 deletion internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"bytes"
"fmt"
"io"
"math"
"os"
"path/filepath"
"sort"
"strings"
"syscall"

Expand Down Expand Up @@ -69,13 +71,30 @@ func Run(options *RunOptions) error {
targetDirAbs = filepath.Join(dir, targetDir)
}

orderedArchives := make([]archive.Archive, 0, len(options.Archives))
for _, archive := range options.Archives {
orderedArchives = append(orderedArchives, archive)
}
sort.Slice(orderedArchives, func(a, b int) bool {
prioA := orderedArchives[a].Options().Priority
prioB := orderedArchives[b].Options().Priority
return prioA > prioB
})

// Build information to process the selection.
for _, slice := range options.Selection.Slices {
extractPackage := extract[slice.Package]
if extractPackage == nil {
var selectedVersion string
var selectedArchive archive.Archive
for _, archive := range options.Archives {
currentPrio := math.MaxInt
for _, archive := range orderedArchives {
if prio := archive.Options().Priority; prio < currentPrio {
if selectedVersion != "" {
break
}
currentPrio = prio
}
version := archive.Version(slice.Package)
if version == "" {
continue
Expand Down
118 changes: 103 additions & 15 deletions internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,84 @@ var slicerTests = []slicerTest{{
"/speed/cheetah": "file 0644 e98b0879",
"/speed/ostrich": "file 0644 c8fa2806",
},
}, {
summary: "Pick package from archive with highest priority",
pkgs: []testPackageInfo{
{
name: "hello",
version: "2.0-beta",
archive: "edge",
content: []testutil.TarEntry{
{Header: tar.Header{Name: "./"}},
{Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Edge\n")},
},
},
{
name: "hello",
version: "1.8",
archive: "candidate",
content: []testutil.TarEntry{
{Header: tar.Header{Name: "./"}},
{Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Candidate\n")},
},
},
{
name: "hello",
version: "1.2",
archive: "stable",
content: []testutil.TarEntry{
{Header: tar.Header{Name: "./"}},
{Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Stable\n")},
},
},
{
name: "hello",
version: "1.0",
archive: "obsolete",
content: []testutil.TarEntry{
{Header: tar.Header{Name: "./"}},
{Header: tar.Header{Name: "./hello"}, Content: []byte("Hello, The Obsolete\n")},
},
},
},
release: map[string]string{
"chisel.yaml": `
format: chisel-v1
archives:
edge:
version: 1
suites: [main]
components: [main]
candidate:
version: 1
suites: [main]
components: [main]
priority: 5
stable:
version: 1
suites: [main]
components: [main]
priority: 10
obsolete:
version: 1
suites: [main]
components: [main]
priority: 10
`,
"slices/mydir/hello.yaml": `
package: hello
slices:
all:
contents:
/hello:
`,
},
slices: []setup.SliceKey{
{"hello", "all"},
},
result: map[string]string{
"/hello": "file 0644 b5621b65",
},
}}

const defaultChiselYaml = `
Expand All @@ -685,12 +763,12 @@ type testPackage struct {
}

type testArchive struct {
arch string
pkgs map[string]testPackage
options archive.Options
pkgs map[string]testPackage
}

func (a *testArchive) Options() *archive.Options {
return &archive.Options{Arch: a.arch}
return &a.options
}

func (a *testArchive) Fetch(pkg string) (io.ReadCloser, error) {
Expand Down Expand Up @@ -731,17 +809,28 @@ func (s *S) TestRun(c *C) {
c.Assert(err, IsNil)

archives := map[string]archive.Archive{}
for name, setupArchive := range release.Archives {
archive := &testArchive{
options: archive.Options{
Label: setupArchive.Name,
Version: setupArchive.Version,
Arch: test.arch,
Suites: setupArchive.Suites,
Components: setupArchive.Components,
Pro: setupArchive.Pro,
Priority: setupArchive.Priority,
},
}
if test.pkgs != nil {
archive.pkgs = map[string]testPackage{}
}
archives[name] = archive
}

if test.pkgs != nil {
for _, pkgInfo := range test.pkgs {
archive, ok := archives[pkgInfo.archive].(*testArchive)
if !ok {
archive = &testArchive{
arch: test.arch,
pkgs: map[string]testPackage{},
}
archives[pkgInfo.archive] = archive
}
archive := archives[pkgInfo.archive].(*testArchive)
c.Assert(archive, NotNil)
deb, err := testutil.MakeDeb(pkgInfo.content)
c.Assert(err, IsNil)
archive.pkgs[pkgInfo.name] = testPackage{pkgInfo.version, deb}
Expand All @@ -755,10 +844,9 @@ func (s *S) TestRun(c *C) {
c.Assert(err, IsNil)
pkgs[name] = testPackage{"1", deb}
}
archives["ubuntu"] = &testArchive{
arch: test.arch,
pkgs: pkgs,
}
archive := archives["ubuntu"].(*testArchive)
c.Assert(archive, NotNil)
archive.pkgs = pkgs
}

targetDir := c.MkDir()
Expand Down

0 comments on commit 08ce9bd

Please sign in to comment.