plugin_routes.go 3.0 KB

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