plugin.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package v2
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/pkg/plugingetter"
  8. "github.com/docker/docker/pkg/plugins"
  9. "github.com/opencontainers/go-digest"
  10. )
  11. // Plugin represents an individual plugin.
  12. type Plugin struct {
  13. mu sync.RWMutex
  14. PluginObj types.Plugin `json:"plugin"` // todo: embed struct
  15. pClient *plugins.Client
  16. refCount int
  17. PropagatedMount string // TODO: make private
  18. Rootfs string // TODO: make private
  19. Config digest.Digest
  20. Blobsums []digest.Digest
  21. }
  22. const defaultPluginRuntimeDestination = "/run/docker/plugins"
  23. // ErrInadequateCapability indicates that the plugin did not have the requested capability.
  24. type ErrInadequateCapability struct {
  25. cap string
  26. }
  27. func (e ErrInadequateCapability) Error() string {
  28. return fmt.Sprintf("plugin does not provide %q capability", e.cap)
  29. }
  30. // BasePath returns the path to which all paths returned by the plugin are relative to.
  31. // For Plugin objects this returns the host path of the plugin container's rootfs.
  32. func (p *Plugin) BasePath() string {
  33. return p.Rootfs
  34. }
  35. // Client returns the plugin client.
  36. func (p *Plugin) Client() *plugins.Client {
  37. p.mu.RLock()
  38. defer p.mu.RUnlock()
  39. return p.pClient
  40. }
  41. // SetPClient set the plugin client.
  42. func (p *Plugin) SetPClient(client *plugins.Client) {
  43. p.mu.Lock()
  44. defer p.mu.Unlock()
  45. p.pClient = client
  46. }
  47. // IsV1 returns true for V1 plugins and false otherwise.
  48. func (p *Plugin) IsV1() bool {
  49. return false
  50. }
  51. // Name returns the plugin name.
  52. func (p *Plugin) Name() string {
  53. return p.PluginObj.Name
  54. }
  55. // FilterByCap query the plugin for a given capability.
  56. func (p *Plugin) FilterByCap(capability string) (*Plugin, error) {
  57. capability = strings.ToLower(capability)
  58. for _, typ := range p.PluginObj.Config.Interface.Types {
  59. if typ.Capability == capability && typ.Prefix == "docker" {
  60. return p, nil
  61. }
  62. }
  63. return nil, ErrInadequateCapability{capability}
  64. }
  65. // InitEmptySettings initializes empty settings for a plugin.
  66. func (p *Plugin) InitEmptySettings() {
  67. p.PluginObj.Settings.Mounts = make([]types.PluginMount, len(p.PluginObj.Config.Mounts))
  68. copy(p.PluginObj.Settings.Mounts, p.PluginObj.Config.Mounts)
  69. p.PluginObj.Settings.Devices = make([]types.PluginDevice, len(p.PluginObj.Config.Linux.Devices))
  70. copy(p.PluginObj.Settings.Devices, p.PluginObj.Config.Linux.Devices)
  71. p.PluginObj.Settings.Env = make([]string, 0, len(p.PluginObj.Config.Env))
  72. for _, env := range p.PluginObj.Config.Env {
  73. if env.Value != nil {
  74. p.PluginObj.Settings.Env = append(p.PluginObj.Settings.Env, fmt.Sprintf("%s=%s", env.Name, *env.Value))
  75. }
  76. }
  77. p.PluginObj.Settings.Args = make([]string, len(p.PluginObj.Config.Args.Value))
  78. copy(p.PluginObj.Settings.Args, p.PluginObj.Config.Args.Value)
  79. }
  80. // Set is used to pass arguments to the plugin.
  81. func (p *Plugin) Set(args []string) error {
  82. p.mu.Lock()
  83. defer p.mu.Unlock()
  84. if p.PluginObj.Enabled {
  85. return fmt.Errorf("cannot set on an active plugin, disable plugin before setting")
  86. }
  87. sets, err := newSettables(args)
  88. if err != nil {
  89. return err
  90. }
  91. // TODO(vieux): lots of code duplication here, needs to be refactored.
  92. next:
  93. for _, s := range sets {
  94. // range over all the envs in the config
  95. for _, env := range p.PluginObj.Config.Env {
  96. // found the env in the config
  97. if env.Name == s.name {
  98. // is it settable ?
  99. if ok, err := s.isSettable(allowedSettableFieldsEnv, env.Settable); err != nil {
  100. return err
  101. } else if !ok {
  102. return fmt.Errorf("%q is not settable", s.prettyName())
  103. }
  104. // is it, so lets update the settings in memory
  105. updateSettingsEnv(&p.PluginObj.Settings.Env, &s)
  106. continue next
  107. }
  108. }
  109. // range over all the mounts in the config
  110. for _, mount := range p.PluginObj.Config.Mounts {
  111. // found the mount in the config
  112. if mount.Name == s.name {
  113. // is it settable ?
  114. if ok, err := s.isSettable(allowedSettableFieldsMounts, mount.Settable); err != nil {
  115. return err
  116. } else if !ok {
  117. return fmt.Errorf("%q is not settable", s.prettyName())
  118. }
  119. // it is, so lets update the settings in memory
  120. *mount.Source = s.value
  121. continue next
  122. }
  123. }
  124. // range over all the devices in the config
  125. for _, device := range p.PluginObj.Config.Linux.Devices {
  126. // found the device in the config
  127. if device.Name == s.name {
  128. // is it settable ?
  129. if ok, err := s.isSettable(allowedSettableFieldsDevices, device.Settable); err != nil {
  130. return err
  131. } else if !ok {
  132. return fmt.Errorf("%q is not settable", s.prettyName())
  133. }
  134. // it is, so lets update the settings in memory
  135. *device.Path = s.value
  136. continue next
  137. }
  138. }
  139. // found the name in the config
  140. if p.PluginObj.Config.Args.Name == s.name {
  141. // is it settable ?
  142. if ok, err := s.isSettable(allowedSettableFieldsArgs, p.PluginObj.Config.Args.Settable); err != nil {
  143. return err
  144. } else if !ok {
  145. return fmt.Errorf("%q is not settable", s.prettyName())
  146. }
  147. // it is, so lets update the settings in memory
  148. p.PluginObj.Settings.Args = strings.Split(s.value, " ")
  149. continue next
  150. }
  151. return fmt.Errorf("setting %q not found in the plugin configuration", s.name)
  152. }
  153. return nil
  154. }
  155. // IsEnabled returns the active state of the plugin.
  156. func (p *Plugin) IsEnabled() bool {
  157. p.mu.RLock()
  158. defer p.mu.RUnlock()
  159. return p.PluginObj.Enabled
  160. }
  161. // GetID returns the plugin's ID.
  162. func (p *Plugin) GetID() string {
  163. p.mu.RLock()
  164. defer p.mu.RUnlock()
  165. return p.PluginObj.ID
  166. }
  167. // GetSocket returns the plugin socket.
  168. func (p *Plugin) GetSocket() string {
  169. p.mu.RLock()
  170. defer p.mu.RUnlock()
  171. return p.PluginObj.Config.Interface.Socket
  172. }
  173. // GetTypes returns the interface types of a plugin.
  174. func (p *Plugin) GetTypes() []types.PluginInterfaceType {
  175. p.mu.RLock()
  176. defer p.mu.RUnlock()
  177. return p.PluginObj.Config.Interface.Types
  178. }
  179. // GetRefCount returns the reference count.
  180. func (p *Plugin) GetRefCount() int {
  181. p.mu.RLock()
  182. defer p.mu.RUnlock()
  183. return p.refCount
  184. }
  185. // AddRefCount adds to reference count.
  186. func (p *Plugin) AddRefCount(count int) {
  187. p.mu.Lock()
  188. defer p.mu.Unlock()
  189. p.refCount += count
  190. }
  191. // Acquire increments the plugin's reference count
  192. // This should be followed up by `Release()` when the plugin is no longer in use.
  193. func (p *Plugin) Acquire() {
  194. p.AddRefCount(plugingetter.Acquire)
  195. }
  196. // Release decrements the plugin's reference count
  197. // This should only be called when the plugin is no longer in use, e.g. with
  198. // via `Acquire()` or getter.Get("name", "type", plugingetter.Acquire)
  199. func (p *Plugin) Release() {
  200. p.AddRefCount(plugingetter.Release)
  201. }