backend_linux.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // +build linux
  2. package plugin
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/api/types"
  14. "github.com/docker/docker/pkg/archive"
  15. "github.com/docker/docker/pkg/chrootarchive"
  16. "github.com/docker/docker/pkg/stringid"
  17. "github.com/docker/docker/plugin/distribution"
  18. "github.com/docker/docker/plugin/v2"
  19. "golang.org/x/net/context"
  20. )
  21. // Disable deactivates a plugin, which implies that they cannot be used by containers.
  22. func (pm *Manager) Disable(name string) error {
  23. p, err := pm.pluginStore.GetByName(name)
  24. if err != nil {
  25. return err
  26. }
  27. if err := pm.disable(p); err != nil {
  28. return err
  29. }
  30. pm.pluginEventLogger(p.GetID(), name, "disable")
  31. return nil
  32. }
  33. // Enable activates a plugin, which implies that they are ready to be used by containers.
  34. func (pm *Manager) Enable(name string, config *types.PluginEnableConfig) error {
  35. p, err := pm.pluginStore.GetByName(name)
  36. if err != nil {
  37. return err
  38. }
  39. p.TimeoutInSecs = config.Timeout
  40. if err := pm.enable(p, false); err != nil {
  41. return err
  42. }
  43. pm.pluginEventLogger(p.GetID(), name, "enable")
  44. return nil
  45. }
  46. // Inspect examines a plugin config
  47. func (pm *Manager) Inspect(name string) (tp types.Plugin, err error) {
  48. p, err := pm.pluginStore.GetByName(name)
  49. if err != nil {
  50. return tp, err
  51. }
  52. return p.PluginObj, nil
  53. }
  54. // Pull pulls a plugin and computes the privileges required to install it.
  55. func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
  56. ref, err := distribution.GetRef(name)
  57. if err != nil {
  58. logrus.Debugf("error in distribution.GetRef: %v", err)
  59. return nil, err
  60. }
  61. name = ref.String()
  62. if p, _ := pm.pluginStore.GetByName(name); p != nil {
  63. logrus.Debug("plugin already exists")
  64. return nil, fmt.Errorf("%s exists", name)
  65. }
  66. pluginID := stringid.GenerateNonCryptoID()
  67. if err := os.MkdirAll(filepath.Join(pm.libRoot, pluginID), 0755); err != nil {
  68. logrus.Debugf("error in MkdirAll: %v", err)
  69. return nil, err
  70. }
  71. pd, err := distribution.Pull(ref, pm.registryService, metaHeader, authConfig)
  72. if err != nil {
  73. logrus.Debugf("error in distribution.Pull(): %v", err)
  74. return nil, err
  75. }
  76. if err := distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true); err != nil {
  77. logrus.Debugf("error in distribution.WritePullData(): %v", err)
  78. return nil, err
  79. }
  80. tag := distribution.GetTag(ref)
  81. p := v2.NewPlugin(ref.Name(), pluginID, pm.runRoot, pm.libRoot, tag)
  82. if err := p.InitPlugin(); err != nil {
  83. return nil, err
  84. }
  85. pm.pluginStore.Add(p)
  86. pm.pluginEventLogger(pluginID, name, "pull")
  87. return p.ComputePrivileges(), nil
  88. }
  89. // List displays the list of plugins and associated metadata.
  90. func (pm *Manager) List() ([]types.Plugin, error) {
  91. plugins := pm.pluginStore.GetAll()
  92. out := make([]types.Plugin, 0, len(plugins))
  93. for _, p := range plugins {
  94. out = append(out, p.PluginObj)
  95. }
  96. return out, nil
  97. }
  98. // Push pushes a plugin to the store.
  99. func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
  100. p, err := pm.pluginStore.GetByName(name)
  101. if err != nil {
  102. return err
  103. }
  104. dest := filepath.Join(pm.libRoot, p.GetID())
  105. config, err := ioutil.ReadFile(filepath.Join(dest, "config.json"))
  106. if err != nil {
  107. return err
  108. }
  109. var dummy types.Plugin
  110. err = json.Unmarshal(config, &dummy)
  111. if err != nil {
  112. return err
  113. }
  114. rootfs, err := archive.Tar(filepath.Join(dest, "rootfs"), archive.Gzip)
  115. if err != nil {
  116. return err
  117. }
  118. defer rootfs.Close()
  119. _, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, ioutil.NopCloser(bytes.NewReader(config)), rootfs)
  120. // XXX: Ignore returning digest for now.
  121. // Since digest needs to be written to the ProgressWriter.
  122. return err
  123. }
  124. // Remove deletes plugin's root directory.
  125. func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
  126. p, err := pm.pluginStore.GetByName(name)
  127. if err != nil {
  128. return err
  129. }
  130. if !config.ForceRemove {
  131. p.RLock()
  132. if p.RefCount > 0 {
  133. p.RUnlock()
  134. return fmt.Errorf("plugin %s is in use", p.Name())
  135. }
  136. p.RUnlock()
  137. if p.IsEnabled() {
  138. return fmt.Errorf("plugin %s is enabled", p.Name())
  139. }
  140. }
  141. if p.IsEnabled() {
  142. if err := pm.disable(p); err != nil {
  143. logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
  144. }
  145. }
  146. pm.pluginStore.Remove(p)
  147. os.RemoveAll(filepath.Join(pm.libRoot, p.GetID()))
  148. pm.pluginEventLogger(p.GetID(), name, "remove")
  149. return nil
  150. }
  151. // Set sets plugin args
  152. func (pm *Manager) Set(name string, args []string) error {
  153. p, err := pm.pluginStore.GetByName(name)
  154. if err != nil {
  155. return err
  156. }
  157. return p.Set(args)
  158. }
  159. // CreateFromContext creates a plugin from the given pluginDir which contains
  160. // both the rootfs and the config.json and a repoName with optional tag.
  161. func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.Reader, options *types.PluginCreateOptions) error {
  162. pluginID := stringid.GenerateNonCryptoID()
  163. pluginDir := filepath.Join(pm.libRoot, pluginID)
  164. if err := os.MkdirAll(pluginDir, 0755); err != nil {
  165. return err
  166. }
  167. // In case an error happens, remove the created directory.
  168. if err := pm.createFromContext(ctx, pluginID, pluginDir, tarCtx, options); err != nil {
  169. if err := os.RemoveAll(pluginDir); err != nil {
  170. logrus.Warnf("unable to remove %q from failed plugin creation: %v", pluginDir, err)
  171. }
  172. return err
  173. }
  174. return nil
  175. }
  176. func (pm *Manager) createFromContext(ctx context.Context, pluginID, pluginDir string, tarCtx io.Reader, options *types.PluginCreateOptions) error {
  177. if err := chrootarchive.Untar(tarCtx, pluginDir, nil); err != nil {
  178. return err
  179. }
  180. repoName := options.RepoName
  181. ref, err := distribution.GetRef(repoName)
  182. if err != nil {
  183. return err
  184. }
  185. name := ref.Name()
  186. tag := distribution.GetTag(ref)
  187. p := v2.NewPlugin(name, pluginID, pm.runRoot, pm.libRoot, tag)
  188. if err := p.InitPlugin(); err != nil {
  189. return err
  190. }
  191. if err := pm.pluginStore.Add(p); err != nil {
  192. return err
  193. }
  194. pm.pluginEventLogger(p.GetID(), repoName, "create")
  195. return nil
  196. }