-
Notifications
You must be signed in to change notification settings - Fork 574
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
many: modify snap run to understand component hooks #13976
many: modify snap run to understand component hooks #13976
Conversation
b06f281
to
0523065
Compare
85f919d
to
6878d5b
Compare
6878d5b
to
4e1002a
Compare
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #13976 +/- ##
==========================================
- Coverage 78.86% 78.71% -0.15%
==========================================
Files 1043 1050 +7
Lines 134595 137490 +2895
==========================================
+ Hits 106144 108232 +2088
- Misses 21837 22476 +639
- Partials 6614 6782 +168
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
fe158d1
to
02b71cf
Compare
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.
did a first pass, bunch of questions
snap/info_test.go
Outdated
|
||
// TODO: fix this annoying import cycle issue | ||
container := func(p string) (snap.Container, error) { | ||
return snapdir.New(p), nil |
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.
you can use a hook from snapdir into snap I suppose to fix this?
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.
Addressed in 1e559b9, first timing adding one of those, so might be wrong.
snap/info.go
Outdated
|
||
revision, err := ParseRevision(rev) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot read revision %s: %s", rev, err) |
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.
s/read/parse/ ? also this error message doesn't seem to contain a lot of context
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.
Addressed in 4b1b48a.
cmd/snap/cmd_run.go
Outdated
if hook != nil { | ||
snapName, _ = snap.SplitSnapComponentInstanceName(snapTarget) | ||
} else { | ||
snapName, _ = snap.SplitSnapApp(snapTarget) | ||
} |
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.
I don't understand the if here ? do we need to make the difference if snap.SplitSnapComponentInstanceName works in all cases?
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.
I think this was handled better by the refactor in the following commit.
cmd/snap/cmd_run.go
Outdated
snapConfine, err := snapdHelperPath("snap-confine") | ||
if err != nil { | ||
return err | ||
} | ||
if !osutil.FileExists(snapConfine) { | ||
if hook != nil { | ||
logger.Noticef("WARNING: skipping running hook %q of snap %q: missing snap-confine", hook.Name, info.InstanceName()) | ||
if runner.Hook() != nil { |
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.
have a IsHook predicate as well instead?
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.
Added in bfc4a0d.
cmd/snap-exec/main.go
Outdated
} | ||
|
||
return execApp(snapApp, revision, opts.Command, extraArgs) | ||
return execApp(snapName, revision, opts.Command, extraArgs) |
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.
I would have expected snapTarget here?? missing tests or am I confused?
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.
Addressed in 79aad91. I moved the parsing of the target into the execHook
and execApp
functions themselves, to hopefully make things a bit easier to read.
1e559b9
to
b099df9
Compare
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.
Couple of comments below
snap/info.go
Outdated
@@ -1585,6 +1585,32 @@ func ReadCurrentInfo(snapName string) (*Info, error) { | |||
return ReadInfo(snapName, &SideInfo{Revision: revision}) | |||
} | |||
|
|||
func ReadCurrentComponentInfo(component string, info *Info, container func(string) (Container, error)) (*ComponentInfo, error) { | |||
link := filepath.Join(ComponentsBaseDir(info.InstanceName()), info.Revision.String(), component) |
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.
This could use snap.ComponentLinkPath()
now that #14060 has been merged.
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.
That function takes a ContainerPlaceInfo
, which will be a componentPlaceInfo
. That type's constructor expects a revision, which we don't have here.
snap.ComponentLinkPath()
could just take a the component's name, which is the only thing that is used here:
func ComponentLinkPath(cpi ContainerPlaceInfo, snapRev Revision) string {
instanceName, compName, _ := strings.Cut(cpi.ContainerName(), "+")
compBase := ComponentsBaseDir(instanceName)
return filepath.Join(compBase, snapRev.String(), compName)
}
You fine with me making that change, so that I can use the function here?
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.
Sure, that is fine
cmd/snap/cmd_run.go
Outdated
// TODO: we need to figure out how to get the component revision to set | ||
// the environment variables that we provide to the hook | ||
componentRevision := snap.Revision{} | ||
_ = componentRevision |
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.
Maybe we need a --comprev
argument as we are using -r
for the snap revision for hooks, and use it in runHookAndWait
. Not sure though why we do that instead of reading the snap information as in the non-hook case, probably @pedronis knows.
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.
there was the idea to run hooks for the non-current revisions but I'm not sure it can work in practice. maybe we should revisit this
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.
Sorry, this TODO is old and I forgot to remove it. We are able to get the revision of the component ever since the change that @alfonsosanchezbeato made with the component symlinks. Although yes, I'm not sure what to do with running component hooks for revisions of a component that is not the current one. I'm not sure how that would work? What is the use case for that?
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.
thanks for the changes, some questions
cmd/snap/cmd_run.go
Outdated
// TODO: we need to figure out how to get the component revision to set | ||
// the environment variables that we provide to the hook | ||
componentRevision := snap.Revision{} | ||
_ = componentRevision |
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.
there was the idea to run hooks for the non-current revisions but I'm not sure it can work in practice. maybe we should revisit this
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.
thank you, couple small comments
snap/component.go
Outdated
func ComponentLinkPath(cpi ContainerPlaceInfo, snapRev Revision) string { | ||
instanceName, compName, _ := strings.Cut(cpi.ContainerName(), "+") | ||
func ComponentLinkPath(cref naming.ComponentRef, snapRev Revision) string { | ||
instanceName, compName, _ := strings.Cut(cref.String(), "+") |
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.
do we need the Cut? we could use cref.SnapName and cref.ComponentName, no?
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.
Whoops, that was part of the motivation for this change.
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.
Actually, I'm realizing now that this change might be wrong. I believe naming.ComponentRef
shouldn't ever carry a snap's instance name, just the regular name? @alfonsosanchezbeato is that correct?
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.
That's correct, it should carry the snap name, not the instance name in principle.
@@ -211,3 +211,11 @@ func (s *SnapDir) ListDir(path string) ([]string, error) { | |||
func (s *SnapDir) Unpack(src, dstDir string) error { | |||
return fmt.Errorf("unpack is not supported with snaps of type snapdir") | |||
} | |||
|
|||
func NewContainerFromDir(path string) snap.Container { |
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.
probably want a doc comment on this
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.
LGTM, thanks for the changes
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.
thanks, some small comments and a nitpick
snap/info.go
Outdated
// able to set the revision of the component. we create it so that we can | ||
// use ComponentLinkPath, which doesn't use the revision. | ||
cpi := MinimalComponentContainerPlaceInfo(component, Revision{}, info.InstanceName()) | ||
|
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.
nitpick: I would leave this blank line
link := filepath.Join(ComponentsBaseDir(info.InstanceName()), info.Revision.String(), component) | ||
// NOTE: creating this here is a bit of a hack, since we aren't actually | ||
// able to set the revision of the component. we create it so that we can | ||
// use ComponentLinkPath, which doesn't use the revision. |
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.
does the ComponentLinkPath doc mention is not consuming the revision?
snap/info.go
Outdated
@@ -1594,7 +1594,12 @@ var NewContainerFromDir func(snapName string) Container = func(snapName string) | |||
// ReadCurrentComponentInfo reads the ComponentInfo for the currently linked | |||
// revision of the given component associated with the given snap. | |||
func ReadCurrentComponentInfo(component string, info *Info) (*ComponentInfo, error) { | |||
link := filepath.Join(ComponentsBaseDir(info.InstanceName()), info.Revision.String(), component) | |||
// NOTE: creating this here is a bit of a hack, since we aren't actually |
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.
s/NOTE/TODO, when we have a bit more time or other use cases we should reconsider this
…urrent revision of a component for a snap revision
…resent snap hooks, component hooks, and apps This commit doesn't need to be here, and things will work without it. But things were getting a bit complicated in runSnapConfine with arguments that represented different things based on what we were running.
352811e
to
715fed6
Compare
This PR enables the "snap run" command to run component hooks. It modifies the meaning of the "--hook" parameter a bit to allow snapd to run component hooks like so: