plugin.go 8.3 KB

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