2016-05-16 11:50:55 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-08-26 13:02:38 -04:00
|
|
|
"github.com/docker/docker/plugin/store"
|
|
|
|
"github.com/docker/docker/plugin/v2"
|
2016-05-16 11:50:55 -04:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
2016-08-24 15:42:37 -04:00
|
|
|
var (
|
|
|
|
manager *Manager
|
|
|
|
)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
func (pm *Manager) restorePlugin(p *v2.Plugin) error {
|
|
|
|
p.RuntimeSourcePath = filepath.Join(pm.runRoot, p.GetID())
|
|
|
|
if p.IsEnabled() {
|
2016-06-15 13:39:33 -04:00
|
|
|
return pm.restore(p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 11:02:12 -04:00
|
|
|
type eventLogger func(id, name, action string)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
|
|
|
// Manager controls the plugin subsystem.
|
|
|
|
type Manager struct {
|
2016-07-18 11:02:12 -04:00
|
|
|
libRoot string
|
|
|
|
runRoot string
|
2016-09-07 20:01:10 -04:00
|
|
|
pluginStore *store.Store
|
2016-07-18 11:02:12 -04:00
|
|
|
containerdClient libcontainerd.Client
|
|
|
|
registryService registry.Service
|
|
|
|
liveRestore bool
|
|
|
|
pluginEventLogger eventLogger
|
2016-05-16 11:50:55 -04: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-07 20:01:10 -04:00
|
|
|
func Init(root string, ps *store.Store, remote libcontainerd.Remote, rs registry.Service, liveRestore bool, evL eventLogger) (err error) {
|
2016-05-16 11:50:55 -04:00
|
|
|
if manager != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
root = filepath.Join(root, "plugins")
|
|
|
|
manager = &Manager{
|
2016-07-18 11:02:12 -04:00
|
|
|
libRoot: root,
|
|
|
|
runRoot: "/run/docker",
|
2016-09-07 20:01:10 -04:00
|
|
|
pluginStore: ps,
|
2016-07-18 11:02:12 -04:00
|
|
|
registryService: rs,
|
|
|
|
liveRestore: liveRestore,
|
|
|
|
pluginEventLogger: evL,
|
2016-05-16 11:50:55 -04:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(manager.runRoot, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
manager.containerdClient, err = remote.Client(manager)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-15 13:39:33 -04:00
|
|
|
if err := manager.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:27:36 -04:00
|
|
|
// StateChanged updates plugin internals using libcontainerd events.
|
2016-05-16 11:50:55 -04:00
|
|
|
func (pm *Manager) StateChanged(id string, e libcontainerd.StateInfo) error {
|
2016-07-03 21:38:06 -04:00
|
|
|
logrus.Debugf("plugin state changed %s %#v", id, e)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-07-01 14:36:11 -04:00
|
|
|
switch e.State {
|
|
|
|
case libcontainerd.StateExit:
|
2016-10-05 17:02:47 -04:00
|
|
|
p, err := pm.pluginStore.GetByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.RLock()
|
|
|
|
if p.ExitChan != nil {
|
2016-08-26 13:02:38 -04:00
|
|
|
close(p.ExitChan)
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
2016-10-05 17:02:47 -04:00
|
|
|
restart := p.Restart
|
|
|
|
p.RUnlock()
|
|
|
|
p.RemoveFromDisk()
|
|
|
|
if restart {
|
|
|
|
pm.enable(p, true)
|
|
|
|
}
|
2016-07-01 14:36:11 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) init() error {
|
|
|
|
dt, err := os.Open(filepath.Join(pm.libRoot, "plugins.json"))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer dt.Close()
|
2016-06-15 13:39:33 -04:00
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
plugins := make(map[string]*v2.Plugin)
|
|
|
|
if err := json.NewDecoder(dt).Decode(&plugins); err != nil {
|
2016-05-16 11:50:55 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-08-26 13:02:38 -04:00
|
|
|
pm.pluginStore.SetAll(plugins)
|
2016-05-16 11:50:55 -04:00
|
|
|
|
2016-06-15 13:39:33 -04:00
|
|
|
var group sync.WaitGroup
|
2016-08-26 13:02:38 -04:00
|
|
|
group.Add(len(plugins))
|
|
|
|
for _, p := range plugins {
|
|
|
|
go func(p *v2.Plugin) {
|
2016-06-15 13:39:33 -04:00
|
|
|
defer group.Done()
|
|
|
|
if err := pm.restorePlugin(p); err != nil {
|
2016-07-22 11:24:54 -04:00
|
|
|
logrus.Errorf("failed to restore plugin '%s': %s", p.Name(), err)
|
2016-06-15 13:39:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-26 13:02:38 -04:00
|
|
|
pm.pluginStore.Add(p)
|
|
|
|
requiresManualRestore := !pm.liveRestore && p.IsEnabled()
|
2016-06-15 13:39:33 -04:00
|
|
|
|
|
|
|
if requiresManualRestore {
|
|
|
|
// if liveRestore is not enabled, the plugin will be stopped now so we should enable it
|
2016-08-09 14:49:28 -04:00
|
|
|
if err := pm.enable(p, true); err != nil {
|
2016-07-22 11:24:54 -04:00
|
|
|
logrus.Errorf("failed to enable plugin '%s': %s", p.Name(), err)
|
2016-06-15 13:39:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(p)
|
|
|
|
}
|
2016-08-03 19:22:12 -04:00
|
|
|
group.Wait()
|
2016-05-16 11:50:55 -04: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 17:39:52 -04: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
|
|
|
|
}
|
|
|
|
}
|