forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
362 lines (325 loc) · 10.2 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package builder
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"time"
dockercmd "github.com/docker/docker/builder/command"
"github.com/docker/docker/builder/parser"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
"github.com/openshift/source-to-image/pkg/git"
"github.com/openshift/source-to-image/pkg/tar"
docker "github.com/fsouza/go-dockerclient"
)
// urlCheckTimeout is the timeout used to check the source URL
// If fetching the URL exceeds the timeout, then the build will
// not proceed further and stop
const urlCheckTimeout = 16 * time.Second
// DockerBuilder builds Docker images given a git repository URL
type DockerBuilder struct {
dockerClient DockerClient
authPresent bool
auth docker.AuthConfiguration
git git.Git
tar tar.Tar
build *api.Build
urlTimeout time.Duration
}
// NewDockerBuilder creates a new instance of DockerBuilder
func NewDockerBuilder(dockerClient DockerClient, authCfg docker.AuthConfiguration, authPresent bool, build *api.Build) *DockerBuilder {
return &DockerBuilder{
dockerClient: dockerClient,
authPresent: authPresent,
auth: authCfg,
build: build,
git: git.New(),
tar: tar.New(),
urlTimeout: urlCheckTimeout,
}
}
// Build executes a Docker build
func (d *DockerBuilder) Build() error {
buildDir, err := ioutil.TempDir("", "docker-build")
if err != nil {
return err
}
if err = d.fetchSource(buildDir); err != nil {
return err
}
if err = d.addBuildParameters(buildDir); err != nil {
return err
}
glog.V(4).Infof("Starting Docker build from %s/%s BuildConfig ...", d.build.Namespace, d.build.Name)
if err = d.dockerBuild(buildDir); err != nil {
return err
}
tag := d.build.Parameters.Output.DockerImageReference
defer removeImage(d.dockerClient, tag)
dockerImageRef := d.build.Parameters.Output.DockerImageReference
if len(dockerImageRef) != 0 {
// Get the Docker push authentication
pushAuthConfig, authPresent := dockercfg.NewHelper().GetDockerAuth(
dockerImageRef,
dockercfg.PushAuthType,
)
if authPresent {
glog.V(3).Infof("Using Docker authentication provided")
d.auth = pushAuthConfig
}
glog.Infof("Pushing %s image ...", dockerImageRef)
if err := pushImage(d.dockerClient, tag, d.auth); err != nil {
return fmt.Errorf("Failed to push image: %v", err)
}
glog.Infof("Successfully pushed %s", dockerImageRef)
}
return nil
}
// checkSourceURI performs a check on the URI associated with the build
// to make sure that it is live before proceeding with the build.
func (d *DockerBuilder) checkSourceURI() error {
rawurl := d.build.Parameters.Source.Git.URI
if !d.git.ValidCloneSpec(rawurl) {
return fmt.Errorf("Invalid git source url: %s", rawurl)
}
if strings.HasPrefix(rawurl, "git://") || strings.HasPrefix(rawurl, "git@") {
return nil
}
if !strings.HasPrefix(rawurl, "http://") && !strings.HasPrefix(rawurl, "https://") {
rawurl = fmt.Sprintf("https://%s", rawurl)
}
srcURL, err := url.Parse(rawurl)
if err != nil {
return err
}
host := srcURL.Host
if strings.Index(host, ":") == -1 {
switch srcURL.Scheme {
case "http":
host += ":80"
case "https":
host += ":443"
}
}
dialer := net.Dialer{Timeout: d.urlTimeout}
conn, err := dialer.Dial("tcp", host)
if err != nil {
return err
}
return conn.Close()
}
// fetchSource retrieves the git source from the repository. If a commit ID
// is included in the build revision, that commit ID is checked out. Otherwise
// if a ref is included in the source definition, that ref is checked out.
func (d *DockerBuilder) fetchSource(dir string) error {
if err := d.checkSourceURI(); err != nil {
return err
}
if err := d.git.Clone(d.build.Parameters.Source.Git.URI, dir); err != nil {
return err
}
if d.build.Parameters.Source.Git.Ref == "" &&
(d.build.Parameters.Revision == nil ||
d.build.Parameters.Revision.Git == nil ||
d.build.Parameters.Revision.Git.Commit == "") {
return nil
}
if d.build.Parameters.Revision != nil &&
d.build.Parameters.Revision.Git != nil &&
d.build.Parameters.Revision.Git.Commit != "" {
return d.git.Checkout(dir, d.build.Parameters.Revision.Git.Commit)
}
return d.git.Checkout(dir, d.build.Parameters.Source.Git.Ref)
}
// addBuildParameters checks if a Image is set to replace the default base image.
// If that's the case then change the Dockerfile to make the build with the given image.
// Also append the environment variables in the Dockerfile.
func (d *DockerBuilder) addBuildParameters(dir string) error {
dockerfilePath := filepath.Join(dir, "Dockerfile")
if d.build.Parameters.Strategy.DockerStrategy != nil && len(d.build.Parameters.Source.ContextDir) > 0 {
dockerfilePath = filepath.Join(dir, d.build.Parameters.Source.ContextDir, "Dockerfile")
}
fileStat, err := os.Lstat(dockerfilePath)
if err != nil {
return err
}
filePerm := fileStat.Mode()
fileData, err := ioutil.ReadFile(dockerfilePath)
if err != nil {
return err
}
var newFileData string
if d.build.Parameters.Strategy.DockerStrategy.From != nil && d.build.Parameters.Strategy.DockerStrategy.From.Kind == "DockerImage" {
newFileData, err = replaceValidCmd(dockercmd.From, d.build.Parameters.Strategy.DockerStrategy.From.Name, fileData)
if err != nil {
return err
}
} else {
newFileData = newFileData + string(fileData)
}
envVars := getBuildEnvVars(d.build)
first := true
for k, v := range envVars {
if first {
newFileData += fmt.Sprintf("ENV %s=\"%s\"", k, v)
first = false
} else {
newFileData += fmt.Sprintf(" \\\n\t%s=\"%s\"", k, v)
}
}
newFileData += "\n"
if ioutil.WriteFile(dockerfilePath, []byte(newFileData), filePerm); err != nil {
return err
}
return nil
}
// invalidCmdErr represents an error returned from replaceValidCmd
// when an invalid Dockerfile command has been passed to
// replaceValidCmd
var invalidCmdErr = errors.New("invalid Dockerfile command")
// replaceCmdErr represents an error returned from replaceValidCmd
// when a command which has more than one valid occurrences inside
// a Dockerfile has been passed or the specified command cannot
// be found
var replaceCmdErr = errors.New("cannot replace given Dockerfile command")
// replaceValidCmd replaces the valid occurrence of a command
// in a Dockerfile with the given replaceArgs
func replaceValidCmd(cmd, replaceArgs string, fileData []byte) (string, error) {
if _, ok := dockercmd.Commands[cmd]; !ok {
return "", invalidCmdErr
}
buf := bytes.NewBuffer(fileData)
// Parse with Docker parser
node, err := parser.Parse(buf)
if err != nil {
return "", errors.New("cannot parse Dockerfile: " + err.Error())
}
pos := traverseAST(cmd, node)
if pos == 0 {
return "", replaceCmdErr
}
// Re-initialize the buffer
buf = bytes.NewBuffer(fileData)
var newFileData string
var index int
var replaceNextLn bool
for {
line, err := buf.ReadString('\n')
if err != nil && err != io.EOF {
return "", err
}
line = strings.TrimSpace(line)
// The current line starts with the specified command (cmd)
if strings.HasPrefix(strings.ToUpper(line), strings.ToUpper(cmd)) {
index++
// The current line finishes on a backslash.
// All we need to do is replace the next line
// with our specified replaceArgs
if line[len(line)-1:] == "\\" && index == pos {
replaceNextLn = true
args := strings.Split(line, " ")
if len(args) > 2 {
// Keep just our Dockerfile command and the backslash
newFileData += args[0] + " \\" + "\n"
} else {
newFileData += line + "\n"
}
continue
}
// Normal ending line
if index == pos {
line = fmt.Sprintf("%s %s", strings.ToUpper(cmd), replaceArgs)
}
}
// Previous line ended on a backslash
// This line contains command arguments
if replaceNextLn {
if line[len(line)-1:] == "\\" {
// Ignore all successive lines terminating on a backslash
// since they all are going to be replaced by replaceArgs
continue
}
replaceNextLn = false
line = replaceArgs
}
if err == io.EOF {
// Otherwise, the new Dockerfile will have one newline
// more in the end
newFileData += line
break
}
newFileData += line + "\n"
}
// Parse output for validation
buf = bytes.NewBuffer([]byte(newFileData))
if _, err := parser.Parse(buf); err != nil {
return "", errors.New("cannot parse new Dockerfile: " + err.Error())
}
return newFileData, nil
}
// traverseAST traverses the Abstract Syntax Tree output
// from the Docker parser and returns the valid position
// of the command it was requested to look for.
//
// Note that this function is intended to be used with
// Dockerfile commands that should be specified only once
// in a Dockerfile (FROM, CMD, ENTRYPOINT)
func traverseAST(cmd string, node *parser.Node) int {
switch cmd {
case dockercmd.From, dockercmd.Entrypoint, dockercmd.Cmd:
default:
return 0
}
index := 0
if node.Value == cmd {
index++
}
for _, n := range node.Children {
index += traverseAST(cmd, n)
}
if node.Next != nil {
for n := node.Next; n != nil; n = n.Next {
if len(n.Children) > 0 {
index += traverseAST(cmd, n)
} else if n.Value == cmd {
index++
}
}
}
return index
}
// setupPullSecret provides a Docker authentication configuration when the
// PullSecret is specified.
func (d *DockerBuilder) setupPullSecret() (*docker.AuthConfigurations, error) {
if len(os.Getenv("PULL_DOCKERCFG_PATH")) == 0 {
return nil, nil
}
r, err := os.Open(os.Getenv("PULL_DOCKERCFG_PATH"))
if err != nil {
return nil, fmt.Errorf("'%s': %s", os.Getenv("PULL_DOCKERCFG_PATH"), err)
}
return docker.NewAuthConfigurations(r)
}
// dockerBuild performs a docker build on the source that has been retrieved
func (d *DockerBuilder) dockerBuild(dir string) error {
var noCache bool
if d.build.Parameters.Strategy.DockerStrategy != nil {
if d.build.Parameters.Source.ContextDir != "" {
dir = filepath.Join(dir, d.build.Parameters.Source.ContextDir)
}
noCache = d.build.Parameters.Strategy.DockerStrategy.NoCache
}
auth, err := d.setupPullSecret()
if err != nil {
return err
}
return buildImage(d.dockerClient, dir, noCache, d.build.Parameters.Output.DockerImageReference, d.tar, auth)
}