Skip to content

Conversation

@kyrofa
Copy link
Contributor

@kyrofa kyrofa commented Aug 22, 2016

Make some more progress on LP: #1586465 by adding some more logic and subcommands to snapctl. Stub out initial set command. This should allow other subcommands to be easily added (e.g. for interface hooks).

Add some more logic and subcommands to snapctl. Stub out initial set
command. This should allow other subcommands to be easily added (e.g.
for interface hooks).

Updates: #1586465

Signed-off-by: Kyle Fazzari <kyle@canonical.com>

// Generate a secure, random ID for this handler
idBytes := make([]byte, 32)
_, err := rand.Read(idBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this block for any amount of time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses getrandom(), which by default uses /dev/urandom, which shouldn't block.

Signed-off-by: Kyle Fazzari <kyle@canonical.com>
@kyrofa kyrofa force-pushed the feature/1586465/hookmanager_snapctl branch from c708671 to 4aaded3 Compare August 23, 2016 17:12
…kmanager_snapctl

Signed-off-by: Kyle Fazzari <kyle@canonical.com>
state: s,
runner: runner,
repository: newRepository(),
activeHandlers: newHandlerCollection(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/activeHandlers/handlers/ (we don't have any inactive ones)

I've mentioned this in earlier reviews: it doesn't feel like the additional abstraction there is gaining us much. As a thought experiment, try to inline a map and a simple mutex here, and see how much code you need to write here, vs. how much code you drop.

That said, I believe what we really want here is a map of context, not handlers. The reason being that context is a concrete and common implementation, and it would be simple and convenient to introduce a HookHandler method on context to navigate from a context into its handler. Doing that in the opposite direction is boring, because every single handler will need to reimplement the same method to return its context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

kyrofa added 5 commits August 29, 2016 09:47
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
daemon/api.go Outdated
return BadRequest("cannot run snapctl: %s", err)
}

stdout, stderr, err := ctlcmd.RunCommand(context, snapctlRequest.Args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one might be just ctlcmd.Run now that we have cmd in the package name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Signed-off-by: Kyle Fazzari <kyle@canonical.com>

// RunSnapctl requests a snapctl run for the given options.
func (client *Client) RunSnapctl(options SnapCtlOptions) (stdout, stderr []byte, err error) {
func (client *Client) RunSnapctl(options hookstate.SnapCtlRequest) (stdout, stderr []byte, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options was right on this end, per other similar methods. If the point is reusing the same type, it's okay to keep it as options everywhere.

This should also be received as a pointer, as the other option values in client.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that, as we agreed before, when reusing such types we should have them inside client rather than inside the other internal package, so it's obvious when we're adding code that makes no sense for the client end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this back to client.SnapCtlOptions, and used that throughout for consistency.

@niemeyer
Copy link
Contributor

LGTM with these last few points sorted.

kyrofa added 5 commits August 29, 2016 14:34
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
Signed-off-by: Kyle Fazzari <kyle@canonical.com>
@niemeyer niemeyer changed the title daemon,overlord: add subcommand handling to snapctl. daemon,overlord: add subcommand handling to snapctl Aug 30, 2016
exit 1
fi

if [[ ! "$output" =~ .*"no context for ID: \"foo\"".* ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spread test still broken here. I'd suggest the more usual:

echo "$output" | grep -q 'no context for ID: "foo"'

No need for ifs or the extra message here. Note that English sentence below is a direct translation of the code, which in this specific case is more clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively, using the bash expression syntax, but simply:

[[ "$output" =~ 'no context for ID: "foo"' ]]

I've just confirmed that it traces properly as well, and actually better than echo+grep.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, and the problem is actually on the former line:

error: cannot run snapctl: cannot run snapctl: no context for ID: "foo"
+ output='Running snapctl. It should be in my PATH and not cause denials.'
+ [[ ! Running snapctl. It should be in my PATH and not cause denials. =~ .*no context for ID: "foo".* ]]

Need something like output="$(snap ... 2>&1 >/dev/null)" to capture only stderr.

Copy link
Contributor Author

@kyrofa kyrofa Aug 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, good catch, thanks! Passing now.


// NewMockHandler returns a new MockHandler.
func NewMockHandler() *MockHandler {
return &MockHandler{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon my ignorance here, but given goes default policy of zeroing values, why explicatively init them here? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're not missing anything, I'm being dumb. Fixed now, thanks!

@mvo5
Copy link
Contributor

mvo5 commented Aug 30, 2016

Looks good, just one question inline

@kyrofa kyrofa merged commit 6a6234a into canonical:master Aug 30, 2016
@kyrofa kyrofa deleted the feature/1586465/hookmanager_snapctl branch August 30, 2016 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants