plugins.go 6.0 KB

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