123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package build
- import (
- "bytes"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "runtime"
- "strconv"
- "strings"
- "sync"
- "github.com/docker/docker/api/server/httputils"
- "github.com/docker/docker/api/types"
- "github.com/docker/docker/api/types/backend"
- "github.com/docker/docker/api/types/container"
- "github.com/docker/docker/api/types/versions"
- "github.com/docker/docker/pkg/ioutils"
- "github.com/docker/docker/pkg/progress"
- "github.com/docker/docker/pkg/streamformatter"
- units "github.com/docker/go-units"
- "github.com/pkg/errors"
- "github.com/sirupsen/logrus"
- "golang.org/x/net/context"
- )
- type invalidIsolationError string
- func (e invalidIsolationError) Error() string {
- return fmt.Sprintf("Unsupported isolation: %q", string(e))
- }
- func (e invalidIsolationError) InvalidParameter() {}
- func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBuildOptions, error) {
- version := httputils.VersionFromContext(ctx)
- options := &types.ImageBuildOptions{}
- if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
- options.Remove = true
- } else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
- options.Remove = true
- } else {
- options.Remove = httputils.BoolValue(r, "rm")
- }
- if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
- options.PullParent = true
- }
- options.Dockerfile = r.FormValue("dockerfile")
- options.SuppressOutput = httputils.BoolValue(r, "q")
- options.NoCache = httputils.BoolValue(r, "nocache")
- options.ForceRemove = httputils.BoolValue(r, "forcerm")
- options.MemorySwap = httputils.Int64ValueOrZero(r, "memswap")
- options.Memory = httputils.Int64ValueOrZero(r, "memory")
- options.CPUShares = httputils.Int64ValueOrZero(r, "cpushares")
- options.CPUPeriod = httputils.Int64ValueOrZero(r, "cpuperiod")
- options.CPUQuota = httputils.Int64ValueOrZero(r, "cpuquota")
- options.CPUSetCPUs = r.FormValue("cpusetcpus")
- options.CPUSetMems = r.FormValue("cpusetmems")
- options.CgroupParent = r.FormValue("cgroupparent")
- options.NetworkMode = r.FormValue("networkmode")
- options.Tags = r.Form["t"]
- options.ExtraHosts = r.Form["extrahosts"]
- options.SecurityOpt = r.Form["securityopt"]
- options.Squash = httputils.BoolValue(r, "squash")
- options.Target = r.FormValue("target")
- options.RemoteContext = r.FormValue("remote")
- if r.Form.Get("shmsize") != "" {
- shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
- if err != nil {
- return nil, err
- }
- options.ShmSize = shmSize
- }
- if i := container.Isolation(r.FormValue("isolation")); i != "" {
- if !container.Isolation.IsValid(i) {
- return nil, invalidIsolationError(i)
- }
- options.Isolation = i
- }
- if runtime.GOOS != "windows" && options.SecurityOpt != nil {
- return nil, validationError{fmt.Errorf("The daemon on this platform does not support setting security options on build")}
- }
- var buildUlimits = []*units.Ulimit{}
- ulimitsJSON := r.FormValue("ulimits")
- if ulimitsJSON != "" {
- if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
- return nil, errors.Wrap(validationError{err}, "error reading ulimit settings")
- }
- options.Ulimits = buildUlimits
- }
- // Note that there are two ways a --build-arg might appear in the
- // json of the query param:
- // "foo":"bar"
- // and "foo":nil
- // The first is the normal case, ie. --build-arg foo=bar
- // or --build-arg foo
- // where foo's value was picked up from an env var.
- // The second ("foo":nil) is where they put --build-arg foo
- // but "foo" isn't set as an env var. In that case we can't just drop
- // the fact they mentioned it, we need to pass that along to the builder
- // so that it can print a warning about "foo" being unused if there is
- // no "ARG foo" in the Dockerfile.
- buildArgsJSON := r.FormValue("buildargs")
- if buildArgsJSON != "" {
- var buildArgs = map[string]*string{}
- if err := json.Unmarshal([]byte(buildArgsJSON), &buildArgs); err != nil {
- return nil, errors.Wrap(validationError{err}, "error reading build args")
- }
- options.BuildArgs = buildArgs
- }
- labelsJSON := r.FormValue("labels")
- if labelsJSON != "" {
- var labels = map[string]string{}
- if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
- return nil, errors.Wrap(validationError{err}, "error reading labels")
- }
- options.Labels = labels
- }
- cacheFromJSON := r.FormValue("cachefrom")
- if cacheFromJSON != "" {
- var cacheFrom = []string{}
- if err := json.Unmarshal([]byte(cacheFromJSON), &cacheFrom); err != nil {
- return nil, err
- }
- options.CacheFrom = cacheFrom
- }
- options.SessionID = r.FormValue("session")
- return options, nil
- }
- func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- report, err := br.backend.PruneCache(ctx)
- if err != nil {
- return err
- }
- return httputils.WriteJSON(w, http.StatusOK, report)
- }
- type validationError struct {
- cause error
- }
- func (e validationError) Error() string {
- return e.cause.Error()
- }
- func (e validationError) InvalidParameter() {}
- func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
- var (
- notVerboseBuffer = bytes.NewBuffer(nil)
- version = httputils.VersionFromContext(ctx)
- )
- w.Header().Set("Content-Type", "application/json")
- output := ioutils.NewWriteFlusher(w)
- defer output.Close()
- errf := func(err error) error {
- if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
- output.Write(notVerboseBuffer.Bytes())
- }
- // 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(streamformatter.FormatError(err))
- if err != nil {
- logrus.Warnf("could not write error response: %v", err)
- }
- return nil
- }
- buildOptions, err := newImageBuildOptions(ctx, r)
- if err != nil {
- return errf(err)
- }
- buildOptions.AuthConfigs = getAuthConfigs(r.Header)
- if buildOptions.Squash && !br.daemon.HasExperimental() {
- return validationError{errors.New("squash is only supported with experimental mode")}
- }
- out := io.Writer(output)
- if buildOptions.SuppressOutput {
- out = notVerboseBuffer
- }
- // 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 := streamformatter.NewJSONProgressOutput(out, true)
- return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", buildOptions.RemoteContext)
- }
- wantAux := versions.GreaterThanOrEqualTo(version, "1.30")
- imgID, err := br.backend.Build(ctx, backend.BuildConfig{
- Source: r.Body,
- Options: buildOptions,
- ProgressWriter: buildProgressWriter(out, wantAux, createProgressReader),
- })
- if err != nil {
- return errf(err)
- }
- // Everything worked so if -q was provided the output from the daemon
- // should be just the image ID and we'll print that to stdout.
- if buildOptions.SuppressOutput {
- fmt.Fprintln(streamformatter.NewStdoutWriter(output), imgID)
- }
- return nil
- }
- func getAuthConfigs(header http.Header) map[string]types.AuthConfig {
- authConfigs := map[string]types.AuthConfig{}
- authConfigsEncoded := header.Get("X-Registry-Config")
- if authConfigsEncoded == "" {
- return authConfigs
- }
- authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded))
- // Pulling an image does not error when no auth is provided so to remain
- // consistent with the existing api decode errors are ignored
- json.NewDecoder(authConfigsJSON).Decode(&authConfigs)
- return authConfigs
- }
- type syncWriter struct {
- w io.Writer
- mu sync.Mutex
- }
- func (s *syncWriter) Write(b []byte) (count int, err error) {
- s.mu.Lock()
- count, err = s.w.Write(b)
- s.mu.Unlock()
- return
- }
- func buildProgressWriter(out io.Writer, wantAux bool, createProgressReader func(io.ReadCloser) io.ReadCloser) backend.ProgressWriter {
- out = &syncWriter{w: out}
- var aux *streamformatter.AuxFormatter
- if wantAux {
- aux = &streamformatter.AuxFormatter{Writer: out}
- }
- return backend.ProgressWriter{
- Output: out,
- StdoutFormatter: streamformatter.NewStdoutWriter(out),
- StderrFormatter: streamformatter.NewStderrWriter(out),
- AuxFormatter: aux,
- ProgressReaderFunc: createProgressReader,
- }
- }
|