Browse code

Move build endpoint handler from daemon (#21972)

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tõnis Tiigi authored on 2016/04/14 02:21:00
Showing 3 changed files
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"strings"
11 11
 
12 12
 	"github.com/Sirupsen/logrus"
13
+	"github.com/docker/docker/api/types/backend"
13 14
 	"github.com/docker/docker/builder"
14 15
 	"github.com/docker/docker/builder/dockerfile/parser"
15 16
 	"github.com/docker/docker/image"
... ...
@@ -84,6 +85,27 @@ func NewBuildManager(b builder.Backend) (bm *BuildManager) {
84 84
 	return &BuildManager{backend: b}
85 85
 }
86 86
 
87
+// BuildFromContext builds a new image from a given context.
88
+func (bm *BuildManager) BuildFromContext(ctx context.Context, src io.ReadCloser, remote string, buildOptions *types.ImageBuildOptions, pg backend.ProgressWriter) (string, error) {
89
+	buildContext, dockerfileName, err := builder.DetectContextFromRemoteURL(src, remote, pg.ProgressReaderFunc)
90
+	if err != nil {
91
+		return "", err
92
+	}
93
+	defer func() {
94
+		if err := buildContext.Close(); err != nil {
95
+			logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
96
+		}
97
+	}()
98
+	if len(dockerfileName) > 0 {
99
+		buildOptions.Dockerfile = dockerfileName
100
+	}
101
+	b, err := NewBuilder(ctx, buildOptions, bm.backend, builder.DockerIgnoreContext{ModifiableContext: buildContext}, nil)
102
+	if err != nil {
103
+		return "", err
104
+	}
105
+	return b.build(pg.StdoutFormatter, pg.StderrFormatter, pg.Output)
106
+}
107
+
87 108
 // NewBuilder creates a new Dockerfile builder from an optional dockerfile and a Config.
88 109
 // If dockerfile is nil, the Dockerfile specified by Config.DockerfileName,
89 110
 // will be read from the Context passed to Build().
... ...
@@ -160,17 +182,6 @@ func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
160 160
 	return repoAndTags, nil
161 161
 }
162 162
 
163
-// Build creates a NewBuilder, which builds the image.
164
-func (bm *BuildManager) Build(clientCtx context.Context, config *types.ImageBuildOptions, context builder.Context, stdout io.Writer, stderr io.Writer, out io.Writer) (string, error) {
165
-	b, err := NewBuilder(clientCtx, config, bm.backend, context, nil)
166
-	if err != nil {
167
-		return "", err
168
-	}
169
-	img, err := b.build(config, context, stdout, stderr, out)
170
-	return img, err
171
-
172
-}
173
-
174 163
 // build runs the Dockerfile builder from a context and a docker object that allows to make calls
175 164
 // to Docker.
176 165
 //
... ...
@@ -184,9 +195,7 @@ func (bm *BuildManager) Build(clientCtx context.Context, config *types.ImageBuil
184 184
 // * Tag image, if applicable.
185 185
 // * Print a happy message and return the image ID.
186 186
 //
187
-func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context, stdout io.Writer, stderr io.Writer, out io.Writer) (string, error) {
188
-	b.options = config
189
-	b.context = context
187
+func (b *Builder) build(stdout io.Writer, stderr io.Writer, out io.Writer) (string, error) {
190 188
 	b.Stdout = stdout
191 189
 	b.Stderr = stderr
192 190
 	b.Output = out
... ...
@@ -198,7 +207,7 @@ func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context
198 198
 		}
199 199
 	}
200 200
 
201
-	repoAndTags, err := sanitizeRepoAndTags(config.Tags)
201
+	repoAndTags, err := sanitizeRepoAndTags(b.options.Tags)
202 202
 	if err != nil {
203 203
 		return "", err
204 204
 	}
205 205
deleted file mode 100644
... ...
@@ -1,33 +0,0 @@
1
-package daemon
2
-
3
-import (
4
-	"io"
5
-
6
-	"github.com/Sirupsen/logrus"
7
-	"github.com/docker/docker/api/types/backend"
8
-	"github.com/docker/docker/builder"
9
-	"github.com/docker/docker/builder/dockerfile"
10
-	"github.com/docker/engine-api/types"
11
-	"golang.org/x/net/context"
12
-)
13
-
14
-// BuildFromContext builds a new image from a given context.
15
-func (daemon *Daemon) BuildFromContext(ctx context.Context, src io.ReadCloser, remote string, buildOptions *types.ImageBuildOptions, pg backend.ProgressWriter) (string, error) {
16
-	buildContext, dockerfileName, err := builder.DetectContextFromRemoteURL(src, remote, pg.ProgressReaderFunc)
17
-	if err != nil {
18
-		return "", err
19
-	}
20
-	defer func() {
21
-		if err := buildContext.Close(); err != nil {
22
-			logrus.Debugf("[BUILDER] failed to remove temporary context: %v", err)
23
-		}
24
-	}()
25
-	if len(dockerfileName) > 0 {
26
-		buildOptions.Dockerfile = dockerfileName
27
-	}
28
-
29
-	m := dockerfile.NewBuildManager(daemon)
30
-	return m.Build(ctx, buildOptions,
31
-		builder.DockerIgnoreContext{ModifiableContext: buildContext},
32
-		pg.StdoutFormatter, pg.StderrFormatter, pg.Output)
33
-}
... ...
@@ -24,6 +24,7 @@ import (
24 24
 	"github.com/docker/docker/api/server/router/network"
25 25
 	systemrouter "github.com/docker/docker/api/server/router/system"
26 26
 	"github.com/docker/docker/api/server/router/volume"
27
+	"github.com/docker/docker/builder/dockerfile"
27 28
 	"github.com/docker/docker/cli"
28 29
 	"github.com/docker/docker/cliconfig"
29 30
 	"github.com/docker/docker/daemon"
... ...
@@ -429,7 +430,7 @@ func initRouter(s *apiserver.Server, d *daemon.Daemon) {
429 429
 		image.NewRouter(d, decoder),
430 430
 		systemrouter.NewRouter(d),
431 431
 		volume.NewRouter(d),
432
-		build.NewRouter(d),
432
+		build.NewRouter(dockerfile.NewBuildManager(d)),
433 433
 	}
434 434
 	if d.NetworkControllerEnabled() {
435 435
 		routers = append(routers, network.NewRouter(d))