2015-12-17 19:17:50 -05:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2015-10-27 20:29:21 -04:00
|
|
|
"bytes"
|
2015-12-17 19:17:50 -05:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-06-07 15:15:50 -04:00
|
|
|
"runtime"
|
2015-12-17 19:17:50 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-03-22 16:57:24 -04:00
|
|
|
"sync"
|
2015-12-17 19:17:50 -05:00
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
apierrors "github.com/docker/docker/api/errors"
|
2015-12-17 19:17:50 -05:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-29 18:51:14 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2015-12-17 19:17:50 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2017-03-20 18:22:29 -04:00
|
|
|
units "github.com/docker/go-units"
|
2017-04-13 14:37:32 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-12-17 19:17:50 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBuildOptions, error) {
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
|
|
options := &types.ImageBuildOptions{}
|
2016-04-19 10:56:54 -04:00
|
|
|
if httputils.BoolValue(r, "forcerm") && versions.GreaterThanOrEqualTo(version, "1.12") {
|
2015-12-29 15:49:17 -05:00
|
|
|
options.Remove = true
|
2016-04-19 10:56:54 -04:00
|
|
|
} else if r.FormValue("rm") == "" && versions.GreaterThanOrEqualTo(version, "1.12") {
|
2015-12-29 15:49:17 -05:00
|
|
|
options.Remove = true
|
|
|
|
} else {
|
|
|
|
options.Remove = httputils.BoolValue(r, "rm")
|
|
|
|
}
|
2016-04-19 10:56:54 -04:00
|
|
|
if httputils.BoolValue(r, "pull") && versions.GreaterThanOrEqualTo(version, "1.16") {
|
2015-12-29 15:49:17 -05:00
|
|
|
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")
|
2016-03-06 07:29:23 -05:00
|
|
|
options.NetworkMode = r.FormValue("networkmode")
|
2016-01-20 18:32:02 -05:00
|
|
|
options.Tags = r.Form["t"]
|
2017-01-13 10:01:58 -05:00
|
|
|
options.ExtraHosts = r.Form["extrahosts"]
|
2016-06-07 15:15:50 -04:00
|
|
|
options.SecurityOpt = r.Form["securityopt"]
|
2016-04-21 12:08:37 -04:00
|
|
|
options.Squash = httputils.BoolValue(r, "squash")
|
2017-04-10 18:27:42 -04:00
|
|
|
options.Target = r.FormValue("target")
|
2017-03-20 18:22:29 -04:00
|
|
|
options.RemoteContext = r.FormValue("remote")
|
2015-12-29 15:49:17 -05:00
|
|
|
|
|
|
|
if r.Form.Get("shmsize") != "" {
|
|
|
|
shmSize, err := strconv.ParseInt(r.Form.Get("shmsize"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.ShmSize = shmSize
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
if i := container.Isolation(r.FormValue("isolation")); i != "" {
|
|
|
|
if !container.Isolation.IsValid(i) {
|
2015-12-29 15:49:17 -05:00
|
|
|
return nil, fmt.Errorf("Unsupported isolation: %q", i)
|
|
|
|
}
|
2016-02-03 15:07:00 -05:00
|
|
|
options.Isolation = i
|
2015-12-29 15:49:17 -05:00
|
|
|
}
|
|
|
|
|
2016-06-07 15:15:50 -04:00
|
|
|
if runtime.GOOS != "windows" && options.SecurityOpt != nil {
|
2017-05-21 13:50:55 -04:00
|
|
|
return nil, fmt.Errorf("The daemon on this platform does not support setting security options on build")
|
2016-06-07 15:15:50 -04:00
|
|
|
}
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
var buildUlimits = []*units.Ulimit{}
|
|
|
|
ulimitsJSON := r.FormValue("ulimits")
|
|
|
|
if ulimitsJSON != "" {
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(ulimitsJSON), &buildUlimits); err != nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.Ulimits = buildUlimits
|
|
|
|
}
|
|
|
|
|
2016-12-03 08:46:04 -05:00
|
|
|
// 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.
|
2017-04-13 14:37:32 -04:00
|
|
|
buildArgsJSON := r.FormValue("buildargs")
|
2015-12-29 15:49:17 -05:00
|
|
|
if buildArgsJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var buildArgs = map[string]*string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(buildArgsJSON), &buildArgs); err != nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.BuildArgs = buildArgs
|
|
|
|
}
|
2016-12-03 08:46:04 -05:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
labelsJSON := r.FormValue("labels")
|
|
|
|
if labelsJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var labels = map[string]string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
|
2016-03-16 17:52:34 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.Labels = labels
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:38:00 -04:00
|
|
|
cacheFromJSON := r.FormValue("cachefrom")
|
|
|
|
if cacheFromJSON != "" {
|
2017-04-13 14:37:32 -04:00
|
|
|
var cacheFrom = []string{}
|
2016-09-22 17:38:00 -04:00
|
|
|
if err := json.Unmarshal([]byte(cacheFromJSON), &cacheFrom); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
options.CacheFrom = cacheFrom
|
|
|
|
}
|
2017-05-15 15:59:15 -04:00
|
|
|
options.SessionID = r.FormValue("session")
|
2016-09-22 17:38:00 -04:00
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2017-04-06 08:33:56 -04:00
|
|
|
var (
|
|
|
|
notVerboseBuffer = bytes.NewBuffer(nil)
|
|
|
|
version = httputils.VersionFromContext(ctx)
|
|
|
|
)
|
2015-12-17 19:17:50 -05:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
output := ioutils.NewWriteFlusher(w)
|
|
|
|
defer output.Close()
|
|
|
|
errf := func(err error) error {
|
2015-12-29 15:49:17 -05:00
|
|
|
if httputils.BoolValue(r, "q") && notVerboseBuffer.Len() > 0 {
|
2015-10-27 20:29:21 -04:00
|
|
|
output.Write(notVerboseBuffer.Bytes())
|
|
|
|
}
|
2015-12-17 19:17:50 -05:00
|
|
|
// 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
|
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
_, err = w.Write(streamformatter.FormatError(err))
|
2015-12-17 19:17:50 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("could not write error response: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:49:17 -05:00
|
|
|
buildOptions, err := newImageBuildOptions(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return errf(err)
|
2015-12-17 19:17:50 -05:00
|
|
|
}
|
2017-04-13 14:37:32 -04:00
|
|
|
buildOptions.AuthConfigs = getAuthConfigs(r.Header)
|
|
|
|
|
|
|
|
if buildOptions.Squash && !br.daemon.HasExperimental() {
|
|
|
|
return apierrors.NewBadRequestError(
|
|
|
|
errors.New("squash is only supported with experimental mode"))
|
|
|
|
}
|
2015-12-17 19:17:50 -05:00
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
out := io.Writer(output)
|
|
|
|
if buildOptions.SuppressOutput {
|
|
|
|
out = notVerboseBuffer
|
|
|
|
}
|
|
|
|
|
2015-12-17 19:17:50 -05:00
|
|
|
// 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 {
|
2017-05-01 14:54:56 -04:00
|
|
|
progressOutput := streamformatter.NewJSONProgressOutput(out, true)
|
2017-03-20 18:22:29 -04:00
|
|
|
return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", buildOptions.RemoteContext)
|
2015-12-17 19:17:50 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
wantAux := versions.GreaterThanOrEqualTo(version, "1.30")
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
imgID, err := br.backend.Build(ctx, backend.BuildConfig{
|
|
|
|
Source: r.Body,
|
|
|
|
Options: buildOptions,
|
2017-04-06 08:33:56 -04:00
|
|
|
ProgressWriter: buildProgressWriter(out, wantAux, createProgressReader),
|
2017-04-13 14:37:32 -04:00
|
|
|
})
|
2015-12-17 19:17:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return errf(err)
|
|
|
|
}
|
|
|
|
|
2015-10-27 20:29:21 -04:00
|
|
|
// 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.
|
2015-12-29 15:49:17 -05:00
|
|
|
if buildOptions.SuppressOutput {
|
2017-05-01 14:54:56 -04:00
|
|
|
fmt.Fprintln(streamformatter.NewStdoutWriter(output), imgID)
|
2015-10-27 20:29:21 -04:00
|
|
|
}
|
2015-12-17 19:17:50 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-13 14:37:32 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
func buildProgressWriter(out io.Writer, wantAux bool, createProgressReader func(io.ReadCloser) io.ReadCloser) backend.ProgressWriter {
|
2017-04-13 14:37:32 -04:00
|
|
|
out = &syncWriter{w: out}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
var aux *streamformatter.AuxFormatter
|
|
|
|
if wantAux {
|
|
|
|
aux = &streamformatter.AuxFormatter{Writer: out}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
return backend.ProgressWriter{
|
|
|
|
Output: out,
|
2017-05-01 14:54:56 -04:00
|
|
|
StdoutFormatter: streamformatter.NewStdoutWriter(out),
|
|
|
|
StderrFormatter: streamformatter.NewStderrWriter(out),
|
2017-04-06 08:33:56 -04:00
|
|
|
AuxFormatter: aux,
|
2017-04-13 14:37:32 -04:00
|
|
|
ProgressReaderFunc: createProgressReader,
|
|
|
|
}
|
|
|
|
}
|