Add volume API/CLI

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2015-08-26 13:37:52 -04:00
parent 7ef08f39ed
commit b3b7eb2723
49 changed files with 1455 additions and 394 deletions
+7 -2
View File
@@ -11,8 +11,8 @@ func (a *volumeDriverAdapter) Name() string {
return a.name
}
func (a *volumeDriverAdapter) Create(name string) (volume.Volume, error) {
err := a.proxy.Create(name)
func (a *volumeDriverAdapter) Create(name string, opts map[string]string) (volume.Volume, error) {
err := a.proxy.Create(name, opts)
if err != nil {
return nil, err
}
@@ -33,6 +33,11 @@ type volumeAdapter struct {
eMount string // ephemeral host volume path
}
type proxyVolume struct {
Name string
Mountpoint string
}
func (a *volumeAdapter) Name() string {
return a.name
}
-25
View File
@@ -1,25 +0,0 @@
//go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type VolumeDriver -name VolumeDriver
package volumedrivers
import "github.com/docker/docker/volume"
// 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}
}
// VolumeDriver defines the available functions that volume plugins must implement.
type VolumeDriver interface {
// Create a volume with the given name
Create(name string) (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)
}
+24
View File
@@ -1,3 +1,5 @@
//go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type VolumeDriver -name VolumeDriver
package volumedrivers
import (
@@ -13,6 +15,28 @@ import (
var drivers = &driverExtpoint{extensions: make(map[string]volume.Driver)}
// 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
// VolumeDriver defines the available functions that volume plugins must implement.
type VolumeDriver interface {
// 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)
}
type driverExtpoint struct {
extensions map[string]volume.Driver
sync.Mutex
+3 -1
View File
@@ -14,19 +14,21 @@ type volumeDriverProxy struct {
type volumeDriverProxyCreateRequest struct {
Name string
Opts opts
}
type volumeDriverProxyCreateResponse struct {
Err string
}
func (pp *volumeDriverProxy) Create(name string) (err error) {
func (pp *volumeDriverProxy) Create(name string, opts opts) (err error) {
var (
req volumeDriverProxyCreateRequest
ret volumeDriverProxyCreateResponse
)
req.Name = name
req.Opts = opts
if err = pp.Call("VolumeDriver.Create", req, &ret); err != nil {
return
}
+1 -1
View File
@@ -50,7 +50,7 @@ func TestVolumeRequestError(t *testing.T) {
driver := volumeDriverProxy{client}
if err = driver.Create("volume"); err == nil {
if err = driver.Create("volume", nil); err == nil {
t.Fatal("Expected error, was nil")
}
+56 -45
View File
@@ -23,7 +23,11 @@ const (
volumesPathName = "volumes"
)
var oldVfsDir = filepath.Join("vfs", "dir")
var (
// ErrNotFound is the typed error returned when the requested volume name can't be found
ErrNotFound = errors.New("volume not found")
oldVfsDir = filepath.Join("vfs", "dir")
)
// New instantiates a new Root instance with the provided scope. Scope
// is the base path that the Root instance uses to store its
@@ -54,6 +58,7 @@ func New(scope string) (*Root, error) {
path: r.DataPath(name),
}
}
return r, nil
}
@@ -67,6 +72,15 @@ type Root struct {
volumes map[string]*localVolume
}
// List lists all the volumes
func (r *Root) List() []volume.Volume {
var ls []volume.Volume
for _, v := range r.volumes {
ls = append(ls, v)
}
return ls
}
// DataPath returns the constructed path of this volume.
func (r *Root) DataPath(volumeName string) string {
return filepath.Join(r.path, volumeName, VolumeDataPathName)
@@ -80,27 +94,28 @@ func (r *Root) Name() string {
// Create creates a new volume.Volume with the provided name, creating
// the underlying directory tree required for this volume in the
// process.
func (r *Root) Create(name string) (volume.Volume, error) {
func (r *Root) Create(name string, _ map[string]string) (volume.Volume, error) {
r.m.Lock()
defer r.m.Unlock()
v, exists := r.volumes[name]
if !exists {
path := r.DataPath(name)
if err := os.MkdirAll(path, 0755); err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
}
return nil, err
}
v = &localVolume{
driverName: r.Name(),
name: name,
path: path,
}
r.volumes[name] = v
if exists {
return v, nil
}
v.use()
path := r.DataPath(name)
if err := os.MkdirAll(path, 0755); err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
}
return nil, err
}
v = &localVolume{
driverName: r.Name(),
name: name,
path: path,
}
r.volumes[name] = v
return v, nil
}
@@ -115,24 +130,32 @@ func (r *Root) Remove(v volume.Volume) error {
if !ok {
return errors.New("unknown volume type")
}
lv.release()
if lv.usedCount == 0 {
realPath, err := filepath.EvalSymlinks(lv.path)
if err != nil {
return err
}
if !r.scopedPath(realPath) {
return fmt.Errorf("Unable to remove a directory of out the Docker root: %s", realPath)
}
if err := os.RemoveAll(realPath); err != nil {
return err
}
delete(r.volumes, lv.name)
return os.RemoveAll(filepath.Dir(lv.path))
realPath, err := filepath.EvalSymlinks(lv.path)
if err != nil {
return err
}
return nil
if !r.scopedPath(realPath) {
return fmt.Errorf("Unable to remove a directory of out the Docker root: %s", realPath)
}
if err := os.RemoveAll(realPath); err != nil {
return err
}
delete(r.volumes, lv.name)
return os.RemoveAll(filepath.Dir(lv.path))
}
// Get looks up the volume for the given name and returns it if found
func (r *Root) Get(name string) (volume.Volume, error) {
r.m.Lock()
v, exists := r.volumes[name]
r.m.Unlock()
if !exists {
return nil, ErrNotFound
}
return v, nil
}
// scopedPath verifies that the path where the volume is located
@@ -188,15 +211,3 @@ func (v *localVolume) Mount() (string, error) {
func (v *localVolume) Unmount() error {
return nil
}
func (v *localVolume) use() {
v.m.Lock()
v.usedCount++
v.m.Unlock()
}
func (v *localVolume) release() {
v.m.Lock()
v.usedCount--
v.m.Unlock()
}
+1 -1
View File
@@ -9,7 +9,7 @@ type Driver interface {
// Name returns the name of the volume driver.
Name() string
// Create makes a new volume with the given id.
Create(string) (Volume, error)
Create(name string, opts map[string]string) (Volume, error)
// Remove deletes the volume.
Remove(Volume) error
}