Skip to content

Commit

Permalink
Check for plugin state before enable and disable.
Browse files Browse the repository at this point in the history
This prevents unnecessary API call to containerd.

Signed-off-by: Anusha Ragunathan <[email protected]>
  • Loading branch information
anusha-ragunathan committed Jul 26, 2016
1 parent 8f11896 commit b867f6c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
21 changes: 21 additions & 0 deletions integration-cli/docker_cli_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,24 @@ func (s *DockerSuite) TestPluginInstallImage(c *check.C) {
c.Assert(err, checker.NotNil)
c.Assert(out, checker.Contains, "content is not a plugin")
}

func (s *DockerSuite) TestPluginEnableDisableNegative(c *check.C) {
testRequires(c, DaemonIsLinux, ExperimentalDaemon)
out, _, err := dockerCmdWithError("plugin", "install", "--grant-all-permissions", pName)
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Contains, pName)

out, _, err = dockerCmdWithError("plugin", "enable", pName)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Contains, "already enabled")

_, _, err = dockerCmdWithError("plugin", "disable", pName)
c.Assert(err, checker.IsNil)

out, _, err = dockerCmdWithError("plugin", "disable", pName)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Contains, "already disabled")

_, _, err = dockerCmdWithError("plugin", "remove", pName)
c.Assert(err, checker.IsNil)
}
13 changes: 13 additions & 0 deletions plugin/manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package plugin

import (
"fmt"
"os"
"path/filepath"
"syscall"
Expand All @@ -20,19 +21,28 @@ import (
)

func (pm *Manager) enable(p *plugin) error {
if p.P.Active {
return fmt.Errorf("plugin %s is already enabled", p.Name())
}
spec, err := pm.initSpec(p)
if err != nil {
return err
}

p.restartManager = restartmanager.New(container.RestartPolicy{Name: "always"}, 0)
if err := pm.containerdClient.Create(p.P.ID, libcontainerd.Spec(*spec), libcontainerd.WithRestartManager(p.restartManager)); err != nil { // POC-only
if err := p.restartManager.Cancel(); err != nil {
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
}
return err
}

socket := p.P.Manifest.Interface.Socket
p.client, err = plugins.NewClient("unix://"+filepath.Join(p.runtimeSourcePath, socket), nil)
if err != nil {
if err := p.restartManager.Cancel(); err != nil {
logrus.Errorf("enable: restartManager.Cancel failed due to %v", err)
}
return err
}

Expand Down Expand Up @@ -114,6 +124,9 @@ func (pm *Manager) initSpec(p *plugin) (*specs.Spec, error) {
}

func (pm *Manager) disable(p *plugin) error {
if !p.P.Active {
return fmt.Errorf("plugin %s is already disabled", p.Name())
}
if err := p.restartManager.Cancel(); err != nil {
logrus.Error(err)
}
Expand Down

0 comments on commit b867f6c

Please sign in to comment.