123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- package local
- import (
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "strings"
- "github.com/Sirupsen/logrus"
- "github.com/docker/distribution/digest"
- "github.com/docker/docker/api/server/httputils"
- "github.com/docker/docker/api/types"
- "github.com/docker/docker/builder"
- "github.com/docker/docker/builder/dockerfile"
- "github.com/docker/docker/daemon/daemonbuilder"
- derr "github.com/docker/docker/errors"
- "github.com/docker/docker/pkg/archive"
- "github.com/docker/docker/pkg/chrootarchive"
- "github.com/docker/docker/pkg/ioutils"
- "github.com/docker/docker/pkg/progress"
- "github.com/docker/docker/pkg/streamformatter"
- "github.com/docker/docker/pkg/ulimit"
- "github.com/docker/docker/reference"
- "github.com/docker/docker/runconfig"
- "github.com/docker/docker/utils"
- "golang.org/x/net/context"
- )
- func (s *router) 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") == "" && version.GreaterThanOrEqualTo("1.13") {
- pause = true
- }
- c, _, err := runconfig.DecodeContainerConfig(r.Body)
- if err != nil && err != io.EOF { //Do not fail if body is empty.
- return err
- }
- if c == nil {
- c = &runconfig.Config{}
- }
- if !s.daemon.Exists(cname) {
- return derr.ErrorCodeNoSuchContainer.WithArgs(cname)
- }
- newConfig, err := dockerfile.BuildFromConfig(c, r.Form["changes"])
- if err != nil {
- return err
- }
- commitCfg := &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: newConfig,
- MergeConfigs: true,
- }
- imgID, err := s.daemon.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 *router) 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")
- )
- 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{}
- }
- }
- var (
- err error
- output = ioutils.NewWriteFlusher(w)
- )
- defer output.Close()
- w.Header().Set("Content-Type", "application/json")
- if image != "" { //pull
- // Special case: "pull -a" may send an image name with a
- // trailing :. This is ugly, but let's not break API
- // compatibility.
- image = strings.TrimSuffix(image, ":")
- var ref reference.Named
- ref, err = reference.ParseNamed(image)
- if err == nil {
- if tag != "" {
- // The "tag" could actually be a digest.
- var dgst digest.Digest
- dgst, err = digest.ParseDigest(tag)
- if err == nil {
- ref, err = reference.WithDigest(ref, dgst)
- } else {
- ref, err = reference.WithTag(ref, tag)
- }
- }
- if err == nil {
- metaHeaders := map[string][]string{}
- for k, v := range r.Header {
- if strings.HasPrefix(k, "X-Meta-") {
- metaHeaders[k] = v
- }
- }
- err = s.daemon.PullImage(ref, metaHeaders, authConfig, output)
- }
- }
- } else { //import
- var newRef reference.Named
- if repo != "" {
- var err error
- newRef, err = reference.ParseNamed(repo)
- if err != nil {
- return err
- }
- if _, isCanonical := newRef.(reference.Canonical); isCanonical {
- return errors.New("cannot import digest reference")
- }
- if tag != "" {
- newRef, err = reference.WithTag(newRef, tag)
- if err != nil {
- return err
- }
- }
- }
- 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
- var newConfig *runconfig.Config
- newConfig, err = dockerfile.BuildFromConfig(&runconfig.Config{}, r.Form["changes"])
- if err != nil {
- return err
- }
- err = s.daemon.ImportImage(src, newRef, message, r.Body, output, newConfig)
- }
- if err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *router) 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)
- }
- }
- ref, err := reference.ParseNamed(vars["name"])
- if err != nil {
- return err
- }
- tag := r.Form.Get("tag")
- if tag != "" {
- // Push by digest is not supported, so only tags are supported.
- ref, err = reference.WithTag(ref, tag)
- if err != nil {
- return err
- }
- }
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- w.Header().Set("Content-Type", "application/json")
- if err := s.daemon.PushImage(ref, metaHeaders, authConfig, output); err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *router) 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.daemon.ExportImage(names, output); err != nil {
- if !output.Flushed() {
- return err
- }
- sf := streamformatter.NewJSONStreamFormatter()
- output.Write(sf.FormatError(err))
- }
- return nil
- }
- func (s *router) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- return s.daemon.LoadImage(r.Body, w)
- }
- func (s *router) 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.daemon.ImageDelete(name, force, prune)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, list)
- }
- func (s *router) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- imageInspect, err := s.daemon.LookupImage(vars["name"])
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, imageInspect)
- }
- func (s *router) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- var (
- authConfigs = map[string]types.AuthConfig{}
- authConfigsEncoded = r.Header.Get("X-Registry-Config")
- buildConfig = &dockerfile.Config{}
- )
- if authConfigsEncoded != "" {
- authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
- if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); 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.
- }
- }
- w.Header().Set("Content-Type", "application/json")
- version := httputils.VersionFromContext(ctx)
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- sf := streamformatter.NewJSONStreamFormatter()
- errf := func(err error) error {
- // Do not write the error in the http output if it's still empty.
- // This prevents from writing a 200(OK) when there is an internal error.
- if !output.Flushed() {
- return err
- }
- _, err = w.Write(sf.FormatError(errors.New(utils.GetErrorMessage(err))))
- if err != nil {
- logrus.Warnf("could not write error response: %v", err)
- }
- return nil
- }
- if httputils.BoolValue(r, "forcerm") && version.GreaterThanOrEqualTo("1.12") {
- buildConfig.Remove = true
- } else if r.FormValue("rm") == "" && version.GreaterThanOrEqualTo("1.12") {
- buildConfig.Remove = true
- } else {
- buildConfig.Remove = httputils.BoolValue(r, "rm")
- }
- if httputils.BoolValue(r, "pull") && version.GreaterThanOrEqualTo("1.16") {
- buildConfig.Pull = true
- }
- repoAndTags, err := sanitizeRepoAndTags(r.Form["t"])
- if err != nil {
- return errf(err)
- }
- buildConfig.DockerfileName = r.FormValue("dockerfile")
- buildConfig.Verbose = !httputils.BoolValue(r, "q")
- buildConfig.UseCache = !httputils.BoolValue(r, "nocache")
- buildConfig.ForceRemove = httputils.BoolValue(r, "forcerm")
- buildConfig.MemorySwap = httputils.Int64ValueOrZero(r, "memswap")
- buildConfig.Memory = httputils.Int64ValueOrZero(r, "memory")
- buildConfig.CPUShares = httputils.Int64ValueOrZero(r, "cpushares")
- buildConfig.CPUPeriod = httputils.Int64ValueOrZero(r, "cpuperiod")
- buildConfig.CPUQuota = httputils.Int64ValueOrZero(r, "cpuquota")
- buildConfig.CPUSetCpus = r.FormValue("cpusetcpus")
- buildConfig.CPUSetMems = r.FormValue("cpusetmems")
- buildConfig.CgroupParent = r.FormValue("cgroupparent")
- if r.Form.Get("shmsize") != "" {
- shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
- if err != nil {
- return errf(err)
- }
- buildConfig.ShmSize = &shmSize
- }
- if i := runconfig.IsolationLevel(r.FormValue("isolation")); i != "" {
- if !runconfig.IsolationLevel.IsValid(i) {
- return errf(fmt.Errorf("Unsupported isolation: %q", i))
- }
- buildConfig.Isolation = i
- }
- var buildUlimits = []*ulimit.Ulimit{}
- ulimitsJSON := r.FormValue("ulimits")
- if ulimitsJSON != "" {
- if err := json.NewDecoder(strings.NewReader(ulimitsJSON)).Decode(&buildUlimits); err != nil {
- return errf(err)
- }
- buildConfig.Ulimits = buildUlimits
- }
- var buildArgs = map[string]string{}
- buildArgsJSON := r.FormValue("buildargs")
- if buildArgsJSON != "" {
- if err := json.NewDecoder(strings.NewReader(buildArgsJSON)).Decode(&buildArgs); err != nil {
- return errf(err)
- }
- buildConfig.BuildArgs = buildArgs
- }
- remoteURL := r.FormValue("remote")
- // Currently, only used if context is from a remote url.
- // Look at code in DetectContextFromRemoteURL for more information.
- createProgressReader := func(in io.ReadCloser) io.ReadCloser {
- progressOutput := sf.NewProgressOutput(output, true)
- return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", remoteURL)
- }
- var (
- context builder.ModifiableContext
- dockerfileName string
- )
- context, dockerfileName, err = daemonbuilder.DetectContextFromRemoteURL(r.Body, remoteURL, createProgressReader)
- if err != nil {
- return errf(err)
- }
- defer func() {
- if err := context.Close(); err != nil {
- logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
- }
- }()
- uidMaps, gidMaps := s.daemon.GetUIDGIDMaps()
- defaultArchiver := &archive.Archiver{
- Untar: chrootarchive.Untar,
- UIDMaps: uidMaps,
- GIDMaps: gidMaps,
- }
- docker := &daemonbuilder.Docker{
- Daemon: s.daemon,
- OutOld: output,
- AuthConfigs: authConfigs,
- Archiver: defaultArchiver,
- }
- b, err := dockerfile.NewBuilder(buildConfig, docker, builder.DockerIgnoreContext{ModifiableContext: context}, nil)
- if err != nil {
- return errf(err)
- }
- b.Stdout = &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
- b.Stderr = &streamformatter.StderrFormatter{Writer: output, StreamFormatter: sf}
- if closeNotifier, ok := w.(http.CloseNotifier); ok {
- finished := make(chan struct{})
- defer close(finished)
- go func() {
- select {
- case <-finished:
- case <-closeNotifier.CloseNotify():
- logrus.Infof("Client disconnected, cancelling job: build")
- b.Cancel()
- }
- }()
- }
- if len(dockerfileName) > 0 {
- b.DockerfileName = dockerfileName
- }
- imgID, err := b.Build()
- if err != nil {
- return errf(err)
- }
- for _, rt := range repoAndTags {
- if err := s.daemon.TagImage(rt, imgID); err != nil {
- return errf(err)
- }
- }
- return nil
- }
- // sanitizeRepoAndTags parses the raw "t" parameter received from the client
- // to a slice of repoAndTag.
- // It also validates each repoName and tag.
- func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
- var (
- repoAndTags []reference.Named
- // This map is used for deduplicating the "-t" parameter.
- uniqNames = make(map[string]struct{})
- )
- for _, repo := range names {
- if repo == "" {
- continue
- }
- ref, err := reference.ParseNamed(repo)
- if err != nil {
- return nil, err
- }
- ref = reference.WithDefaultTag(ref)
- if _, isCanonical := ref.(reference.Canonical); isCanonical {
- return nil, errors.New("build tag cannot contain a digest")
- }
- nameWithTag := ref.String()
- if _, exists := uniqNames[nameWithTag]; !exists {
- uniqNames[nameWithTag] = struct{}{}
- repoAndTags = append(repoAndTags, ref)
- }
- }
- return repoAndTags, nil
- }
- func (s *router) 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.daemon.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 *router) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- name := vars["name"]
- history, err := s.daemon.ImageHistory(name)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, history)
- }
- func (s *router) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- if err := httputils.ParseForm(r); err != nil {
- return err
- }
- repo := r.Form.Get("repo")
- tag := r.Form.Get("tag")
- newTag, err := reference.WithName(repo)
- if err != nil {
- return err
- }
- if tag != "" {
- if newTag, err = reference.WithTag(newTag, tag); err != nil {
- return err
- }
- }
- if err := s.daemon.TagImage(newTag, vars["name"]); err != nil {
- return err
- }
- w.WriteHeader(http.StatusCreated)
- return nil
- }
- func (s *router) 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
- }
- }
- query, err := s.daemon.SearchRegistryForImages(r.Form.Get("term"), config, headers)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, query.Results)
- }
|