2015-12-30 18:20:41 +01:00
|
|
|
package image
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-06-01 13:38:14 -07:00
|
|
|
"strconv"
|
2015-07-28 14:35:24 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-16 19:07:41 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-11-11 15:34:01 +01:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-09-06 11:46:37 -07:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2015-07-28 14:35:24 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2016-06-01 13:38:14 -07:00
|
|
|
"github.com/docker/docker/registry"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cname := r.Form.Get("container")
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
pause := httputils.BoolValue(r, "pause")
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
2016-04-19 16:56:54 +02:00
|
|
|
if r.FormValue("pause") == "" && versions.GreaterThanOrEqualTo(version, "1.13") {
|
2015-07-28 14:35:24 -04:00
|
|
|
pause = true
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:22:23 -04:00
|
|
|
c, _, _, err := s.decoder.DecodeConfig(r.Body)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil && err != io.EOF { //Do not fail if body is empty.
|
|
|
|
return err
|
|
|
|
}
|
2015-12-09 20:07:53 +01:00
|
|
|
if c == nil {
|
2015-12-18 13:36:17 -05:00
|
|
|
c = &container.Config{}
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 19:07:41 -04:00
|
|
|
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"],
|
2015-12-09 20:07:53 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 10:53:47 -08:00
|
|
|
imgID, err := s.backend.Commit(cname, commitCfg)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:27:55 -07:00
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{
|
2015-09-06 13:26:40 -04:00
|
|
|
ID: string(imgID),
|
2015-07-28 14:35:24 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an image from Pull or from Import
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-08-20 04:01:50 +00:00
|
|
|
image = r.Form.Get("fromImage")
|
|
|
|
repo = r.Form.Get("repo")
|
|
|
|
tag = r.Form.Get("tag")
|
|
|
|
message = r.Form.Get("message")
|
2016-01-24 18:02:21 +01:00
|
|
|
err error
|
|
|
|
output = ioutils.NewWriteFlusher(w)
|
2015-07-28 14:35:24 -04:00
|
|
|
)
|
2015-11-02 16:11:28 -08:00
|
|
|
defer output.Close()
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
if image != "" { //pull
|
2016-04-07 14:29:18 -07:00
|
|
|
metaHeaders := map[string][]string{}
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if strings.HasPrefix(k, "X-Meta-") {
|
|
|
|
metaHeaders[k] = v
|
2015-11-18 14:20:54 -08:00
|
|
|
}
|
2016-04-07 14:29:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 14:29:18 -07:00
|
|
|
|
|
|
|
err = s.backend.PullImage(ctx, image, tag, metaHeaders, authConfig, output)
|
2015-11-18 14:20:54 -08:00
|
|
|
} else { //import
|
2015-07-28 14:35:24 -04:00
|
|
|
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
|
2016-04-07 14:29:18 -07:00
|
|
|
err = s.backend.ImportImage(src, repo, tag, message, r.Body, output, r.Form["changes"])
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if !output.Flushed() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
|
|
|
output.Write(sf.FormatError(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-07-28 14:35:24 -04:00
|
|
|
metaHeaders := map[string][]string{}
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if strings.HasPrefix(k, "X-Meta-") {
|
|
|
|
metaHeaders[k] = v
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-12-11 20:11:42 -08:00
|
|
|
authConfig := &types.AuthConfig{}
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
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
|
2015-12-11 20:11:42 -08:00
|
|
|
authConfig = &types.AuthConfig{}
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:29:18 -07:00
|
|
|
image := vars["name"]
|
2015-11-18 14:20:54 -08:00
|
|
|
tag := r.Form.Get("tag")
|
|
|
|
|
2015-07-28 14:35:24 -04:00
|
|
|
output := ioutils.NewWriteFlusher(w)
|
2015-11-02 16:11:28 -08:00
|
|
|
defer output.Close()
|
2015-07-28 14:35:24 -04:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2016-04-07 14:29:18 -07:00
|
|
|
if err := s.backend.PushImage(ctx, image, tag, metaHeaders, authConfig, output); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
if !output.Flushed() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
|
|
|
output.Write(sf.FormatError(err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
|
|
|
|
|
|
output := ioutils.NewWriteFlusher(w)
|
2015-11-02 16:11:28 -08:00
|
|
|
defer output.Close()
|
2015-07-28 14:35:24 -04:00
|
|
|
var names []string
|
|
|
|
if name, ok := vars["name"]; ok {
|
|
|
|
names = []string{name}
|
|
|
|
} else {
|
|
|
|
names = r.Form["names"]
|
|
|
|
}
|
|
|
|
|
2016-02-22 10:53:47 -08:00
|
|
|
if err := s.backend.ExportImage(names, output); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
if !output.Flushed() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
|
|
|
output.Write(sf.FormatError(err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 17:09:42 -05:00
|
|
|
func (s *imageRouter) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-02-03 21:31:47 -05:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
quiet := httputils.BoolValueOrDefault(r, "quiet", true)
|
2016-04-12 22:45:42 -04:00
|
|
|
|
2016-08-09 17:27:32 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2016-04-12 22:45:42 -04:00
|
|
|
|
2016-08-09 17:27:32 -07:00
|
|
|
output := ioutils.NewWriteFlusher(w)
|
|
|
|
defer output.Close()
|
|
|
|
if err := s.backend.LoadImage(r.Body, output, quiet); err != nil {
|
|
|
|
output.Write(streamformatter.NewJSONStreamFormatter().FormatError(err))
|
2016-04-12 22:45:42 -04:00
|
|
|
}
|
2016-08-09 17:27:32 -07:00
|
|
|
return nil
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) deleteImages(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
2015-08-15 00:30:25 -07:00
|
|
|
|
2015-11-03 17:19:18 -08:00
|
|
|
if strings.TrimSpace(name) == "" {
|
2015-08-15 00:30:25 -07:00
|
|
|
return fmt.Errorf("image name cannot be blank")
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
force := httputils.BoolValue(r, "force")
|
|
|
|
prune := !httputils.BoolValue(r, "noprune")
|
2015-07-28 14:35:24 -04:00
|
|
|
|
2016-02-22 10:53:47 -08:00
|
|
|
list, err := s.backend.ImageDelete(name, force, prune)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, list)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2016-02-22 10:53:47 -08:00
|
|
|
imageInspect, err := s.backend.LookupImage(vars["name"])
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, imageInspect)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:34:01 +01:00
|
|
|
imageFilters, err := filters.FromParam(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
filterParam := r.Form.Get("filter")
|
|
|
|
if versions.LessThan(version, "1.28") && filterParam != "" {
|
|
|
|
imageFilters.Add("reference", filterParam)
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := s.backend.Images(imageFilters, httputils.BoolValue(r, "all"), false)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, images)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-07-28 14:35:24 -04:00
|
|
|
name := vars["name"]
|
2016-02-22 10:53:47 -08:00
|
|
|
history, err := s.backend.ImageHistory(name)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, history)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-04-07 14:29:18 -07:00
|
|
|
if err := s.backend.TagImage(vars["name"], r.Form.Get("repo"), r.Form.Get("tag")); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:41 +01:00
|
|
|
func (s *imageRouter) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-07-28 14:35:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
2015-12-11 20:11:42 -08:00
|
|
|
config *types.AuthConfig
|
2015-07-28 14:35:24 -04:00
|
|
|
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
|
2015-12-11 20:11:42 -08:00
|
|
|
config = &types.AuthConfig{}
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if strings.HasPrefix(k, "X-Meta-") {
|
|
|
|
headers[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 13:38:14 -07:00
|
|
|
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)
|
2015-07-28 14:35:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, query.Results)
|
2015-07-28 14:35:24 -04:00
|
|
|
}
|
2016-08-23 16:25:43 -07:00
|
|
|
|
|
|
|
func (s *imageRouter) postImagesPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:46:37 -08:00
|
|
|
pruneFilters, err := filters.FromParam(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
2016-08-23 16:25:43 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:46:37 -08:00
|
|
|
pruneReport, err := s.backend.ImagesPrune(pruneFilters)
|
2016-08-23 16:25:43 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
|
|
|
|
}
|