2016-10-06 10:09:54 -04:00
|
|
|
// +build linux
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-07-25 17:06:27 -04:00
|
|
|
"fmt"
|
2016-05-16 11:50:55 -04:00
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
2016-07-01 14:36:11 -04:00
|
|
|
"time"
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-11-14 18:07:21 -05:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/oci"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2016-08-26 13:02:38 -04:00
|
|
|
"github.com/docker/docker/plugin/v2"
|
2016-10-06 10:09:54 -04:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-05-16 11:50:55 -04:00
|
|
|
)
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
func (pm *Manager) enable(p *v2.Plugin, c *controller, force bool) error {
|
2016-08-26 13:02:38 -04:00
|
|
|
if p.IsEnabled() && !force {
|
2016-07-25 17:06:27 -04:00
|
|
|
return fmt.Errorf("plugin %s is already enabled", p.Name())
|
|
|
|
}
|
2016-08-26 13:02:38 -04:00
|
|
|
spec, err := p.InitSpec(oci.DefaultSpec(), pm.libRoot)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 14:36:56 -05:00
|
|
|
|
|
|
|
c.restart = true
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := pm.containerdClient.Create(p.GetID(), "", "", specs.Spec(*spec), attachToLog(p.GetID())); err != nil {
|
2016-05-16 11:50:55 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
client, err := plugins.NewClientWithTimeout("unix://"+filepath.Join(p.GetRuntimeSourcePath(), p.GetSocket()), nil, c.timeoutInSecs)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-05-16 11:50:55 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
p.SetPClient(client)
|
2016-08-26 13:02:38 -04:00
|
|
|
pm.pluginStore.SetState(p, true)
|
2016-09-06 17:30:55 -04:00
|
|
|
pm.pluginStore.CallHandler(p)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
func (pm *Manager) restore(p *v2.Plugin) error {
|
2016-10-17 17:39:52 -04:00
|
|
|
return pm.containerdClient.Restore(p.GetID(), attachToLog(p.GetID()))
|
2016-06-15 13:39:33 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
func shutdownPlugin(p *v2.Plugin, c *controller, containerdClient libcontainerd.Client) {
|
2016-11-14 18:07:21 -05:00
|
|
|
pluginID := p.GetID()
|
|
|
|
|
|
|
|
err := containerdClient.Signal(pluginID, int(syscall.SIGTERM))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Sending SIGTERM to plugin failed with error: %v", err)
|
|
|
|
} else {
|
|
|
|
select {
|
2016-12-01 14:36:56 -05:00
|
|
|
case <-c.exitChan:
|
2016-11-14 18:07:21 -05:00
|
|
|
logrus.Debug("Clean shutdown of plugin")
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
logrus.Debug("Force shutdown plugin")
|
|
|
|
if err := containerdClient.Signal(pluginID, int(syscall.SIGKILL)); err != nil {
|
|
|
|
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
func (pm *Manager) disable(p *v2.Plugin, c *controller) error {
|
2016-08-26 13:02:38 -04:00
|
|
|
if !p.IsEnabled() {
|
2016-07-25 17:06:27 -04:00
|
|
|
return fmt.Errorf("plugin %s is already disabled", p.Name())
|
|
|
|
}
|
2016-11-14 18:07:21 -05:00
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-08-26 13:02:38 -04:00
|
|
|
pm.pluginStore.SetState(p, false)
|
2016-05-16 11:50:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-01 14:36:11 -04:00
|
|
|
|
|
|
|
// Shutdown stops all plugins and called during daemon shutdown.
|
|
|
|
func (pm *Manager) Shutdown() {
|
2016-08-26 13:02:38 -04:00
|
|
|
plugins := pm.pluginStore.GetAll()
|
|
|
|
for _, p := range plugins {
|
2016-12-01 14:36:56 -05:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
if pm.liveRestore && p.IsEnabled() {
|
|
|
|
logrus.Debug("Plugin active when liveRestore is set, skipping shutdown")
|
2016-07-22 11:53:26 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-08-26 13:02:38 -04:00
|
|
|
if pm.containerdClient != nil && p.IsEnabled() {
|
2016-12-01 14:36:56 -05:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|