1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/plugins/discovery.go
David Calavera 839d64ffb1 Remote plugins plumbing.
Signed-off-by: David Calavera <david.calavera@gmail.com>
2015-05-15 11:04:58 -07:00

78 lines
1.4 KiB
Go

package plugins
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
)
const defaultLocalRegistry = "/usr/share/docker/plugins"
var (
ErrNotFound = errors.New("Plugin not found")
)
type Registry interface {
Plugins() ([]*Plugin, error)
Plugin(name string) (*Plugin, error)
}
type LocalRegistry struct {
path string
}
func newLocalRegistry(path string) *LocalRegistry {
if len(path) == 0 {
path = defaultLocalRegistry
}
return &LocalRegistry{path}
}
func (l *LocalRegistry) Plugin(name string) (*Plugin, error) {
filepath := filepath.Join(l.path, name)
specpath := filepath + ".spec"
if fi, err := os.Stat(specpath); err == nil {
return readPluginInfo(specpath, fi)
}
socketpath := filepath + ".sock"
if fi, err := os.Stat(socketpath); err == nil {
return readPluginInfo(socketpath, fi)
}
return nil, ErrNotFound
}
func readPluginInfo(path string, fi os.FileInfo) (*Plugin, error) {
name := strings.Split(fi.Name(), ".")[0]
if fi.Mode()&os.ModeSocket != 0 {
return &Plugin{
Name: name,
Addr: "unix://" + path,
}, nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
addr := strings.TrimSpace(string(content))
u, err := url.Parse(addr)
if err != nil {
return nil, err
}
if len(u.Scheme) == 0 {
return nil, fmt.Errorf("Unknown protocol")
}
return &Plugin{
Name: name,
Addr: addr,
}, nil
}