plugin.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package v2 // import "github.com/docker/docker/plugin/v2"
  2. import (
  3. "fmt"
  4. "net"
  5. "path/filepath"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/pkg/plugingetter"
  11. "github.com/docker/docker/pkg/plugins"
  12. "github.com/opencontainers/go-digest"
  13. "github.com/opencontainers/runtime-spec/specs-go"
  14. )
  15. // Plugin represents an individual plugin.
  16. type Plugin struct {
  17. mu sync.RWMutex
  18. PluginObj types.Plugin `json:"plugin"` // todo: embed struct
  19. pClient *plugins.Client
  20. refCount int
  21. Rootfs string // TODO: make private
  22. Config digest.Digest
  23. Blobsums []digest.Digest
  24. modifyRuntimeSpec func(*specs.Spec)
  25. SwarmServiceID string
  26. timeout time.Duration
  27. addr net.Addr
  28. }
  29. const defaultPluginRuntimeDestination = "/run/docker/plugins"
  30. // ErrInadequateCapability indicates that the plugin did not have the requested capability.
  31. type ErrInadequateCapability struct {
  32. cap string
  33. }
  34. func (e ErrInadequateCapability) Error() string {
  35. return fmt.Sprintf("plugin does not provide %q capability", e.cap)
  36. }
  37. // ScopedPath returns the path scoped to the plugin rootfs
  38. func (p *Plugin) ScopedPath(s string) string {
  39. if p.PluginObj.Config.PropagatedMount != "" && strings.HasPrefix(s, p.PluginObj.Config.PropagatedMount) {
  40. // re-scope to the propagated mount path on the host
  41. return filepath.Join(filepath.Dir(p.Rootfs), "propagated-mount", strings.TrimPrefix(s, p.PluginObj.Config.PropagatedMount))
  42. }
  43. return filepath.Join(p.Rootfs, s)
  44. }
  45. // Client returns the plugin client.
  46. // Deprecated: use p.Addr() and manually create the client
  47. func (p *Plugin) Client() *plugins.Client {
  48. p.mu.RLock()
  49. defer p.mu.RUnlock()
  50. return p.pClient
  51. }
  52. // SetPClient set the plugin client.
  53. // Deprecated: Hardcoded plugin client is deprecated
  54. func (p *Plugin) SetPClient(client *plugins.Client) {
  55. p.mu.Lock()
  56. defer p.mu.Unlock()
  57. p.pClient = client
  58. }
  59. // IsV1 returns true for V1 plugins and false otherwise.
  60. func (p *Plugin) IsV1() bool {
  61. return false
  62. }
  63. // Name returns the plugin name.
  64. func (p *Plugin) Name() string {
  65. return p.PluginObj.Name
  66. }
  67. // FilterByCap query the plugin for a given capability.
  68. func (p *Plugin) FilterByCap(capability string) (*Plugin, error) {
  69. capability = strings.ToLower(capability)
  70. for _, typ := range p.PluginObj.Config.Interface.Types {
  71. if typ.Capability == capability && typ.Prefix == "docker" {
  72. return p, nil
  73. }
  74. }
  75. return nil, ErrInadequateCapability{capability}
  76. }
  77. // InitEmptySettings initializes empty settings for a plugin.
  78. func (p *Plugin) InitEmptySettings() {
  79. p.PluginObj.Settings.Mounts = make([]types.PluginMount, len(p.PluginObj.Config.Mounts))
  80. copy(p.PluginObj.Settings.Mounts, p.PluginObj.Config.Mounts)
  81. p.PluginObj.Settings.Devices = make([]types.PluginDevice, len(p.PluginObj.Config.Linux.Devices))
  82. copy(p.PluginObj.Settings.Devices, p.PluginObj.Config.Linux.Devices)
  83. p.PluginObj.Settings.Env = make([]string, 0, len(p.PluginObj.Config.Env))
  84. for _, env := range p.PluginObj.Config.Env {
  85. if env.Value != nil {
  86. p.PluginObj.Settings.Env = append(p.PluginObj.Settings.Env, fmt.Sprintf("%s=%s", env.Name, *env.Value))
  87. }
  88. }
  89. p.PluginObj.Settings.Args = make([]string, len(p.PluginObj.Config.Args.Value))
  90. copy(p.PluginObj.Settings.Args, p.PluginObj.Config.Args.Value)
  91. }
  92. // Set is used to pass arguments to the plugin.
  93. func (p *Plugin) Set(args []string) error {
  94. p.mu.Lock()
  95. defer p.mu.Unlock()
  96. if p.PluginObj.Enabled {
  97. return fmt.Errorf("cannot set on an active plugin, disable plugin before setting")
  98. }
  99. sets, err := newSettables(args)
  100. if err != nil {
  101. return err
  102. }
  103. // TODO(vieux): lots of code duplication here, needs to be refactored.
  104. next:
  105. for _, s := range sets {
  106. // range over all the envs in the config
  107. for _, env := range p.PluginObj.Config.Env {
  108. // found the env in the config
  109. if env.Name == s.name {
  110. // is it settable ?
  111. if ok, err := s.isSettable(allowedSettableFieldsEnv, env.Settable); err != nil {
  112. return err
  113. } else if !ok {
  114. return fmt.Errorf("%q is not settable", s.prettyName())
  115. }
  116. // is it, so lets update the settings in memory
  117. updateSettingsEnv(&p.PluginObj.Settings.Env, &s)
  118. continue next
  119. }
  120. }
  121. // range over all the mounts in the config
  122. for _, mount := range p.PluginObj.Config.Mounts {
  123. // found the mount in the config
  124. if mount.Name == s.name {
  125. // is it settable ?
  126. if ok, err := s.isSettable(allowedSettableFieldsMounts, mount.Settable); err != nil {
  127. return err
  128. } else if !ok {
  129. return fmt.Errorf("%q is not settable", s.prettyName())
  130. }
  131. // it is, so lets update the settings in memory
  132. if mount.Source == nil {
  133. return fmt.Errorf("Plugin config has no mount source")
  134. }
  135. *mount.Source = s.value
  136. continue next
  137. }
  138. }
  139. // range over all the devices in the config
  140. for _, device := range p.PluginObj.Config.Linux.Devices {
  141. // found the device in the config
  142. if device.Name == s.name {
  143. // is it settable ?
  144. if ok, err := s.isSettable(allowedSettableFieldsDevices, device.Settable); err != nil {
  145. return err
  146. } else if !ok {
  147. return fmt.Errorf("%q is not settable", s.prettyName())
  148. }
  149. // it is, so lets update the settings in memory
  150. if device.Path == nil {
  151. return fmt.Errorf("Plugin config has no device path")
  152. }
  153. *device.Path = s.value
  154. continue next
  155. }
  156. }
  157. // found the name in the config
  158. if p.PluginObj.Config.Args.Name == s.name {
  159. // is it settable ?
  160. if ok, err := s.isSettable(allowedSettableFieldsArgs, p.PluginObj.Config.Args.Settable); err != nil {
  161. return err
  162. } else if !ok {
  163. return fmt.Errorf("%q is not settable", s.prettyName())
  164. }
  165. // it is, so lets update the settings in memory
  166. p.PluginObj.Settings.Args = strings.Split(s.value, " ")
  167. continue next
  168. }
  169. return fmt.Errorf("setting %q not found in the plugin configuration", s.name)
  170. }
  171. return nil
  172. }
  173. // IsEnabled returns the active state of the plugin.
  174. func (p *Plugin) IsEnabled() bool {
  175. p.mu.RLock()
  176. defer p.mu.RUnlock()
  177. return p.PluginObj.Enabled
  178. }
  179. // GetID returns the plugin's ID.
  180. func (p *Plugin) GetID() string {
  181. p.mu.RLock()
  182. defer p.mu.RUnlock()
  183. return p.PluginObj.ID
  184. }
  185. // GetSocket returns the plugin socket.
  186. func (p *Plugin) GetSocket() string {
  187. p.mu.RLock()
  188. defer p.mu.RUnlock()
  189. return p.PluginObj.Config.Interface.Socket
  190. }
  191. // GetTypes returns the interface types of a plugin.
  192. func (p *Plugin) GetTypes() []types.PluginInterfaceType {
  193. p.mu.RLock()
  194. defer p.mu.RUnlock()
  195. return p.PluginObj.Config.Interface.Types
  196. }
  197. // GetRefCount returns the reference count.
  198. func (p *Plugin) GetRefCount() int {
  199. p.mu.RLock()
  200. defer p.mu.RUnlock()
  201. return p.refCount
  202. }
  203. // AddRefCount adds to reference count.
  204. func (p *Plugin) AddRefCount(count int) {
  205. p.mu.Lock()
  206. defer p.mu.Unlock()
  207. p.refCount += count
  208. }
  209. // Acquire increments the plugin's reference count
  210. // This should be followed up by `Release()` when the plugin is no longer in use.
  211. func (p *Plugin) Acquire() {
  212. p.AddRefCount(plugingetter.Acquire)
  213. }
  214. // Release decrements the plugin's reference count
  215. // This should only be called when the plugin is no longer in use, e.g. with
  216. // via `Acquire()` or getter.Get("name", "type", plugingetter.Acquire)
  217. func (p *Plugin) Release() {
  218. p.AddRefCount(plugingetter.Release)
  219. }
  220. // SetSpecOptModifier sets the function to use to modify the generated
  221. // runtime spec.
  222. func (p *Plugin) SetSpecOptModifier(f func(*specs.Spec)) {
  223. p.mu.Lock()
  224. p.modifyRuntimeSpec = f
  225. p.mu.Unlock()
  226. }
  227. // Timeout gets the currently configured connection timeout.
  228. // This should be used when dialing the plugin.
  229. func (p *Plugin) Timeout() time.Duration {
  230. p.mu.RLock()
  231. t := p.timeout
  232. p.mu.RUnlock()
  233. return t
  234. }
  235. // SetTimeout sets the timeout to use for dialing.
  236. func (p *Plugin) SetTimeout(t time.Duration) {
  237. p.mu.Lock()
  238. p.timeout = t
  239. p.mu.Unlock()
  240. }
  241. // Addr returns the net.Addr to use to connect to the plugin socket
  242. func (p *Plugin) Addr() net.Addr {
  243. p.mu.RLock()
  244. addr := p.addr
  245. p.mu.RUnlock()
  246. return addr
  247. }
  248. // SetAddr sets the plugin address which can be used for dialing the plugin.
  249. func (p *Plugin) SetAddr(addr net.Addr) {
  250. p.mu.Lock()
  251. p.addr = addr
  252. p.mu.Unlock()
  253. }
  254. // Protocol is the protocol that should be used for interacting with the plugin.
  255. func (p *Plugin) Protocol() string {
  256. if p.PluginObj.Config.Interface.ProtocolScheme != "" {
  257. return p.PluginObj.Config.Interface.ProtocolScheme
  258. }
  259. return plugins.ProtocolSchemeHTTPV1
  260. }