plugin_experimental.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // +build experimental
  2. package v2
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/pkg/plugins"
  12. "github.com/docker/docker/pkg/system"
  13. "github.com/opencontainers/runtime-spec/specs-go"
  14. )
  15. const defaultPluginRuntimeDestination = "/run/docker/plugins"
  16. // ErrInadequateCapability indicates that the plugin did not have the requested capability.
  17. type ErrInadequateCapability string
  18. func (cap ErrInadequateCapability) Error() string {
  19. return fmt.Sprintf("plugin does not provide %q capability", cap)
  20. }
  21. func newPluginObj(name, id, tag string) types.Plugin {
  22. return types.Plugin{Name: name, ID: id, Tag: tag}
  23. }
  24. // NewPlugin creates a plugin.
  25. func NewPlugin(name, id, runRoot, tag string) *Plugin {
  26. return &Plugin{
  27. PluginObj: newPluginObj(name, id, tag),
  28. RuntimeSourcePath: filepath.Join(runRoot, id),
  29. }
  30. }
  31. // Client returns the plugin client.
  32. func (p *Plugin) Client() *plugins.Client {
  33. return p.PClient
  34. }
  35. // IsV1 returns true for V1 plugins and false otherwise.
  36. func (p *Plugin) IsV1() bool {
  37. return false
  38. }
  39. // Name returns the plugin name.
  40. func (p *Plugin) Name() string {
  41. name := p.PluginObj.Name
  42. if len(p.PluginObj.Tag) > 0 {
  43. // TODO: this feels hacky, maybe we should be storing the distribution reference rather than splitting these
  44. name += ":" + p.PluginObj.Tag
  45. }
  46. return name
  47. }
  48. // FilterByCap query the plugin for a given capability.
  49. func (p *Plugin) FilterByCap(capability string) (*Plugin, error) {
  50. capability = strings.ToLower(capability)
  51. for _, typ := range p.PluginObj.Manifest.Interface.Types {
  52. if typ.Capability == capability && typ.Prefix == "docker" {
  53. return p, nil
  54. }
  55. }
  56. return nil, ErrInadequateCapability(capability)
  57. }
  58. // RemoveFromDisk deletes the plugin's runtime files from disk.
  59. func (p *Plugin) RemoveFromDisk() error {
  60. return os.RemoveAll(p.RuntimeSourcePath)
  61. }
  62. // InitPlugin populates the plugin object from the plugin manifest file.
  63. func (p *Plugin) InitPlugin(libRoot string) error {
  64. dt, err := os.Open(filepath.Join(libRoot, p.PluginObj.ID, "manifest.json"))
  65. if err != nil {
  66. return err
  67. }
  68. err = json.NewDecoder(dt).Decode(&p.PluginObj.Manifest)
  69. dt.Close()
  70. if err != nil {
  71. return err
  72. }
  73. p.PluginObj.Config.Mounts = make([]types.PluginMount, len(p.PluginObj.Manifest.Mounts))
  74. for i, mount := range p.PluginObj.Manifest.Mounts {
  75. p.PluginObj.Config.Mounts[i] = mount
  76. }
  77. p.PluginObj.Config.Env = make([]string, 0, len(p.PluginObj.Manifest.Env))
  78. for _, env := range p.PluginObj.Manifest.Env {
  79. if env.Value != nil {
  80. p.PluginObj.Config.Env = append(p.PluginObj.Config.Env, fmt.Sprintf("%s=%s", env.Name, *env.Value))
  81. }
  82. }
  83. copy(p.PluginObj.Config.Args, p.PluginObj.Manifest.Args.Value)
  84. f, err := os.Create(filepath.Join(libRoot, p.PluginObj.ID, "plugin-config.json"))
  85. if err != nil {
  86. return err
  87. }
  88. err = json.NewEncoder(f).Encode(&p.PluginObj.Config)
  89. f.Close()
  90. return err
  91. }
  92. // Set is used to pass arguments to the plugin.
  93. func (p *Plugin) Set(args []string) error {
  94. m := make(map[string]string, len(args))
  95. for _, arg := range args {
  96. i := strings.Index(arg, "=")
  97. if i < 0 {
  98. return fmt.Errorf("No equal sign '=' found in %s", arg)
  99. }
  100. m[arg[:i]] = arg[i+1:]
  101. }
  102. return errors.New("not implemented")
  103. }
  104. // ComputePrivileges takes the manifest file and computes the list of access necessary
  105. // for the plugin on the host.
  106. func (p *Plugin) ComputePrivileges() types.PluginPrivileges {
  107. m := p.PluginObj.Manifest
  108. var privileges types.PluginPrivileges
  109. if m.Network.Type != "null" && m.Network.Type != "bridge" {
  110. privileges = append(privileges, types.PluginPrivilege{
  111. Name: "network",
  112. Description: "",
  113. Value: []string{m.Network.Type},
  114. })
  115. }
  116. for _, mount := range m.Mounts {
  117. if mount.Source != nil {
  118. privileges = append(privileges, types.PluginPrivilege{
  119. Name: "mount",
  120. Description: "",
  121. Value: []string{*mount.Source},
  122. })
  123. }
  124. }
  125. for _, device := range m.Devices {
  126. if device.Path != nil {
  127. privileges = append(privileges, types.PluginPrivilege{
  128. Name: "device",
  129. Description: "",
  130. Value: []string{*device.Path},
  131. })
  132. }
  133. }
  134. if len(m.Capabilities) > 0 {
  135. privileges = append(privileges, types.PluginPrivilege{
  136. Name: "capabilities",
  137. Description: "",
  138. Value: m.Capabilities,
  139. })
  140. }
  141. return privileges
  142. }
  143. // IsEnabled returns the active state of the plugin.
  144. func (p *Plugin) IsEnabled() bool {
  145. p.RLock()
  146. defer p.RUnlock()
  147. return p.PluginObj.Enabled
  148. }
  149. // GetID returns the plugin's ID.
  150. func (p *Plugin) GetID() string {
  151. p.RLock()
  152. defer p.RUnlock()
  153. return p.PluginObj.ID
  154. }
  155. // GetSocket returns the plugin socket.
  156. func (p *Plugin) GetSocket() string {
  157. p.RLock()
  158. defer p.RUnlock()
  159. return p.PluginObj.Manifest.Interface.Socket
  160. }
  161. // GetTypes returns the interface types of a plugin.
  162. func (p *Plugin) GetTypes() []types.PluginInterfaceType {
  163. p.RLock()
  164. defer p.RUnlock()
  165. return p.PluginObj.Manifest.Interface.Types
  166. }
  167. // InitSpec creates an OCI spec from the plugin's config.
  168. func (p *Plugin) InitSpec(s specs.Spec, libRoot string) (*specs.Spec, error) {
  169. rootfs := filepath.Join(libRoot, p.PluginObj.ID, "rootfs")
  170. s.Root = specs.Root{
  171. Path: rootfs,
  172. Readonly: false, // TODO: all plugins should be readonly? settable in manifest?
  173. }
  174. mounts := append(p.PluginObj.Config.Mounts, types.PluginMount{
  175. Source: &p.RuntimeSourcePath,
  176. Destination: defaultPluginRuntimeDestination,
  177. Type: "bind",
  178. Options: []string{"rbind", "rshared"},
  179. })
  180. for _, mount := range mounts {
  181. m := specs.Mount{
  182. Destination: mount.Destination,
  183. Type: mount.Type,
  184. Options: mount.Options,
  185. }
  186. // TODO: if nil, then it's required and user didn't set it
  187. if mount.Source != nil {
  188. m.Source = *mount.Source
  189. }
  190. if m.Source != "" && m.Type == "bind" {
  191. fi, err := os.Lstat(filepath.Join(rootfs, m.Destination)) // TODO: followsymlinks
  192. if err != nil {
  193. return nil, err
  194. }
  195. if fi.IsDir() {
  196. if err := os.MkdirAll(m.Source, 0700); err != nil {
  197. return nil, err
  198. }
  199. }
  200. }
  201. s.Mounts = append(s.Mounts, m)
  202. }
  203. envs := make([]string, 1, len(p.PluginObj.Config.Env)+1)
  204. envs[0] = "PATH=" + system.DefaultPathEnv
  205. envs = append(envs, p.PluginObj.Config.Env...)
  206. args := append(p.PluginObj.Manifest.Entrypoint, p.PluginObj.Config.Args...)
  207. cwd := p.PluginObj.Manifest.Workdir
  208. if len(cwd) == 0 {
  209. cwd = "/"
  210. }
  211. s.Process = specs.Process{
  212. Terminal: false,
  213. Args: args,
  214. Cwd: cwd,
  215. Env: envs,
  216. }
  217. return &s, nil
  218. }