2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-09-06 13:26:40 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-05-07 01:28:55 -04:00
|
|
|
"sort"
|
2015-09-06 13:26:40 -04:00
|
|
|
"strings"
|
2017-05-15 17:54:27 -04:00
|
|
|
"time"
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2018-07-02 22:31:05 -04:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-04-13 13:21:00 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-15 17:54:27 -04:00
|
|
|
"github.com/docker/docker/builder/fscache"
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-05-25 17:03:29 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-04-06 08:33:56 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-09-13 15:49:04 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2018-06-02 12:46:53 -04:00
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/parser"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/shell"
|
2017-07-28 19:04:34 -04:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-07-02 22:31:05 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-04-04 12:28:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-04-13 14:37:32 -04:00
|
|
|
"golang.org/x/sync/syncmap"
|
2015-09-06 13:26:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var validCommitCommands = map[string]bool{
|
2016-04-18 05:48:13 -04:00
|
|
|
"cmd": true,
|
|
|
|
"entrypoint": true,
|
|
|
|
"healthcheck": true,
|
|
|
|
"env": true,
|
|
|
|
"expose": true,
|
|
|
|
"label": true,
|
|
|
|
"onbuild": true,
|
|
|
|
"user": true,
|
|
|
|
"volume": true,
|
|
|
|
"workdir": true,
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
const (
|
|
|
|
stepFormat = "Step %d/%d : %v"
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:59:15 -04:00
|
|
|
// SessionGetter is object used to get access to a session by uuid
|
|
|
|
type SessionGetter interface {
|
|
|
|
Get(ctx context.Context, uuid string) (session.Caller, error)
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
// BuildManager is shared across all Builder objects
|
|
|
|
type BuildManager struct {
|
2017-11-16 01:20:33 -05:00
|
|
|
idMapping *idtools.IdentityMapping
|
|
|
|
backend builder.Backend
|
|
|
|
pathCache pathCache // TODO: make this persistent
|
|
|
|
sg SessionGetter
|
|
|
|
fsCache *fscache.FSCache
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuildManager creates a BuildManager
|
2017-11-16 01:20:33 -05:00
|
|
|
func NewBuildManager(b builder.Backend, sg SessionGetter, fsCache *fscache.FSCache, identityMapping *idtools.IdentityMapping) (*BuildManager, error) {
|
2017-05-15 17:54:27 -04:00
|
|
|
bm := &BuildManager{
|
2017-11-16 01:20:33 -05:00
|
|
|
backend: b,
|
|
|
|
pathCache: &syncmap.Map{},
|
|
|
|
sg: sg,
|
|
|
|
idMapping: identityMapping,
|
|
|
|
fsCache: fsCache,
|
2017-03-27 21:36:28 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
if err := fsCache.RegisterTransport(remotecontext.ClientSessionRemote, NewClientSessionTransport()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bm, nil
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build starts a new build from a BuildConfig
|
|
|
|
func (bm *BuildManager) Build(ctx context.Context, config backend.BuildConfig) (*builder.Result, error) {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsTriggered.Inc()
|
2017-04-13 14:37:32 -04:00
|
|
|
if config.Options.Dockerfile == "" {
|
|
|
|
config.Options.Dockerfile = builder.DefaultDockerfileName
|
|
|
|
}
|
|
|
|
|
|
|
|
source, dockerfile, err := remotecontext.Detect(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
defer func() {
|
|
|
|
if source != nil {
|
2017-04-13 14:37:32 -04:00
|
|
|
if err := source.Close(); err != nil {
|
|
|
|
logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
|
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
}
|
|
|
|
}()
|
2017-04-13 14:37:32 -04:00
|
|
|
|
2017-05-15 15:59:15 -04:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
if src, err := bm.initializeClientSession(ctx, cancel, config.Options); err != nil {
|
2017-05-15 15:59:15 -04:00
|
|
|
return nil, err
|
2017-05-15 17:54:27 -04:00
|
|
|
} else if src != nil {
|
|
|
|
source = src
|
2017-05-15 15:59:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
builderOptions := builderOptions{
|
|
|
|
Options: config.Options,
|
|
|
|
ProgressWriter: config.ProgressWriter,
|
|
|
|
Backend: bm.backend,
|
|
|
|
PathCache: bm.pathCache,
|
2017-11-16 01:20:33 -05:00
|
|
|
IDMapping: bm.idMapping,
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
2018-07-02 22:31:05 -04:00
|
|
|
b, err := newBuilder(ctx, builderOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.build(source, dockerfile)
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
func (bm *BuildManager) initializeClientSession(ctx context.Context, cancel func(), options *types.ImageBuildOptions) (builder.Source, error) {
|
2017-05-15 15:59:15 -04:00
|
|
|
if options.SessionID == "" || bm.sg == nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
return nil, nil
|
2017-05-15 15:59:15 -04:00
|
|
|
}
|
|
|
|
logrus.Debug("client is session enabled")
|
2017-05-15 17:54:27 -04:00
|
|
|
|
2017-11-03 13:36:12 -04:00
|
|
|
connectCtx, cancelCtx := context.WithTimeout(ctx, sessionConnectTimeout)
|
2017-05-15 17:54:27 -04:00
|
|
|
defer cancelCtx()
|
|
|
|
|
2017-11-03 13:36:12 -04:00
|
|
|
c, err := bm.sg.Get(connectCtx, options.SessionID)
|
2017-05-15 15:59:15 -04:00
|
|
|
if err != nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
return nil, err
|
2017-05-15 15:59:15 -04:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-c.Context().Done()
|
|
|
|
cancel()
|
|
|
|
}()
|
2017-05-15 17:54:27 -04:00
|
|
|
if options.RemoteContext == remotecontext.ClientSessionRemote {
|
|
|
|
st := time.Now()
|
2017-06-28 01:41:00 -04:00
|
|
|
csi, err := NewClientSessionSourceIdentifier(ctx, bm.sg, options.SessionID)
|
2017-05-15 17:54:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
src, err := bm.fsCache.SyncFrom(ctx, csi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("sync-time: %v", time.Since(st))
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2017-05-15 15:59:15 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
// builderOptions are the dependencies required by the builder
|
|
|
|
type builderOptions struct {
|
|
|
|
Options *types.ImageBuildOptions
|
|
|
|
Backend builder.Backend
|
|
|
|
ProgressWriter backend.ProgressWriter
|
|
|
|
PathCache pathCache
|
2017-11-16 01:20:33 -05:00
|
|
|
IDMapping *idtools.IdentityMapping
|
2017-04-13 14:37:32 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Builder is a Dockerfile builder
|
2015-12-17 19:17:50 -05:00
|
|
|
// It implements the builder.Backend interface.
|
2015-09-06 13:26:40 -04:00
|
|
|
type Builder struct {
|
2015-12-29 15:49:17 -05:00
|
|
|
options *types.ImageBuildOptions
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
2017-04-06 08:33:56 -04:00
|
|
|
Aux *streamformatter.AuxFormatter
|
2016-01-20 18:32:02 -05:00
|
|
|
Output io.Writer
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2016-03-18 17:42:40 -04:00
|
|
|
docker builder.Backend
|
|
|
|
clientCtx context.Context
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-11-16 01:20:33 -05:00
|
|
|
idMapping *idtools.IdentityMapping
|
2017-04-13 18:44:36 -04:00
|
|
|
disableCommit bool
|
|
|
|
imageSources *imageSources
|
|
|
|
pathCache pathCache
|
|
|
|
containerManager *containerManager
|
|
|
|
imageProber ImageProber
|
2018-07-02 22:31:05 -04:00
|
|
|
platform *specs.Platform
|
2016-01-20 18:32:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
// newBuilder creates a new Dockerfile builder from an optional dockerfile and a Options.
|
2018-07-02 22:31:05 -04:00
|
|
|
func newBuilder(clientCtx context.Context, options builderOptions) (*Builder, error) {
|
2017-04-13 14:37:32 -04:00
|
|
|
config := options.Options
|
2015-09-06 13:26:40 -04:00
|
|
|
if config == nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
config = new(types.ImageBuildOptions)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-17 20:08:01 -04:00
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
b := &Builder{
|
2017-04-13 18:44:36 -04:00
|
|
|
clientCtx: clientCtx,
|
|
|
|
options: config,
|
|
|
|
Stdout: options.ProgressWriter.StdoutFormatter,
|
|
|
|
Stderr: options.ProgressWriter.StderrFormatter,
|
|
|
|
Aux: options.ProgressWriter.AuxFormatter,
|
|
|
|
Output: options.ProgressWriter.Output,
|
|
|
|
docker: options.Backend,
|
2017-11-16 01:20:33 -05:00
|
|
|
idMapping: options.IDMapping,
|
2017-04-13 18:44:36 -04:00
|
|
|
imageSources: newImageSources(clientCtx, options),
|
|
|
|
pathCache: options.PathCache,
|
2017-08-24 14:48:16 -04:00
|
|
|
imageProber: newImageProber(options.Backend, config.CacheFrom, config.NoCache),
|
2017-04-13 18:44:36 -04:00
|
|
|
containerManager: newContainerManager(options.Backend),
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-17 20:08:01 -04:00
|
|
|
|
2018-07-02 22:31:05 -04:00
|
|
|
// same as in Builder.Build in builder/builder-next/builder.go
|
|
|
|
// TODO: remove once config.Platform is of type specs.Platform
|
|
|
|
if config.Platform != "" {
|
|
|
|
sp, err := platforms.Parse(config.Platform)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := system.ValidatePlatform(sp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.platform = &sp
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 01:28:55 -04:00
|
|
|
// Build 'LABEL' command(s) from '--label' options and add to the last stage
|
|
|
|
func buildLabelOptions(labels map[string]string, stages []instructions.Stage) {
|
|
|
|
keys := []string{}
|
|
|
|
for key := range labels {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the label to have a repeatable order
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
value := labels[key]
|
|
|
|
stages[len(stages)-1].AddCommand(instructions.NewLabelCommand(key, value, true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:37:32 -04:00
|
|
|
// Build runs the Dockerfile builder by parsing the Dockerfile and executing
|
|
|
|
// the instructions from the file.
|
|
|
|
func (b *Builder) build(source builder.Source, dockerfile *parser.Result) (*builder.Result, error) {
|
2017-05-05 18:52:11 -04:00
|
|
|
defer b.imageSources.Unmount()
|
2017-03-15 18:28:06 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
stages, metaArgs, err := instructions.Parse(dockerfile.AST)
|
|
|
|
if err != nil {
|
|
|
|
if instructions.IsUnknownInstruction(err) {
|
|
|
|
buildsFailed.WithValues(metricsUnknownInstructionError).Inc()
|
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
if b.options.Target != "" {
|
|
|
|
targetIx, found := instructions.HasStage(stages, b.options.Target)
|
|
|
|
if !found {
|
|
|
|
buildsFailed.WithValues(metricsBuildTargetNotReachableError).Inc()
|
2018-04-27 06:40:59 -04:00
|
|
|
return nil, errdefs.InvalidParameter(errors.Errorf("failed to reach build target %s in Dockerfile", b.options.Target))
|
2017-05-22 11:21:17 -04:00
|
|
|
}
|
|
|
|
stages = stages[:targetIx+1]
|
|
|
|
}
|
2017-04-04 16:34:19 -04:00
|
|
|
|
2018-05-07 01:28:55 -04:00
|
|
|
// Add 'LABEL' command specified by '--label' option to the last stage
|
|
|
|
buildLabelOptions(b.options.Labels, stages)
|
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
dockerfile.PrintWarnings(b.Stderr)
|
|
|
|
dispatchState, err := b.dispatchDockerfileWithCancellation(stages, metaArgs, dockerfile.EscapeToken, source)
|
2017-04-04 16:34:19 -04:00
|
|
|
if err != nil {
|
2017-04-13 14:37:32 -04:00
|
|
|
return nil, err
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
if dispatchState.imageID == "" {
|
2017-04-24 07:32:01 -04:00
|
|
|
buildsFailed.WithValues(metricsDockerfileEmptyError).Inc()
|
2017-04-13 14:37:32 -04:00
|
|
|
return nil, errors.New("No image was generated. Is your Dockerfile empty?")
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return &builder.Result{ImageID: dispatchState.imageID, FromImage: dispatchState.baseImage}, nil
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
|
|
|
|
2017-04-06 08:33:56 -04:00
|
|
|
func emitImageID(aux *streamformatter.AuxFormatter, state *dispatchState) error {
|
|
|
|
if aux == nil || state.imageID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-11 14:48:42 -04:00
|
|
|
return aux.Emit("", types.BuildResult{ID: state.imageID})
|
2017-04-06 08:33:56 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 13:49:13 -04:00
|
|
|
func processMetaArg(meta instructions.ArgCommand, shlex *shell.Lex, args *BuildArgs) error {
|
2018-01-30 18:58:21 -05:00
|
|
|
// shell.Lex currently only support the concatenated string format
|
2017-05-22 11:21:17 -04:00
|
|
|
envs := convertMapToEnvList(args.GetAllAllowed())
|
|
|
|
if err := meta.Expand(func(word string) (string, error) {
|
|
|
|
return shlex.ProcessWord(word, envs)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args.AddArg(meta.Key, meta.Value)
|
|
|
|
args.AddMetaArg(meta.Key, meta.Value)
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-13 00:06:04 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func printCommand(out io.Writer, currentCommandIndex int, totalCommands int, cmd interface{}) int {
|
|
|
|
fmt.Fprintf(out, stepFormat, currentCommandIndex, totalCommands, cmd)
|
|
|
|
fmt.Fprintln(out)
|
|
|
|
return currentCommandIndex + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) dispatchDockerfileWithCancellation(parseResult []instructions.Stage, metaArgs []instructions.ArgCommand, escapeToken rune, source builder.Source) (*dispatchState, error) {
|
|
|
|
dispatchRequest := dispatchRequest{}
|
2018-05-07 13:49:13 -04:00
|
|
|
buildArgs := NewBuildArgs(b.options.BuildArgs)
|
2017-05-22 11:21:17 -04:00
|
|
|
totalCommands := len(metaArgs) + len(parseResult)
|
|
|
|
currentCommandIndex := 1
|
|
|
|
for _, stage := range parseResult {
|
|
|
|
totalCommands += len(stage.Commands)
|
|
|
|
}
|
2018-01-30 18:58:21 -05:00
|
|
|
shlex := shell.NewLex(escapeToken)
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, meta := range metaArgs {
|
|
|
|
currentCommandIndex = printCommand(b.Stdout, currentCommandIndex, totalCommands, &meta)
|
|
|
|
|
|
|
|
err := processMetaArg(meta, shlex, buildArgs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-06 08:33:56 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stagesResults := newStagesBuildResults()
|
2017-04-06 08:33:56 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, stage := range parseResult {
|
|
|
|
if err := stagesResults.checkStageNameAvailable(stage.Name); err != nil {
|
|
|
|
return nil, err
|
2017-04-10 18:27:42 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
dispatchRequest = newDispatchRequest(b, escapeToken, source, buildArgs, stagesResults)
|
2017-04-10 18:27:42 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
currentCommandIndex = printCommand(b.Stdout, currentCommandIndex, totalCommands, stage.SourceCode)
|
|
|
|
if err := initializeStage(dispatchRequest, &stage); err != nil {
|
|
|
|
return nil, err
|
2017-04-26 17:45:16 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
dispatchRequest.state.updateRunConfig()
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> %s\n", stringid.TruncateID(dispatchRequest.state.imageID))
|
|
|
|
for _, cmd := range stage.Commands {
|
|
|
|
select {
|
|
|
|
case <-b.clientCtx.Done():
|
|
|
|
logrus.Debug("Builder: build cancelled!")
|
|
|
|
fmt.Fprint(b.Stdout, "Build cancelled\n")
|
|
|
|
buildsFailed.WithValues(metricsBuildCanceled).Inc()
|
|
|
|
return nil, errors.New("Build cancelled")
|
|
|
|
default:
|
|
|
|
// Not cancelled yet, keep going...
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
currentCommandIndex = printCommand(b.Stdout, currentCommandIndex, totalCommands, cmd)
|
|
|
|
|
|
|
|
if err := dispatch(dispatchRequest, cmd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dispatchRequest.state.updateRunConfig()
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> %s\n", stringid.TruncateID(dispatchRequest.state.imageID))
|
|
|
|
|
|
|
|
}
|
|
|
|
if err := emitImageID(b.Aux, dispatchRequest.state); err != nil {
|
2017-04-26 17:45:16 -04:00
|
|
|
return nil, err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
buildArgs.MergeReferencedArgs(dispatchRequest.state.buildArgs)
|
|
|
|
if err := commitStage(dispatchRequest.state, stagesResults); err != nil {
|
|
|
|
return nil, err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
buildArgs.WarnOnUnusedBuildArgs(b.Stdout)
|
|
|
|
return dispatchRequest.state, nil
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2016-01-29 14:28:30 -05:00
|
|
|
// BuildFromConfig builds directly from `changes`, treating it as if it were the contents of a Dockerfile
|
|
|
|
// It will:
|
2016-01-05 08:33:20 -05:00
|
|
|
// - Call parse.Parse() to get an AST root for the concatenated Dockerfile entries.
|
|
|
|
// - Do build by calling builder.dispatch() to call all entries' handling routines
|
|
|
|
//
|
|
|
|
// BuildFromConfig is used by the /commit endpoint, with the changes
|
|
|
|
// coming from the query parameter of the same name.
|
|
|
|
//
|
|
|
|
// TODO: Remove?
|
2017-09-19 15:14:46 -04:00
|
|
|
func BuildFromConfig(config *container.Config, changes []string, os string) (*container.Config, error) {
|
2017-11-20 11:33:20 -05:00
|
|
|
if !system.IsOSSupported(os) {
|
|
|
|
return nil, errdefs.InvalidParameter(system.ErrNotSupportedOperatingSystem)
|
|
|
|
}
|
2017-04-21 15:08:11 -04:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
dockerfile, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2018-07-02 22:31:05 -04:00
|
|
|
b, err := newBuilder(context.Background(), builderOptions{
|
2017-08-08 15:43:48 -04:00
|
|
|
Options: &types.ImageBuildOptions{NoCache: true},
|
2017-08-24 14:48:16 -04:00
|
|
|
})
|
2018-07-02 22:31:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-17 20:08:01 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// ensure that the commands are valid
|
2017-04-26 17:45:16 -04:00
|
|
|
for _, n := range dockerfile.AST.Children {
|
2015-09-06 13:26:40 -04:00
|
|
|
if !validCommitCommands[n.Value] {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(errors.Errorf("%s is not a valid change command", n.Value))
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Stdout = ioutil.Discard
|
|
|
|
b.Stderr = ioutil.Discard
|
|
|
|
b.disableCommit = true
|
|
|
|
|
2018-05-19 07:38:54 -04:00
|
|
|
var commands []instructions.Command
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, n := range dockerfile.AST.Children {
|
|
|
|
cmd, err := instructions.ParseCommand(n)
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2017-05-22 11:21:17 -04:00
|
|
|
}
|
|
|
|
commands = append(commands, cmd)
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 13:49:13 -04:00
|
|
|
dispatchRequest := newDispatchRequest(b, dockerfile.EscapeToken, nil, NewBuildArgs(b.options.BuildArgs), newStagesBuildResults())
|
2017-11-22 16:31:26 -05:00
|
|
|
// We make mutations to the configuration, ensure we have a copy
|
|
|
|
dispatchRequest.state.runConfig = copyRunConfig(config)
|
2017-05-22 11:21:17 -04:00
|
|
|
dispatchRequest.state.imageID = config.Image
|
2017-09-19 15:14:46 -04:00
|
|
|
dispatchRequest.state.operatingSystem = os
|
2017-05-22 11:21:17 -04:00
|
|
|
for _, cmd := range commands {
|
|
|
|
err := dispatch(dispatchRequest, cmd)
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2016-09-13 00:06:04 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
dispatchRequest.state.updateRunConfig()
|
2016-09-13 00:06:04 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
return dispatchRequest.state.runConfig, nil
|
2017-04-04 16:34:19 -04:00
|
|
|
}
|
2016-09-13 00:06:04 -04:00
|
|
|
|
2017-05-22 11:21:17 -04:00
|
|
|
func convertMapToEnvList(m map[string]string) []string {
|
|
|
|
result := []string{}
|
|
|
|
for k, v := range m {
|
|
|
|
result = append(result, k+"="+v)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-22 11:21:17 -04:00
|
|
|
return result
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|