2016-05-16 15:50:55 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2016-12-12 23:05:53 +00:00
|
|
|
"io/ioutil"
|
2016-05-16 15:50:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-12-12 23:05:53 +00:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2016-12-28 03:38:13 +00:00
|
|
|
"sort"
|
2016-11-22 19:21:34 +00:00
|
|
|
"strings"
|
2016-05-16 15:50:55 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2017-01-11 21:54:52 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/libcontainerd"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
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/v2"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2017-01-07 01:23:18 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-12-12 23:05:53 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-05-16 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
const configFileName = "config.json"
|
|
|
|
const rootFSFileName = "rootfs"
|
|
|
|
|
|
|
|
var validFullID = regexp.MustCompile(`^([a-f0-9]{64})$`)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-08-26 17:02:38 +00:00
|
|
|
func (pm *Manager) restorePlugin(p *v2.Plugin) error {
|
|
|
|
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
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// ManagerConfig defines configuration needed to start new manager.
|
|
|
|
type ManagerConfig struct {
|
|
|
|
Store *Store // remove
|
|
|
|
Executor libcontainerd.Remote
|
|
|
|
RegistryService registry.Service
|
|
|
|
LiveRestoreEnabled bool // TODO: remove
|
|
|
|
LogPluginEvent eventLogger
|
|
|
|
Root string
|
|
|
|
ExecRoot string
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
// Manager controls the plugin subsystem.
|
|
|
|
type Manager struct {
|
2016-12-12 23:05:53 +00:00
|
|
|
config ManagerConfig
|
|
|
|
mu sync.RWMutex // protects cMap
|
|
|
|
muGC sync.RWMutex // protects blobstore deletions
|
|
|
|
cMap map[*v2.Plugin]*controller
|
|
|
|
containerdClient libcontainerd.Client
|
|
|
|
blobStore *basicBlobStore
|
2016-12-01 19:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// pluginRegistryService ensures that all resolved repositories
|
|
|
|
// are of the plugin class.
|
|
|
|
type pluginRegistryService struct {
|
|
|
|
registry.Service
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
func (s pluginRegistryService) ResolveRepository(name reference.Named) (repoInfo *registry.RepositoryInfo, err error) {
|
|
|
|
repoInfo, err = s.Service.ResolveRepository(name)
|
|
|
|
if repoInfo != nil {
|
|
|
|
repoInfo.Class = "plugin"
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
// NewManager returns a new plugin manager.
|
|
|
|
func NewManager(config ManagerConfig) (*Manager, error) {
|
|
|
|
if config.RegistryService != nil {
|
|
|
|
config.RegistryService = pluginRegistryService{config.RegistryService}
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
manager := &Manager{
|
|
|
|
config: config,
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
if err := os.MkdirAll(manager.config.Root, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", manager.config.Root)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(manager.config.ExecRoot, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", manager.config.ExecRoot)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(manager.tmpDir(), 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mkdir %v", manager.tmpDir())
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
manager.containerdClient, err = config.Executor.Client(manager) // todo: move to another struct
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2016-12-12 23:05:53 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create containerd client")
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
manager.blobStore, err = newBasicBlobStore(filepath.Join(manager.config.Root, "storage/blobs"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
manager.cMap = make(map[*v2.Plugin]*controller)
|
2016-12-12 23:05:53 +00:00
|
|
|
if err := manager.reload(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to restore plugins")
|
|
|
|
}
|
|
|
|
return manager, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) tmpDir() string {
|
|
|
|
return filepath.Join(pm.config.Root, "tmp")
|
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-12-12 23:05:53 +00:00
|
|
|
p, err := pm.config.Store.GetV2Plugin(id)
|
2016-10-05 21:02:47 +00:00
|
|
|
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-12-12 23:05:53 +00:00
|
|
|
os.RemoveAll(filepath.Join(pm.config.ExecRoot, id))
|
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)
|
|
|
|
}
|
2017-02-03 04:08:35 +00:00
|
|
|
propRoot := filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
if err := mount.Unmount(propRoot); err != nil {
|
|
|
|
logrus.Warn("Could not unmount %s: %v", propRoot, err)
|
|
|
|
}
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
|
|
|
|
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-12 23:05:53 +00:00
|
|
|
func (pm *Manager) reload() error { // todo: restore
|
|
|
|
dir, err := ioutil.ReadDir(pm.config.Root)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
2016-12-12 23:05:53 +00:00
|
|
|
return errors.Wrapf(err, "failed to read %v", pm.config.Root)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
plugins := make(map[string]*v2.Plugin)
|
2016-12-12 23:05:53 +00:00
|
|
|
for _, v := range dir {
|
|
|
|
if validFullID.MatchString(v.Name()) {
|
|
|
|
p, err := pm.loadPlugin(v.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
plugins[p.GetID()] = p
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
pm.config.Store.SetAll(plugins)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(plugins))
|
2016-08-26 17:02:38 +00:00
|
|
|
for _, p := range plugins {
|
2016-12-12 23:05:53 +00:00
|
|
|
c := &controller{} // todo: remove this
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.cMap[p] = c
|
2016-08-26 17:02:38 +00:00
|
|
|
go func(p *v2.Plugin) {
|
2016-12-12 23:05:53 +00:00
|
|
|
defer wg.Done()
|
2016-06-15 17:39:33 +00:00
|
|
|
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 != "" {
|
2016-12-12 23:05:53 +00:00
|
|
|
p.Rootfs = filepath.Join(pm.config.Root, p.PluginObj.ID, "rootfs")
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 != "" {
|
2017-02-03 04:08:35 +00:00
|
|
|
propRoot := filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount")
|
|
|
|
|
|
|
|
// check if we need to migrate an older propagated mount from before
|
|
|
|
// these mounts were stored outside the plugin rootfs
|
|
|
|
if _, err := os.Stat(propRoot); os.IsNotExist(err) {
|
|
|
|
if _, err := os.Stat(p.PropagatedMount); err == nil {
|
|
|
|
// make sure nothing is mounted here
|
|
|
|
// don't care about errors
|
|
|
|
mount.Unmount(p.PropagatedMount)
|
|
|
|
if err := os.Rename(p.PropagatedMount, propRoot); err != nil {
|
|
|
|
logrus.WithError(err).WithField("dir", propRoot).Error("error migrating propagated mount storage")
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(p.PropagatedMount, 0755); err != nil {
|
|
|
|
logrus.WithError(err).WithField("dir", p.PropagatedMount).Error("error migrating propagated mount storage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(propRoot, 0755); err != nil {
|
|
|
|
logrus.Errorf("failed to create PropagatedMount directory at %s: %v", propRoot, err)
|
|
|
|
}
|
2016-11-22 19:21:34 +00:00
|
|
|
// 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-12-12 23:05:53 +00:00
|
|
|
pm.save(p)
|
|
|
|
requiresManualRestore := !pm.config.LiveRestoreEnabled && 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-12-12 23:05:53 +00:00
|
|
|
wg.Wait()
|
2016-05-16 15:50:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
func (pm *Manager) loadPlugin(id string) (*v2.Plugin, error) {
|
|
|
|
p := filepath.Join(pm.config.Root, id, configFileName)
|
|
|
|
dt, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error reading %v", p)
|
|
|
|
}
|
|
|
|
var plugin v2.Plugin
|
|
|
|
if err := json.Unmarshal(dt, &plugin); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error decoding %v", p)
|
|
|
|
}
|
|
|
|
return &plugin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) save(p *v2.Plugin) error {
|
|
|
|
pluginJSON, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal plugin json")
|
|
|
|
}
|
|
|
|
if err := ioutils.AtomicWriteFile(filepath.Join(pm.config.Root, p.GetID(), configFileName), pluginJSON, 0600); err != nil {
|
2017-01-17 18:27:01 +00:00
|
|
|
return errors.Wrap(err, "failed to write atomically plugin json")
|
2016-12-12 23:05:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC cleans up unrefrenced blobs. This is recommended to run in a goroutine
|
|
|
|
func (pm *Manager) GC() {
|
|
|
|
pm.muGC.Lock()
|
|
|
|
defer pm.muGC.Unlock()
|
|
|
|
|
|
|
|
whitelist := make(map[digest.Digest]struct{})
|
|
|
|
for _, p := range pm.config.Store.GetAll() {
|
|
|
|
whitelist[p.Config] = struct{}{}
|
|
|
|
for _, b := range p.Blobsums {
|
|
|
|
whitelist[b] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pm.blobStore.gc(whitelist)
|
|
|
|
}
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
func validatePrivileges(requiredPrivileges, privileges types.PluginPrivileges) error {
|
2016-12-28 03:38:13 +00:00
|
|
|
if !isEqual(requiredPrivileges, privileges, isEqualPrivilege) {
|
2016-12-12 23:05:53 +00:00
|
|
|
return errors.New("incorrect privileges")
|
|
|
|
}
|
2016-12-28 03:38:13 +00:00
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:38:13 +00:00
|
|
|
func isEqual(arrOne, arrOther types.PluginPrivileges, compare func(x, y types.PluginPrivilege) bool) bool {
|
|
|
|
if len(arrOne) != len(arrOther) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(arrOne)
|
|
|
|
sort.Sort(arrOther)
|
|
|
|
|
|
|
|
for i := 1; i < arrOne.Len(); i++ {
|
|
|
|
if !compare(arrOne[i], arrOther[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEqualPrivilege(a, b types.PluginPrivilege) bool {
|
|
|
|
if a.Name != b.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.DeepEqual(a.Value, b.Value)
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:05:53 +00:00
|
|
|
func configToRootFS(c []byte) (*image.RootFS, error) {
|
|
|
|
var pluginConfig types.PluginConfig
|
|
|
|
if err := json.Unmarshal(c, &pluginConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-27 23:36:12 +00:00
|
|
|
// validation for empty rootfs is in distribution code
|
|
|
|
if pluginConfig.Rootfs == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-12-12 23:05:53 +00:00
|
|
|
|
|
|
|
return rootFSFromPlugin(pluginConfig.Rootfs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootFSFromPlugin(pluginfs *types.PluginConfigRootfs) *image.RootFS {
|
|
|
|
rootFS := image.RootFS{
|
|
|
|
Type: pluginfs.Type,
|
|
|
|
DiffIDs: make([]layer.DiffID, len(pluginfs.DiffIds)),
|
|
|
|
}
|
|
|
|
for i := range pluginfs.DiffIds {
|
|
|
|
rootFS.DiffIDs[i] = layer.DiffID(pluginfs.DiffIds[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rootFS
|
|
|
|
}
|