plugins.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. // wait for activation to finish
  65. activateWait *sync.Cond
  66. // error produced by activation
  67. activateErr error
  68. // keeps track of callback handlers run against this plugin
  69. handlersRun bool
  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.runHandlers()
  97. p.activateWait.L.Unlock()
  98. return p.activateErr
  99. }
  100. p.activateErr = p.activateWithLock()
  101. p.runHandlers()
  102. p.activateWait.L.Unlock()
  103. p.activateWait.Broadcast()
  104. return p.activateErr
  105. }
  106. // runHandlers runs the registered handlers for the implemented plugin types
  107. // This should only be run after activation, and while the activation lock is held.
  108. func (p *Plugin) runHandlers() {
  109. if !p.activated() {
  110. return
  111. }
  112. handlers.RLock()
  113. if !p.handlersRun {
  114. for _, iface := range p.Manifest.Implements {
  115. hdlrs, handled := handlers.extpointHandlers[iface]
  116. if !handled {
  117. continue
  118. }
  119. for _, handler := range hdlrs {
  120. handler(p.name, p.client)
  121. }
  122. }
  123. p.handlersRun = true
  124. }
  125. handlers.RUnlock()
  126. }
  127. // activated returns if the plugin has already been activated.
  128. // This should only be called with the activation lock held
  129. func (p *Plugin) activated() bool {
  130. return p.Manifest != nil
  131. }
  132. func (p *Plugin) activateWithLock() error {
  133. c, err := NewClient(p.Addr, p.TLSConfig)
  134. if err != nil {
  135. return err
  136. }
  137. p.client = c
  138. m := new(Manifest)
  139. if err = p.client.Call("Plugin.Activate", nil, m); err != nil {
  140. return err
  141. }
  142. p.Manifest = m
  143. return nil
  144. }
  145. func (p *Plugin) waitActive() error {
  146. p.activateWait.L.Lock()
  147. for !p.activated() && p.activateErr == nil {
  148. p.activateWait.Wait()
  149. }
  150. p.activateWait.L.Unlock()
  151. return p.activateErr
  152. }
  153. func (p *Plugin) implements(kind string) bool {
  154. if p.Manifest == nil {
  155. return false
  156. }
  157. for _, driver := range p.Manifest.Implements {
  158. if driver == kind {
  159. return true
  160. }
  161. }
  162. return false
  163. }
  164. func load(name string) (*Plugin, error) {
  165. return loadWithRetry(name, true)
  166. }
  167. func loadWithRetry(name string, retry bool) (*Plugin, error) {
  168. registry := newLocalRegistry()
  169. start := time.Now()
  170. var retries int
  171. for {
  172. pl, err := registry.Plugin(name)
  173. if err != nil {
  174. if !retry {
  175. return nil, err
  176. }
  177. timeOff := backoff(retries)
  178. if abort(start, timeOff) {
  179. return nil, err
  180. }
  181. retries++
  182. logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
  183. time.Sleep(timeOff)
  184. continue
  185. }
  186. storage.Lock()
  187. if pl, exists := storage.plugins[name]; exists {
  188. storage.Unlock()
  189. return pl, pl.activate()
  190. }
  191. storage.plugins[name] = pl
  192. storage.Unlock()
  193. err = pl.activate()
  194. if err != nil {
  195. storage.Lock()
  196. delete(storage.plugins, name)
  197. storage.Unlock()
  198. }
  199. return pl, err
  200. }
  201. }
  202. func get(name string) (*Plugin, error) {
  203. storage.Lock()
  204. pl, ok := storage.plugins[name]
  205. storage.Unlock()
  206. if ok {
  207. return pl, pl.activate()
  208. }
  209. return load(name)
  210. }
  211. // Get returns the plugin given the specified name and requested implementation.
  212. func Get(name, imp string) (*Plugin, error) {
  213. pl, err := get(name)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if err := pl.waitActive(); err == nil && pl.implements(imp) {
  218. logrus.Debugf("%s implements: %s", name, imp)
  219. return pl, nil
  220. }
  221. return nil, ErrNotImplements
  222. }
  223. // Handle adds the specified function to the extpointHandlers.
  224. func Handle(iface string, fn func(string, *Client)) {
  225. handlers.Lock()
  226. hdlrs, ok := handlers.extpointHandlers[iface]
  227. if !ok {
  228. hdlrs = []func(string, *Client){}
  229. }
  230. hdlrs = append(hdlrs, fn)
  231. handlers.extpointHandlers[iface] = hdlrs
  232. storage.Lock()
  233. for _, p := range storage.plugins {
  234. p.activateWait.L.Lock()
  235. if p.activated() && p.implements(iface) {
  236. p.handlersRun = false
  237. }
  238. p.activateWait.L.Unlock()
  239. }
  240. storage.Unlock()
  241. handlers.Unlock()
  242. }
  243. // GetAll returns all the plugins for the specified implementation
  244. func GetAll(imp string) ([]*Plugin, error) {
  245. pluginNames, err := Scan()
  246. if err != nil {
  247. return nil, err
  248. }
  249. type plLoad struct {
  250. pl *Plugin
  251. err error
  252. }
  253. chPl := make(chan *plLoad, len(pluginNames))
  254. var wg sync.WaitGroup
  255. for _, name := range pluginNames {
  256. storage.Lock()
  257. pl, ok := storage.plugins[name]
  258. storage.Unlock()
  259. if ok {
  260. chPl <- &plLoad{pl, nil}
  261. continue
  262. }
  263. wg.Add(1)
  264. go func(name string) {
  265. defer wg.Done()
  266. pl, err := loadWithRetry(name, false)
  267. chPl <- &plLoad{pl, err}
  268. }(name)
  269. }
  270. wg.Wait()
  271. close(chPl)
  272. var out []*Plugin
  273. for pl := range chPl {
  274. if pl.err != nil {
  275. logrus.Error(pl.err)
  276. continue
  277. }
  278. if err := pl.pl.waitActive(); err == nil && pl.pl.implements(imp) {
  279. out = append(out, pl.pl)
  280. }
  281. }
  282. return out, nil
  283. }