forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
184 lines (165 loc) · 5.42 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
package builder
import (
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/source-to-image/pkg/git"
"github.com/openshift/source-to-image/pkg/tar"
)
// 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
// imageRegex is used to substitute image names in buildconfigs with immutable image ids at build time.
var imageRegex = regexp.MustCompile(`(?mi)^\s*FROM\s+\w+.+`)
// 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
}
if err = d.dockerBuild(buildDir); err != nil {
return err
}
tag := d.build.Parameters.Output.DockerImageReference
defer removeImage(d.dockerClient, tag)
if len(d.build.Parameters.Output.DockerImageReference) != 0 {
return pushImage(d.dockerClient, tag, d.auth)
}
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.Image != "" {
newFileData = imageRegex.ReplaceAllLiteralString(string(fileData), fmt.Sprintf("FROM %s", d.build.Parameters.Strategy.DockerStrategy.Image))
} else {
newFileData = newFileData + string(fileData)
}
envVars := getBuildEnvVars(d.build)
for k, v := range envVars {
newFileData = newFileData + fmt.Sprintf("ENV %s %s\n", k, v)
}
if ioutil.WriteFile(dockerfilePath, []byte(newFileData), filePerm); err != nil {
return err
}
return nil
}
// 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
}
return buildImage(d.dockerClient, dir, noCache, d.build.Parameters.Output.DockerImageReference, d.tar)
}