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

feat: support multiple archives with "priority" field #160

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions cmd/chisel/cmd_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func makeSamplePackage(pkg string, slices []string) *setup.Package {
}

var sampleRelease = &setup.Release{
DefaultArchive: "ubuntu",

Archives: map[string]*setup.Archive{
"ubuntu": {
Name: "ubuntu",
Expand Down
8 changes: 0 additions & 8 deletions cmd/chisel/cmd_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg1_myslice1"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand All @@ -37,7 +36,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg2"},
stdout: `
package: mypkg2
archive: ubuntu
slices:
myslice:
contents:
Expand All @@ -49,7 +47,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg1_myslice2", "mypkg1_myslice1"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand All @@ -65,7 +62,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg1_myslice1", "mypkg2", "mypkg1_myslice2"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand All @@ -76,7 +72,6 @@ var infoTests = []infoTest{{
- mypkg2_myslice
---
package: mypkg2
archive: ubuntu
slices:
myslice:
contents:
Expand All @@ -88,7 +83,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg1_myslice1", "mypkg1"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand All @@ -104,7 +98,6 @@ var infoTests = []infoTest{{
query: []string{"mypkg1_myslice1", "mypkg1_myslice1", "mypkg1_myslice1"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand All @@ -121,7 +114,6 @@ var infoTests = []infoTest{{
query: []string{"foo", "mypkg1_myslice1", "bar_foo"},
stdout: `
package: mypkg1
archive: ubuntu
slices:
myslice1:
contents:
Expand Down
52 changes: 36 additions & 16 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,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 @@ -33,6 +32,7 @@ type Archive struct {
Version string
Suites []string
Components []string
Priority int
PubKeys []*packet.PublicKey
}

Expand Down Expand Up @@ -229,6 +229,28 @@ func (r *Release) validate() error {
return err
}

// Check for archive priority conflicts.
priorities := make(map[int]*Archive)
for _, archive := range r.Archives {
if old, ok := priorities[archive.Priority]; ok {
if old.Name > archive.Name {
archive, old = old, archive
}
return fmt.Errorf("chisel.yaml: archives %q and %q have the same priority value of %d", old.Name, archive.Name, archive.Priority)
}
priorities[archive.Priority] = archive
}

// Check that archives pinned in packages are defined.
for _, pkg := range r.Packages {
if pkg.Archive == "" {
continue
}
if _, ok := r.Archives[pkg.Archive]; !ok {
return fmt.Errorf("%s: package refers to undefined archive %q", pkg.Path, pkg.Archive)
}
}

return nil
}

Expand Down Expand Up @@ -355,9 +377,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 @@ -370,11 +389,16 @@ type yamlRelease struct {
PubKeys map[string]yamlPubKey `yaml:"public-keys"`
}

const (
MaxArchivePriority = 1000
MinArchivePriority = -1000
)

type yamlArchive struct {
Version string `yaml:"version"`
Suites []string `yaml:"suites"`
Components []string `yaml:"components"`
Default bool `yaml:"default"`
Priority int `yaml:"priority"`
PubKeys []string `yaml:"public-keys"`
}

Expand Down Expand Up @@ -534,14 +558,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
}
if len(details.PubKeys) == 0 {
return nil, fmt.Errorf("%s: archive %q missing public-keys field", fileName, archiveName)
}
Expand All @@ -553,11 +569,15 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) {
}
archiveKeys = append(archiveKeys, key)
}
if details.Priority > MaxArchivePriority || details.Priority < MinArchivePriority {
return nil, fmt.Errorf("%s: archive %q has invalid priority value of %d", fileName, archiveName, details.Priority)
}
release.Archives[archiveName] = &Archive{
Name: archiveName,
Version: details.Version,
Suites: details.Suites,
Components: details.Components,
Priority: details.Priority,
PubKeys: archiveKeys,
}
}
Expand Down
Loading
Loading