2014-10-29 01:15:40 +00:00
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2014-12-24 00:01:38 +00:00
|
|
|
"github.com/docker/distribution/storagedriver"
|
2014-10-29 01:15:40 +00:00
|
|
|
)
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// driverFactories stores an internal mapping between storage driver names and their respective
|
|
|
|
// factories
|
2014-10-29 01:15:40 +00:00
|
|
|
var driverFactories = make(map[string]StorageDriverFactory)
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// StorageDriverFactory is a factory interface for creating storagedriver.StorageDriver interfaces
|
2014-10-29 01:15:40 +00:00
|
|
|
// Storage drivers should call Register() with a factory to make the driver available by name
|
|
|
|
type StorageDriverFactory interface {
|
2014-10-29 19:14:19 +00:00
|
|
|
// Create returns a new storagedriver.StorageDriver with the given parameters
|
2014-10-29 01:15:40 +00:00
|
|
|
// Parameters will vary by driver and may be ignored
|
|
|
|
// Each parameter key must only consist of lowercase letters and numbers
|
2014-12-18 03:06:55 +00:00
|
|
|
Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error)
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register makes a storage driver available by the provided name.
|
|
|
|
// If Register is called twice with the same name or if driver factory is nil, it panics.
|
|
|
|
func Register(name string, factory StorageDriverFactory) {
|
|
|
|
if factory == nil {
|
|
|
|
panic("Must not provide nil StorageDriverFactory")
|
|
|
|
}
|
|
|
|
_, registered := driverFactories[name]
|
|
|
|
if registered {
|
|
|
|
panic(fmt.Sprintf("StorageDriverFactory named %s already registered", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
driverFactories[name] = factory
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new storagedriver.StorageDriver with the given name and parameters
|
|
|
|
// To run in-process, the StorageDriverFactory must first be registered with the given name
|
|
|
|
// If no in-process drivers are found with the given name, this attempts to create an IPC driver
|
|
|
|
// If no in-process or external drivers are found, an InvalidStorageDriverError is returned
|
2014-12-18 03:06:55 +00:00
|
|
|
func Create(name string, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
2014-10-29 01:15:40 +00:00
|
|
|
driverFactory, ok := driverFactories[name]
|
|
|
|
if !ok {
|
2014-12-05 22:05:37 +00:00
|
|
|
return nil, InvalidStorageDriverError{name}
|
|
|
|
|
|
|
|
// NOTE(stevvooe): We are disabling storagedriver ipc for now, as the
|
|
|
|
// server and client need to be updated for the changed API calls and
|
|
|
|
// there were some problems libchan hanging. We'll phase this
|
|
|
|
// functionality back in over the next few weeks.
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// No registered StorageDriverFactory found, try ipc
|
2014-12-05 22:05:37 +00:00
|
|
|
// driverClient, err := ipc.NewDriverClient(name, parameters)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, InvalidStorageDriverError{name}
|
|
|
|
// }
|
|
|
|
// err = driverClient.Start()
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// return driverClient, nil
|
2014-10-29 01:15:40 +00:00
|
|
|
}
|
|
|
|
return driverFactory.Create(parameters)
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// InvalidStorageDriverError records an attempt to construct an unregistered storage driver
|
2014-10-29 01:15:40 +00:00
|
|
|
type InvalidStorageDriverError struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err InvalidStorageDriverError) Error() string {
|
|
|
|
return fmt.Sprintf("StorageDriver not registered: %s", err.Name)
|
|
|
|
}
|