123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package plugin
- import (
- "encoding/base64"
- "encoding/json"
- "net/http"
- "strconv"
- "strings"
- "github.com/docker/docker/api/server/httputils"
- "github.com/docker/docker/api/types"
- "golang.org/x/net/context"
- )
- func (pr *pluginRouter) pullPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- metaHeaders := map[string][]string{}
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- metaHeaders[k] = v
- }
- }
- // Get X-Registry-Auth
- authEncoded := r.Header.Get("X-Registry-Auth")
- authConfig := &types.AuthConfig{}
- if authEncoded != "" {
- authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
- if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
- authConfig = &types.AuthConfig{}
- }
- }
- privileges, err := pr.backend.Pull(r.FormValue("name"), metaHeaders, authConfig)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, privileges)
- }
- func (pr *pluginRouter) createPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- options := &types.PluginCreateOptions{
- RepoName: r.FormValue("name")}
- if err := pr.backend.CreateFromContext(ctx, r.Body, options); err != nil {
- return err
- }
- w.WriteHeader(http.StatusNoContent)
- return nil
- }
- func (pr *pluginRouter) enablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- name := vars["name"]
- timeout, err := strconv.Atoi(r.Form.Get("timeout"))
- if err != nil {
- return err
- }
- config := &types.PluginEnableConfig{Timeout: timeout}
- return pr.backend.Enable(name, config)
- }
- func (pr *pluginRouter) disablePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- return pr.backend.Disable(vars["name"])
- }
- func (pr *pluginRouter) removePlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- name := vars["name"]
- config := &types.PluginRmConfig{
- ForceRemove: httputils.BoolValue(r, "force"),
- }
- return pr.backend.Remove(name, config)
- }
- func (pr *pluginRouter) pushPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- metaHeaders := map[string][]string{}
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- metaHeaders[k] = v
- }
- }
- // Get X-Registry-Auth
- authEncoded := r.Header.Get("X-Registry-Auth")
- authConfig := &types.AuthConfig{}
- if authEncoded != "" {
- authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
- if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
- authConfig = &types.AuthConfig{}
- }
- }
- return pr.backend.Push(vars["name"], metaHeaders, authConfig)
- }
- func (pr *pluginRouter) setPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- var args []string
- if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
- return err
- }
- if err := pr.backend.Set(vars["name"], args); err != nil {
- return err
- }
- w.WriteHeader(http.StatusNoContent)
- return nil
- }
- func (pr *pluginRouter) listPlugins(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- l, err := pr.backend.List()
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, l)
- }
- func (pr *pluginRouter) inspectPlugin(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- result, err := pr.backend.Inspect(vars["name"])
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, result)
- }
|