plugins.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "github.com/Sirupsen/logrus"
  28. "github.com/docker/docker/pkg/tlsconfig"
  29. )
  30. var (
  31. // ErrNotImplements is returned if the plugin does not implement the requested driver.
  32. ErrNotImplements = errors.New("Plugin does not implement the requested driver")
  33. )
  34. type plugins struct {
  35. sync.Mutex
  36. plugins map[string]*Plugin
  37. }
  38. var (
  39. storage = plugins{plugins: make(map[string]*Plugin)}
  40. extpointHandlers = make(map[string]func(string, *Client))
  41. )
  42. // Manifest lists what a plugin implements.
  43. type Manifest struct {
  44. // List of subsystem the plugin implements.
  45. Implements []string
  46. }
  47. // Plugin is the definition of a docker plugin.
  48. type Plugin struct {
  49. // Name of the plugin
  50. Name string `json:"-"`
  51. // Address of the plugin
  52. Addr string
  53. // TLS configuration of the plugin
  54. TLSConfig tlsconfig.Options
  55. // Client attached to the plugin
  56. Client *Client `json:"-"`
  57. // Manifest of the plugin (see above)
  58. Manifest *Manifest `json:"-"`
  59. }
  60. func newLocalPlugin(name, addr string) *Plugin {
  61. return &Plugin{
  62. Name: name,
  63. Addr: addr,
  64. TLSConfig: tlsconfig.Options{InsecureSkipVerify: true},
  65. }
  66. }
  67. func (p *Plugin) activate() error {
  68. c, err := NewClient(p.Addr, p.TLSConfig)
  69. if err != nil {
  70. return err
  71. }
  72. p.Client = c
  73. m := new(Manifest)
  74. if err = p.Client.Call("Plugin.Activate", nil, m); err != nil {
  75. return err
  76. }
  77. logrus.Debugf("%s's manifest: %v", p.Name, m)
  78. p.Manifest = m
  79. for _, iface := range m.Implements {
  80. handler, handled := extpointHandlers[iface]
  81. if !handled {
  82. continue
  83. }
  84. handler(p.Name, p.Client)
  85. }
  86. return nil
  87. }
  88. func load(name string) (*Plugin, error) {
  89. registry := newLocalRegistry()
  90. pl, err := registry.Plugin(name)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if err := pl.activate(); err != nil {
  95. return nil, err
  96. }
  97. return pl, nil
  98. }
  99. func get(name string) (*Plugin, error) {
  100. storage.Lock()
  101. defer storage.Unlock()
  102. pl, ok := storage.plugins[name]
  103. if ok {
  104. return pl, nil
  105. }
  106. pl, err := load(name)
  107. if err != nil {
  108. return nil, err
  109. }
  110. logrus.Debugf("Plugin: %v", pl)
  111. storage.plugins[name] = pl
  112. return pl, nil
  113. }
  114. // Get returns the plugin given the specified name and requested implementation.
  115. func Get(name, imp string) (*Plugin, error) {
  116. pl, err := get(name)
  117. if err != nil {
  118. return nil, err
  119. }
  120. for _, driver := range pl.Manifest.Implements {
  121. logrus.Debugf("%s implements: %s", name, driver)
  122. if driver == imp {
  123. return pl, nil
  124. }
  125. }
  126. return nil, ErrNotImplements
  127. }
  128. // Handle adds the specified function to the extpointHandlers.
  129. func Handle(iface string, fn func(string, *Client)) {
  130. extpointHandlers[iface] = fn
  131. }