github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/volume/drivers/extpoint.go (about) 1 //go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type VolumeDriver -name VolumeDriver 2 3 package volumedrivers 4 5 import ( 6 "fmt" 7 "sync" 8 9 "github.com/docker/docker/pkg/plugins" 10 "github.com/docker/docker/volume" 11 ) 12 13 // currently created by hand. generation tool would generate this like: 14 // $ extpoint-gen Driver > volume/extpoint.go 15 16 var drivers = &driverExtpoint{extensions: make(map[string]volume.Driver)} 17 18 // NewVolumeDriver returns a driver has the given name mapped on the given client. 19 func NewVolumeDriver(name string, c client) volume.Driver { 20 proxy := &volumeDriverProxy{c} 21 return &volumeDriverAdapter{name, proxy} 22 } 23 24 type opts map[string]string 25 26 // VolumeDriver defines the available functions that volume plugins must implement. 27 type VolumeDriver interface { 28 // Create a volume with the given name 29 Create(name string, opts opts) (err error) 30 // Remove the volume with the given name 31 Remove(name string) (err error) 32 // Get the mountpoint of the given volume 33 Path(name string) (mountpoint string, err error) 34 // Mount the given volume and return the mountpoint 35 Mount(name string) (mountpoint string, err error) 36 // Unmount the given volume 37 Unmount(name string) (err error) 38 } 39 40 type driverExtpoint struct { 41 extensions map[string]volume.Driver 42 sync.Mutex 43 } 44 45 // Register associates the given driver to the given name, checking if 46 // the name is already associated 47 func Register(extension volume.Driver, name string) bool { 48 drivers.Lock() 49 defer drivers.Unlock() 50 if name == "" { 51 return false 52 } 53 _, exists := drivers.extensions[name] 54 if exists { 55 return false 56 } 57 drivers.extensions[name] = extension 58 return true 59 } 60 61 // Unregister dissociates the name from it's driver, if the association exists. 62 func Unregister(name string) bool { 63 drivers.Lock() 64 defer drivers.Unlock() 65 _, exists := drivers.extensions[name] 66 if !exists { 67 return false 68 } 69 delete(drivers.extensions, name) 70 return true 71 } 72 73 // Lookup returns the driver associated with the given name. If a 74 // driver with the given name has not been registered it checks if 75 // there is a VolumeDriver plugin available with the given name. 76 func Lookup(name string) (volume.Driver, error) { 77 drivers.Lock() 78 ext, ok := drivers.extensions[name] 79 drivers.Unlock() 80 if ok { 81 return ext, nil 82 } 83 pl, err := plugins.Get(name, "VolumeDriver") 84 if err != nil { 85 return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err) 86 } 87 88 drivers.Lock() 89 defer drivers.Unlock() 90 if ext, ok := drivers.extensions[name]; ok { 91 return ext, nil 92 } 93 94 d := NewVolumeDriver(name, pl.Client) 95 drivers.extensions[name] = d 96 return d, nil 97 }