123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- package image
- import (
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "strings"
- "github.com/docker/docker/api/server/httputils"
- "github.com/docker/docker/api/types/backend"
- "github.com/docker/docker/pkg/ioutils"
- "github.com/docker/docker/pkg/streamformatter"
- "github.com/docker/docker/registry"
- "github.com/docker/engine-api/types"
- "github.com/docker/engine-api/types/container"
- "github.com/docker/engine-api/types/versions"
- "golang.org/x/net/context"
- )
- func (s *imageRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- if err := httputils.CheckForJSON(r); err != nil {
- return err
- }
- cname := r.Form.Get("container")
- pause := httputils.BoolValue(r, "pause")
- version := httputils.VersionFromContext(ctx)
- if r.FormValue("pause") == "" && versions.GreaterThanOrEqualTo(version, "1.13") {
- pause = true
- }
- c, _, _, err := s.decoder.DecodeConfig(r.Body)
- if err != nil && err != io.EOF { //Do not fail if body is empty.
- return err
- }
- if c == nil {
- c = &container.Config{}
- }
- commitCfg := &backend.ContainerCommitConfig{
- ContainerCommitConfig: types.ContainerCommitConfig{
- Pause: pause,
- Repo: r.Form.Get("repo"),
- Tag: r.Form.Get("tag"),
- Author: r.Form.Get("author"),
- Comment: r.Form.Get("comment"),
- Config: c,
- MergeConfigs: true,
- },
- Changes: r.Form["changes"],
- }
- imgID, err := s.backend.Commit(cname, commitCfg)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerCommitResponse{
- ID: string(imgID),
- })
- }
- // Creates an image from Pull or from Import
- func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- var (
- image = r.Form.Get("fromImage")
- repo = r.Form.Get("repo")
- tag = r.Form.Get("tag")
- message = r.Form.Get("message")
- err error
- output = ioutils.NewWriteFlusher(w)
- )
- defer output.Close()
- w.Header().Set("Content-Type", "application/json")
- if image != "" { //pull
- metaHeaders := map[string][]string{}
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- metaHeaders[k] = v
- }
- }
- 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 {
- // for a pull it is not an error if no auth was given
- // to increase compatibility with the existing api it is defaulting to be empty
- authConfig = &types.AuthConfig{}
- }
- }
- err = s.backend.PullImage(ctx, image, tag, metaHeaders, authConfig, output)
- } else { //import
- src := r.Form.Get("fromSrc")
- // 'err' MUST NOT be defined within this block, we need any error
- // generated from the download to be available to the output
- // stream processing below
- err = s.backend.ImportImage(src, repo, tag, message, r.Body, output, r.Form["changes"])
- }
- if err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *imageRouter) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- metaHeaders := map[string][]string{}
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- metaHeaders[k] = v
- }
- }
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- authConfig := &types.AuthConfig{}
- authEncoded := r.Header.Get("X-Registry-Auth")
- if authEncoded != "" {
- // the new format is to handle the authConfig as a header
- authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
- if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
- // to increase compatibility to existing api it is defaulting to be empty
- authConfig = &types.AuthConfig{}
- }
- } else {
- // the old format is supported for compatibility if there was no authConfig header
- if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil {
- return fmt.Errorf("Bad parameters and missing X-Registry-Auth: %v", err)
- }
- }
- image := vars["name"]
- tag := r.Form.Get("tag")
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- w.Header().Set("Content-Type", "application/json")
- if err := s.backend.PushImage(ctx, image, tag, metaHeaders, authConfig, output); err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *imageRouter) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- w.Header().Set("Content-Type", "application/x-tar")
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- var names []string
- if name, ok := vars["name"]; ok {
- names = []string{name}
- } else {
- names = r.Form["names"]
- }
- if err := s.backend.ExportImage(names, output); err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *imageRouter) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- quiet := httputils.BoolValueOrDefault(r, "quiet", true)
- if !quiet {
- w.Header().Set("Content-Type", "application/json")
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- if err := s.backend.LoadImage(r.Body, output, quiet); err != nil {
- output.Write(streamformatter.NewJSONStreamFormatter().FormatError(err))
- }
- return nil
- }
- return s.backend.LoadImage(r.Body, w, quiet)
- }
- func (s *imageRouter) deleteImages(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"]
- if strings.TrimSpace(name) == "" {
- return fmt.Errorf("image name cannot be blank")
- }
- force := httputils.BoolValue(r, "force")
- prune := !httputils.BoolValue(r, "noprune")
- list, err := s.backend.ImageDelete(name, force, prune)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, list)
- }
- func (s *imageRouter) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- imageInspect, err := s.backend.LookupImage(vars["name"])
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, imageInspect)
- }
- func (s *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- // FIXME: The filter parameter could just be a match filter
- images, err := s.backend.Images(r.Form.Get("filters"), r.Form.Get("filter"), httputils.BoolValue(r, "all"))
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, images)
- }
- func (s *imageRouter) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- name := vars["name"]
- history, err := s.backend.ImageHistory(name)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, history)
- }
- func (s *imageRouter) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- if err := s.backend.TagImage(vars["name"], r.Form.Get("repo"), r.Form.Get("tag")); err != nil {
- return err
- }
- w.WriteHeader(http.StatusCreated)
- return nil
- }
- func (s *imageRouter) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- var (
- config *types.AuthConfig
- authEncoded = r.Header.Get("X-Registry-Auth")
- headers = map[string][]string{}
- )
- if authEncoded != "" {
- authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
- if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
- // for a search it is not an error if no auth was given
- // to increase compatibility with the existing api it is defaulting to be empty
- config = &types.AuthConfig{}
- }
- }
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- headers[k] = v
- }
- }
- limit := registry.DefaultSearchLimit
- if r.Form.Get("limit") != "" {
- limitValue, err := strconv.Atoi(r.Form.Get("limit"))
- if err != nil {
- return err
- }
- limit = limitValue
- }
- query, err := s.backend.SearchRegistryForImages(ctx, r.Form.Get("filters"), r.Form.Get("term"), limit, config, headers)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, query.Results)
- }
|