2015-05-14 13:05:39 -04:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2015-05-27 18:21:18 -04:00
|
|
|
"encoding/json"
|
2015-05-14 13:05:39 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-07-25 04:35:07 -04:00
|
|
|
// ErrNotFound plugin not found
|
2015-05-14 13:05:39 -04:00
|
|
|
ErrNotFound = errors.New("Plugin not found")
|
2015-06-15 18:35:49 -04:00
|
|
|
socketsPath = "/run/docker/plugins"
|
|
|
|
specsPaths = []string{"/etc/docker/plugins", "/usr/lib/docker/plugins"}
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Registry defines behavior of a registry of plugins.
|
2015-05-14 13:05:39 -04:00
|
|
|
type Registry interface {
|
2015-07-25 04:35:07 -04:00
|
|
|
// Plugins lists all plugins.
|
2015-05-14 13:05:39 -04:00
|
|
|
Plugins() ([]*Plugin, error)
|
2015-07-25 04:35:07 -04:00
|
|
|
// Plugin returns the plugin registered with the given name (or returns an error).
|
2015-05-14 13:05:39 -04:00
|
|
|
Plugin(name string) (*Plugin, error)
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// LocalRegistry defines a registry that is local (using unix socket).
|
2015-06-15 18:35:49 -04:00
|
|
|
type LocalRegistry struct{}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
func newLocalRegistry() LocalRegistry {
|
|
|
|
return LocalRegistry{}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Plugin returns the plugin registered with the given name (or returns an error).
|
2015-05-14 13:05:39 -04:00
|
|
|
func (l *LocalRegistry) Plugin(name string) (*Plugin, error) {
|
2015-06-15 18:35:49 -04:00
|
|
|
socketpaths := pluginPaths(socketsPath, name, ".sock")
|
2015-05-27 18:21:18 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
for _, p := range socketpaths {
|
|
|
|
if fi, err := os.Stat(p); err == nil && fi.Mode()&os.ModeSocket != 0 {
|
|
|
|
return newLocalPlugin(name, "unix://"+p), nil
|
|
|
|
}
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
var txtspecpaths []string
|
|
|
|
for _, p := range specsPaths {
|
|
|
|
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".spec")...)
|
|
|
|
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".json")...)
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-05-27 18:21:18 -04:00
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
for _, p := range txtspecpaths {
|
|
|
|
if _, err := os.Stat(p); err == nil {
|
|
|
|
if strings.HasSuffix(p, ".json") {
|
|
|
|
return readPluginJSONInfo(name, p)
|
|
|
|
}
|
|
|
|
return readPluginInfo(name, p)
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
func readPluginInfo(name, path string) (*Plugin, error) {
|
2015-05-14 13:05:39 -04:00
|
|
|
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 18:21:18 -04:00
|
|
|
return newLocalPlugin(name, addr), 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 13:05:39 -04:00
|
|
|
}
|
2015-06-15 18:35:49 -04:00
|
|
|
|
|
|
|
func pluginPaths(base, name, ext string) []string {
|
|
|
|
return []string{
|
|
|
|
filepath.Join(base, name+ext),
|
|
|
|
filepath.Join(base, name, name+ext),
|
|
|
|
}
|
|
|
|
}
|