discovery.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package plugins
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net/url"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "sync"
  12. )
  13. var (
  14. // ErrNotFound plugin not found
  15. ErrNotFound = errors.New("plugin not found")
  16. socketsPath = "/run/docker/plugins"
  17. )
  18. // localRegistry defines a registry that is local (using unix socket).
  19. type localRegistry struct{}
  20. func newLocalRegistry() localRegistry {
  21. return localRegistry{}
  22. }
  23. // Scan scans all the plugin paths and returns all the names it found
  24. func Scan() ([]string, error) {
  25. var names []string
  26. if err := filepath.Walk(socketsPath, func(path string, fi os.FileInfo, err error) error {
  27. if err != nil {
  28. return nil
  29. }
  30. if fi.Mode()&os.ModeSocket != 0 {
  31. name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))
  32. names = append(names, name)
  33. }
  34. return nil
  35. }); err != nil {
  36. return nil, err
  37. }
  38. for _, path := range specsPaths {
  39. if err := filepath.Walk(path, func(p string, fi os.FileInfo, err error) error {
  40. if err != nil || fi.IsDir() {
  41. return nil
  42. }
  43. name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))
  44. names = append(names, name)
  45. return nil
  46. }); err != nil {
  47. return nil, err
  48. }
  49. }
  50. return names, nil
  51. }
  52. // Plugin returns the plugin registered with the given name (or returns an error).
  53. func (l *localRegistry) Plugin(name string) (*Plugin, error) {
  54. socketpaths := pluginPaths(socketsPath, name, ".sock")
  55. for _, p := range socketpaths {
  56. if fi, err := os.Stat(p); err == nil && fi.Mode()&os.ModeSocket != 0 {
  57. return NewLocalPlugin(name, "unix://"+p), nil
  58. }
  59. }
  60. var txtspecpaths []string
  61. for _, p := range specsPaths {
  62. txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".spec")...)
  63. txtspecpaths = append(txtspecpaths, pluginPaths(p, name, ".json")...)
  64. }
  65. for _, p := range txtspecpaths {
  66. if _, err := os.Stat(p); err == nil {
  67. if strings.HasSuffix(p, ".json") {
  68. return readPluginJSONInfo(name, p)
  69. }
  70. return readPluginInfo(name, p)
  71. }
  72. }
  73. return nil, ErrNotFound
  74. }
  75. func readPluginInfo(name, path string) (*Plugin, error) {
  76. content, err := ioutil.ReadFile(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. addr := strings.TrimSpace(string(content))
  81. u, err := url.Parse(addr)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if len(u.Scheme) == 0 {
  86. return nil, fmt.Errorf("Unknown protocol")
  87. }
  88. return NewLocalPlugin(name, addr), nil
  89. }
  90. func readPluginJSONInfo(name, path string) (*Plugin, error) {
  91. f, err := os.Open(path)
  92. if err != nil {
  93. return nil, err
  94. }
  95. defer f.Close()
  96. var p Plugin
  97. if err := json.NewDecoder(f).Decode(&p); err != nil {
  98. return nil, err
  99. }
  100. p.name = name
  101. if p.TLSConfig != nil && len(p.TLSConfig.CAFile) == 0 {
  102. p.TLSConfig.InsecureSkipVerify = true
  103. }
  104. p.activateWait = sync.NewCond(&sync.Mutex{})
  105. return &p, nil
  106. }
  107. func pluginPaths(base, name, ext string) []string {
  108. return []string{
  109. filepath.Join(base, name+ext),
  110. filepath.Join(base, name, name+ext),
  111. }
  112. }