plugin_routes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package plugin
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/docker/docker/api/server/httputils"
  9. "github.com/docker/docker/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) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  38. if err := httputils.ParseForm(r); err != nil {
  39. return err
  40. }
  41. options := &types.PluginCreateOptions{
  42. RepoName: r.FormValue("name")}
  43. if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
  44. return err
  45. }
  46. w.WriteHeader(http.StatusNoContent)
  47. return nil
  48. }
  49. func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  50. if err := httputils.ParseForm(r); err != nil {
  51. return err
  52. }
  53. name := vars["name"]
  54. timeout, err := strconv.Atoi(r.Form.Get("timeout"))
  55. if err != nil {
  56. return err
  57. }
  58. config := &types.PluginEnableConfig{Timeout: timeout}
  59. return pr.backend.Enable(name, config)
  60. }
  61. func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  62. return pr.backend.Disable(vars["name"])
  63. }
  64. func (pr *pluginRouter) removePlugin(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. name := vars["name"]
  69. config := &types.PluginRmConfig{
  70. ForceRemove: httputils.BoolValue(r, "force"),
  71. }
  72. return pr.backend.Remove(name, config)
  73. }
  74. func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  75. if err := httputils.ParseForm(r); err != nil {
  76. return err
  77. }
  78. metaHeaders := map[string][]string{}
  79. for k, v := range r.Header {
  80. if strings.HasPrefix(k, "X-Meta-") {
  81. metaHeaders[k] = v
  82. }
  83. }
  84. // Get X-Registry-Auth
  85. authEncoded := r.Header.Get("X-Registry-Auth")
  86. authConfig := &types.AuthConfig{}
  87. if authEncoded != "" {
  88. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  89. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  90. authConfig = &types.AuthConfig{}
  91. }
  92. }
  93. return pr.backend.Push(vars["name"], metaHeaders, authConfig)
  94. }
  95. func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  96. var args []string
  97. if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
  98. return err
  99. }
  100. if err := pr.backend.Set(vars["name"], args); err != nil {
  101. return err
  102. }
  103. w.WriteHeader(http.StatusNoContent)
  104. return nil
  105. }
  106. func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  107. l, err := pr.backend.List()
  108. if err != nil {
  109. return err
  110. }
  111. return httputils.WriteJSON(w, http.StatusOK, l)
  112. }
  113. func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  114. result, err := pr.backend.Inspect(vars["name"])
  115. if err != nil {
  116. return err
  117. }
  118. return httputils.WriteJSON(w, http.StatusOK, result)
  119. }