2016-08-26 13:02:38 -04:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
2016-10-06 10:09:54 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-08-26 13:02:38 -04:00
|
|
|
"sync"
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
"github.com/docker/distribution/digest"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-30 16:02:45 -05:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
2016-08-26 13:02:38 -04:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin represents an individual plugin.
|
|
|
|
type Plugin struct {
|
2016-12-12 18:05:53 -05:00
|
|
|
mu sync.RWMutex
|
|
|
|
PluginObj types.Plugin `json:"plugin"` // todo: embed struct
|
|
|
|
pClient *plugins.Client
|
|
|
|
refCount int
|
|
|
|
PropagatedMount string // TODO: make private
|
|
|
|
Rootfs string // TODO: make private
|
|
|
|
|
|
|
|
Config digest.Digest
|
|
|
|
Blobsums []digest.Digest
|
2016-08-26 13:02:38 -04:00
|
|
|
}
|
2016-10-06 10:09:54 -04:00
|
|
|
|
|
|
|
const defaultPluginRuntimeDestination = "/run/docker/plugins"
|
|
|
|
|
|
|
|
// ErrInadequateCapability indicates that the plugin did not have the requested capability.
|
2016-10-25 19:12:56 -04:00
|
|
|
type ErrInadequateCapability struct {
|
|
|
|
cap string
|
|
|
|
}
|
2016-10-06 10:09:54 -04:00
|
|
|
|
2016-10-25 19:12:56 -04:00
|
|
|
func (e ErrInadequateCapability) Error() string {
|
|
|
|
return fmt.Sprintf("plugin does not provide %q capability", e.cap)
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
2016-11-22 14:21:34 -05:00
|
|
|
// BasePath returns the path to which all paths returned by the plugin are relative to.
|
|
|
|
// For Plugin objects this returns the host path of the plugin container's rootfs.
|
|
|
|
func (p *Plugin) BasePath() string {
|
|
|
|
return p.Rootfs
|
|
|
|
}
|
|
|
|
|
2016-10-06 10:09:54 -04:00
|
|
|
// Client returns the plugin client.
|
|
|
|
func (p *Plugin) Client() *plugins.Client {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
|
|
|
|
return p.pClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPClient set the plugin client.
|
|
|
|
func (p *Plugin) SetPClient(client *plugins.Client) {
|
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
p.pClient = client
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsV1 returns true for V1 plugins and false otherwise.
|
|
|
|
func (p *Plugin) IsV1() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the plugin name.
|
|
|
|
func (p *Plugin) Name() string {
|
2016-12-12 18:05:53 -05:00
|
|
|
return p.PluginObj.Name
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FilterByCap query the plugin for a given capability.
|
|
|
|
func (p *Plugin) FilterByCap(capability string) (*Plugin, error) {
|
|
|
|
capability = strings.ToLower(capability)
|
2016-11-07 21:51:47 -05:00
|
|
|
for _, typ := range p.PluginObj.Config.Interface.Types {
|
2016-10-06 10:09:54 -04:00
|
|
|
if typ.Capability == capability && typ.Prefix == "docker" {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 19:12:56 -04:00
|
|
|
return nil, ErrInadequateCapability{capability}
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
// InitEmptySettings initializes empty settings for a plugin.
|
|
|
|
func (p *Plugin) InitEmptySettings() {
|
2016-11-07 21:51:47 -05:00
|
|
|
p.PluginObj.Settings.Mounts = make([]types.PluginMount, len(p.PluginObj.Config.Mounts))
|
2016-11-22 14:21:34 -05:00
|
|
|
copy(p.PluginObj.Settings.Mounts, p.PluginObj.Config.Mounts)
|
2016-12-15 18:35:53 -05:00
|
|
|
p.PluginObj.Settings.Devices = make([]types.PluginDevice, len(p.PluginObj.Config.Linux.Devices))
|
2016-11-16 19:18:43 -05:00
|
|
|
copy(p.PluginObj.Settings.Devices, p.PluginObj.Config.Linux.Devices)
|
2016-12-15 18:35:53 -05:00
|
|
|
p.PluginObj.Settings.Env = make([]string, 0, len(p.PluginObj.Config.Env))
|
2016-11-07 21:51:47 -05:00
|
|
|
for _, env := range p.PluginObj.Config.Env {
|
2016-10-06 10:09:54 -04:00
|
|
|
if env.Value != nil {
|
2016-11-07 21:51:47 -05:00
|
|
|
p.PluginObj.Settings.Env = append(p.PluginObj.Settings.Env, fmt.Sprintf("%s=%s", env.Name, *env.Value))
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 14:21:34 -05:00
|
|
|
p.PluginObj.Settings.Args = make([]string, len(p.PluginObj.Config.Args.Value))
|
2016-11-07 21:51:47 -05:00
|
|
|
copy(p.PluginObj.Settings.Args, p.PluginObj.Config.Args.Value)
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set is used to pass arguments to the plugin.
|
|
|
|
func (p *Plugin) Set(args []string) error {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2016-10-31 20:07:05 -04:00
|
|
|
|
|
|
|
if p.PluginObj.Enabled {
|
|
|
|
return fmt.Errorf("cannot set on an active plugin, disable plugin before setting")
|
|
|
|
}
|
|
|
|
|
|
|
|
sets, err := newSettables(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:15:31 -05:00
|
|
|
// TODO(vieux): lots of code duplication here, needs to be refactored.
|
|
|
|
|
2016-10-31 20:07:05 -04:00
|
|
|
next:
|
|
|
|
for _, s := range sets {
|
2016-11-07 21:51:47 -05:00
|
|
|
// range over all the envs in the config
|
|
|
|
for _, env := range p.PluginObj.Config.Env {
|
|
|
|
// found the env in the config
|
2016-10-31 20:07:05 -04:00
|
|
|
if env.Name == s.name {
|
|
|
|
// is it settable ?
|
|
|
|
if ok, err := s.isSettable(allowedSettableFieldsEnv, env.Settable); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return fmt.Errorf("%q is not settable", s.prettyName())
|
|
|
|
}
|
2016-11-07 21:51:47 -05:00
|
|
|
// is it, so lets update the settings in memory
|
2016-11-07 20:15:31 -05:00
|
|
|
updateSettingsEnv(&p.PluginObj.Settings.Env, &s)
|
|
|
|
continue next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// range over all the mounts in the config
|
|
|
|
for _, mount := range p.PluginObj.Config.Mounts {
|
|
|
|
// found the mount in the config
|
|
|
|
if mount.Name == s.name {
|
|
|
|
// is it settable ?
|
|
|
|
if ok, err := s.isSettable(allowedSettableFieldsMounts, mount.Settable); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return fmt.Errorf("%q is not settable", s.prettyName())
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is, so lets update the settings in memory
|
|
|
|
*mount.Source = s.value
|
|
|
|
continue next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// range over all the devices in the config
|
2016-11-16 19:18:43 -05:00
|
|
|
for _, device := range p.PluginObj.Config.Linux.Devices {
|
2016-11-07 20:15:31 -05:00
|
|
|
// found the device in the config
|
|
|
|
if device.Name == s.name {
|
|
|
|
// is it settable ?
|
|
|
|
if ok, err := s.isSettable(allowedSettableFieldsDevices, device.Settable); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return fmt.Errorf("%q is not settable", s.prettyName())
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is, so lets update the settings in memory
|
|
|
|
*device.Path = s.value
|
2016-10-31 20:07:05 -04:00
|
|
|
continue next
|
|
|
|
}
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
2016-10-31 20:07:05 -04:00
|
|
|
|
2016-11-07 20:15:31 -05:00
|
|
|
// found the name in the config
|
|
|
|
if p.PluginObj.Config.Args.Name == s.name {
|
|
|
|
// is it settable ?
|
|
|
|
if ok, err := s.isSettable(allowedSettableFieldsArgs, p.PluginObj.Config.Args.Settable); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return fmt.Errorf("%q is not settable", s.prettyName())
|
|
|
|
}
|
|
|
|
|
|
|
|
// it is, so lets update the settings in memory
|
|
|
|
p.PluginObj.Settings.Args = strings.Split(s.value, " ")
|
|
|
|
continue next
|
|
|
|
}
|
2016-10-31 20:07:05 -04:00
|
|
|
|
|
|
|
return fmt.Errorf("setting %q not found in the plugin configuration", s.name)
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
2016-10-31 20:07:05 -04:00
|
|
|
|
2016-12-12 18:05:53 -05:00
|
|
|
return nil
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnabled returns the active state of the plugin.
|
|
|
|
func (p *Plugin) IsEnabled() bool {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
2016-10-06 10:09:54 -04:00
|
|
|
|
|
|
|
return p.PluginObj.Enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the plugin's ID.
|
|
|
|
func (p *Plugin) GetID() string {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
2016-10-06 10:09:54 -04:00
|
|
|
|
|
|
|
return p.PluginObj.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSocket returns the plugin socket.
|
|
|
|
func (p *Plugin) GetSocket() string {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
2016-10-06 10:09:54 -04:00
|
|
|
|
2016-11-07 21:51:47 -05:00
|
|
|
return p.PluginObj.Config.Interface.Socket
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTypes returns the interface types of a plugin.
|
|
|
|
func (p *Plugin) GetTypes() []types.PluginInterfaceType {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
2016-10-06 10:09:54 -04:00
|
|
|
|
2016-11-07 21:51:47 -05:00
|
|
|
return p.PluginObj.Config.Interface.Types
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 14:36:56 -05:00
|
|
|
// GetRefCount returns the reference count.
|
|
|
|
func (p *Plugin) GetRefCount() int {
|
|
|
|
p.mu.RLock()
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
|
|
|
|
return p.refCount
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:34:30 -05:00
|
|
|
// AddRefCount adds to reference count.
|
|
|
|
func (p *Plugin) AddRefCount(count int) {
|
2016-12-01 14:36:56 -05:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
2016-12-09 12:34:30 -05:00
|
|
|
p.refCount += count
|
2016-12-01 14:36:56 -05:00
|
|
|
}
|
|
|
|
|
2016-11-30 16:02:45 -05:00
|
|
|
// Acquire increments the plugin's reference count
|
|
|
|
// This should be followed up by `Release()` when the plugin is no longer in use.
|
|
|
|
func (p *Plugin) Acquire() {
|
|
|
|
p.AddRefCount(plugingetter.ACQUIRE)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release decrements the plugin's reference count
|
|
|
|
// This should only be called when the plugin is no longer in use, e.g. with
|
|
|
|
// via `Acquire()` or getter.Get("name", "type", plugingetter.ACQUIRE)
|
|
|
|
func (p *Plugin) Release() {
|
|
|
|
p.AddRefCount(plugingetter.RELEASE)
|
|
|
|
}
|