-
Notifications
You must be signed in to change notification settings - Fork 0
feat: store package support #16
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
base: bin-slices
Are you sure you want to change the base?
Changes from all commits
643a52f
46a353a
dac1ec7
97bfd5b
88d0cc3
bbea434
0f2a6a2
2ee8aa9
fdce477
c728291
fd0534e
885c5d3
ea50c19
0da89bb
4fe25b1
2bb1b3e
e299985
a035161
3cdb869
79c437e
684f809
4a4472c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,13 +21,15 @@ import ( | |
| "github.com/canonical/chisel/internal/manifestutil" | ||
| "github.com/canonical/chisel/internal/scripts" | ||
| "github.com/canonical/chisel/internal/setup" | ||
| "github.com/canonical/chisel/internal/store" | ||
| ) | ||
|
|
||
| const manifestMode fs.FileMode = 0644 | ||
|
|
||
| type RunOptions struct { | ||
| Selection *setup.Selection | ||
| Archives map[string]archive.Archive | ||
| Stores map[string]store.Store | ||
| TargetDir string | ||
| } | ||
|
|
||
|
|
@@ -50,8 +52,8 @@ type pkgSourceInfo struct { | |
| arch string | ||
| kind sourceKind | ||
| archive archive.Archive | ||
| // TODO: add store handle when store support is implemented. | ||
| pkg *setup.Package | ||
| store store.Store | ||
| pkg *setup.Package | ||
| } | ||
|
|
||
| type contentChecker struct { | ||
|
|
@@ -107,7 +109,7 @@ func Run(options *RunOptions) error { | |
| targetDir = filepath.Join(dir, targetDir) | ||
| } | ||
|
|
||
| pkgSources, err := resolvePkgSources(options.Archives, options.Selection) | ||
| pkgSources, err := resolvePkgSources(options.Archives, options.Stores, options.Selection) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -170,19 +172,27 @@ func Run(options *RunOptions) error { | |
| continue | ||
| } | ||
| src := pkgSources[slice.Package] | ||
| // Store packages are distributed as "ar" archives, whose extraction is | ||
| // not yet implemented. Fail until store handling and the "ar" format | ||
| // support are in place. | ||
| var reader io.ReadSeekCloser | ||
| if src.kind == sourceStore { | ||
| return fmt.Errorf("cannot fetch package %q from store: store packages are not yet supported", src.pkg.Name) | ||
| } | ||
| reader, info, err := src.archive.Fetch(src.pkg.RealName) | ||
| if err != nil { | ||
| return err | ||
| // The store channel track is "<default-track>-<store version>", | ||
| // e.g. "3.1-26.10". The version pins the release series. | ||
| // Risk is left unspecified for now; the store applies its default. | ||
| // In the future the risk will optionnaly come from the CLI. | ||
| track := src.pkg.DefaultTrack + "-" + src.store.Options().Version | ||
| reader, _, err = src.store.Fetch(src.pkg.RealName, track, "") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| var info *archive.PackageInfo | ||
| reader, info, err = src.archive.Fetch(src.pkg.RealName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| pkgInfos = append(pkgInfos, info) | ||
| } | ||
| defer reader.Close() | ||
| packages[slice.Package] = reader | ||
| pkgInfos = append(pkgInfos, info) | ||
| } | ||
|
|
||
| // When creating content, record if a path is known and whether they are | ||
|
|
@@ -262,6 +272,12 @@ func Run(options *RunOptions) error { | |
| if reader == nil { | ||
| continue | ||
| } | ||
| src := pkgSources[slice.Package] | ||
| // Store packages are distributed as plain tarballs, whose extraction | ||
| // is not yet implemented. Fail until the format support is in place. | ||
| if src.kind != sourceArchive { | ||
| return fmt.Errorf("cannot extract package %q from store: store packages are not yet supported", src.pkg.RealName) | ||
| } | ||
| err := deb.Extract(reader, &deb.ExtractOptions{ | ||
| Package: slice.Package, | ||
| Extract: extract[slice.Package], | ||
|
|
@@ -516,9 +532,9 @@ func createFile(targetDir, relPath string, pathInfo setup.PathInfo) (*fsutil.Ent | |
| // resolvePkgSources determines the source for each package in the selection. | ||
| // For archive packages it selects the highest priority archive containing the | ||
| // package unless a particular archive is pinned within the slice definition | ||
| // file. For store packages it records the store reference. It returns a map | ||
| // file. For store packages it records the opened store handle. It returns a map | ||
| // of pkgSourceInfo indexed by package names. | ||
| func resolvePkgSources(archives map[string]archive.Archive, selection *setup.Selection) (map[string]*pkgSourceInfo, error) { | ||
| func resolvePkgSources(archives map[string]archive.Archive, stores map[string]store.Store, selection *setup.Selection) (map[string]*pkgSourceInfo, error) { | ||
| sortedArchives := make([]*setup.Archive, 0, len(selection.Release.Archives)) | ||
| for _, archive := range selection.Release.Archives { | ||
| if archive.Priority < 0 { | ||
|
|
@@ -539,10 +555,15 @@ func resolvePkgSources(archives map[string]archive.Archive, selection *setup.Sel | |
| } | ||
| pkg := selection.Release.Packages[s.Package] | ||
| if pkg.Store != "" { | ||
| storeHandle := stores[pkg.Store] | ||
| if storeHandle == nil { | ||
| return nil, fmt.Errorf("internal error: no store handle for store %q", pkg.Store) | ||
| } | ||
| pkgSources[pkg.Name] = &pkgSourceInfo{ | ||
| // TODO: Fill with the live store handle when store support is implemented. | ||
| kind: sourceStore, | ||
| pkg: pkg, | ||
| arch: storeHandle.Options().Arch, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hesitant in #15 because with only that diff it looked weird. However, it looks okay here. You can postpone the refactor for I am not sure the other PR will not get comments about this so it might be helpful to copy the idea of this diff in a comment in canonical/chisel just in case.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I added https://github.com/upils/chisel/pull/15/files#r3479609705 (and did the same on the PR submitted to the main repo) |
||
| kind: sourceStore, | ||
| store: storeHandle, | ||
| pkg: pkg, | ||
| } | ||
|
upils marked this conversation as resolved.
|
||
| continue | ||
| } | ||
|
|
@@ -575,22 +596,5 @@ func resolvePkgSources(archives map[string]archive.Archive, selection *setup.Sel | |
| } | ||
| } | ||
|
|
||
| // Until a store is implemented as a package source there is no proper way to | ||
| // determine the architecture for store packages. | ||
| // So relying on the fact that all packages in a selection share the same architecture, | ||
| // we can borrow it from any archive package that was already resolved. | ||
| var arch string | ||
| for _, src := range pkgSources { | ||
| if src.kind == sourceArchive { | ||
| arch = src.arch | ||
| break | ||
| } | ||
| } | ||
| for _, src := range pkgSources { | ||
| if src.kind == sourceStore { | ||
| src.arch = arch | ||
| } | ||
| } | ||
|
|
||
| return pkgSources, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package store | ||
|
|
||
| import "net/http" | ||
|
|
||
| var ( | ||
| ValidateDownloadURL = validateDownloadURL | ||
| BinStagingEnvVar = binStagingEnvVar | ||
| ) | ||
|
|
||
| func FakeDo(do func(req *http.Request) (*http.Response, error)) (restore func()) { | ||
| _httpDo := httpDo | ||
| _bulkDo := bulkDo | ||
| httpDo = do | ||
| bulkDo = do | ||
| return func() { | ||
| httpDo = _httpDo | ||
| bulkDo = _bulkDo | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| ) | ||
|
|
||
| // Avoid importing the log type information unnecessarily. There's a small cost | ||
| // associated with using an interface rather than the type. Depending on how | ||
| // often the logger is plugged in, it would be worth using the type instead. | ||
| type log_Logger interface { | ||
| Output(calldepth int, s string) error | ||
| } | ||
|
|
||
| var globalLoggerLock sync.Mutex | ||
| var globalLogger log_Logger | ||
| var globalDebug bool | ||
|
|
||
| // Specify the *log.Logger object where log messages should be sent to. | ||
| func SetLogger(logger log_Logger) { | ||
| globalLoggerLock.Lock() | ||
| globalLogger = logger | ||
| globalLoggerLock.Unlock() | ||
| } | ||
|
|
||
| // Enable the delivery of debug messages to the logger. Only meaningful | ||
| // if a logger is also set. | ||
| func SetDebug(debug bool) { | ||
| globalLoggerLock.Lock() | ||
| globalDebug = debug | ||
| globalLoggerLock.Unlock() | ||
| } | ||
|
|
||
| // logf sends to the logger registered via SetLogger the string resulting | ||
| // from running format and args through Sprintf. | ||
| func logf(format string, args ...any) { | ||
| globalLoggerLock.Lock() | ||
| defer globalLoggerLock.Unlock() | ||
| if globalLogger != nil { | ||
| globalLogger.Output(2, fmt.Sprintf(format, args...)) | ||
| } | ||
| } | ||
|
|
||
| // debugf sends to the logger registered via SetLogger the string resulting | ||
| // from running format and args through Sprintf, but only if debugging was | ||
| // enabled via SetDebug. | ||
| func debugf(format string, args ...any) { | ||
| globalLoggerLock.Lock() | ||
| defer globalLoggerLock.Unlock() | ||
| if globalDebug && globalLogger != nil { | ||
| globalLogger.Output(2, fmt.Sprintf(format, args...)) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the comments in the other PR, this distinction makes sense and makes the code simpler in a way. Normally in Go you don't define generics to abstract over a type (i.e. a store) but to abstract over an operation, in this case there are clearly two:
As we discussed in the previous PR, this was contemplated but it turned out to be much more complex AFAIK. I also see the difficulty in Go where the type system is not powerful so it is hard to model fetching where
archive.fetch(name)takes only one args vs three instore.fetch(name, track, risk)for example.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We agree. I could have reworked the
Archiveinterface to have something more generic, used for archives and stores, but in reality they are different enough that 2 different interfaces made more sense.As I go through the complete implementation I may discover that this new interface must be tweaked.