2015-09-22 15:50:48 -04:00
|
|
|
//go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type volumeDriver -name VolumeDriver
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
package volumedrivers
|
|
|
|
|
|
|
|
import (
|
2015-05-22 13:37:00 -04:00
|
|
|
"fmt"
|
2015-05-19 16:05:25 -04:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
"github.com/docker/docker/volume"
|
|
|
|
)
|
|
|
|
|
|
|
|
// currently created by hand. generation tool would generate this like:
|
|
|
|
// $ extpoint-gen Driver > volume/extpoint.go
|
|
|
|
|
|
|
|
var drivers = &driverExtpoint{extensions: make(map[string]volume.Driver)}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
const extName = "VolumeDriver"
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
// NewVolumeDriver returns a driver has the given name mapped on the given client.
|
|
|
|
func NewVolumeDriver(name string, c client) volume.Driver {
|
|
|
|
proxy := &volumeDriverProxy{c}
|
|
|
|
return &volumeDriverAdapter{name, proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
type opts map[string]string
|
2015-09-23 16:29:14 -04:00
|
|
|
type list []*proxyVolume
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-09-11 18:19:38 -04: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 09:25:32 -04:00
|
|
|
// Create a volume with the given name
|
|
|
|
Create(name string, opts opts) (err error)
|
|
|
|
// 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
|
|
|
|
Mount(name string) (mountpoint string, err error)
|
|
|
|
// Unmount the given volume
|
|
|
|
Unmount(name string) (err error)
|
2015-09-23 16:29:14 -04:00
|
|
|
// List lists all the volumes known to the driver
|
|
|
|
List() (volumes list, err error)
|
|
|
|
// Get retreives the volume with the requested name
|
|
|
|
Get(name string) (volume *proxyVolume, err error)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
type driverExtpoint struct {
|
|
|
|
extensions map[string]volume.Driver
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Register associates the given driver to the given name, checking if
|
|
|
|
// the name is already associated
|
2015-05-19 16:05:25 -04:00
|
|
|
func Register(extension volume.Driver, name string) bool {
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, exists := drivers.extensions[name]
|
|
|
|
if exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
drivers.extensions[name] = extension
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Unregister dissociates the name from it's driver, if the association exists.
|
2015-05-19 16:05:25 -04:00
|
|
|
func Unregister(name string) bool {
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
_, exists := drivers.extensions[name]
|
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(drivers.extensions, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-21 13:50:10 -04:00
|
|
|
// Lookup returns the driver associated with the given name. If a
|
|
|
|
// driver with the given name has not been registered it checks if
|
|
|
|
// there is a VolumeDriver plugin available with the given name.
|
2015-05-20 17:53:53 -04:00
|
|
|
func Lookup(name string) (volume.Driver, error) {
|
2015-05-19 16:05:25 -04:00
|
|
|
drivers.Lock()
|
|
|
|
ext, ok := drivers.extensions[name]
|
2015-08-19 01:04:22 -04:00
|
|
|
drivers.Unlock()
|
2015-05-19 16:05:25 -04:00
|
|
|
if ok {
|
2015-05-20 17:53:53 -04:00
|
|
|
return ext, nil
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
pl, err := plugins.Get(name, extName)
|
2015-05-19 16:05:25 -04:00
|
|
|
if err != nil {
|
2015-05-22 13:37:00 -04:00
|
|
|
return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err)
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
2015-08-19 01:04:22 -04:00
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
if ext, ok := drivers.extensions[name]; ok {
|
|
|
|
return ext, nil
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
d := NewVolumeDriver(name, pl.Client)
|
|
|
|
drivers.extensions[name] = d
|
2015-05-20 17:53:53 -04:00
|
|
|
return d, nil
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-09-18 19:58:05 -04:00
|
|
|
|
|
|
|
// GetDriver returns a volume driver by it's name.
|
|
|
|
// If the driver is empty, it looks for the local driver.
|
|
|
|
func GetDriver(name string) (volume.Driver, error) {
|
|
|
|
if name == "" {
|
|
|
|
name = volume.DefaultDriverName
|
|
|
|
}
|
|
|
|
return Lookup(name)
|
|
|
|
}
|
2015-10-23 02:08:26 -04: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
|
|
|
|
for driverName := range drivers.extensions {
|
|
|
|
driverList = append(driverList, driverName)
|
|
|
|
}
|
|
|
|
return driverList
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
|
|
|
|
// GetAllDrivers lists all the registered drivers
|
|
|
|
func GetAllDrivers() ([]volume.Driver, error) {
|
|
|
|
plugins, err := plugins.GetAll(extName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ds []volume.Driver
|
|
|
|
|
|
|
|
drivers.Lock()
|
|
|
|
defer drivers.Unlock()
|
|
|
|
|
|
|
|
for _, d := range drivers.extensions {
|
|
|
|
ds = append(ds, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range plugins {
|
|
|
|
ext, ok := drivers.extensions[p.Name]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ext = NewVolumeDriver(p.Name, p.Client)
|
|
|
|
drivers.extensions[p.Name] = ext
|
|
|
|
ds = append(ds, ext)
|
|
|
|
}
|
|
|
|
return ds, nil
|
|
|
|
}
|