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-08-26 13:02:38 -04:00
|
|
|
func (pm *Manager) enable(p *v2.Plugin, force bool) error {
|
|
|
|
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-10-05 17:02:47 -04:00
|
|
|
p.Lock()
|
|
|
|
p.Restart = true
|
|
|
|
p.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-11-21 12:24:01 -05:00
|
|
|
p.PClient, err = plugins.NewClientWithTimeout("unix://"+filepath.Join(p.RuntimeSourcePath, p.GetSocket()), nil, p.TimeoutInSecs)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err != nil {
|
2016-10-05 17:02:47 -04:00
|
|
|
p.Lock()
|
|
|
|
p.Restart = false
|
|
|
|
p.Unlock()
|
2016-11-16 17:42:46 -05:00
|
|
|
shutdownPlugin(p, pm.containerdClient)
|
2016-05-16 11:50:55 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-11-14 18:07:21 -05:00
|
|
|
func shutdownPlugin(p *v2.Plugin, containerdClient libcontainerd.Client) {
|
|
|
|
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 {
|
|
|
|
case <-p.ExitChan:
|
|
|
|
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-08-26 13:02:38 -04:00
|
|
|
func (pm *Manager) disable(p *v2.Plugin) error {
|
|
|
|
if !p.IsEnabled() {
|
2016-07-25 17:06:27 -04:00
|
|
|
return fmt.Errorf("plugin %s is already disabled", p.Name())
|
|
|
|
}
|
2016-10-05 17:02:47 -04:00
|
|
|
p.Lock()
|
|
|
|
p.Restart = false
|
|
|
|
p.Unlock()
|
2016-11-14 18:07:21 -05:00
|
|
|
|
|
|
|
shutdownPlugin(p, 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 {
|
|
|
|
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-10-05 17:02:47 -04:00
|
|
|
p.Lock()
|
2016-08-26 13:02:38 -04:00
|
|
|
p.ExitChan = make(chan bool)
|
2016-10-05 17:02:47 -04:00
|
|
|
p.Restart = false
|
|
|
|
p.Unlock()
|
2016-11-14 18:07:21 -05:00
|
|
|
shutdownPlugin(p, pm.containerdClient)
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|