2018-02-05 16:05:59 -05:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
import (
|
2015-05-27 18:21:18 -04:00
|
|
|
"encoding/json"
|
2015-05-14 13:05:39 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-04-20 08:38:34 -04:00
|
|
|
"sync"
|
2018-01-25 20:41:45 -05:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-07-25 04:35:07 -04:00
|
|
|
// ErrNotFound plugin not found
|
2015-12-20 13:44:01 -05:00
|
|
|
ErrNotFound = errors.New("plugin not found")
|
2015-06-15 18:35:49 -04:00
|
|
|
socketsPath = "/run/docker/plugins"
|
2015-05-14 13:05:39 -04:00
|
|
|
)
|
|
|
|
|
2015-12-17 05:05:50 -05:00
|
|
|
// localRegistry defines a registry that is local (using unix socket).
|
|
|
|
type localRegistry struct{}
|
2015-05-14 13:05:39 -04:00
|
|
|
|
2015-12-17 05:05:50 -05:00
|
|
|
func newLocalRegistry() localRegistry {
|
|
|
|
return localRegistry{}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
// Scan scans all the plugin paths and returns all the names it found
|
|
|
|
func Scan() ([]string, error) {
|
|
|
|
var names []string
|
2021-08-24 06:10:50 -04:00
|
|
|
dirEntries, err := os.ReadDir(socketsPath)
|
2018-01-25 20:41:45 -05:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, errors.Wrap(err, "error reading dir entries")
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
for _, entry := range dirEntries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
fi, err := os.Stat(filepath.Join(socketsPath, entry.Name(), entry.Name()+".sock"))
|
2018-01-25 20:41:45 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
|
2021-08-27 17:52:39 -04:00
|
|
|
entry = fileInfoToDirEntry(fi)
|
2015-09-23 16:29:14 -04:00
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if entry.Type()&os.ModeSocket != 0 {
|
|
|
|
names = append(names, strings.TrimSuffix(filepath.Base(entry.Name()), filepath.Ext(entry.Name())))
|
2015-09-23 16:29:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:41:45 -05:00
|
|
|
for _, p := range specsPaths {
|
2021-08-24 06:10:50 -04:00
|
|
|
dirEntries, err := os.ReadDir(p)
|
2018-01-25 20:41:45 -05:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, errors.Wrap(err, "error reading dir entries")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range dirEntries {
|
|
|
|
if fi.IsDir() {
|
2021-08-24 06:10:50 -04:00
|
|
|
infos, err := os.ReadDir(filepath.Join(p, fi.Name()))
|
2018-01-25 20:41:45 -05:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
if strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())) == fi.Name() {
|
|
|
|
fi = info
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(fi.Name())
|
|
|
|
switch ext {
|
|
|
|
case ".spec", ".json":
|
|
|
|
plugin := strings.TrimSuffix(fi.Name(), ext)
|
|
|
|
names = append(names, plugin)
|
|
|
|
default:
|
2015-09-23 16:29:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Plugin returns the plugin registered with the given name (or returns an error).
|
2015-12-17 05:05:50 -05: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 {
|
2016-05-16 11:50:55 -04:00
|
|
|
return NewLocalPlugin(name, "unix://"+p), nil
|
2015-06-15 18:35:49 -04:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 20:41:45 -05:00
|
|
|
return nil, errors.Wrapf(ErrNotFound, "could not find plugin %s in v1 plugin registry", name)
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 18:35:49 -04:00
|
|
|
func readPluginInfo(name, path string) (*Plugin, error) {
|
2021-08-24 06:10:50 -04:00
|
|
|
content, err := os.ReadFile(path)
|
2015-05-14 13:05:39 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
return NewLocalPlugin(name, addr), nil
|
2015-05-27 18:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-05-16 11:50:55 -04:00
|
|
|
p.name = name
|
2016-07-26 04:37:30 -04:00
|
|
|
if p.TLSConfig != nil && len(p.TLSConfig.CAFile) == 0 {
|
2015-05-27 18:21:18 -04:00
|
|
|
p.TLSConfig.InsecureSkipVerify = true
|
|
|
|
}
|
2016-04-20 08:38:34 -04:00
|
|
|
p.activateWait = sync.NewCond(&sync.Mutex{})
|
2015-05-27 18:21:18 -04:00
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|