2015-09-22 22:50:48 +03:00
|
|
|
//go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type volumeDriver -name VolumeDriver
|
2015-06-12 16:25:32 +03:00
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
package volumedrivers
|
|
|
|
|
|
|
|
import (
|
2015-05-22 20:37:00 +03:00
|
|
|
"fmt"
|
2017-01-22 09:59:29 +03:00
|
|
|
"sort"
|
2015-05-19 23:05:25 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-01-20 20:12:51 +03:00
|
|
|
"github.com/docker/docker/pkg/locker"
|
2016-10-07 23:53:14 +03:00
|
|
|
getter "github.com/docker/docker/pkg/plugingetter"
|
2015-05-19 23:05:25 +03:00
|
|
|
"github.com/docker/docker/volume"
|
|
|
|
)
|
|
|
|
|
|
|
|
// currently created by hand. generation tool would generate this like:
|
|
|
|
// $ extpoint-gen Driver > volume/extpoint.go
|
|
|
|
|
2016-09-08 03:01:10 +03:00
|
|
|
var drivers = &driverExtpoint{
|
|
|
|
extensions: make(map[string]volume.Driver),
|
|
|
|
driverLock: &locker.Locker{},
|
|
|
|
}
|
2015-05-19 23:05:25 +03:00
|
|
|
|
2015-09-23 23:29:14 +03:00
|
|
|
const extName = "VolumeDriver"
|
|
|
|
|
2015-06-12 16:25:32 +03:00
|
|
|
// NewVolumeDriver returns a driver has the given name mapped on the given client.
|
2016-11-22 22:21:34 +03:00
|
|
|
func NewVolumeDriver(name string, baseHostPath string, c client) volume.Driver {
|
2015-06-12 16:25:32 +03:00
|
|
|
proxy := &volumeDriverProxy{c}
|
2016-11-22 22:21:34 +03:00
|
|
|
return &volumeDriverAdapter{name: name, baseHostPath: baseHostPath, proxy: proxy}
|
2015-06-12 16:25:32 +03:00
|
|
|
}
|
|
|
|
|
2015-09-12 01:19:38 +03:00
|
|
|
// volumeDriver defines the available functions that volume plugins must implement.
|
|
|
|
// This interface is only defined to generate the proxy objects.
|
|
|
|
// It's not intended to be public or reused.
|
|
|
|
type volumeDriver interface {
|
2015-06-12 16:25:32 +03:00
|
|
|
// Create a volume with the given name
|
2016-04-10 00:42:24 +03:00
|
|
|
Create(name string, opts map[string]string) (err error)
|
2015-06-12 16:25:32 +03:00
|
|
|
// Remove the volume with the given name
|
|
|
|
Remove(name string) (err error)
|
|
|
|
// Get the mountpoint of the given volume
|
|
|
|
Path(name string) (mountpoint string, err error)
|
|
|
|
// Mount the given volume and return the mountpoint
|
2016-03-08 05:41:44 +03:00
|
|
|
Mount(name, id string) (mountpoint string, err error)
|
2015-06-12 16:25:32 +03:00
|
|
|
// Unmount the given volume
|
2016-03-08 05:41:44 +03:00
|
|
|
Unmount(name, id string) (err error)
|
2015-09-23 23:29:14 +03:00
|
|
|
// List lists all the volumes known to the driver
|
2016-04-10 00:42:24 +03:00
|
|
|
List() (volumes []*proxyVolume, err error)
|
2016-02-12 02:21:52 +03:00
|
|
|
// Get retrieves the volume with the requested name
|
2015-09-23 23:29:14 +03:00
|
|
|
Get(name string) (volume *proxyVolume, err error)
|
2016-04-11 18:17:52 +03:00
|
|
|
// Capabilities gets the list of capabilities of the driver
|
|
|
|
Capabilities() (capabilities volume.Capability, err error)
|
2015-06-12 16:25:32 +03:00
|
|
|
}
|
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
type driverExtpoint struct {
|
|
|
|
extensions map[string]volume.Driver
|
|
|
|
sync.Mutex
|
2016-09-08 03:01:10 +03:00
|
|
|
driverLock *locker.Locker
|
|
|
|
plugingetter getter.PluginGetter
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPluginGetter sets the plugingetter
|
|
|
|
func RegisterPluginGetter(plugingetter getter.PluginGetter) {
|
|
|
|
drivers.plugingetter = plugingetter
|
2015-05-19 23:05:25 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 20:50:10 +03:00
|
|
|
// Register associates the given driver to the given name, checking if
|
|
|
|
// the name is already associated
|
2015-05-19 23:05:25 +03:00
|
|
|
func Register(extension volume.Driver, name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
2016-01-20 20:12:51 +03:00
|
|
|
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
_, exists := drivers.extensions[name]
|
|
|
|
if exists {
|
|
|
|
return false
|
|
|
|
}
|
2016-04-11 18:17:52 +03:00
|
|
|
|
|
|
|
if err := validateDriver(extension); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
drivers.extensions[name] = extension
|
2016-09-08 03:01:10 +03:00
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-09 19:17:57 +03:00
|
|
|
// Unregister dissociates the name from its driver, if the association exists.
|
2015-05-19 23:05:25 +03:00
|
|
|
func Unregister(name string) bool {
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
2016-01-20 20:12:51 +03:00
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
_, exists := drivers.extensions[name]
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(drivers.extensions, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:50:55 +03:00
|
|
|
// lookup returns the driver associated with the given name. If a
|
2015-07-21 20:50:10 +03:00
|
|
|
// driver with the given name has not been registered it checks if
|
|
|
|
// there is a VolumeDriver plugin available with the given name.
|
2016-09-07 16:59:15 +03:00
|
|
|
func lookup(name string, mode int) (volume.Driver, error) {
|
2016-01-20 20:12:51 +03:00
|
|
|
drivers.driverLock.Lock(name)
|
|
|
|
defer drivers.driverLock.Unlock(name)
|
|
|
|
|
2015-05-19 23:05:25 +03:00
|
|
|
drivers.Lock()
|
|
|
|
ext, ok := drivers.extensions[name]
|
2015-08-19 08:04:22 +03:00
|
|
|
drivers.Unlock()
|
2015-05-19 23:05:25 +03:00
|
|
|
if ok {
|
2015-05-21 00:53:53 +03:00
|
|
|
return ext, nil
|
2015-05-19 23:05:25 +03:00
|
|
|
}
|
2016-12-13 02:05:53 +03:00
|
|
|
if drivers.plugingetter != nil {
|
|
|
|
p, err := drivers.plugingetter.Get(name, extName, mode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err)
|
|
|
|
}
|
2016-01-20 20:12:51 +03:00
|
|
|
|
2016-12-13 02:05:53 +03:00
|
|
|
d := NewVolumeDriver(p.Name(), p.BasePath(), p.Client())
|
|
|
|
if err := validateDriver(d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-11 18:17:52 +03:00
|
|
|
|
2016-12-13 02:05:53 +03:00
|
|
|
if p.IsV1() {
|
|
|
|
drivers.Lock()
|
|
|
|
drivers.extensions[name] = d
|
|
|
|
drivers.Unlock()
|
|
|
|
}
|
|
|
|
return d, nil
|
2016-07-19 01:39:27 +03:00
|
|
|
}
|
2016-12-13 02:05:53 +03:00
|
|
|
return nil, fmt.Errorf("Error looking up volume plugin %s", name)
|
2015-05-19 23:05:25 +03:00
|
|
|
}
|
2015-09-19 02:58:05 +03:00
|
|
|
|
2016-04-11 18:17:52 +03:00
|
|
|
func validateDriver(vd volume.Driver) error {
|
|
|
|
scope := vd.Scope()
|
|
|
|
if scope != volume.LocalScope && scope != volume.GlobalScope {
|
|
|
|
return fmt.Errorf("Driver %q provided an invalid capability scope: %s", vd.Name(), scope)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-09 19:17:57 +03:00
|
|
|
// GetDriver returns a volume driver by its name.
|
2015-09-19 02:58:05 +03:00
|
|
|
// If the driver is empty, it looks for the local driver.
|
|
|
|
func GetDriver(name string) (volume.Driver, error) {
|
|
|
|
if name == "" {
|
|
|
|
name = volume.DefaultDriverName
|
|
|
|
}
|
2016-12-20 05:18:43 +03:00
|
|
|
return lookup(name, getter.Lookup)
|
2016-09-07 16:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDriver returns a volume driver by its name and increments RefCount.
|
|
|
|
// If the driver is empty, it looks for the local driver.
|
|
|
|
func CreateDriver(name string) (volume.Driver, error) {
|
|
|
|
if name == "" {
|
|
|
|
name = volume.DefaultDriverName
|
|
|
|
}
|
2016-12-20 05:18:43 +03:00
|
|
|
return lookup(name, getter.Acquire)
|
2016-09-07 16:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDriver returns a volume driver by its name and decrements RefCount..
|
|
|
|
// If the driver is empty, it looks for the local driver.
|
|
|
|
func RemoveDriver(name string) (volume.Driver, error) {
|
|
|
|
if name == "" {
|
|
|
|
name = volume.DefaultDriverName
|
|
|
|
}
|
2016-12-20 05:18:43 +03:00
|
|
|
return lookup(name, getter.Release)
|
2015-09-19 02:58:05 +03:00
|
|
|
}
|
2015-10-23 09:08:26 +03:00
|
|
|
|
|
|
|
// GetDriverList returns list of volume drivers registered.
|
|
|
|
// If no driver is registered, empty string list will be returned.
|
|
|
|
func GetDriverList() []string {
|
|
|
|
var driverList []string
|
2016-01-20 20:12:51 +03:00
|
|
|
drivers.Lock()
|
2015-10-23 09:08:26 +03:00
|
|
|
for driverName := range drivers.extensions {
|
|
|
|
driverList = append(driverList, driverName)
|
|
|
|
}
|
2016-01-20 20:12:51 +03:00
|
|
|
drivers.Unlock()
|
2017-01-22 09:59:29 +03:00
|
|
|
sort.Strings(driverList)
|
2015-10-23 09:08:26 +03:00
|
|
|
return driverList
|
|
|
|
}
|
2015-09-23 23:29:14 +03:00
|
|
|
|
|
|
|
// GetAllDrivers lists all the registered drivers
|
|
|
|
func GetAllDrivers() ([]volume.Driver, error) {
|
2016-12-13 02:05:53 +03:00
|
|
|
var plugins []getter.CompatPlugin
|
|
|
|
if drivers.plugingetter != nil {
|
|
|
|
var err error
|
|
|
|
plugins, err = drivers.plugingetter.GetAllByCap(extName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error listing plugins: %v", err)
|
|
|
|
}
|
2015-09-23 23:29:14 +03:00
|
|
|
}
|
|
|
|
var ds []volume.Driver
|
|
|
|
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
|
|
|
|
for _, d := range drivers.extensions {
|
|
|
|
ds = append(ds, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range plugins {
|
2016-05-16 18:50:55 +03:00
|
|
|
name := p.Name()
|
2016-11-29 11:17:35 +03:00
|
|
|
|
|
|
|
if _, ok := drivers.extensions[name]; ok {
|
2015-09-23 23:29:14 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-01-20 20:12:51 +03:00
|
|
|
|
2016-11-29 11:17:35 +03:00
|
|
|
ext := NewVolumeDriver(name, p.BasePath(), p.Client())
|
2016-09-08 03:01:10 +03:00
|
|
|
if p.IsV1() {
|
2016-07-19 01:39:27 +03:00
|
|
|
drivers.extensions[name] = ext
|
|
|
|
}
|
2015-09-23 23:29:14 +03:00
|
|
|
ds = append(ds, ext)
|
|
|
|
}
|
|
|
|
return ds, nil
|
|
|
|
}
|