-
Notifications
You must be signed in to change notification settings - Fork 377
/
builder.go
334 lines (283 loc) · 8.85 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
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
//go:generate go run go.uber.org/mock/mockgen -source builder.go -package builder -destination builder_mock.go ComponentBuilderFactory ComponentBuilder
package builder
import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/digitalocean/doctl/commands/charm"
"github.com/digitalocean/doctl/commands/charm/template"
"github.com/digitalocean/godo"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/pkg/stdcopy"
)
// StaticSiteNginxImage is the nginx image used for static site container images.
const StaticSiteNginxImage = "nginx:alpine"
var ErrNotFound = fmt.Errorf("image not found")
// ComponentBuilderFactory is the interface for creating a component builder.
type ComponentBuilderFactory interface {
NewComponentBuilder(DockerEngineClient, string, *godo.AppSpec, NewBuilderOpts) (ComponentBuilder, error)
}
// ComponentBuilder is the interface of building one or more components.
type ComponentBuilder interface {
Build(context.Context) (ComponentBuilderResult, error)
}
// ComponentBuilderResult ...
type ComponentBuilderResult struct {
Image string
BuildDuration time.Duration
ExitCode int
}
type baseComponentBuilder struct {
cli DockerEngineClient
contextDir string
spec *godo.AppSpec
component godo.AppBuildableComponentSpec
registry string
envOverrides map[string]string
buildCommandOverride string
copyOnWriteSemantics bool
noCache bool
logWriter io.Writer
}
func (b baseComponentBuilder) AppImageOutputName() string {
ref := fmt.Sprintf("%s:dev", b.component.GetName())
if b.registry != "" {
ref = fmt.Sprintf("%s/%s", b.registry, ref)
}
return ref
}
func (b baseComponentBuilder) StaticSiteImageOutputName() string {
return b.AppImageOutputName() + "-static"
}
func (b baseComponentBuilder) getLogWriter() io.Writer {
if b.logWriter == nil {
return os.Stdout
}
return b.logWriter
}
func (b baseComponentBuilder) getEnvMap() (map[string]string, error) {
envMap := map[string]string{}
lw := b.getLogWriter()
template.Render(lw, `{{success checkmark}} configuring build environment variables{{nl}}`, nil)
subLW := charm.IndentWriter(lw, 2)
addEnvs := func(envs ...*godo.AppVariableDefinition) {
for _, e := range envs {
if e.Type == godo.AppVariableType_Secret {
template.Render(subLW, `{{success checkmark}} ignoring SECRET variable {{highlight .GetKey}}{{nl}}`, e)
continue
}
if e.Scope != godo.AppVariableScope_RunTime {
val := e.Value
envMap[e.Key] = val
}
}
}
addEnvs(b.spec.GetEnvs()...)
addEnvs(b.component.GetEnvs()...)
for k, v := range b.envOverrides {
v := v
_, exists := envMap[k]
if !exists {
// TODO: if interactive prompt to auto add to spec
return nil, fmt.Errorf("variable not in found in app spec: %s", k)
}
template.Render(subLW, `{{success checkmark}} overriding value for variable {{highlight .}}{{nl}}`, k)
envMap[k] = v
}
return envMap, nil
}
func GetImage(ctx context.Context, cli DockerEngineClient, ref string) (*types.ImageSummary, error) {
images, err := cli.ImageList(ctx, types.ImageListOptions{
Filters: filters.NewArgs(filters.Arg("reference", ref)),
})
if err != nil {
return nil, err
}
if len(images) == 0 {
return nil, ErrNotFound
}
return &images[0], nil
}
func ImageExists(ctx context.Context, cli DockerEngineClient, ref string) (bool, error) {
image, err := GetImage(ctx, cli, ref)
if err != nil {
if err == ErrNotFound {
return false, nil
}
return false, err
}
return image != nil, nil
}
func (b *baseComponentBuilder) getStaticNginxConfig() string {
return `
server {
listen 8080;
listen [::]:8080;
resolver 127.0.0.11;
autoindex off;
server_name _;
server_tokens off;
root /www;
gzip_static on;
}
`
}
// ContainerExecError contains additional data on a container exec error.
type ContainerExecError struct {
Err error
ExitCode int
}
// Error returns the underlying error.
func (e ContainerExecError) Error() string {
return e.Err.Error()
}
func (b *baseComponentBuilder) runExec(ctx context.Context, containerID string, command, env []string, output io.Writer, input io.Reader) error {
if output == nil {
output = io.Discard
}
execRes, err := b.cli.ContainerExecCreate(ctx, containerID, types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
AttachStdin: input != nil,
Env: env,
Cmd: command,
})
if err != nil {
return fmt.Errorf("creating container exec: %w", err)
}
// read the output
attachRes, err := b.cli.ContainerExecAttach(ctx, execRes.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("attaching to container exec: %w", err)
}
defer attachRes.Close()
outputDone := make(chan error)
var wg sync.WaitGroup
if input != nil {
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(attachRes.Conn, input)
attachRes.CloseWrite()
}()
}
go func() {
// StdCopy demultiplexes the stream into two separate stdout and stderr buffers
_, err := stdcopy.StdCopy(output, output, attachRes.Reader)
outputDone <- err
}()
select {
case err = <-outputDone:
case <-ctx.Done():
err = ctx.Err()
}
if err != nil {
return err
}
wg.Wait()
// the exec process completed. check its exit code and return an error if it failed.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
res, err := b.cli.ContainerExecInspect(ctx, execRes.ID)
if err != nil {
return fmt.Errorf("inspecting container: %w", err)
} else if res.ExitCode > 0 {
return ContainerExecError{
Err: fmt.Errorf("command exited with a non-zero status code"),
ExitCode: res.ExitCode,
}
}
return nil
}
// NewBuilderOpts ...
type NewBuilderOpts struct {
Component string
Registry string
EnvOverride map[string]string
BuildCommandOverride string
CNBBuilderImageOverride string
LogWriter io.Writer
Versioning Versioning
LocalCacheDir string
NoCache bool
}
// Versioning contains build versioning configuration.
type Versioning struct {
CNB *godo.AppBuildConfigCNBVersioning
}
// DefaultComponentBuilderFactory is the standard component builder factory.
type DefaultComponentBuilderFactory struct{}
// NewComponentBuilder returns the correct builder type depending upon the provided
// app and component.
func (f *DefaultComponentBuilderFactory) NewComponentBuilder(cli DockerEngineClient, contextDir string, spec *godo.AppSpec, opts NewBuilderOpts) (ComponentBuilder, error) {
if opts.Component == "" {
return nil, errors.New("component is required")
}
component, err := godo.GetAppSpecComponent[godo.AppBuildableComponentSpec](spec, opts.Component)
if err != nil {
return nil, err
}
if component == nil {
return nil, fmt.Errorf("component %s does not exist", opts.Component)
}
// NOTE(ntate); We don't provide this as a configurable argument today.
// We always assume we want copy-on-write. Caching occurs through re-use of the built OCI image.
// This may change in the future so we provide as an argument to the baseComponentBuilder.
copyOnWriteSemantics := true
base := baseComponentBuilder{
cli,
contextDir,
spec,
component,
opts.Registry,
opts.EnvOverride,
opts.BuildCommandOverride,
copyOnWriteSemantics,
opts.NoCache,
opts.LogWriter,
}
if dockerComponent, ok := component.(godo.AppDockerBuildableComponentSpec); ok && dockerComponent.GetDockerfilePath() != "" {
return &DockerComponentBuilder{base, dockerComponent}, nil
}
if cnbComponent, ok := component.(godo.AppCNBBuildableComponentSpec); ok {
var cnbVersioning CNBVersioning
for _, bp := range opts.Versioning.CNB.GetBuildpacks() {
cnbVersioning.Buildpacks = append(cnbVersioning.Buildpacks, &Buildpack{
ID: bp.ID,
Version: fmt.Sprintf("%d.0.0", bp.MajorVersion),
})
}
return &CNBComponentBuilder{
baseComponentBuilder: base,
cnbComponent: cnbComponent,
versioning: cnbVersioning,
localCacheDir: opts.LocalCacheDir,
builderImageOverride: opts.CNBBuilderImageOverride,
}, nil
}
return nil, errors.New("component was not buildable by either Docker or CNB build system")
}
// IsCNBBuild indicates whether the component will be built using the CNB builder.
func IsCNBBuild(spec godo.AppBuildableComponentSpec) bool {
_, cnbBuildable := spec.(godo.AppCNBBuildableComponentSpec)
if !cnbBuildable {
return false
}
if dockerBuildable, ok := spec.(godo.AppDockerBuildableComponentSpec); ok && dockerBuildable.GetDockerfilePath() != "" {
return false
}
return true
}
// IsDockerBuild indicates whether the component will be built using the Docker builder.
func IsDockerBuild(spec godo.AppBuildableComponentSpec) bool {
dockerBuildable, ok := spec.(godo.AppDockerBuildableComponentSpec)
if !ok {
return false
}
return dockerBuildable.GetDockerfilePath() != ""
}