2016-05-16 15:50:55 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-11-22 19:21:34 +00:00
|
|
|
"strings"
|
2016-05-16 15:50:55 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-11-22 19:21:34 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-08-26 17:02:38 +00:00
|
|
|
"github.com/docker/docker/plugin/store"
|
|
|
|
"github.com/docker/docker/plugin/v2"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
2016-08-24 19:42:37 +00:00
|
|
|
var (
|
|
|
|
manager *Manager
|
|
|
|
)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
func (pm *Manager) restorePlugin(p *v2.Plugin) error {
|
2016-12-01 19:36:56 +00:00
|
|
|
p.Restore(pm.runRoot)
|
2016-08-26 17:02:38 +00:00
|
|
|
if p.IsEnabled() {
|
2016-06-15 17:39:33 +00:00
|
|
|
return pm.restore(p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 15:02:12 +00:00
|
|
|
type eventLogger func(id, name, action string)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
|
|
|
// Manager controls the plugin subsystem.
|
|
|
|
type Manager struct {
|
2016-07-18 15:02:12 +00:00
|
|
|
libRoot string
|
|
|
|
runRoot string
|
2016-09-08 00:01:10 +00:00
|
|
|
pluginStore *store.Store
|
2016-07-18 15:02:12 +00:00
|
|
|
containerdClient libcontainerd.Client
|
|
|
|
registryService registry.Service
|
|
|
|
liveRestore bool
|
|
|
|
pluginEventLogger eventLogger
|
2016-12-01 19:36:56 +00:00
|
|
|
mu sync.RWMutex // protects cMap
|
|
|
|
cMap map[*v2.Plugin]*controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// controller represents the manager's control on a plugin.
|
|
|
|
type controller struct {
|
|
|
|
restart bool
|
|
|
|
exitChan chan bool
|
|
|
|
timeoutInSecs int
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetManager returns the singleton plugin Manager
|
|
|
|
func GetManager() *Manager {
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init (was NewManager) instantiates the singleton Manager.
|
|
|
|
// TODO: revert this to NewManager once we get rid of all the singletons.
|
2016-09-08 00:01:10 +00:00
|
|
|
func Init(root string, ps *store.Store, remote libcontainerd.Remote, rs registry.Service, liveRestore bool, evL eventLogger) (err error) {
|
2016-05-16 15:50:55 +00:00
|
|
|
if manager != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
root = filepath.Join(root, "plugins")
|
|
|
|
manager = &Manager{
|
2016-07-18 15:02:12 +00:00
|
|
|
libRoot: root,
|
2016-11-22 19:21:34 +00:00
|
|
|
runRoot: "/run/docker/plugins",
|
2016-09-08 00:01:10 +00:00
|
|
|
pluginStore: ps,
|
2016-07-18 15:02:12 +00:00
|
|
|
registryService: rs,
|
|
|
|
liveRestore: liveRestore,
|
|
|
|
pluginEventLogger: evL,
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(manager.runRoot, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
manager.containerdClient, err = remote.Client(manager)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
manager.cMap = make(map[*v2.Plugin]*controller)
|
2016-12-14 21:16:12 +00:00
|
|
|
return manager.reload()
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 16:27:36 +00:00
|
|
|
// StateChanged updates plugin internals using libcontainerd events.
|
2016-05-16 15:50:55 +00:00
|
|
|
func (pm *Manager) StateChanged(id string, e libcontainerd.StateInfo) error {
|
2016-07-04 01:38:06 +00:00
|
|
|
logrus.Debugf("plugin state changed %s %#v", id, e)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-07-01 18:36:11 +00:00
|
|
|
switch e.State {
|
|
|
|
case libcontainerd.StateExit:
|
2016-10-05 21:02:47 +00:00
|
|
|
p, err := pm.pluginStore.GetByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
|
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
|
|
|
|
if c.exitChan != nil {
|
|
|
|
close(c.exitChan)
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
restart := c.restart
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-10-05 21:02:47 +00:00
|
|
|
p.RemoveFromDisk()
|
2016-11-22 19:21:34 +00:00
|
|
|
|
|
|
|
if p.PropagatedMount != "" {
|
|
|
|
if err := mount.Unmount(p.PropagatedMount); err != nil {
|
|
|
|
logrus.Warnf("Could not unmount %s: %v", p.PropagatedMount, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:02:47 +00:00
|
|
|
if restart {
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.enable(p, c, true)
|
2016-10-05 21:02:47 +00:00
|
|
|
}
|
2016-07-01 18:36:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
// reload is used on daemon restarts to load the manager's state
|
|
|
|
func (pm *Manager) reload() error {
|
2016-05-16 15:50:55 +00:00
|
|
|
dt, err := os.Open(filepath.Join(pm.libRoot, "plugins.json"))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-06-25 03:57:21 +00:00
|
|
|
defer dt.Close()
|
2016-06-15 17:39:33 +00:00
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
plugins := make(map[string]*v2.Plugin)
|
|
|
|
if err := json.NewDecoder(dt).Decode(&plugins); err != nil {
|
2016-05-16 15:50:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
pm.pluginStore.SetAll(plugins)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-06-15 17:39:33 +00:00
|
|
|
var group sync.WaitGroup
|
2016-08-26 17:02:38 +00:00
|
|
|
group.Add(len(plugins))
|
|
|
|
for _, p := range plugins {
|
2016-12-01 19:36:56 +00:00
|
|
|
c := &controller{}
|
|
|
|
pm.cMap[p] = c
|
2016-08-26 17:02:38 +00:00
|
|
|
go func(p *v2.Plugin) {
|
2016-06-15 17:39:33 +00:00
|
|
|
defer group.Done()
|
|
|
|
if err := pm.restorePlugin(p); err != nil {
|
2016-07-22 15:24:54 +00:00
|
|
|
logrus.Errorf("failed to restore plugin '%s': %s", p.Name(), err)
|
2016-06-15 17:39:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.Rootfs != "" {
|
|
|
|
p.Rootfs = filepath.Join(pm.libRoot, p.PluginObj.ID, "rootfs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should only enable rootfs propagation for certain plugin types that need it.
|
|
|
|
for _, typ := range p.PluginObj.Config.Interface.Types {
|
2016-12-10 16:40:01 +00:00
|
|
|
if (typ.Capability == "volumedriver" || typ.Capability == "graphdriver") && typ.Prefix == "docker" && strings.HasPrefix(typ.Version, "1.") {
|
2016-11-22 19:21:34 +00:00
|
|
|
if p.PluginObj.Config.PropagatedMount != "" {
|
|
|
|
// TODO: sanitize PropagatedMount and prevent breakout
|
|
|
|
p.PropagatedMount = filepath.Join(p.Rootfs, p.PluginObj.Config.PropagatedMount)
|
|
|
|
if err := os.MkdirAll(p.PropagatedMount, 0755); err != nil {
|
|
|
|
logrus.Errorf("failed to create PropagatedMount directory at %s: %v", p.PropagatedMount, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 17:42:58 +00:00
|
|
|
pm.pluginStore.Update(p)
|
2016-08-26 17:02:38 +00:00
|
|
|
requiresManualRestore := !pm.liveRestore && p.IsEnabled()
|
2016-06-15 17:39:33 +00:00
|
|
|
|
|
|
|
if requiresManualRestore {
|
|
|
|
// if liveRestore is not enabled, the plugin will be stopped now so we should enable it
|
2016-12-01 19:36:56 +00:00
|
|
|
if err := pm.enable(p, c, true); err != nil {
|
2016-07-22 15:24:54 +00:00
|
|
|
logrus.Errorf("failed to enable plugin '%s': %s", p.Name(), err)
|
2016-06-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(p)
|
|
|
|
}
|
2016-08-03 23:22:12 +00:00
|
|
|
group.Wait()
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logHook struct{ id string }
|
|
|
|
|
|
|
|
func (logHook) Levels() []logrus.Level {
|
|
|
|
return logrus.AllLevels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l logHook) Fire(entry *logrus.Entry) error {
|
|
|
|
entry.Data = logrus.Fields{"plugin": l.id}
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-17 21:39:52 +00:00
|
|
|
|
|
|
|
func attachToLog(id string) func(libcontainerd.IOPipe) error {
|
|
|
|
return func(iop libcontainerd.IOPipe) error {
|
|
|
|
iop.Stdin.Close()
|
|
|
|
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.Hooks.Add(logHook{id})
|
|
|
|
// TODO: cache writer per id
|
|
|
|
w := logger.Writer()
|
|
|
|
go func() {
|
|
|
|
io.Copy(w, iop.Stdout)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
// TODO: update logrus and use logger.WriterLevel
|
|
|
|
io.Copy(w, iop.Stderr)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|