2016-10-06 14:09:54 +00:00
|
|
|
// +build linux
|
2016-05-16 15:50:55 +00:00
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-12-12 23:05:53 +00:00
|
|
|
"encoding/json"
|
2016-07-25 21:06:27 +00:00
|
|
|
"fmt"
|
2016-12-12 23:05:53 +00:00
|
|
|
"os"
|
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"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/daemon/initlayer"
|
2016-11-14 23:07:21 +00:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-11-22 19:21:34 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-08-26 17:02:38 +00:00
|
|
|
"github.com/docker/docker/plugin/v2"
|
2016-10-06 14:09:54 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-05-16 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
func (pm *Manager) enable(p *v2.Plugin, c *controller, force bool) error {
|
2016-12-12 23:05:53 +00:00
|
|
|
p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs")
|
2016-08-26 17:02:38 +00:00
|
|
|
if p.IsEnabled() && !force {
|
2016-07-25 21:06:27 +00:00
|
|
|
return fmt.Errorf("plugin %s is already enabled", p.Name())
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
spec, err := p.InitSpec(pm.config.ExecRoot)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
|
|
|
|
c.restart = true
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.PropagatedMount != "" {
|
|
|
|
if err := mount.MakeRShared(p.PropagatedMount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
if err := initlayer.Setup(filepath.Join(pm.config.Root, p.PluginObj.ID, rootFSFileName), 0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-17 21:39:52 +00:00
|
|
|
if err := pm.containerdClient.Create(p.GetID(), "", "", specs.Spec(*spec), attachToLog(p.GetID())); err != nil {
|
2016-12-12 20:56:44 +00:00
|
|
|
if p.PropagatedMount != "" {
|
|
|
|
if err := mount.Unmount(p.PropagatedMount); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", p.PropagatedMount, err)
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 17:53:10 +00:00
|
|
|
return pm.pluginPostStart(p, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) pluginPostStart(p *v2.Plugin, c *controller) error {
|
2016-12-12 23:05:53 +00:00
|
|
|
client, err := plugins.NewClientWithTimeout("unix://"+filepath.Join(pm.config.ExecRoot, p.GetID(), p.GetSocket()), nil, c.timeoutInSecs)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-05-16 15:50:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
p.SetPClient(client)
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetState(p, true)
|
|
|
|
pm.config.Store.CallHandler(p)
|
|
|
|
|
|
|
|
return pm.save(p)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
func (pm *Manager) restore(p *v2.Plugin) error {
|
2016-12-09 17:53:10 +00:00
|
|
|
if err := pm.containerdClient.Restore(p.GetID(), attachToLog(p.GetID())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
if pm.config.LiveRestoreEnabled {
|
2016-12-09 17:53:10 +00:00
|
|
|
c := &controller{}
|
|
|
|
if pids, _ := pm.containerdClient.GetPidsForContainer(p.GetID()); len(pids) == 0 {
|
|
|
|
// plugin is not running, so follow normal startup procedure
|
|
|
|
return pm.enable(p, c, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.exitChan = make(chan bool)
|
|
|
|
c.restart = true
|
|
|
|
pm.mu.Lock()
|
|
|
|
pm.cMap[p] = c
|
|
|
|
pm.mu.Unlock()
|
|
|
|
return pm.pluginPostStart(p, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-06-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
func shutdownPlugin(p *v2.Plugin, c *controller, containerdClient libcontainerd.Client) {
|
2016-11-14 23:07:21 +00: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 19:36:56 +00:00
|
|
|
case <-c.exitChan:
|
2016-11-14 23:07:21 +00: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 19:36:56 +00:00
|
|
|
func (pm *Manager) disable(p *v2.Plugin, c *controller) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
if !p.IsEnabled() {
|
2016-07-25 21:06:27 +00:00
|
|
|
return fmt.Errorf("plugin %s is already disabled", p.Name())
|
|
|
|
}
|
2016-11-14 23:07:21 +00:00
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetState(p, false)
|
|
|
|
return pm.save(p)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-07-01 18:36:11 +00:00
|
|
|
|
|
|
|
// Shutdown stops all plugins and called during daemon shutdown.
|
|
|
|
func (pm *Manager) Shutdown() {
|
2016-12-12 23:05:53 +00:00
|
|
|
plugins := pm.config.Store.GetAll()
|
2016-08-26 17:02:38 +00:00
|
|
|
for _, p := range plugins {
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
if pm.config.LiveRestoreEnabled && p.IsEnabled() {
|
2016-08-26 17:02:38 +00:00
|
|
|
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() {
|
2016-12-01 19:36:56 +00:00
|
|
|
c.restart = false
|
|
|
|
shutdownPlugin(p, c, pm.containerdClient)
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
// createPlugin creates a new plugin. take lock before calling.
|
|
|
|
func (pm *Manager) createPlugin(name string, configDigest digest.Digest, blobsums []digest.Digest, rootFSDir string, privileges *types.PluginPrivileges) (p *v2.Plugin, err error) {
|
|
|
|
if err := pm.config.Store.validateName(name); err != nil { // todo: this check is wrong. remove store
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configRC, err := pm.blobStore.Get(configDigest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer configRC.Close()
|
|
|
|
|
|
|
|
var config types.PluginConfig
|
|
|
|
dec := json.NewDecoder(configRC)
|
|
|
|
if err := dec.Decode(&config); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to parse config")
|
|
|
|
}
|
|
|
|
if dec.More() {
|
|
|
|
return nil, errors.New("invalid config json")
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredPrivileges, err := computePrivileges(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if privileges != nil {
|
|
|
|
if err := validatePrivileges(requiredPrivileges, *privileges); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = &v2.Plugin{
|
|
|
|
PluginObj: types.Plugin{
|
|
|
|
Name: name,
|
|
|
|
ID: stringid.GenerateRandomID(),
|
|
|
|
Config: config,
|
|
|
|
},
|
|
|
|
Config: configDigest,
|
|
|
|
Blobsums: blobsums,
|
|
|
|
}
|
|
|
|
p.InitEmptySettings()
|
|
|
|
|
|
|
|
pdir := filepath.Join(pm.config.Root, p.PluginObj.ID)
|
|
|
|
if err := os.MkdirAll(pdir, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", pdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(pdir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := os.Rename(rootFSDir, filepath.Join(pdir, rootFSFileName)); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to rename rootfs")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pm.save(p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pm.config.Store.Add(p) // todo: remove
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|