forked from openshift/origin
/
executor.go
297 lines (264 loc) · 8.19 KB
/
executor.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
package sti
import (
"bufio"
"bytes"
"fmt"
"io"
"path/filepath"
"time"
"github.com/golang/glog"
"github.com/openshift/source-to-image/pkg/sti/api"
"github.com/openshift/source-to-image/pkg/sti/docker"
"github.com/openshift/source-to-image/pkg/sti/errors"
"github.com/openshift/source-to-image/pkg/sti/git"
"github.com/openshift/source-to-image/pkg/sti/script"
"github.com/openshift/source-to-image/pkg/sti/tar"
"github.com/openshift/source-to-image/pkg/sti/util"
)
const (
// maxErrorOutput is the maximum length of the error output saved for processing
maxErrorOutput = 1024
// defaultLocation is the default location of the scripts and sources in image
defaultLocation = "/tmp"
)
// requestHandler encapsulates dependencies needed to fulfill requests.
type requestHandler struct {
request *api.Request
result *api.Result
postExecutor postExecutor
errorChecker errorChecker
installer script.Installer
git git.Git
fs util.FileSystem
docker docker.Docker
tar tar.Tar
}
type postExecutor interface {
PostExecute(containerID string, location string) error
}
type errorChecker interface {
wasExpectedError(text string) bool
}
// newRequestHandler returns a new handler for a given request.
func newRequestHandler(req *api.Request) (*requestHandler, error) {
glog.V(2).Infof("Using docker socket: %s", req.DockerSocket)
docker, err := docker.NewDocker(req.DockerSocket)
if err != nil {
return nil, err
}
return &requestHandler{
request: req,
docker: docker,
installer: script.NewInstaller(req.BaseImage, req.ScriptsURL, docker),
git: git.NewGit(),
fs: util.NewFileSystem(),
tar: tar.NewTar(),
}, nil
}
func (h *requestHandler) setup(requiredScripts, optionalScripts []api.Script) (err error) {
if h.request.WorkingDir, err = h.fs.CreateWorkingDirectory(); err != nil {
return err
}
h.result = &api.Result{
Success: false,
WorkingDir: h.request.WorkingDir,
}
// immediately pull the image if forcepull is true, that way later code that
// references the image will have it pre-pulled and can just inspect the image.
if h.request.ForcePull {
err = h.docker.PullImage(h.request.BaseImage)
} else {
_, err = h.docker.CheckAndPull(h.request.BaseImage)
}
if err != nil {
return
}
// fetch sources, for theirs .sti/bin might contain sti scripts
if len(h.request.Source) > 0 {
if err = h.fetchSource(); err != nil {
return err
}
}
dirs := []string{"upload/scripts", "downloads/scripts", "downloads/defaultScripts"}
for _, v := range dirs {
if err = h.fs.MkdirAll(filepath.Join(h.request.WorkingDir, v)); err != nil {
return err
}
}
if h.request.ExternalRequiredScripts, err = h.installer.DownloadAndInstall(
requiredScripts, h.request.WorkingDir, true); err != nil {
return err
}
if h.request.ExternalOptionalScripts, err = h.installer.DownloadAndInstall(
optionalScripts, h.request.WorkingDir, false); err != nil {
glog.Warningf("Failed downloading optional scripts: %v", err)
}
return nil
}
func (h *requestHandler) generateConfigEnv() (configEnv []string) {
if len(h.request.Environment) > 0 {
for key, val := range h.request.Environment {
configEnv = append(configEnv, key+"="+val)
}
}
return
}
func (h *requestHandler) execute(command api.Script) error {
glog.V(2).Infof("Using image name %s", h.request.BaseImage)
uploadDir := filepath.Join(h.request.WorkingDir, "upload")
tarFileName, err := h.tar.CreateTarFile(h.request.WorkingDir, uploadDir)
if err != nil {
return err
}
tarFile, err := h.fs.Open(tarFileName)
if err != nil {
return err
}
defer tarFile.Close()
expectedError := ""
outReader, outWriter := io.Pipe()
errReader, errWriter := io.Pipe()
defer outReader.Close()
defer outWriter.Close()
defer errReader.Close()
defer errWriter.Close()
opts := docker.RunContainerOptions{
Image: h.request.BaseImage,
Stdout: outWriter,
Stderr: errWriter,
PullImage: h.request.ForcePull,
ExternalScripts: h.request.ExternalRequiredScripts,
ScriptsURL: h.request.ScriptsURL,
Location: h.request.Location,
Command: command,
Env: h.generateConfigEnv(),
PostExec: h,
}
if !h.request.LayeredBuild {
opts.Stdin = tarFile
}
// goroutine to stream container's output
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
if glog.V(2) || command == api.Usage {
glog.Info(scanner.Text())
}
}
}(outReader)
// goroutine to stream container's error
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
text := scanner.Text()
if glog.V(1) {
glog.Errorf(text)
}
if h.errorChecker != nil && h.errorChecker.wasExpectedError(text) &&
len(expectedError) < maxErrorOutput {
expectedError += text + "; "
}
}
}(errReader)
err = h.docker.RunContainer(opts)
if e, ok := err.(errors.ContainerError); ok {
return errors.NewContainerError(h.request.BaseImage, e.ErrorCode, expectedError)
}
return err
}
func (h *requestHandler) build() error {
// create Dockerfile
buffer := bytes.Buffer{}
location := h.request.Location
if len(location) == 0 {
location = defaultLocation
}
buffer.WriteString(fmt.Sprintf("FROM %s\n", h.request.BaseImage))
buffer.WriteString(fmt.Sprintf("ADD scripts %s\n", filepath.Join(location, "scripts")))
buffer.WriteString(fmt.Sprintf("ADD src %s\n", filepath.Join(location, "src")))
uploadDir := filepath.Join(h.request.WorkingDir, "upload")
if err := h.fs.WriteFile(filepath.Join(uploadDir, "Dockerfile"), buffer.Bytes()); err != nil {
return err
}
glog.V(2).Infof("Writing custom Dockerfile to %s", uploadDir)
tarFileName, err := h.tar.CreateTarFile(h.request.WorkingDir, uploadDir)
if err != nil {
return err
}
tarFile, err := h.fs.Open(tarFileName)
if err != nil {
return err
}
defer tarFile.Close()
newBaseImage := fmt.Sprintf("%s-%d", h.request.BaseImage, time.Now().UnixNano())
outReader, outWriter := io.Pipe()
defer outReader.Close()
defer outWriter.Close()
opts := docker.BuildImageOptions{
Name: newBaseImage,
Stdin: tarFile,
Stdout: outWriter,
}
// goroutine to stream container's output
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
glog.V(2).Info(scanner.Text())
}
}(outReader)
glog.V(2).Infof("Building new image %s with scripts and sources already inside", newBaseImage)
if err = h.docker.BuildImage(opts); err != nil {
return err
}
// upon successful build we need to modify current request
h.request.LayeredBuild = true
// new image name
h.request.BaseImage = newBaseImage
// the scripts are inside the image
h.request.ExternalRequiredScripts = false
h.request.ScriptsURL = "image://" + filepath.Join(location, "scripts")
// the source is also inside the image
h.request.Location = filepath.Join(location, "src")
return nil
}
func (h *requestHandler) PostExecute(containerID string, location string) (err error) {
h.result.Success = true
if h.postExecutor != nil {
err = h.postExecutor.PostExecute(containerID, location)
if err != nil {
glog.Errorf("An error occurred in post executor: %v", err)
}
}
return err
}
func (h *requestHandler) cleanup() {
if h.request.PreserveWorkingDir {
glog.Infof("Temporary directory '%s' will be saved, not deleted", h.request.WorkingDir)
} else {
glog.V(2).Infof("Removing temporary directory %s", h.request.WorkingDir)
h.fs.RemoveDirectory(h.request.WorkingDir)
}
if h.request.LayeredBuild {
glog.V(2).Infof("Removing temporary image %s", h.request.BaseImage)
h.docker.RemoveImage(h.request.BaseImage)
}
}
func (h *requestHandler) fetchSource() error {
targetSourceDir := filepath.Join(h.request.WorkingDir, "upload", "src")
glog.V(1).Infof("Downloading %s to directory %s", h.request.Source, targetSourceDir)
if h.git.ValidCloneSpec(h.request.Source) {
if err := h.git.Clone(h.request.Source, targetSourceDir); err != nil {
glog.Errorf("Git clone failed: %+v", err)
return err
}
if h.request.Ref != "" {
glog.V(1).Infof("Checking out ref %s", h.request.Ref)
if err := h.git.Checkout(targetSourceDir, h.request.Ref); err != nil {
return err
}
}
} else {
h.fs.Copy(h.request.Source, targetSourceDir)
}
return nil
}