Browse code

Make daemonbuilder.Docker leaner.

Currently builder.Backend is implemented by daemonbuilder.Docker{} for
the daemon. This registration happens in the API/server code. However,
this is too implementation specific. Ideally we should be able to specify
that docker daemon (or any other) is implementing the Backend and abstract
the implementation details. So we should remove package daemonbuilder
dependency in build_routes.go

With this change, daemonbuilder.Docker is nothing more than the daemon.
A follow on change will remove the daemonbuilder package and move relevant
methods under daemon, so that API only knows about the backend.

Also cleanup code in api/client/build.go. docker cli always performs build
context tar download for remoteURLs and sends an empty remoteContext. So
remove relevant dead code.

Signed-off-by: Anusha Ragunathan <anusha@docker.com>

Anusha Ragunathan authored on 2016/01/13 07:38:55
Showing 6 changed files
... ...
@@ -77,9 +77,8 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
77 77
 	cmd.ParseFlags(args, true)
78 78
 
79 79
 	var (
80
-		context  io.ReadCloser
81
-		isRemote bool
82
-		err      error
80
+		context io.ReadCloser
81
+		err     error
83 82
 	)
84 83
 
85 84
 	_, err = exec.LookPath("git")
... ...
@@ -215,18 +214,12 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
215 215
 		}
216 216
 	}
217 217
 
