plugin_routes.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if err := httputils.ParseForm(r); err != nil {
  45. return err
  46. }
  47. name := vars["name"]
  48. config := &types.PluginRmConfig{
  49. ForceRemove: httputils.BoolValue(r, "force"),
  50. }
  51. return pr.backend.Remove(name, config)
  52. }
  53. func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  54. if err := httputils.ParseForm(r); err != nil {
  55. return err
  56. }
  57. metaHeaders := map[string][]string{}
  58. for k, v := range r.Header {
  59. if strings.HasPrefix(k, "X-Meta-") {
  60. metaHeaders[k] = v
  61. }
  62. }
  63. // Get X-Registry-Auth
  64. authEncoded := r.Header.Get("X-Registry-Auth")
  65. authConfig := &types.AuthConfig{}
  66. if authEncoded != "" {
  67. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  68. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  69. authConfig = &types.AuthConfig{}
  70. }
  71. }
  72. return pr.backend.Push(vars["name"], metaHeaders, authConfig)
  73. }
  74. func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  75. var args []string
  76. if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
  77. return err
  78. }
  79. return pr.backend.Set(vars["name"], args)
  80. }
  81. func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  82. l, err := pr.backend.List()
  83. if err != nil {
  84. return err
  85. }
  86. return httputils.WriteJSON(w, http.StatusOK, l)
  87. }
  88. func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  89. result, err := pr.backend.Inspect(vars["name"])
  90. if err != nil {
  91. return err
  92. }
  93. return httputils.WriteJSON(w, http.StatusOK, result)
  94. }