forked from box-builder/box
/
builder.go
298 lines (245 loc) · 6.94 KB
/
builder.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
package builder
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"github.com/erikh/box/builder/executor"
"github.com/erikh/box/builder/executor/docker"
"github.com/erikh/box/copy"
"github.com/erikh/box/logger"
"github.com/fatih/color"
mruby "github.com/mitchellh/go-mruby"
)
var pulls = map[string]chan struct{}{}
var pullMutex = new(sync.Mutex)
// BuildConfig is a struct containing the configuration for the builder.
type BuildConfig struct {
Cache bool
TTY bool // controls terminal codes
ShowRun bool
OmitFuncs []string
Context context.Context
Runner chan struct{}
FileName string
Logger *logger.Logger
}
// BuildResult is an encapsulated tuple of *mruby.MrbValue and error used for
// communicating... build results.
type BuildResult struct {
FileName string
Value *mruby.MrbValue
Err error
}
// Builder implements the builder core.
type Builder struct {
Logger *logger.Logger // public so its output can be tested
result BuildResult
config *BuildConfig
mrb *mruby.Mrb
exec executor.Executor
afterFunc *mruby.MrbValue
}
func (b *Builder) keep(name string) bool {
for _, fun := range b.config.OmitFuncs {
if name == fun {
return false
}
}
return true
}
// NewBuilder creates a new builder. Returns error on docker or mruby issues.
func NewBuilder(bc BuildConfig) (*Builder, error) {
if !bc.TTY {
color.NoColor = true
copy.NoTTY = true
}
log := bc.Logger
if log == nil {
log = logger.New(bc.FileName)
}
exec, err := NewExecutor(bc.Context, "docker", log, bc.ShowRun, bc.Cache, bc.TTY)
if err != nil {
return nil, err
}
builder := &Builder{
Logger: log,
config: &bc,
mrb: mruby.NewMrb(),
exec: exec,
}
for name, def := range verbJumpTable {
if builder.keep(name) {
builder.AddVerb(name, def)
}
}
for name, def := range funcJumpTable {
if builder.keep(name) {
inner := def.fun
fn := func(m *mruby.Mrb, self *mruby.MrbValue) (mruby.Value, mruby.Value) {
return inner(builder, m, self)
}
builder.mrb.TopSelf().SingletonClass().DefineMethod(name, fn, def.argSpec)
}
}
return builder, nil
}
// Tag tags the last image yielded by the builder with the provided name.
func (b *Builder) Tag(name string) error {
return b.exec.Image().Tag(name)
}
// ImageID returns the latest known Image identifier that we committed. At the
// end of the run this will be the golden docker image.
func (b *Builder) ImageID() string {
return b.exec.Image().ImageID()
}
func (b *Builder) wrapVerbFunc(name string, vd *verbDefinition) mruby.Func {
return func(m *mruby.Mrb, self *mruby.MrbValue) (mruby.Value, mruby.Value) {
select {
case <-b.config.Context.Done():
if b.config.Context.Err() != nil {
return nil, createException(m, b.config.Context.Err().Error())
}
return nil, nil
default:
}
strArgs := extractStringArgs(m.GetArgs())
cacheKey := strings.Join(append([]string{name}, strArgs...), ", ")
cacheKey = base64.StdEncoding.EncodeToString([]byte(cacheKey))
b.Logger.BuildStep(name, strings.Join(strArgs, ", "))
if os.Getenv("BOX_DEBUG") != "" {
content, _ := json.MarshalIndent(b.exec.Config(), "", " ")
fmt.Println(string(content))
}
cached, err := b.exec.Image().CheckCache(cacheKey)
if err != nil {
return nil, createException(m, err.Error())
}
// if we don't do this for debug, we will step past it on successive runs
if !cached || name == "debug" {
return vd.verbFunc(b, cacheKey, m.GetArgs(), m, self)
}
return nil, nil
}
}
// AddVerb adds a function to the mruby dispatch as well as adding hooks around
// the call to ensure containers are committed and intermediate layers are
// cleared.
func (b *Builder) AddVerb(name string, vd *verbDefinition) {
b.mrb.TopSelf().SingletonClass().DefineMethod(name, b.wrapVerbFunc(name, vd), vd.argSpec)
}
// RunCode runs the ruby value (a proc) and returns the result. It does not
// close the run channel.
func (b *Builder) RunCode(val *mruby.MrbValue, stackKeep int) (BuildResult, int) {
keep, res, err := b.mrb.RunWithContext(val, b.mrb.TopSelf(), stackKeep)
b.result = BuildResult{
FileName: b.FileName(),
Value: res,
Err: err,
}
if err != nil {
return b.result, keep
}
if res != nil {
b.result = BuildResult{
FileName: b.FileName(),
Value: res,
}
return b.result, keep
}
if _, err := b.exec.Layers().MakeImage(b.exec.Config()); err != nil {
b.result.Value = nil
b.result.Err = err
return b.result, keep
}
_, err = b.mrb.Yield(b.afterFunc)
if err != nil {
b.result.Err = err
return b.result, keep
}
b.result.Value = mruby.String(b.ImageID()).MrbValue(b.mrb)
b.result.Err = nil
return b.result, keep
}
// Result returns the latest cached result from any run invocation. The
// behavior is undefined if called before any Run()-style invocation.
func (b *Builder) Result() BuildResult {
return b.result
}
// Run runs the script set by the BuildConfig. It closes the run channel when
// it finishes.
func (b *Builder) Run() BuildResult {
defer close(b.config.Runner)
script, err := ioutil.ReadFile(b.config.FileName)
if err != nil {
return BuildResult{
FileName: b.FileName(),
Err: err,
}
}
return b.RunScript(string(script))
}
// RunScript runs the provided script. It does not close the run channel.
func (b *Builder) RunScript(script string) BuildResult {
b.result = BuildResult{
FileName: b.FileName(),
}
if _, err := b.mrb.LoadString(script); err != nil {
b.result.Err = err
return b.result
}
if _, err := b.exec.Layers().MakeImage(b.exec.Config()); err != nil {
b.result.Err = err
return b.result
}
if b.afterFunc != nil {
_, err := b.mrb.Yield(b.afterFunc)
if err != nil {
b.result.Err = err
return b.result
}
}
b.result.Value = mruby.String(b.ImageID()).MrbValue(b.mrb)
return b.result
}
// Mrb returns the mrb (mruby) instance the builder is using.
func (b *Builder) Mrb() *mruby.Mrb {
return b.mrb
}
// FileName returns the filename that invoked the build.
func (b *Builder) FileName() string {
return b.config.FileName
}
// Wait waits for the build to complete.
func (b *Builder) Wait() BuildResult {
<-b.config.Runner
return b.result
}
// SetContext sets the execution context.
func (b *Builder) SetContext(ctx context.Context) {
b.config.Context = ctx
b.exec.SetContext(ctx)
}
// Close tears down all functions of the builder, preparing it for exit.
func (b *Builder) Close() error {
b.mrb.EnableGC()
b.mrb.FullGC()
b.mrb.Close()
return nil
}
// NewExecutor returns a valid executor for the given name, or error.
func NewExecutor(ctx context.Context, name string, log *logger.Logger, showRun, useCache, tty bool) (executor.Executor, error) {
switch name {
case "docker":
return docker.NewDocker(ctx, log, showRun, useCache, tty)
}
return nil, fmt.Errorf("Executor %q not found", name)
}
// ResetPulls is a function to facilitate testing of the coordinated pull functionality.
func ResetPulls() {
pulls = map[string]chan struct{}{}
}