plugins.go 6.4 KB

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