plugins.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/docker/pkg/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. logrus.Debugf("%s's manifest: %v", p.Name, m)
  87. p.Manifest = m
  88. for _, iface := range m.Implements {
  89. handler, handled := extpointHandlers[iface]
  90. if !handled {
  91. continue
  92. }
  93. handler(p.Name, p.Client)
  94. }
  95. return nil
  96. }
  97. func load(name string) (*Plugin, error) {
  98. return loadWithRetry(name, true)
  99. }
  100. func loadWithRetry(name string, retry bool) (*Plugin, error) {
  101. registry := newLocalRegistry()
  102. start := time.Now()
  103. var retries int
  104. for {
  105. pl, err := registry.Plugin(name)
  106. if err != nil {
  107. if !retry {
  108. return nil, err
  109. }
  110. timeOff := backoff(retries)
  111. if abort(start, timeOff) {
  112. return nil, err
  113. }
  114. retries++
  115. logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
  116. time.Sleep(timeOff)
  117. continue
  118. }
  119. storage.Lock()
  120. storage.plugins[name] = pl
  121. storage.Unlock()
  122. err = pl.activate()
  123. if err != nil {
  124. storage.Lock()
  125. delete(storage.plugins, name)
  126. storage.Unlock()
  127. }
  128. return pl, err
  129. }
  130. }
  131. func get(name string) (*Plugin, error) {
  132. storage.Lock()
  133. pl, ok := storage.plugins[name]
  134. storage.Unlock()
  135. if ok {
  136. return pl, pl.activate()
  137. }
  138. return load(name)
  139. }
  140. // Get returns the plugin given the specified name and requested implementation.
  141. func Get(name, imp string) (*Plugin, error) {
  142. pl, err := get(name)
  143. if err != nil {
  144. return nil, err
  145. }
  146. for _, driver := range pl.Manifest.Implements {
  147. logrus.Debugf("%s implements: %s", name, driver)
  148. if driver == imp {
  149. return pl, nil
  150. }
  151. }
  152. return nil, ErrNotImplements
  153. }
  154. // Handle adds the specified function to the extpointHandlers.
  155. func Handle(iface string, fn func(string, *Client)) {
  156. extpointHandlers[iface] = fn
  157. }