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