/
build.go
355 lines (294 loc) · 8.64 KB
/
build.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
package launchpad
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
dockerclient "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/afero"
"go.jetpack.io/launchpad/goutil/errorutil"
"go.jetpack.io/launchpad/padcli/hook"
"go.jetpack.io/launchpad/padcli/jetconfig"
"go.jetpack.io/launchpad/padcli/provider"
"go.jetpack.io/launchpad/pkg/docker"
"go.jetpack.io/launchpad/pkg/jetlog"
"go.jetpack.io/launchpad/pkg/kubevalidate"
)
const (
dockerProjectIdLabel = "jetpack.io/project-id"
)
type BuildOptions struct {
// fields in abc order
AppName string
BuildArgs map[string]string
LifecycleHook hook.LifecycleHook
// Pre-built local image to use
LocalImage string
// ProjectDir is the absolute path to the module
ProjectDir string
ProjectId string
// Platform informs docker which architecture to build for.
// Examples: linux/arm64 (for M1 macs) linux/amd64 (for intel macs)
Platform string
Services map[string]jetconfig.Builder
RemoteCache bool
RepoConfig provider.RepoConfig // required for remote cache feature
ImageRepoForCache string
TagPrefix string
}
func (b *BuildOptions) GetBuilders() map[string]jetconfig.Builder {
return lo.PickBy(
b.Services,
func(name string, s jetconfig.Builder) bool {
return s.GetBuildCommand() != ""
},
)
}
func (b *BuildOptions) GetRepoHost() string {
if b == nil || b.RepoConfig == nil {
return ""
}
return strings.Split(b.RepoConfig.GetImageRepoPrefix(), "/")[0]
}
type BuildPlan struct {
// keep fields in abc order.
dockerfilePath string
// Best explained via example. If the full URL of the image is:
// us-central1-docker.pkg.dev/jetpack-dev/jetpack-internal-demo/py-hello-world:34fd45
//
// then:
// registry.uri is us-central1-docker.pkg.dev
// imageName is py-hello-world
// imageRepo is jetpack-dev/jetpack-internal-demo/py-hello-world
// imageTag is 34fd45
image *LocalImage
imageLabels map[string]string
// projectDir is the absolute path to the directory of the project
projectDir string
buildOpts *BuildOptions
}
func (p *BuildPlan) requiresDockerfile() bool {
return planShouldUseDockerfile(p.buildOpts)
}
type BuildOutput struct {
Duration time.Duration
Image *LocalImage
}
func (o *BuildOutput) DidBuildUsingDockerfile() bool {
return o != nil && o.Image != nil && *o.Image != ""
}
func (o *BuildOutput) SetDuration(d time.Duration) {
if o != nil {
o.Duration = d
}
}
// build is the internal implementation of Build. See docs there.
func build(
ctx context.Context,
fs afero.Fs,
opts *BuildOptions,
) (*BuildOutput, error) {
plan, err := makeBuildPlan(ctx, opts)
if err != nil {
return nil, errors.Wrap(err, "failed to make build plan")
}
err = validateBuildPlan(plan, fs)
if err != nil {
return nil, errors.Wrap(err, "failed to validate build plan")
}
err = executeBuildPlan(ctx, plan)
if err != nil {
return nil, errors.Wrap(err, "failed to execute build plan")
}
return &BuildOutput{Image: plan.image}, nil
}
func makeBuildPlan(
ctx context.Context,
opts *BuildOptions,
) (*BuildPlan, error) {
useDockerfile := planShouldUseDockerfile(opts)
dockerfilePath := ""
imageName := ""
imageTag := ""
if useDockerfile {
var err error
dockerfilePath, err = getDockerfilePath(opts.ProjectDir)
if err != nil {
return nil, errors.WithStack(err)
}
if dockerfilePath != "" {
imageName, imageTag, err = getImageNameAndTag(ctx, opts)
if err != nil {
return nil, errors.WithStack(err)
}
}
} else if opts.LocalImage != "" {
imageInfo := strings.Split(opts.LocalImage, ":")
if len(imageInfo) > 0 {
imageName = imageInfo[0]
if len(imageInfo) == 2 {
imageTag = imageInfo[1]
}
}
}
plan := &BuildPlan{
dockerfilePath: dockerfilePath,
image: newLocalImageWithTag(imageName, imageTag),
projectDir: opts.ProjectDir,
imageLabels: map[string]string{dockerProjectIdLabel: opts.ProjectId},
buildOpts: opts,
}
return plan, nil
}
func planShouldUseDockerfile(opts *BuildOptions) bool {
if len(opts.Services) == 0 {
return true
}
hasServiceWithEmptyImage := lo.SomeBy(
lo.Values(opts.Services),
func(s jetconfig.Builder) bool {
return s.GetImage() == ""
},
)
// If any service doesn't have an image specified,
// then we need to build a docker image for it to use.
return hasServiceWithEmptyImage && opts.LocalImage == ""
}
// getDockerfilePath returns the location of the docker file in the module directory.
// A return value of an empty string and nil error means that no Dockerfile was found.
func getDockerfilePath(modulePath string) (string, error) {
path := filepath.Join(modulePath, "Dockerfile")
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", errors.WithStack(err)
}
return path, nil
}
func validateBuildPlan(plan *BuildPlan, fs afero.Fs) error {
// verify that the projectDir is a directory
isDir, err := afero.DirExists(fs, plan.projectDir)
if err != nil {
return errors.WithStack(err)
}
if !isDir {
return errors.Errorf(
"Expect module-path to be a directory, but %s is not",
plan.projectDir,
)
}
if plan.dockerfilePath == "" && plan.requiresDockerfile() {
return errorutil.NewUserError(
"Dockerfile missing.\n" +
"- Please add a Dockerfile manually under your app directory.\n" +
"- You can find an example Dockerfile at " +
"https://github.com/jetpack-io/project-templates/blob/main/api/Dockerfile.\n" +
"- Alternatively, to use a pre-existing image, you can add an Image: field to your service in" +
" launchpad.yaml or jetconfig.yaml.",
)
}
return nil
}
func executeBuildPlan(ctx context.Context, plan *BuildPlan) error {
for name, b := range plan.buildOpts.GetBuilders() {
jetlog.Logger(ctx).Printf(
"Working dir: %s\nBuilding service %s using \"%s\"\n",
b.GetPath(),
name,
b.GetBuildCommand(),
)
cmd := exec.CommandContext(
ctx,
"/bin/sh",
"-c",
b.GetBuildCommand(),
)
cmd.Dir = b.GetPath()
out, err := cmd.CombinedOutput()
// TODO(landau): We could return output here.
jetlog.Logger(ctx).Print(string(out))
if err != nil {
return errors.Wrapf(err, "failed to execute build command for service %s", name)
}
}
if plan.dockerfilePath != "" {
if err := executePlanUsingDocker(ctx, plan); err != nil {
return errors.Wrap(err, "failed to execute build plan using docker")
}
}
return nil
}
func executePlanUsingDocker(ctx context.Context, plan *BuildPlan) (err error) {
jetlog.Logger(ctx).IndentedPrintf("Building Docker image with Dockerfile at: %s\n", plan.dockerfilePath)
// Pulling out in case we want to allow customizing this var in the future.
dockerfile := "Dockerfile"
imageBuildOptions := docker.BuildOpts{
BuildArgs: lo.MapValues(plan.buildOpts.BuildArgs, func(val, _ string) *string {
return &val
}),
Dockerfile: dockerfile,
Platform: plan.buildOpts.Platform,
Tags: []string{plan.image.String()},
Labels: plan.imageLabels,
}
return docker.Build(ctx, filepath.Dir(plan.dockerfilePath), imageBuildOptions)
}
// getImageNameAndTag returns a valid docker image name
// and its imageTag is appended.
func getImageNameAndTag(ctx context.Context, opts *BuildOptions) (string, string, error) {
name := opts.AppName
name, err := kubevalidate.ToValidName(filepath.Base(name))
if err != nil {
return "", "", errors.WithStack(err)
}
return name, generateDateImageTag(opts.TagPrefix), nil
}
// DockerCleanup deletes all docker images that are not the latest based on timestamp.
// This will only delete the images that belong to the current project.
func DockerCleanup(ctx context.Context, labelIdentifier string) error {
cli, err := dockerclient.NewClientWithOpts(
dockerclient.FromEnv,
dockerclient.WithAPIVersionNegotiation(),
)
if err != nil {
return errors.WithStack(err)
}
filters := filters.NewArgs()
filters.Add("label", fmt.Sprintf("%s=%s", dockerProjectIdLabel, labelIdentifier))
imgs, err := cli.ImageList(ctx, dockertypes.ImageListOptions{
Filters: filters,
})
if err != nil {
return errors.WithStack(err)
}
var latestTimestamp int64 = -1
for _, d := range imgs {
if d.Labels[dockerProjectIdLabel] == labelIdentifier {
if d.Created > latestTimestamp {
latestTimestamp = d.Created
}
}
}
if latestTimestamp == -1 {
return nil
}
for _, d := range imgs {
if d.Labels[dockerProjectIdLabel] == labelIdentifier && d.Created != latestTimestamp {
_, err = cli.ImageRemove(
ctx,
d.ID,
dockertypes.ImageRemoveOptions{PruneChildren: true, Force: true},
)
return errors.WithStack(err)
}
}
return nil
}