plugin_routes.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package plugin
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "strings"
  7. "github.com/docker/docker/api/server/httputils"
  8. "github.com/docker/docker/api/types"
  9. "golang.org/x/net/context"
  10. )
  11. func (pr *pluginRouter) pullPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  12. if err := httputils.ParseForm(r); err != nil {
  13. return err
  14. }
  15. metaHeaders := map[string][]string{}
  16. for k, v := range r.Header {
  17. if strings.HasPrefix(k, "X-Meta-") {
  18. metaHeaders[k] = v
  19. }
  20. }
  21. // Get X-Registry-Auth
  22. authEncoded := r.Header.Get("X-Registry-Auth")
  23. authConfig := &types.AuthConfig{}
  24. if authEncoded != "" {
  25. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  26. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  27. authConfig = &types.AuthConfig{}
  28. }
  29. }
  30. privileges, err := pr.backend.Pull(r.FormValue("name"), metaHeaders, authConfig)
  31. if err != nil {
  32. return err
  33. }
  34. return httputils.WriteJSON(w, http.StatusOK, privileges)
  35. }
  36. func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  37. if err := httputils.ParseForm(r); err != nil {
  38. return err
  39. }
  40. options := &types.PluginCreateOptions{
  41. RepoName: r.FormValue("name")}
  42. if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
  43. return err
  44. }
  45. w.WriteHeader(http.StatusNoContent)
  46. return nil
  47. }
  48. func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  49. return pr.backend.Enable(vars["name"])
  50. }
  51. func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  52. return pr.backend.Disable(vars["name"])
  53. }
  54. func (pr *pluginRouter) removePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  55. if err := httputils.ParseForm(r); err != nil {
  56. return err
  57. }
  58. name := vars["name"]
  59. config := &types.PluginRmConfig{
  60. ForceRemove: httputils.BoolValue(r, "force"),
  61. }
  62. return pr.backend.Remove(name, config)
  63. }
  64. func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  65. if err := httputils.ParseForm(r); err != nil {
  66. return err
  67. }
  68. metaHeaders := map[string][]string{}
  69. for k, v := range r.Header {
  70. if strings.HasPrefix(k, "X-Meta-") {
  71. metaHeaders[k] = v
  72. }
  73. }
  74. // Get X-Registry-Auth
  75. authEncoded := r.Header.Get("X-Registry-Auth")
  76. authConfig := &types.AuthConfig{}
  77. if authEncoded != "" {
  78. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  79. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  80. authConfig = &types.AuthConfig{}
  81. }
  82. }
  83. return pr.backend.Push(vars["name"], metaHeaders, authConfig)
  84. }
  85. func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  86. var args []string
  87. if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
  88. return err
  89. }
  90. if err := pr.backend.Set(vars["name"], args); err != nil {
  91. return err
  92. }
  93. w.WriteHeader(http.StatusNoContent)
  94. return nil
  95. }
  96. func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  97. l, err := pr.backend.List()
  98. if err != nil {
  99. return err
  100. }
  101. return httputils.WriteJSON(w, http.StatusOK, l)
  102. }
  103. func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  104. result, err := pr.backend.Inspect(vars["name"])
  105. if err != nil {
  106. return err
  107. }
  108. return httputils.WriteJSON(w, http.StatusOK, result)
  109. }