2018-02-05 16:05:59 -05:00
|
|
|
package v2 // import "github.com/docker/docker/plugin/v2"
|
2016-08-26 13:02:38 -04:00
|
|
|
|
|
|
|
import (
|
2016-10-06 10:09:54 -04:00
|
|
|
"fmt"
|
2018-04-24 21:45:00 -04:00
|
|
|
"net"
|
2017-12-14 10:27:10 -05:00
|
|
|
"path/filepath"
|
2016-10-06 10:09:54 -04:00
|
|
|
"strings"
|
2016-08-26 13:02:38 -04:00
|
|
|
"sync"
|
2018-04-24 21:45:00 -04:00
|
|
|
"time"
|
2016-08-26 13:02:38 -04:00
|
|
|
|
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"
|
2019-08-05 10:37:47 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-08-26 13:02:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin represents an individual plugin.
|
|
|
|
type Plugin struct {
|
2017-12-14 09:29:11 -05:00
|
|
|
mu sync.RWMutex
|
|
|
|
PluginObj types.Plugin `json:"plugin"` // todo: embed struct
|
|
|
|
pClient *plugins.Client
|
|
|
|
refCount int
|
|
|
|
Rootfs string // TODO: make private
|
2016-12-12 18:05:53 -05:00
|
|
|
|
|
|
|
Config digest.Digest
|
|
|
|
Blobsums []digest.Digest
|
2017-06-07 13:07:01 -04:00
|
|
|
|
2017-12-13 15:24:51 -05:00
|
|
|
modifyRuntimeSpec func(*specs.Spec)
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
SwarmServiceID string
|
2018-04-24 21:45:00 -04:00
|
|
|
timeout time.Duration
|
|
|
|
addr net.Addr
|
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
|
|
|
}
|
|
|
|
|
2017-12-14 10:27:10 -05:00
|
|
|
// ScopedPath returns the path scoped to the plugin rootfs
|
|
|
|
func (p *Plugin) ScopedPath(s string) string {
|
|
|
|
if p.PluginObj.Config.PropagatedMount != "" && strings.HasPrefix(s, p.PluginObj.Config.PropagatedMount) {
|
|
|
|
// re-scope to the propagated mount path on the host
|
|
|
|
return filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount", strings.TrimPrefix(s, p.PluginObj.Config.PropagatedMount))
|
|
|
|
}
|
|
|
|
return filepath.Join(p.Rootfs, s)
|
2016-11-22 14:21:34 -05:00
|
|
|
}
|
|
|
|
|
2016-10-06 10:09:54 -04:00
|
|
|
// Client returns the plugin client.
|
2018-04-24 21:45:00 -04:00
|
|
|
// Deprecated: use p.Addr() and manually create the client
|
2016-10-06 10:09:54 -04:00
|
|
|
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.
|
2018-04-24 21:45:00 -04:00
|
|
|
// Deprecated: Hardcoded plugin client is deprecated
|
2016-12-01 14:36:56 -05:00
|
|
|
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
|
2017-11-28 16:04:13 -05:00
|
|
|
if mount.Source == nil {
|
|
|
|
return fmt.Errorf("Plugin config has no mount source")
|
|
|
|
}
|
2016-11-07 20:15:31 -05:00
|
|
|
*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
|
2017-11-28 16:04:13 -05:00
|
|
|
if device.Path == nil {
|
|
|
|
return fmt.Errorf("Plugin config has no device path")
|
|
|
|
}
|
2016-11-07 20:15:31 -05:00
|
|
|
*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() {
|
2016-12-19 21:18:43 -05:00
|
|
|
p.AddRefCount(plugingetter.Acquire)
|
2016-11-30 16:02:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release decrements the plugin's reference count
|
|
|
|
// This should only be called when the plugin is no longer in use, e.g. with
|
2016-12-19 21:18:43 -05:00
|
|
|
// via `Acquire()` or getter.Get("name", "type", plugingetter.Acquire)
|
2016-11-30 16:02:45 -05:00
|
|
|
func (p *Plugin) Release() {
|
2016-12-19 21:18:43 -05:00
|
|
|
p.AddRefCount(plugingetter.Release)
|
2016-11-30 16:02:45 -05:00
|
|
|
}
|
2017-12-13 15:24:51 -05:00
|
|
|
|
2018-05-17 08:09:19 -04:00
|
|
|
// SetSpecOptModifier sets the function to use to modify the generated
|
2017-12-13 15:24:51 -05:00
|
|
|
// runtime spec.
|
|
|
|
func (p *Plugin) SetSpecOptModifier(f func(*specs.Spec)) {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.modifyRuntimeSpec = f
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
2018-04-24 21:45:00 -04:00
|
|
|
|
|
|
|
// Timeout gets the currently configured connection timeout.
|
|
|
|
// This should be used when dialing the plugin.
|
|
|
|
func (p *Plugin) Timeout() time.Duration {
|
|
|
|
p.mu.RLock()
|
|
|
|
t := p.timeout
|
|
|
|
p.mu.RUnlock()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeout sets the timeout to use for dialing.
|
|
|
|
func (p *Plugin) SetTimeout(t time.Duration) {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.timeout = t
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the net.Addr to use to connect to the plugin socket
|
|
|
|
func (p *Plugin) Addr() net.Addr {
|
|
|
|
p.mu.RLock()
|
|
|
|
addr := p.addr
|
|
|
|
p.mu.RUnlock()
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddr sets the plugin address which can be used for dialing the plugin.
|
|
|
|
func (p *Plugin) SetAddr(addr net.Addr) {
|
|
|
|
p.mu.Lock()
|
|
|
|
p.addr = addr
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocol is the protocol that should be used for interacting with the plugin.
|
|
|
|
func (p *Plugin) Protocol() string {
|
|
|
|
if p.PluginObj.Config.Interface.ProtocolScheme != "" {
|
|
|
|
return p.PluginObj.Config.Interface.ProtocolScheme
|
|
|
|
}
|
|
|
|
return plugins.ProtocolSchemeHTTPV1
|
|
|
|
}
|