plugin_routes.go 3.2 KB

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