plugins.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 // import "github.com/docker/docker/pkg/plugins"
  24. import (
  25. "errors"
  26. "sync"
  27. "time"
  28. "github.com/docker/go-connections/tlsconfig"
  29. "github.com/sirupsen/logrus"
  30. )
  31. // ProtocolSchemeHTTPV1 is the name of the protocol used for interacting with plugins using this package.
  32. const ProtocolSchemeHTTPV1 = "moby.plugins.http/v1"
  33. var (
  34. // ErrNotImplements is returned if the plugin does not implement the requested driver.
  35. ErrNotImplements = errors.New("Plugin does not implement the requested driver")
  36. )
  37. type plugins struct {
  38. sync.Mutex
  39. plugins map[string]*Plugin
  40. }
  41. type extpointHandlers struct {
  42. sync.RWMutex
  43. extpointHandlers map[string][]func(string, *Client)
  44. }
  45. var (
  46. storage = plugins{plugins: make(map[string]*Plugin)}
  47. handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))}
  48. )
  49. // Manifest lists what a plugin implements.
  50. type Manifest struct {
  51. // List of subsystem the plugin implements.
  52. Implements []string
  53. }
  54. // Plugin is the definition of a docker plugin.
  55. type Plugin struct {
  56. // Name of the plugin
  57. name string
  58. // Address of the plugin
  59. Addr string
  60. // TLS configuration of the plugin
  61. TLSConfig *tlsconfig.Options
  62. // Client attached to the plugin
  63. client *Client
  64. // Manifest of the plugin (see above)
  65. Manifest *Manifest `json:"-"`
  66. // wait for activation to finish
  67. activateWait *sync.Cond
  68. // error produced by activation
  69. activateErr error
  70. // keeps track of callback handlers run against this plugin
  71. handlersRun bool
  72. }
  73. // Name returns the name of the plugin.
  74. func (p *Plugin) Name() string {
  75. return p.name
  76. }
  77. // Client returns a ready-to-use plugin client that can be used to communicate with the plugin.
  78. func (p *Plugin) Client() *Client {
  79. return p.client
  80. }
  81. // Protocol returns the protocol name/version used for plugins in this package.
  82. func (p *Plugin) Protocol() string {
  83. return ProtocolSchemeHTTPV1
  84. }
  85. // IsV1 returns true for V1 plugins and false otherwise.
  86. func (p *Plugin) IsV1() bool {
  87. return true
  88. }
  89. // NewLocalPlugin creates a new local plugin.
  90. func NewLocalPlugin(name, addr string) *Plugin {
  91. return &Plugin{
  92. name: name,
  93. Addr: addr,
  94. // TODO: change to nil
  95. TLSConfig: &tlsconfig.Options{InsecureSkipVerify: true},
  96. activateWait: sync.NewCond(&sync.Mutex{}),
  97. }
  98. }
  99. func (p *Plugin) activate() error {
  100. p.activateWait.L.Lock()
  101. if p.activated() {
  102. p.runHandlers()
  103. p.activateWait.L.Unlock()
  104. return p.activateErr
  105. }
  106. p.activateErr = p.activateWithLock()
  107. p.runHandlers()
  108. p.activateWait.L.Unlock()
  109. p.activateWait.Broadcast()
  110. return p.activateErr
  111. }
  112. // runHandlers runs the registered handlers for the implemented plugin types
  113. // This should only be run after activation, and while the activation lock is held.
  114. func (p *Plugin) runHandlers() {
  115. if !p.activated() {
  116. return
  117. }
  118. handlers.RLock()
  119. if !p.handlersRun {
  120. for _, iface := range p.Manifest.Implements {
  121. hdlrs, handled := handlers.extpointHandlers[iface]
  122. if !handled {
  123. continue
  124. }
  125. for _, handler := range hdlrs {
  126. handler(p.name, p.client)
  127. }
  128. }
  129. p.handlersRun = true
  130. }
  131. handlers.RUnlock()
  132. }
  133. // activated returns if the plugin has already been activated.
  134. // This should only be called with the activation lock held
  135. func (p *Plugin) activated() bool {
  136. return p.Manifest != nil
  137. }
  138. func (p *Plugin) activateWithLock() error {
  139. c, err := NewClient(p.Addr, p.TLSConfig)
  140. if err != nil {
  141. return err
  142. }
  143. p.client = c
  144. m := new(Manifest)
  145. if err = p.client.Call("Plugin.Activate", nil, m); err != nil {
  146. return err
  147. }
  148. p.Manifest = m
  149. return nil
  150. }
  151. func (p *Plugin) waitActive() error {
  152. p.activateWait.L.Lock()
  153. for !p.activated() && p.activateErr == nil {
  154. p.activateWait.Wait()
  155. }
  156. p.activateWait.L.Unlock()
  157. return p.activateErr
  158. }
  159. func (p *Plugin) implements(kind string) bool {
  160. if p.Manifest == nil {
  161. return false
  162. }
  163. for _, driver := range p.Manifest.Implements {
  164. if driver == kind {
  165. return true
  166. }
  167. }
  168. return false
  169. }
  170. func load(name string) (*Plugin, error) {
  171. return loadWithRetry(name, true)
  172. }
  173. func loadWithRetry(name string, retry bool) (*Plugin, error) {
  174. registry := newLocalRegistry()
  175. start := time.Now()
  176. var retries int
  177. for {
  178. pl, err := registry.Plugin(name)
  179. if err != nil {
  180. if !retry {
  181. return nil, err
  182. }
  183. timeOff := backoff(retries)
  184. if abort(start, timeOff) {
  185. return nil, err
  186. }
  187. retries++
  188. logrus.Warnf("Unable to locate plugin: %s, retrying in %v", name, timeOff)
  189. time.Sleep(timeOff)
  190. continue
  191. }
  192. storage.Lock()
  193. if pl, exists := storage.plugins[name]; exists {
  194. storage.Unlock()
  195. return pl, pl.activate()
  196. }
  197. storage.plugins[name] = pl
  198. storage.Unlock()
  199. err = pl.activate()
  200. if err != nil {
  201. storage.Lock()
  202. delete(storage.plugins, name)
  203. storage.Unlock()
  204. }
  205. return pl, err
  206. }
  207. }
  208. func get(name string) (*Plugin, error) {
  209. storage.Lock()
  210. pl, ok := storage.plugins[name]
  211. storage.Unlock()
  212. if ok {
  213. return pl, pl.activate()
  214. }
  215. return load(name)
  216. }
  217. // Get returns the plugin given the specified name and requested implementation.
  218. func Get(name, imp string) (*Plugin, error) {
  219. pl, err := get(name)
  220. if err != nil {
  221. return nil, err
  222. }
  223. if err := pl.waitActive(); err == nil && pl.implements(imp) {
  224. logrus.Debugf("%s implements: %s", name, imp)
  225. return pl, nil
  226. }
  227. return nil, ErrNotImplements
  228. }
  229. // Handle adds the specified function to the extpointHandlers.
  230. func Handle(iface string, fn func(string, *Client)) {
  231. handlers.Lock()
  232. hdlrs, ok := handlers.extpointHandlers[iface]
  233. if !ok {
  234. hdlrs = []func(string, *Client){}
  235. }
  236. hdlrs = append(hdlrs, fn)
  237. handlers.extpointHandlers[iface] = hdlrs
  238. storage.Lock()
  239. for _, p := range storage.plugins {
  240. p.activateWait.L.Lock()
  241. if p.activated() && p.implements(iface) {
  242. p.handlersRun = false
  243. }
  244. p.activateWait.L.Unlock()
  245. }
  246. storage.Unlock()
  247. handlers.Unlock()
  248. }
  249. // GetAll returns all the plugins for the specified implementation
  250. func GetAll(imp string) ([]*Plugin, error) {
  251. pluginNames, err := Scan()
  252. if err != nil {
  253. return nil, err
  254. }
  255. type plLoad struct {
  256. pl *Plugin
  257. err error
  258. }
  259. chPl := make(chan *plLoad, len(pluginNames))
  260. var wg sync.WaitGroup
  261. for _, name := range pluginNames {
  262. storage.Lock()
  263. pl, ok := storage.plugins[name]
  264. storage.Unlock()
  265. if ok {
  266. chPl <- &plLoad{pl, nil}
  267. continue
  268. }
  269. wg.Add(1)
  270. go func(name string) {
  271. defer wg.Done()
  272. pl, err := loadWithRetry(name, false)
  273. chPl <- &plLoad{pl, err}
  274. }(name)
  275. }
  276. wg.Wait()
  277. close(chPl)
  278. var out []*Plugin
  279. for pl := range chPl {
  280. if pl.err != nil {
  281. logrus.Error(pl.err)
  282. continue
  283. }
  284. if err := pl.pl.waitActive(); err == nil && pl.pl.implements(imp) {
  285. out = append(out, pl.pl)
  286. }
  287. }
  288. return out, nil
  289. }