plugins.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Package plugins provides structures and helper functions to manage Docker
  2. // plugins.
  3. //
  4. // Docker discovers plugins by looking for them in the plugin directory whenever
  5. // a user or container tries to use one by name. UNIX domain socket files must
  6. // be located under /run/docker/plugins, whereas spec files can be located
  7. // either under /etc/docker/plugins or /usr/lib/docker/plugins. This is handled
  8. // by the Registry interface, which lets you list all plugins or get a plugin by
  9. // its name if it exists.
  10. //
  11. // The plugins need to implement an HTTP server and bind this to the UNIX socket
  12. // or the address specified in the spec files.
  13. // A handshake is send at /Plugin.Activate, and plugins are expected to return
  14. // a Manifest with a list of of Docker subsystems which this plugin implements.
  15. //
  16. // In order to use a plugins, you can use the ``Get`` with the name of the
  17. // plugin and the subsystem it implements.
  18. //
  19. // plugin, err := plugins.Get("example", "VolumeDriver")
  20. // if err != nil {
  21. // return fmt.Errorf("Error looking up volume plugin example: %v", err)
  22. // }
  23. package plugins
  24. import (
  25. "errors"
  26. "sync"
  27. "time"
  28. "github.com/Sirupsen/logrus"
  29. "github.com/docker/go-connections/tlsconfig"
  30. )
  31. var (
  32. // ErrNotImplements is returned if the plugin does not implement the requested driver.
  33. ErrNotImplements = errors.New("Plugin does not implement the requested driver")
  34. )
  35. type plugins struct {
  36. sync.Mutex
  37. plugins map[string]*Plugin
  38. }
  39. var (
  40. storage = plugins{plugins: make(map[string]*Plugin)}
  41. extpointHandlers = make(map[string]func(string, *Client))
  42. )
  43. // Manifest lists what a plugin implements.
  44. type Manifest struct {
  45. // List of subsystem the plugin implements.
  46. Implements []string
  47. }
  48. // Plugin is the definition of a docker plugin.
  49. type Plugin struct {
  50. // Name of the plugin
  51. Name string `json:"-"`
  52. // Address of the plugin
  53. Addr string
  54. // TLS configuration of the plugin
  55. TLSConfig tlsconfig.Options
  56. // Client attached to the plugin
  57. Client *Client `json:"-"`
  58. // Manifest of the plugin (see above)
  59. Manifest *Manifest `json:"-"`
  60. activatErr error
  61. activateOnce sync.Once
  62. }
  63. func newLocalPlugin(name, addr string) *Plugin {
  64. return &Plugin{
  65. Name: name,
  66. Addr: addr,
  67. TLSConfig: tlsconfig.Options{InsecureSkipVerify: true},
  68. }
  69. }
  70. func (p *Plugin) activate() error {
  71. p.activateOnce.Do(func() {
  72. p.activatErr = p.activateWithLock()
  73. })
  74. return p.activatErr
  75. }
  76. func (p *Plugin) activateWithLock() error {
  77. c, err := NewClient(p.Addr, p.TLSConfig)
  78. if err != nil {
  79. return err
  80. }
  81. p.Client = c
  82. m := new(Manifest)
  83. if err = p.Client.Call("Plugin.Activate", nil, m); err != nil {
  84. return err
  85. }
  86. p.Manifest = m
  87. for _, iface := range m.Implements {
  88. handler, handled := extpointHandlers[iface]
  89. if !handled {
  90. continue
  91. }
  92. handler(p.Name, p.Client)
  93. }
  94. return nil
  95. }
  96. func (p *Plugin) implements(kind string) bool {
  97. for _, driver := range p.Manifest.Implements {
  98. if driver == kind {
  99. return true
  100. }
  101. }
  102. return false
  103. }
  104. func load(name string) (*Plugin, error) {
  105. return loadWithRetry(name, true)
  106. }
  107. func loadWithRetry(name string, retry bool) (*Plugin, error) {
  108. registry := newLocalRegistry()
  109. start := time.Now()
  110. var retries int
  111. for {
  112. pl, err := registry.Plugin(name)
  113. if err != nil {
  114. if !retry {
  115. return nil, err
  116. }
  117. timeOff := backoff(retries)
  118. if abort(start, timeOff) {
  119. return nil, err
  120. }
  121. retries++
  122. logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
  123. time.Sleep(timeOff)
  124. continue
  125. }
  126. storage.Lock()
  127. storage.plugins[name] = pl
  128. storage.Unlock()
  129. err = pl.activate()
  130. if err != nil {
  131. storage.Lock()
  132. delete(storage.plugins, name)
  133. storage.Unlock()
  134. }
  135. return pl, err
  136. }
  137. }
  138. func get(name string) (*Plugin, error) {
  139. storage.Lock()
  140. pl, ok := storage.plugins[name]
  141. storage.Unlock()
  142. if ok {
  143. return pl, pl.activate()
  144. }
  145. return load(name)
  146. }
  147. // Get returns the plugin given the specified name and requested implementation.
  148. func Get(name, imp string) (*Plugin, error) {
  149. pl, err := get(name)
  150. if err != nil {
  151. return nil, err
  152. }
  153. if pl.implements(imp) {
  154. logrus.Debugf("%s implements: %s", name, imp)
  155. return pl, nil
  156. }
  157. return nil, ErrNotImplements
  158. }
  159. // Handle adds the specified function to the extpointHandlers.
  160. func Handle(iface string, fn func(string, *Client)) {
  161. extpointHandlers[iface] = fn
  162. }
  163. // GetAll returns all the plugins for the specified implementation
  164. func GetAll(imp string) ([]*Plugin, error) {
  165. pluginNames, err := Scan()
  166. if err != nil {
  167. return nil, err
  168. }
  169. type plLoad struct {
  170. pl *Plugin
  171. err error
  172. }
  173. chPl := make(chan *plLoad, len(pluginNames))
  174. var wg sync.WaitGroup
  175. for _, name := range pluginNames {
  176. if pl, ok := storage.plugins[name]; ok {
  177. chPl <- &plLoad{pl, nil}
  178. continue
  179. }
  180. wg.Add(1)
  181. go func(name string) {
  182. defer wg.Done()
  183. pl, err := loadWithRetry(name, false)
  184. chPl <- &plLoad{pl, err}
  185. }(name)
  186. }
  187. wg.Wait()
  188. close(chPl)
  189. var out []*Plugin
  190. for pl := range chPl {
  191. if pl.err != nil {
  192. logrus.Error(err)
  193. continue
  194. }
  195. if pl.pl.implements(imp) {
  196. out = append(out, pl.pl)
  197. }
  198. }
  199. return out, nil
  200. }