2015-05-14 17:05:39 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2015-05-27 22:21:18 +00:00
|
|
|
"encoding/json"
|
2015-05-14 17:05:39 +00:00
|
|
|
"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 {
|
2015-05-27 22:21:18 +00:00
|
|
|
return readPluginSpecInfo(specpath, fi)
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2015-05-27 22:21:18 +00:00
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
socketpath := filepath + ".sock"
|
|
|
|
if fi, err := os.Stat(socketpath); err == nil {
|
2015-05-27 22:21:18 +00:00
|
|
|
return readPluginSocketInfo(socketpath, fi)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonpath := filepath + ".json"
|
|
|
|
if _, err := os.Stat(jsonpath); err == nil {
|
|
|
|
return readPluginJSONInfo(name, jsonpath)
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2015-05-27 22:21:18 +00:00
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2015-05-27 22:21:18 +00:00
|
|
|
func readPluginSpecInfo(path string, fi os.FileInfo) (*Plugin, error) {
|
2015-05-14 17:05:39 +00:00
|
|
|
name := strings.Split(fi.Name(), ".")[0]
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-05-27 22:21:18 +00:00
|
|
|
return newLocalPlugin(name, addr), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPluginSocketInfo(path string, fi os.FileInfo) (*Plugin, error) {
|
|
|
|
name := strings.Split(fi.Name(), ".")[0]
|
|
|
|
|
|
|
|
if fi.Mode()&os.ModeSocket == 0 {
|
|
|
|
return nil, fmt.Errorf("%s is not a socket", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newLocalPlugin(name, "unix://"+path), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPluginJSONInfo(name, path string) (*Plugin, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var p Plugin
|
|
|
|
if err := json.NewDecoder(f).Decode(&p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.Name = name
|
|
|
|
if len(p.TLSConfig.CAFile) == 0 {
|
|
|
|
p.TLSConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return &p, nil
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|