-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
156 lines (131 loc) · 3.52 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package plugins // import "github.com/docker/docker/pkg/plugins"
import (
"encoding/json"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"github.com/pkg/errors"
)
var (
// ErrNotFound plugin not found
ErrNotFound = errors.New("plugin not found")
socketsPath = "/run/docker/plugins"
)
// localRegistry defines a registry that is local (using unix socket).
type localRegistry struct{}
func newLocalRegistry() localRegistry {
return localRegistry{}
}
// Scan scans all the plugin paths and returns all the names it found
func Scan() ([]string, error) {
var names []string
dirEntries, err := os.ReadDir(socketsPath)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "error reading dir entries")
}
for _, entry := range dirEntries {
if entry.IsDir() {
fi, err := os.Stat(filepath.Join(socketsPath, entry.Name(), entry.Name()+".sock"))
if err != nil {
continue
}
entry = fs.FileInfoToDirEntry(fi)
}
if entry.Type()&os.ModeSocket != 0 {
names = append(names, strings.TrimSuffix(filepath.Base(entry.Name()), filepath.Ext(entry.Name())))
}
}
for _, p := range specsPaths {
dirEntries, err := os.ReadDir(p)
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "error reading dir entries")
}
for _, fi := range dirEntries {
if fi.IsDir() {
infos, err := os.ReadDir(filepath.Join(p, fi.Name()))
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:
}
}
}
return names, nil
}
// Plugin returns the plugin registered with the given name (or returns an error).
func (l *localRegistry) Plugin(name string) (*Plugin, error) {
socketpaths := pluginPaths(socketsPath, name, ".sock")
for _, p := range socketpaths {
if fi, err := os.Stat(p); err == nil && fi.Mode()&os.ModeSocket != 0 {
return NewLocalPlugin(name, "unix://"+p), nil
}
}
var txtspecpaths []string
for _, p := range specsPaths {
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".spec")...)
txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".json")...)
}
for _, p := range txtspecpaths {
if _, err := os.Stat(p); err == nil {
if strings.HasSuffix(p, ".json") {
return readPluginJSONInfo(name, p)
}
return readPluginInfo(name, p)
}
}
return nil, errors.Wrapf(ErrNotFound, "could not find plugin %s in v1 plugin registry", name)
}
func readPluginInfo(name, path string) (*Plugin, error) {
content, err := os.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 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 p.TLSConfig != nil && len(p.TLSConfig.CAFile) == 0 {
p.TLSConfig.InsecureSkipVerify = true
}
p.activateWait = sync.NewCond(&sync.Mutex{})
return &p, nil
}
func pluginPaths(base, name, ext string) []string {
return []string{
filepath.Join(base, name+ext),
filepath.Join(base, name, name+ext),
}
}