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
|
|
|
|
// a Manifest with a list of of Docker subsystems which this plugin implements.
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
// }
|
2015-05-14 13:05:39 -04:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
2015-08-28 14:55:05 -04:00
|
|
|
"time"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-27 18:21:18 -04:00
|
|
|
"github.com/docker/docker/pkg/tlsconfig"
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-15 07:07:59 -04:00
|
|
|
var (
|
|
|
|
storage = plugins{plugins: make(map[string]*Plugin)}
|
|
|
|
extpointHandlers = make(map[string]func(string, *Client))
|
|
|
|
)
|
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
|
|
|
|
Name string `json:"-"`
|
|
|
|
// Address of the plugin
|
|
|
|
Addr string
|
|
|
|
// TLS configuration of the plugin
|
2015-05-27 18:21:18 -04:00
|
|
|
TLSConfig tlsconfig.Options
|
2015-07-25 04:35:07 -04:00
|
|
|
// Client attached to the plugin
|
|
|
|
Client *Client `json:"-"`
|
|
|
|
// Manifest of the plugin (see above)
|
|
|
|
Manifest *Manifest `json:"-"`
|
2015-08-19 01:04:22 -04:00
|
|
|
|
|
|
|
activatErr error
|
|
|
|
activateOnce sync.Once
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLocalPlugin(name, addr string) *Plugin {
|
|
|
|
return &Plugin{
|
|
|
|
Name: name,
|
|
|
|
Addr: addr,
|
|
|
|
TLSConfig: tlsconfig.Options{InsecureSkipVerify: true},
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) activate() error {
|
2015-08-19 01:04:22 -04:00
|
|
|
p.activateOnce.Do(func() {
|
|
|
|
p.activatErr = p.activateWithLock()
|
|
|
|
})
|
|
|
|
return p.activatErr
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-05-27 18:21:18 -04:00
|
|
|
p.Client = c
|
|
|
|
|
|
|
|
m := new(Manifest)
|
|
|
|
if err = p.Client.Call("Plugin.Activate", nil, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
logrus.Debugf("%s's manifest: %v", p.Name, m)
|
|
|
|
p.Manifest = m
|
2015-05-27 18:21:18 -04:00
|
|
|
|
2015-05-15 07:07:59 -04:00
|
|
|
for _, iface := range m.Implements {
|
|
|
|
handler, handled := extpointHandlers[iface]
|
|
|
|
if !handled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
handler(p.Name, p.Client)
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
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) {
|
|
|
|
pl, err := get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, driver := range pl.Manifest.Implements {
|
|
|
|
logrus.Debugf("%s implements: %s", name, driver)
|
|
|
|
if driver == imp {
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
extpointHandlers[iface] = fn
|
|
|
|
}
|