Initial commit
This commit is contained in:
104
vendor/github.com/hyperhq/hypercli/volume/drivers/adapter.go
generated
vendored
Normal file
104
vendor/github.com/hyperhq/hypercli/volume/drivers/adapter.go
generated
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
package volumedrivers
|
||||
|
||||
import (
|
||||
"github.com/docker/docker/pkg/plugins"
|
||||
"github.com/docker/docker/volume"
|
||||
)
|
||||
|
||||
type volumeDriverAdapter struct {
|
||||
name string
|
||||
proxy *volumeDriverProxy
|
||||
}
|
||||
|
||||
func (a *volumeDriverAdapter) Name() string {
|
||||
return a.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
|
||||
}
|
||||
return &volumeAdapter{
|
||||
proxy: a.proxy,
|
||||
name: name,
|
||||
driverName: a.name}, nil
|
||||
}
|
||||
|
||||
func (a *volumeDriverAdapter) Remove(v volume.Volume) error {
|
||||
return a.proxy.Remove(v.Name())
|
||||
}
|
||||
|
||||
func (a *volumeDriverAdapter) List() ([]volume.Volume, error) {
|
||||
ls, err := a.proxy.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out []volume.Volume
|
||||
for _, vp := range ls {
|
||||
out = append(out, &volumeAdapter{
|
||||
proxy: a.proxy,
|
||||
name: vp.Name,
|
||||
driverName: a.name,
|
||||
eMount: vp.Mountpoint,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (a *volumeDriverAdapter) Get(name string) (volume.Volume, error) {
|
||||
v, err := a.proxy.Get(name)
|
||||
if err != nil {
|
||||
// TODO: remove this hack. Allows back compat with volume drivers that don't support this call
|
||||
if !plugins.IsNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
return a.Create(name, nil)
|
||||
}
|
||||
|
||||
return &volumeAdapter{
|
||||
proxy: a.proxy,
|
||||
name: v.Name,
|
||||
driverName: a.Name(),
|
||||
eMount: v.Mountpoint,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type volumeAdapter struct {
|
||||
proxy *volumeDriverProxy
|
||||
name string
|
||||
driverName string
|
||||
eMount string // ephemeral host volume path
|
||||
}
|
||||
|
||||
type proxyVolume struct {
|
||||
Name string
|
||||
Mountpoint string
|
||||
}
|
||||
|
||||
func (a *volumeAdapter) Name() string {
|
||||
return a.name
|
||||
}
|
||||
|
||||
func (a *volumeAdapter) DriverName() string {
|
||||
return a.driverName
|
||||
}
|
||||
|
||||
func (a *volumeAdapter) Path() string {
|
||||
if len(a.eMount) > 0 {
|
||||
return a.eMount
|
||||
}
|
||||
m, _ := a.proxy.Path(a.name)
|
||||
return m
|
||||
}
|
||||
|
||||
func (a *volumeAdapter) Mount() (string, error) {
|
||||
var err error
|
||||
a.eMount, err = a.proxy.Mount(a.name)
|
||||
return a.eMount, err
|
||||
}
|
||||
|
||||
func (a *volumeAdapter) Unmount() error {
|
||||
return a.proxy.Unmount(a.name)
|
||||
}
|
||||
164
vendor/github.com/hyperhq/hypercli/volume/drivers/extpoint.go
generated
vendored
Normal file
164
vendor/github.com/hyperhq/hypercli/volume/drivers/extpoint.go
generated
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
//go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type volumeDriver -name VolumeDriver
|
||||
|
||||
package volumedrivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/docker/docker/pkg/locker"
|
||||
"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), driverLock: &locker.Locker{}}
|
||||
|
||||
const extName = "VolumeDriver"
|
||||
|
||||
// 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
|
||||
type list []*proxyVolume
|
||||
|
||||
// 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 {
|
||||
// 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)
|
||||
// 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)
|
||||
}
|
||||
|
||||
type driverExtpoint struct {
|
||||
extensions map[string]volume.Driver
|
||||
sync.Mutex
|
||||
driverLock *locker.Locker
|
||||
}
|
||||
|
||||
// Register associates the given driver to the given name, checking if
|
||||
// the name is already associated
|
||||
func Register(extension volume.Driver, name string) bool {
|
||||
if name == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
drivers.Lock()
|
||||
defer drivers.Unlock()
|
||||
|
||||
_, exists := drivers.extensions[name]
|
||||
if exists {
|
||||
return false
|
||||
}
|
||||
drivers.extensions[name] = extension
|
||||
return true
|
||||
}
|
||||
|
||||
// Unregister dissociates the name from it's driver, if the association exists.
|
||||
func Unregister(name string) bool {
|
||||
drivers.Lock()
|
||||
defer drivers.Unlock()
|
||||
|
||||
_, exists := drivers.extensions[name]
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
delete(drivers.extensions, name)
|
||||
return true
|
||||
}
|
||||
|
||||
// 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.
|
||||
func Lookup(name string) (volume.Driver, error) {
|
||||
drivers.driverLock.Lock(name)
|
||||
defer drivers.driverLock.Unlock(name)
|
||||
|
||||
drivers.Lock()
|
||||
ext, ok := drivers.extensions[name]
|
||||
drivers.Unlock()
|
||||
if ok {
|
||||
return ext, nil
|
||||
}
|
||||
|
||||
pl, err := plugins.Get(name, extName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err)
|
||||
}
|
||||
|
||||
drivers.Lock()
|
||||
defer drivers.Unlock()
|
||||
if ext, ok := drivers.extensions[name]; ok {
|
||||
return ext, nil
|
||||
}
|
||||
|
||||
d := NewVolumeDriver(name, pl.Client)
|
||||
drivers.extensions[name] = d
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// GetDriverList returns list of volume drivers registered.
|
||||
// If no driver is registered, empty string list will be returned.
|
||||
func GetDriverList() []string {
|
||||
var driverList []string
|
||||
drivers.Lock()
|
||||
for driverName := range drivers.extensions {
|
||||
driverList = append(driverList, driverName)
|
||||
}
|
||||
drivers.Unlock()
|
||||
return driverList
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
23
vendor/github.com/hyperhq/hypercli/volume/drivers/extpoint_test.go
generated
vendored
Normal file
23
vendor/github.com/hyperhq/hypercli/volume/drivers/extpoint_test.go
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
package volumedrivers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/volume/testutils"
|
||||
)
|
||||
|
||||
func TestGetDriver(t *testing.T) {
|
||||
_, err := GetDriver("missing")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
||||
d, err := GetDriver("fake")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if d.Name() != "fake" {
|
||||
t.Fatalf("Expected fake driver, got %s\n", d.Name())
|
||||
}
|
||||
}
|
||||
207
vendor/github.com/hyperhq/hypercli/volume/drivers/proxy.go
generated
vendored
Normal file
207
vendor/github.com/hyperhq/hypercli/volume/drivers/proxy.go
generated
vendored
Normal file
@@ -0,0 +1,207 @@
|
||||
// generated code - DO NOT EDIT
|
||||
|
||||
package volumedrivers
|
||||
|
||||
import "errors"
|
||||
|
||||
type client interface {
|
||||
Call(string, interface{}, interface{}) error
|
||||
}
|
||||
|
||||
type volumeDriverProxy struct {
|
||||
client
|
||||
}
|
||||
|
||||
type volumeDriverProxyCreateRequest struct {
|
||||
Name string
|
||||
Opts opts
|
||||
}
|
||||
|
||||
type volumeDriverProxyCreateResponse struct {
|
||||
Err string
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyRemoveRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type volumeDriverProxyRemoveResponse struct {
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) Remove(name string) (err error) {
|
||||
var (
|
||||
req volumeDriverProxyRemoveRequest
|
||||
ret volumeDriverProxyRemoveResponse
|
||||
)
|
||||
|
||||
req.Name = name
|
||||
if err = pp.Call("VolumeDriver.Remove", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyPathRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type volumeDriverProxyPathResponse struct {
|
||||
Mountpoint string
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) Path(name string) (mountpoint string, err error) {
|
||||
var (
|
||||
req volumeDriverProxyPathRequest
|
||||
ret volumeDriverProxyPathResponse
|
||||
)
|
||||
|
||||
req.Name = name
|
||||
if err = pp.Call("VolumeDriver.Path", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mountpoint = ret.Mountpoint
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyMountRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type volumeDriverProxyMountResponse struct {
|
||||
Mountpoint string
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) Mount(name string) (mountpoint string, err error) {
|
||||
var (
|
||||
req volumeDriverProxyMountRequest
|
||||
ret volumeDriverProxyMountResponse
|
||||
)
|
||||
|
||||
req.Name = name
|
||||
if err = pp.Call("VolumeDriver.Mount", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mountpoint = ret.Mountpoint
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyUnmountRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type volumeDriverProxyUnmountResponse struct {
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) Unmount(name string) (err error) {
|
||||
var (
|
||||
req volumeDriverProxyUnmountRequest
|
||||
ret volumeDriverProxyUnmountResponse
|
||||
)
|
||||
|
||||
req.Name = name
|
||||
if err = pp.Call("VolumeDriver.Unmount", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyListRequest struct {
|
||||
}
|
||||
|
||||
type volumeDriverProxyListResponse struct {
|
||||
Volumes list
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) List() (volumes list, err error) {
|
||||
var (
|
||||
req volumeDriverProxyListRequest
|
||||
ret volumeDriverProxyListResponse
|
||||
)
|
||||
|
||||
if err = pp.Call("VolumeDriver.List", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
volumes = ret.Volumes
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type volumeDriverProxyGetRequest struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type volumeDriverProxyGetResponse struct {
|
||||
Volume *proxyVolume
|
||||
Err string
|
||||
}
|
||||
|
||||
func (pp *volumeDriverProxy) Get(name string) (volume *proxyVolume, err error) {
|
||||
var (
|
||||
req volumeDriverProxyGetRequest
|
||||
ret volumeDriverProxyGetResponse
|
||||
)
|
||||
|
||||
req.Name = name
|
||||
if err = pp.Call("VolumeDriver.Get", req, &ret); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
volume = ret.Volume
|
||||
|
||||
if ret.Err != "" {
|
||||
err = errors.New(ret.Err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
122
vendor/github.com/hyperhq/hypercli/volume/drivers/proxy_test.go
generated
vendored
Normal file
122
vendor/github.com/hyperhq/hypercli/volume/drivers/proxy_test.go
generated
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
package volumedrivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/pkg/plugins"
|
||||
"github.com/docker/go-connections/tlsconfig"
|
||||
)
|
||||
|
||||
func TestVolumeRequestError(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
server := httptest.NewServer(mux)
|
||||
defer server.Close()
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot create volume"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot remove volume"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot mount volume"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot unmount volume"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Unknown volume"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.List", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot list volumes"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/VolumeDriver.Get", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
|
||||
fmt.Fprintln(w, `{"Err": "Cannot get volume"}`)
|
||||
})
|
||||
|
||||
u, _ := url.Parse(server.URL)
|
||||
client, err := plugins.NewClient("tcp://"+u.Host, tlsconfig.Options{InsecureSkipVerify: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
driver := volumeDriverProxy{client}
|
||||
|
||||
if err = driver.Create("volume", nil); err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Cannot create volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = driver.Mount("volume")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Cannot mount volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
err = driver.Unmount("volume")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Cannot unmount volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
err = driver.Remove("volume")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Cannot remove volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = driver.Path("volume")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "Unknown volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = driver.List()
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Cannot list volumes") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = driver.Get("volume")
|
||||
if err == nil {
|
||||
t.Fatal("Expected error, was nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Cannot get volume") {
|
||||
t.Fatalf("Unexpected error: %v\n", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user