plugins.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // error produced by activation
  61. activateErr error
  62. // specifies if the activation sequence is completed (not if it is successful or not)
  63. activated bool
  64. // wait for activation to finish
  65. activateWait *sync.Cond
  66. }
  67. func newLocalPlugin(name, addr string) *Plugin {
  68. return &Plugin{
  69. Name: name,
  70. Addr: addr,
  71. TLSConfig: tlsconfig.Options{InsecureSkipVerify: true},
  72. activateWait: sync.NewCond(&sync.Mutex{}),
  73. }
  74. }
  75. func (p *Plugin) activate() error {
  76. p.activateWait.L.Lock()
  77. if p.activated {
  78. p.activateWait.L.Unlock()
  79. return p.activateErr
  80. }
  81. p.activateErr = p.activateWithLock()
  82. p.activated = true
  83. p.activateWait.L.Unlock()
  84. p.activateWait.Broadcast()
  85. return p.activateErr
  86. }
  87. func (p *Plugin) activateWithLock() error {
  88. c, err := NewClient(p.Addr, p.TLSConfig)
  89. if err != nil {
  90. return err
  91. }
  92. p.Client = c
  93. m := new(Manifest)
  94. if err = p.Client.Call("Plugin.Activate", nil, m); err != nil {
  95. return err
  96. }
  97. p.Manifest = m
  98. for _, iface := range m.Implements {
  99. handler, handled := extpointHandlers[iface]
  100. if !handled {
  101. continue
  102. }
  103. handler(p.Name, p.Client)
  104. }
  105. return nil
  106. }
  107. func (p *Plugin) waitActive() error {
  108. p.activateWait.L.Lock()
  109. for !p.activated {
  110. p.activateWait.Wait()
  111. }
  112. p.activateWait.L.Unlock()
  113. return p.activateErr
  114. }
  115. func (p *Plugin) implements(kind string) bool {
  116. if err := p.waitActive(); err != nil {
  117. return false
  118. }
  119. for _, driver := range p.Manifest.Implements {
  120. if driver == kind {
  121. return true
  122. }
  123. }
  124. return false
  125. }
  126. func load(name string) (*Plugin, error) {
  127. return loadWithRetry(name, true)
  128. }
  129. func loadWithRetry(name string, retry bool) (*Plugin, error) {
  130. registry := newLocalRegistry()
  131. start := time.Now()
  132. var retries int
  133. for {
  134. pl, err := registry.Plugin(name)
  135. if err != nil {
  136. if !retry {
  137. return nil, err
  138. }
  139. timeOff := backoff(retries)
  140. if abort(start, timeOff) {
  141. return nil, err
  142. }
  143. retries++
  144. logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
  145. time.Sleep(timeOff)
  146. continue
  147. }
  148. storage.Lock()
  149. storage.plugins[name] = pl
  150. storage.Unlock()
  151. err = pl.activate()
  152. if err != nil {
  153. storage.Lock()
  154. delete(storage.plugins, name)
  155. storage.Unlock()
  156. }
  157. return pl, err
  158. }
  159. }
  160. func get(name string) (*Plugin, error) {
  161. storage.Lock()
  162. pl, ok := storage.plugins[name]
  163. storage.Unlock()
  164. if ok {
  165. return pl, pl.activate()
  166. }
  167. return load(name)
  168. }
  169. // Get returns the plugin given the specified name and requested implementation.
  170. func Get(name, imp string) (*Plugin, error) {
  171. pl, err := get(name)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if pl.implements(imp) {
  176. logrus.Debugf("%s implements: %s", name, imp)
  177. return pl, nil
  178. }
  179. return nil, ErrNotImplements
  180. }
  181. // Handle adds the specified function to the extpointHandlers.
  182. func Handle(iface string, fn func(string, *Client)) {
  183. extpointHandlers[iface] = fn
  184. }
  185. // GetAll returns all the plugins for the specified implementation
  186. func GetAll(imp string) ([]*Plugin, error) {
  187. pluginNames, err := Scan()
  188. if err != nil {
  189. return nil, err
  190. }
  191. type plLoad struct {
  192. pl *Plugin
  193. err error
  194. }
  195. chPl := make(chan *plLoad, len(pluginNames))
  196. var wg sync.WaitGroup
  197. for _, name := range pluginNames {
  198. if pl, ok := storage.plugins[name]; ok {
  199. chPl <- &plLoad{pl, nil}
  200. continue
  201. }
  202. wg.Add(1)
  203. go func(name string) {
  204. defer wg.Done()
  205. pl, err := loadWithRetry(name, false)
  206. chPl <- &plLoad{pl, err}
  207. }(name)
  208. }
  209. wg.Wait()
  210. close(chPl)
  211. var out []*Plugin
  212. for pl := range chPl {
  213. if pl.err != nil {
  214. logrus.Error(pl.err)
  215. continue
  216. }
  217. if pl.pl.implements(imp) {
  218. out = append(out, pl.pl)
  219. }
  220. }
  221. return out, nil
  222. }