backend.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // +build experimental
  2. package plugin
  3. import (
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/pkg/archive"
  10. "github.com/docker/docker/pkg/stringid"
  11. "github.com/docker/docker/plugin/distribution"
  12. "github.com/docker/docker/reference"
  13. "github.com/docker/engine-api/types"
  14. )
  15. // Disable deactivates a plugin, which implies that they cannot be used by containers.
  16. func (pm *Manager) Disable(name string) error {
  17. p, err := pm.get(name)
  18. if err != nil {
  19. return err
  20. }
  21. return pm.disable(p)
  22. }
  23. // Enable activates a plugin, which implies that they are ready to be used by containers.
  24. func (pm *Manager) Enable(name string) error {
  25. p, err := pm.get(name)
  26. if err != nil {
  27. return err
  28. }
  29. return pm.enable(p)
  30. }
  31. // Inspect examines a plugin manifest
  32. func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
  33. p, err := pm.get(name)
  34. if err != nil {
  35. return tp, err
  36. }
  37. return p.P, nil
  38. }
  39. // Pull pulls a plugin and enables it.
  40. func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
  41. ref, err := reference.ParseNamed(name)
  42. if err != nil {
  43. logrus.Debugf("error in reference.ParseNamed: %v", err)
  44. return nil, err
  45. }
  46. name = ref.String()
  47. if p, _ := pm.get(name); p != nil {
  48. logrus.Debugf("plugin already exists")
  49. return nil, fmt.Errorf("%s exists", name)
  50. }
  51. pluginID := stringid.GenerateNonCryptoID()
  52. if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
  53. logrus.Debugf("error in MkdirAll: %v", err)
  54. return nil, err
  55. }
  56. pd, err := distribution.Pull(name, pm.registryService, metaHeader, authConfig)
  57. if err != nil {
  58. logrus.Debugf("error in distribution.Pull(): %v", err)
  59. return nil, err
  60. }
  61. if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
  62. logrus.Debugf("error in distribution.WritePullData(): %v", err)
  63. return nil, err
  64. }
  65. p := pm.newPlugin(ref, pluginID)
  66. if err := pm.initPlugin(p); err != nil {
  67. return nil, err
  68. }
  69. pm.Lock()
  70. pm.plugins[pluginID] = p
  71. pm.nameToID[name] = pluginID
  72. pm.save()
  73. pm.Unlock()
  74. return computePrivileges(&p.P.Manifest), nil
  75. }
  76. // List displays the list of plugins and associated metadata.
  77. func (pm *Manager) List() ([]types.Plugin, error) {
  78. out := make([]types.Plugin, 0, len(pm.plugins))
  79. for _, p := range pm.plugins {
  80. out = append(out, p.P)
  81. }
  82. return out, nil
  83. }
  84. // Push pushes a plugin to the store.
  85. func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
  86. p, err := pm.get(name)
  87. dest := filepath.Join(pm.libRoot, p.P.ID)
  88. config, err := os.Open(filepath.Join(dest, "manifest.json"))
  89. if err != nil {
  90. return err
  91. }
  92. rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
  93. if err != nil {
  94. return err
  95. }
  96. _, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, config, rootfs)
  97. // XXX: Ignore returning digest for now.
  98. // Since digest needs to be written to the ProgressWriter.
  99. return err
  100. }
  101. // Remove deletes plugin's root directory.
  102. func (pm *Manager) Remove(name string) error {
  103. p, err := pm.get(name)
  104. if err != nil {
  105. return err
  106. }
  107. return pm.remove(p)
  108. }
  109. // Set sets plugin args
  110. func (pm *Manager) Set(name string, args []string) error {
  111. p, err := pm.get(name)
  112. if err != nil {
  113. return err
  114. }
  115. return pm.set(p, args)
  116. }