Remote plugins plumbing.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
39a74950fe
commit
774251695e
5 changed files with 435 additions and 0 deletions
78
plugins/discovery.go
Normal file
78
plugins/discovery.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue