2015-07-25 04:35:07 -04:00
|
|
|
// Package plugins provides structures and helper functions to manage Docker
|
|
|
|
// plugins.
|
|
|
|
//
|
|
|
|
// Docker discovers plugins by looking for them in the plugin directory whenever
|
|
|
|
// a user or container tries to use one by name. UNIX domain socket files must
|
|
|
|
// be located under /run/docker/plugins, whereas spec files can be located
|
|
|
|
// either under /etc/docker/plugins or /usr/lib/docker/plugins. This is handled
|
|
|
|
// by the Registry interface, which lets you list all plugins or get a plugin by
|
|
|
|
// its name if it exists.
|
|
|
|
//
|
|
|
|
// The plugins need to implement an HTTP server and bind this to the UNIX socket
|
|
|
|
// or the address specified in the spec files.
|
|
|
|
// A handshake is send at /Plugin.Activate, and plugins are expected to return
|
2018-10-05 10:32:14 -04:00
|
|
|
// a Manifest with a list of Docker subsystems which this plugin implements.
|
2015-07-25 04:35:07 -04:00
|
|
|
//
|
|
|
|
// In order to use a plugins, you can use the ``Get`` with the name of the
|
|
|
|
// plugin and the subsystem it implements.
|
|
|
|
//
|
|
|
|
// plugin, err := plugins.Get("example", "VolumeDriver")
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf("Error looking up volume plugin example: %v", err)
|
|
|
|
// }
|
2018-02-05 16:05:59 -05:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
2015-08-28 14:55:05 -04:00
|
|
|
"time"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
2018-04-24 21:45:00 -04:00
|
|
|
// ProtocolSchemeHTTPV1 is the name of the protocol used for interacting with plugins using this package.
|
|
|
|
const ProtocolSchemeHTTPV1 = "moby.plugins.http/v1"
|
|
|
|
|
2015-05-14 13:05:39 -04:00
|
|
|
var (
|
2015-07-25 04:35:07 -04:00
|
|
|
// ErrNotImplements is returned if the plugin does not implement the requested driver.
|
2015-05-14 13:05:39 -04:00
|
|
|
ErrNotImplements = errors.New("Plugin does not implement the requested driver")
|
|
|
|
)
|
|
|
|
|
|
|
|
type plugins struct {
|
|
|
|
sync.Mutex
|
|
|
|
plugins map[string]*Plugin
|
|
|
|
}
|
|
|
|
|
2016-10-18 20:34:09 -04:00
|
|
|
type extpointHandlers struct {
|
|
|
|
sync.RWMutex
|
|
|
|
extpointHandlers map[string][]func(string, *Client)
|
|
|
|
}
|
|
|
|
|
2015-05-15 07:07:59 -04:00
|
|
|
var (
|
2016-10-18 20:34:09 -04:00
|
|
|
storage = plugins{plugins: make(map[string]*Plugin)}
|
|
|
|
handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))}
|
2015-05-15 07:07:59 -04:00
|
|
|
)
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Manifest lists what a plugin implements.
|
2015-05-14 13:05:39 -04:00
|
|
|
type Manifest struct {
|
2015-07-25 04:35:07 -04:00
|
|
|
// List of subsystem the plugin implements.
|
2015-05-14 13:05:39 -04:00
|
|
|
Implements []string
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Plugin is the definition of a docker plugin.
|
2015-05-14 13:05:39 -04:00
|
|
|
type Plugin struct {
|
2015-07-25 04:35:07 -04:00
|
|
|
// Name of the plugin
|
2016-05-16 11:50:55 -04:00
|
|
|
name string
|
2015-07-25 04:35:07 -04:00
|
|
|
// Address of the plugin
|
|
|
|
Addr string
|
|
|
|
// TLS configuration of the plugin
|
2016-05-16 11:50:55 -04:00
|
|
|
TLSConfig *tlsconfig.Options
|
2015-07-25 04:35:07 -04:00
|
|
|
// Client attached to the plugin
|
2016-05-16 11:50:55 -04:00
|
|
|
client *Client
|
2015-07-25 04:35:07 -04:00
|
|
|
// Manifest of the plugin (see above)
|
|
|
|
Manifest *Manifest `json:"-"`
|
2015-08-19 01:04:22 -04:00
|
|
|
|
2016-03-23 15:25:15 -04:00
|
|
|
// wait for activation to finish
|
|
|
|
activateWait *sync.Cond
|
2016-12-27 11:07:22 -05:00
|
|
|
// error produced by activation
|
|
|
|
activateErr error
|
|
|
|
// keeps track of callback handlers run against this plugin
|
|
|
|
handlersRun bool
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
// Name returns the name of the plugin.
|
|
|
|
func (p *Plugin) Name() string {
|
|
|
|
return p.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns a ready-to-use plugin client that can be used to communicate with the plugin.
|
|
|
|
func (p *Plugin) Client() *Client {
|
|
|
|
return p.client
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:45:00 -04:00
|
|
|
// Protocol returns the protocol name/version used for plugins in this package.
|
|
|
|
func (p *Plugin) Protocol() string {
|
|
|
|
return ProtocolSchemeHTTPV1
|
|
|
|
}
|
|
|
|
|
2016-09-07 20:01:10 -04:00
|
|
|
// IsV1 returns true for V1 plugins and false otherwise.
|
|
|
|
func (p *Plugin) IsV1() bool {
|
2016-07-18 18:39:27 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
// NewLocalPlugin creates a new local plugin.
|
|
|
|
func NewLocalPlugin(name, addr string) *Plugin {
|
2015-05-27 18:21:18 -04:00
|
|
|
return &Plugin{
|
2016-05-16 11:50:55 -04:00
|
|
|
name: name,
|
|
|
|
Addr: addr,
|
|
|
|
// TODO: change to nil
|
|
|
|
TLSConfig: &tlsconfig.Options{InsecureSkipVerify: true},
|
2016-03-23 15:25:15 -04:00
|
|
|
activateWait: sync.NewCond(&sync.Mutex{}),
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) activate() error {
|
2016-03-23 15:25:15 -04:00
|
|
|
p.activateWait.L.Lock()
|
2016-12-27 11:07:22 -05:00
|
|
|
|
|
|
|
if p.activated() {
|
|
|
|
p.runHandlers()
|
2016-03-23 15:25:15 -04:00
|
|
|
p.activateWait.L.Unlock()
|
|
|
|
return p.activateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
p.activateErr = p.activateWithLock()
|
|
|
|
|
2016-12-27 11:07:22 -05:00
|
|
|
p.runHandlers()
|
2016-03-23 15:25:15 -04:00
|
|
|
p.activateWait.L.Unlock()
|
|
|
|
p.activateWait.Broadcast()
|
|
|
|
return p.activateErr
|
2015-08-19 01:04:22 -04:00
|
|
|
}
|
|
|
|
|
2016-12-27 11:07:22 -05:00
|
|
|
// runHandlers runs the registered handlers for the implemented plugin types
|
|
|
|
// This should only be run after activation, and while the activation lock is held.
|
|
|
|
func (p *Plugin) runHandlers() {
|
|
|
|
if !p.activated() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers.RLock()
|
|
|
|
if !p.handlersRun {
|
|
|
|
for _, iface := range p.Manifest.Implements {
|
|
|
|
hdlrs, handled := handlers.extpointHandlers[iface]
|
|
|
|
if !handled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, handler := range hdlrs {
|
|
|
|
handler(p.name, p.client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.handlersRun = true
|
|
|
|
}
|
|
|
|
handlers.RUnlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// activated returns if the plugin has already been activated.
|
|
|
|
// This should only be called with the activation lock held
|
|
|
|
func (p *Plugin) activated() bool {
|
|
|
|
return p.Manifest != nil
|
|
|
|
}
|
|
|
|
|
2015-08-19 01:04:22 -04:00
|
|
|
func (p *Plugin) activateWithLock() error {
|
2015-05-27 18:21:18 -04:00
|
|
|
c, err := NewClient(p.Addr, p.TLSConfig)
|
2015-05-14 13:05:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
p.client = c
|
2015-05-27 18:21:18 -04:00
|
|
|
|
|
|
|
m := new(Manifest)
|
2016-05-16 11:50:55 -04:00
|
|
|
if err = p.client.Call("Plugin.Activate", nil, m); err != nil {
|
2015-05-27 18:21:18 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
p.Manifest = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 15:25:15 -04:00
|
|
|
func (p *Plugin) waitActive() error {
|
|
|
|
p.activateWait.L.Lock()
|
2017-01-24 11:08:13 -05:00
|
|
|
for !p.activated() && p.activateErr == nil {
|
2016-03-23 15:25:15 -04:00
|
|
|
p.activateWait.Wait()
|
|
|
|
}
|
|
|
|
p.activateWait.L.Unlock()
|
|
|
|
return p.activateErr
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
func (p *Plugin) implements(kind string) bool {
|
2016-12-27 11:07:22 -05:00
|
|
|
if p.Manifest == nil {
|
2016-03-23 15:25:15 -04:00
|
|
|
return false
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
for _, driver := range p.Manifest.Implements {
|
|
|
|
if driver == kind {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:05:39 -04:00
|
|
|
func load(name string) (*Plugin, error) {
|
2015-08-28 14:55:05 -04:00
|
|
|
return loadWithRetry(name, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadWithRetry(name string, retry bool) (*Plugin, error) {
|
2015-06-15 18:35:49 -04:00
|
|
|
registry := newLocalRegistry()
|
2015-08-28 14:55:05 -04:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var retries int
|
|
|
|
for {
|
|
|
|
pl, err := registry.Plugin(name)
|
|
|
|
if err != nil {
|
|
|
|
if !retry {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeOff := backoff(retries)
|
|
|
|
if abort(start, timeOff) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
retries++
|
|
|
|
logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
|
|
|
|
time.Sleep(timeOff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Lock()
|
2017-01-04 15:29:10 -05:00
|
|
|
if pl, exists := storage.plugins[name]; exists {
|
|
|
|
storage.Unlock()
|
|
|
|
return pl, pl.activate()
|
|
|
|
}
|
2015-08-19 01:04:22 -04:00
|
|
|
storage.plugins[name] = pl
|
2015-08-28 14:55:05 -04:00
|
|
|
storage.Unlock()
|
2015-08-19 01:04:22 -04:00
|
|
|
|
2015-08-28 14:55:05 -04:00
|
|
|
err = pl.activate()
|
2015-08-19 01:04:22 -04:00
|
|
|
|
2015-08-28 14:55:05 -04:00
|
|
|
if err != nil {
|
|
|
|
storage.Lock()
|
|
|
|
delete(storage.plugins, name)
|
|
|
|
storage.Unlock()
|
|
|
|
}
|
2015-08-19 01:04:22 -04:00
|
|
|
|
2015-08-28 14:55:05 -04:00
|
|
|
return pl, err
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func get(name string) (*Plugin, error) {
|
|
|
|
storage.Lock()
|
|
|
|
pl, ok := storage.plugins[name]
|
2015-08-19 01:04:22 -04:00
|
|
|
storage.Unlock()
|
2015-05-14 13:05:39 -04:00
|
|
|
if ok {
|
2015-08-19 01:04:22 -04:00
|
|
|
return pl, pl.activate()
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-08-19 01:04:22 -04:00
|
|
|
return load(name)
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Get returns the plugin given the specified name and requested implementation.
|
2015-05-14 13:05:39 -04:00
|
|
|
func Get(name, imp string) (*Plugin, error) {
|
2019-06-26 05:30:29 -04:00
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("Unable to find plugin without name")
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
pl, err := get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-27 11:07:22 -05:00
|
|
|
if err := pl.waitActive(); err == nil && pl.implements(imp) {
|
2015-09-23 16:29:14 -04:00
|
|
|
logrus.Debugf("%s implements: %s", name, imp)
|
|
|
|
return pl, nil
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
return nil, ErrNotImplements
|
|
|
|
}
|
2015-05-15 07:07:59 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Handle adds the specified function to the extpointHandlers.
|
2015-05-15 07:07:59 -04:00
|
|
|
func Handle(iface string, fn func(string, *Client)) {
|
2016-10-18 20:34:09 -04:00
|
|
|
handlers.Lock()
|
|
|
|
hdlrs, ok := handlers.extpointHandlers[iface]
|
2016-10-15 01:40:28 -04:00
|
|
|
if !ok {
|
2016-10-18 20:34:09 -04:00
|
|
|
hdlrs = []func(string, *Client){}
|
2016-10-15 01:40:28 -04:00
|
|
|
}
|
|
|
|
|
2016-10-18 20:34:09 -04:00
|
|
|
hdlrs = append(hdlrs, fn)
|
|
|
|
handlers.extpointHandlers[iface] = hdlrs
|
2016-12-27 11:07:22 -05:00
|
|
|
|
|
|
|
storage.Lock()
|
2016-10-15 01:40:28 -04:00
|
|
|
for _, p := range storage.plugins {
|
2016-12-27 11:07:22 -05:00
|
|
|
p.activateWait.L.Lock()
|
|
|
|
if p.activated() && p.implements(iface) {
|
|
|
|
p.handlersRun = false
|
|
|
|
}
|
|
|
|
p.activateWait.L.Unlock()
|
2016-10-15 01:40:28 -04:00
|
|
|
}
|
2016-12-27 11:07:22 -05:00
|
|
|
storage.Unlock()
|
|
|
|
|
2016-10-18 20:34:09 -04:00
|
|
|
handlers.Unlock()
|
2015-05-15 07:07:59 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
|
|
|
|
// GetAll returns all the plugins for the specified implementation
|
|
|
|
func GetAll(imp string) ([]*Plugin, error) {
|
|
|
|
pluginNames, err := Scan()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type plLoad struct {
|
|
|
|
pl *Plugin
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:57:44 -05:00
|
|
|
chPl := make(chan *plLoad, len(pluginNames))
|
|
|
|
var wg sync.WaitGroup
|
2015-09-23 16:29:14 -04:00
|
|
|
for _, name := range pluginNames {
|
2017-01-04 15:29:10 -05:00
|
|
|
storage.Lock()
|
|
|
|
pl, ok := storage.plugins[name]
|
|
|
|
storage.Unlock()
|
|
|
|
if ok {
|
2016-02-26 11:57:44 -05:00
|
|
|
chPl <- &plLoad{pl, nil}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
2015-09-23 16:29:14 -04:00
|
|
|
go func(name string) {
|
2016-02-26 11:57:44 -05:00
|
|
|
defer wg.Done()
|
2015-09-23 16:29:14 -04:00
|
|
|
pl, err := loadWithRetry(name, false)
|
2016-02-26 11:57:44 -05:00
|
|
|
chPl <- &plLoad{pl, err}
|
2015-09-23 16:29:14 -04:00
|
|
|
}(name)
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:57:44 -05:00
|
|
|
wg.Wait()
|
|
|
|
close(chPl)
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
var out []*Plugin
|
2016-02-26 11:57:44 -05:00
|
|
|
for pl := range chPl {
|
2015-09-23 16:29:14 -04:00
|
|
|
if pl.err != nil {
|
2016-03-23 15:25:15 -04:00
|
|
|
logrus.Error(pl.err)
|
2015-09-23 16:29:14 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-12-27 11:07:22 -05:00
|
|
|
if err := pl.pl.waitActive(); err == nil && pl.pl.implements(imp) {
|
2015-09-23 16:29:14 -04:00
|
|
|
out = append(out, pl.pl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|