218
-	var remoteContext string
219
-	if isRemote {
220
-		remoteContext = cmd.Arg(0)
221
-	}
222
-
223 218
 	options := types.ImageBuildOptions{
224 219
 		Context:        body,
225 220
 		Memory:         memory,
226 221
 		MemorySwap:     memorySwap,
227 222
 		Tags:           flTags.GetAll(),
228 223
 		SuppressOutput: *suppressOutput,
229
-		RemoteContext:  remoteContext,
230 224
 		NoCache:        *noCache,
231 225
 		Remove:         *rm,
232 226
 		ForceRemove:    *forceRm,
... ...
@@ -16,8 +16,6 @@ import (
16 16
 	"github.com/docker/docker/builder"
17 17
 	"github.com/docker/docker/builder/dockerfile"
18 18
 	"github.com/docker/docker/daemon/daemonbuilder"
19
-	"github.com/docker/docker/pkg/archive"
20
-	"github.com/docker/docker/pkg/chrootarchive"
21 19
 	"github.com/docker/docker/pkg/ioutils"
22 20
 	"github.com/docker/docker/pkg/progress"
23 21
 	"github.com/docker/docker/pkg/streamformatter"
... ...
@@ -206,31 +204,19 @@ func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *
206 206
 		buildOptions.Dockerfile = dockerfileName
207 207
 	}
208 208
 
209
-	uidMaps, gidMaps := br.backend.GetUIDGIDMaps()
210
-	defaultArchiver := &archive.Archiver{
211
-		Untar:   chrootarchive.Untar,
212
-		UIDMaps: uidMaps,
213
-		GIDMaps: gidMaps,
214
-	}
215
-
216
-	docker := &daemonbuilder.Docker{
217
-		Daemon:      br.backend,
218
-		OutOld:      output,
219
-		AuthConfigs: authConfigs,
220
-		Archiver:    defaultArchiver,
221
-	}
222
-	if buildOptions.SuppressOutput {
223
-		docker.OutOld = notVerboseBuffer
224
-	}
225
-
226 209
 	b, err := dockerfile.NewBuilder(
227 210
 		buildOptions, // result of newBuildConfig
228
-		docker,
211
+		&daemonbuilder.Docker{br.backend},
229 212
 		builder.DockerIgnoreContext{ModifiableContext: context},
230 213
 		nil)
231 214
 	if err != nil {
232 215
 		return errf(err)
233 216
 	}
217
+	if buildOptions.SuppressOutput {
218
+		b.Output = notVerboseBuffer
219
+	} else {
220
+		b.Output = output
221
+	}
234 222
 	b.Stdout = &streamformatter.StdoutFormatter{Writer: output, StreamFormatter: sf}
235 223
 	b.Stderr = &streamformatter.StderrFormatter{Writer: output, StreamFormatter: sf}
236 224
 	if buildOptions.SuppressOutput {
... ...
@@ -101,7 +101,7 @@ type Backend interface {
101 101
 	// GetImage looks up a Docker image referenced by `name`.
102 102
 	GetImage(name string) (Image, error)
103 103
 	// Pull tells Docker to pull image referenced by `name`.
104
-	Pull(name string) (Image, error)
104
+	Pull(name string, authConfigs map[string]types.AuthConfig, output io.Writer) (Image, error)
105 105
 	// ContainerAttach attaches to container.
106 106
 	ContainerAttach(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error
107 107
 	// ContainerCreate creates a new Docker container and returns potential warnings
... ...
@@ -67,7 +67,8 @@ type Builder struct {
67 67
 	allowedBuildArgs map[string]bool // list of build-time args that are allowed for expansion/substitution and passing to commands in 'run'.
68 68
 
69 69
 	// TODO: remove once docker.Commit can receive a tag
70
-	id string
70
+	id     string
71
+	Output io.Writer
71 72
 }
72 73
 
73 74
 // NewBuilder creates a new Dockerfile builder from an optional dockerfile and a Config.
... ...
@@ -212,7 +212,7 @@ func from(b *Builder, args []string, attributes map[string]bool, original string
212 212
 			// TODO: shouldn't we error out if error is different from "not found" ?
213 213
 		}
214 214
 		if image == nil {
215
-			image, err = b.docker.Pull(name)
215
+			image, err = b.docker.Pull(name, b.options.AuthConfigs, b.Output)
216 216
 			if err != nil {
217 217
 				return err
218 218
 			}
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/docker/docker/daemon"
15 15
 	"github.com/docker/docker/image"
16 16
 	"github.com/docker/docker/pkg/archive"
17
+	"github.com/docker/docker/pkg/chrootarchive"
17 18
 	"github.com/docker/docker/pkg/httputils"
18 19
 	"github.com/docker/docker/pkg/idtools"
19 20
 	"github.com/docker/docker/pkg/ioutils"
... ...
@@ -27,16 +28,13 @@ import (
27 27
 // Docker implements builder.Backend for the docker Daemon object.
28 28
 type Docker struct {
29 29
 	*daemon.Daemon
30
-	OutOld      io.Writer
31
-	AuthConfigs map[string]types.AuthConfig
32
-	Archiver    *archive.Archiver
33 30
 }
34 31
 
35 32
 // ensure Docker implements builder.Backend
36 33
 var _ builder.Backend = Docker{}
37 34
 
38 35
 // Pull tells Docker to pull image referenced by `name`.
39
-func (d Docker) Pull(name string) (builder.Image, error) {
36
+func (d Docker) Pull(name string, authConfigs map[string]types.AuthConfig, output io.Writer) (builder.Image, error) {
40 37
 	ref, err := reference.ParseNamed(name)
41 38
 	if err != nil {
42 39
 		return nil, err
... ...
@@ -44,7 +42,7 @@ func (d Docker) Pull(name string) (builder.Image, error) {
44 44
 	ref = reference.WithDefaultTag(ref)
45 45
 
46 46
 	pullRegistryAuth := &types.AuthConfig{}
47
-	if len(d.AuthConfigs) > 0 {
47
+	if len(authConfigs) > 0 {
48 48
 		// The request came with a full auth config file, we prefer to use that
49 49
 		repoInfo, err := d.Daemon.RegistryService.ResolveRepository(ref)
50 50
 		if err != nil {
... ...
@@ -52,13 +50,13 @@ func (d Docker) Pull(name string) (builder.Image, error) {
52 52
 		}
53 53
 
54 54
 		resolvedConfig := registry.ResolveAuthConfig(
55
-			d.AuthConfigs,
55
+			authConfigs,
56 56
 			repoInfo.Index,
57 57
 		)
58 58
 		pullRegistryAuth = &resolvedConfig
59 59
 	}
60 60
 
61
-	if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(d.OutOld)); err != nil {
61
+	if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(output)); err != nil {
62 62
 		return nil, err
63 63
 	}
64 64
 	return d.GetImage(name)
... ...
@@ -140,9 +138,16 @@ func (d Docker) BuilderCopy(cID string, destPath string, src builder.FileInfo, d
140 140
 		destExists = false
141 141
 	}
142 142
 
143
+	uidMaps, gidMaps := d.Daemon.GetUIDGIDMaps()
144
+	archiver := &archive.Archiver{
145
+		Untar:   chrootarchive.Untar,
146
+		UIDMaps: uidMaps,
147
+		GIDMaps: gidMaps,
148
+	}
149
+
143 150
 	if src.IsDir() {
144 151
 		// copy as directory
145
-		if err := d.Archiver.CopyWithTar(srcPath, destPath); err != nil {
152
+		if err := archiver.CopyWithTar(srcPath, destPath); err != nil {
146 153
 			return err
147 154
 		}
148 155
 		return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
... ...
@@ -160,7 +165,7 @@ func (d Docker) BuilderCopy(cID string, destPath string, src builder.FileInfo, d
160 160
 		}
161 161
 
162 162
 		// try to successfully untar the orig
163
-		err := d.Archiver.UntarPath(srcPath, tarDest)
163
+		err := archiver.UntarPath(srcPath, tarDest)
164 164
 		if err != nil {
165 165
 			logrus.Errorf("Couldn't untar to %s: %v", tarDest, err)
166 166
 		}
... ...
@@ -175,7 +180,7 @@ func (d Docker) BuilderCopy(cID string, destPath string, src builder.FileInfo, d
175 175
 	if err := idtools.MkdirAllNewAs(filepath.Dir(destPath), 0755, rootUID, rootGID); err != nil {
176 176
 		return err
177 177
 	}
178
-	if err := d.Archiver.CopyFileWithTar(srcPath, destPath); err != nil {
178
+	if err := archiver.CopyFileWithTar(srcPath, destPath); err != nil {
179 179
 		return err
180 180
 	}
181 181