Adds StorageDriverFactory, unifying creation of StorageDrivers

Custom storage drivers can register a factory to create the driver by
name, similar to the database/sql package's Register and Open
factory.Create returns an in-process driver if registered or an IPC
driver if one can be found, erroring otherwise
This standardizes parameter passing for creation of storage drivers

Also adds documentation for storagedriver package and children
This commit is contained in:
Brian Bland 2014-10-28 18:15:40 -07:00
parent ff81f3a719
commit ca0084fad1
15 changed files with 290 additions and 79 deletions

View file

@ -8,13 +8,45 @@ import (
"strings"
"github.com/docker/docker-registry/storagedriver"
"github.com/docker/docker-registry/storagedriver/factory"
)
const DriverName = "filesystem"
const DefaultRootDirectory = "/tmp/registry/storage"
func init() {
factory.Register(DriverName, &filesystemDriverFactory{})
}
// Implements the factory.StorageDriverFactory interface
type filesystemDriverFactory struct{}
func (factory *filesystemDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) {
return FromParameters(parameters), nil
}
// Storage Driver backed by a local filesystem
// All provided paths will be subpaths of the RootDirectory
type FilesystemDriver struct {
rootDirectory string
}
func NewDriver(rootDirectory string) *FilesystemDriver {
// Constructs a new FilesystemDriver with a given parameters map
// Optional Parameters:
// - rootdirectory
func FromParameters(parameters map[string]string) *FilesystemDriver {
var rootDirectory = DefaultRootDirectory
if parameters != nil {
rootDir, ok := parameters["rootdirectory"]
if ok {
rootDirectory = rootDir
}
}
return New(rootDirectory)
}
// Constructs a new FilesystemDriver with a given rootDirectory
func New(rootDirectory string) *FilesystemDriver {
return &FilesystemDriver{rootDirectory}
}
@ -22,6 +54,8 @@ func (d *FilesystemDriver) subPath(subPath string) string {
return path.Join(d.rootDirectory, subPath)
}
// Implement the storagedriver.StorageDriver interface
func (d *FilesystemDriver) GetContent(path string) ([]byte, error) {
contents, err := ioutil.ReadFile(d.subPath(path))
if err != nil {

View file

@ -17,8 +17,8 @@ func init() {
os.RemoveAll(rootDirectory)
filesystemDriverConstructor := func() (storagedriver.StorageDriver, error) {
return NewDriver(rootDirectory), nil
return New(rootDirectory), nil
}
testsuites.RegisterInProcessSuite(filesystemDriverConstructor, testsuites.NeverSkip)
testsuites.RegisterIPCSuite("filesystem", map[string]string{"RootDirectory": rootDirectory}, testsuites.NeverSkip)
testsuites.RegisterIPCSuite(DriverName, map[string]string{"rootdirectory": rootDirectory}, testsuites.NeverSkip)
}