-
Notifications
You must be signed in to change notification settings - Fork 8
/
buildx.go
688 lines (562 loc) · 19 KB
/
buildx.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cluster
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/dockerutil"
"github.com/docker/cli/cli/command"
cliflags "github.com/docker/cli/cli/flags"
"github.com/muesli/reflow/wordwrap"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"namespacelabs.dev/foundation/framework/rpcerrors"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/files"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
const (
metadataFile = "metadata.json"
defaultBuilder = "nsc-remote"
proxyDir = "proxy"
buildkitProxyPath = "buildkit/" + proxyDir
)
func newSetupBuildxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "setup",
Short: "Setup buildx in the current machine, to use Namespace Remote builders.",
}
name := cmd.Flags().String("name", defaultBuilder, "The name of the builder we setup.")
use := cmd.Flags().Bool("use", false, "If true, changes the current builder to nsc-remote.")
background := cmd.Flags().Bool("background", false, "If true, runs the remote builder context in the background.")
createAtStartup := cmd.Flags().Bool("create_at_startup", false, "If true, creates the build clusters eagerly.")
stateDir := cmd.Flags().String("state", "", "If set, stores the remote builder context details in this directory.")
debugDir := cmd.Flags().String("background_debug_dir", "", "If set with --background, the tool populates the specified directory with debug log files.")
_ = cmd.Flags().MarkHidden("background_debug_dir")
useGrpcProxy := cmd.Flags().Bool("use_grpc_proxy", true, "If set, traffic is proxied with transparent grpc proxy instead of raw network proxy")
_ = cmd.Flags().MarkHidden("use_grpc_proxy")
staticWorkerDefFile := cmd.Flags().String("static_worker_definition_path", "", "Injects the gRPC proxy ListWorkers response JSON payload from file")
_ = cmd.Flags().MarkHidden("static_worker_definition_path")
forceCleanup := cmd.Flags().Bool("force_cleanup", false, "If set, it forces a cleanup of any previous buildx proxy running in background.")
waitForLogin := cmd.Flags().Bool("wait_for_login", false, "If set, it blocks waiting for user to login.")
_ = cmd.Flags().MarkHidden("wait_for_login")
annotateBuild := cmd.Flags().Bool("annotate_build", true, "If set, it enable builds annotation when running in Namespace instances.")
_ = cmd.Flags().MarkHidden("annotate_build")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
if *debugDir != "" && !*background {
return fnerrors.New("--background_debug_dir requires --background")
}
if !*useGrpcProxy && *staticWorkerDefFile != "" {
return fnerrors.New("--inject_worker_info_file requires --use_grpc_proxy")
}
// NSL-2249 for brew service, block here until user logs in.
if *waitForLogin {
err := blockWaitForLogin(ctx)
if err != nil {
return err
}
}
dockerCli, err := command.NewDockerCli()
if err != nil {
return err
}
if err := dockerCli.Initialize(cliflags.NewClientOptions()); err != nil {
return err
}
eg := executor.New(ctx, "proxies")
available, err := determineAvailable(ctx)
if err != nil {
return err
}
if len(available) == 0 {
return rpcerrors.Errorf(codes.Internal, "no builders available")
}
state, err := ensureStateDir(*stateDir, buildkitProxyPath)
if err != nil {
return err
}
fmt.Fprintf(console.Debug(ctx), "Using state path %q\n", state)
if proxyAlreadyExists(state) {
if *forceCleanup {
if err := withStore(dockerCli, func(txn *store.Txn) error {
return doBuildxCleanup(ctx, state, txn)
}); err != nil {
console.SetStickyContent(ctx, "build", existingProxyMessage(*stateDir))
return err
}
// Cleanup deletes also the state directory, recreate it
state, err = ensureStateDir(*stateDir, buildkitProxyPath)
if err != nil {
return err
}
} else {
console.SetStickyContent(ctx, "build", existingProxyMessage(*stateDir))
return nil
}
}
md := buildxMetadata{
NodeGroupName: *name,
}
for _, p := range available {
sockPath := filepath.Join(state, fmt.Sprintf("%s.sock", p))
controlSockPath := filepath.Join(state, fmt.Sprintf("control_%s.sock", p))
instanceMD := buildxInstanceMetadata{
Platform: p,
SocketPath: sockPath,
Pid: os.Getpid(), // This will be overwritten if running proxies in background
ControlSocketPath: controlSockPath,
}
if *background {
logFilename := fmt.Sprintf("%s-proxy.log", instanceMD.Platform)
if *debugDir != "" {
instanceMD.DebugLogPath = path.Join(*debugDir, logFilename)
} else {
logDir, err := ensureLogDir(proxyDir)
if err != nil {
return fnerrors.New("failed to create the log folder: %v", err)
}
instanceMD.DebugLogPath = path.Join(logDir, logFilename)
}
}
md.Instances = append(md.Instances, instanceMD)
}
var instances []*BuildClusterInstance
for i, p := range md.Instances {
// Always create one, in case it's needed below. This instance has a zero-ish cost if we never call NewConn.
instance := NewBuildClusterInstance0(p.Platform)
instances = append(instances, instance)
if *background {
if pid, err := startBackgroundProxy(ctx, p, *createAtStartup, *useGrpcProxy, *annotateBuild, *staticWorkerDefFile); err != nil {
return err
} else {
md.Instances[i].Pid = pid
}
} else {
workerInfoResp, err := parseInjectWorkerInfo(*staticWorkerDefFile, p.Platform)
if err != nil {
return fnerrors.New("failed to parse worker info JSON payload: %v", err)
}
bp, err := instance.runBuildProxy(ctx, p.SocketPath, p.ControlSocketPath, *useGrpcProxy, *annotateBuild, workerInfoResp)
if err != nil {
return err
}
defer os.Remove(p.SocketPath)
defer os.Remove(p.ControlSocketPath)
eg.Go(func(ctx context.Context) error {
<-ctx.Done()
return bp.Cleanup()
})
eg.Go(func(ctx context.Context) error {
return bp.Serve(ctx)
})
eg.Go(func(ctx context.Context) error {
return bp.ServeStatus(ctx)
})
eg.Go(func(ctx context.Context) error {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT, syscall.SIGQUIT)
select {
case <-ctx.Done():
fmt.Fprintf(console.Debug(ctx), "Ctx expired.\n")
case <-sigc:
fmt.Fprintf(console.Debug(ctx), "Received signal to exit.\n")
eg.Cancel()
}
return nil
})
}
}
if err := files.WriteJson(filepath.Join(state, metadataFile), md, 0644); err != nil {
return err
}
if *createAtStartup {
eg := executor.New(ctx, "startup")
for _, p := range instances {
p := p // Close p
eg.Go(func(ctx context.Context) error {
_, _, err := p.NewConn(ctx)
return err
})
}
if err := eg.Wait(); err != nil {
return err
}
}
if err := wireBuildx(dockerCli, *name, *use, md); err != nil {
return multierr.New(err, eg.CancelAndWait())
}
// Print info message even if proxy goes in background
console.SetStickyContent(ctx, "build", banner(ctx, *name, *use, available, *background))
if *background {
return nil
}
if err := eg.Wait(); err != nil {
return err
}
fmt.Fprintf(console.Debug(ctx), "Cleaning up docker buildx context.\n")
if err := withStore(dockerCli, func(txn *store.Txn) error {
return txn.Remove(*name)
}); err != nil {
return err
}
fmt.Fprintf(console.Debug(ctx), "Deleting state file.\n")
if err := os.RemoveAll(state); err != nil {
return err
}
return nil
})
return cmd
}
func proxyAlreadyExists(stateDir string) bool {
_, err := os.Stat(filepath.Join(stateDir, metadataFile))
return !os.IsNotExist(err)
}
func existingProxyMessage(customStateDir string) string {
if customStateDir != "" {
return fmt.Sprintf(`Previous remote builder configuration found in %s.
If you want to create a new remote builder context configuration, cleanup the older one first with:
nsc docker buildx cleanup --state %s
`, customStateDir, customStateDir)
} else {
return `Previous remote builder configuration found.
If you want to create a new remote builder context configuration, cleanup the older one first with:
nsc docker buildx cleanup
`
}
}
func blockWaitForLogin(ctx context.Context) error {
// Check immediately if token is valid by calling Namespace
_, err := api.GetProfile(ctx, api.Methods)
if err == nil {
return err
}
// Else, block
for {
select {
case <-time.After(time.Second * 5):
_, err := api.GetProfile(ctx, api.Methods)
if err == nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func ensureStateDir(specified, dir string) (string, error) {
if specified == "" {
// Change state dir from cache, which can be removed at any time,
// to the app's config folder.
// Older state dir might still be under the cache file, so we need to first check that path,
// if it does not exist, we can create the new one, under config path.
oldStateDirPath, err := dirs.Subdir(dir)
if err != nil {
return "", err
}
if proxyAlreadyExists(oldStateDirPath) {
return oldStateDirPath, nil
}
return dirs.Ensure(dirs.ConfigSubdir(dir))
}
s, err := filepath.Abs(specified)
if err != nil {
return "", err
}
return dirs.Ensure(filepath.Join(s, proxyDir), nil)
}
func ensureLogDir(dir string) (string, error) {
return dirs.Ensure(dirs.Logs(dir))
}
type buildxMetadata struct {
NodeGroupName string `json:"node_group_name"`
Instances []buildxInstanceMetadata `json:"instances"`
}
type buildxInstanceMetadata struct {
Platform api.BuildPlatform `json:"build_platform"`
SocketPath string `json:"socket_path"`
Pid int `json:"pid"`
DebugLogPath string `json:"debug_log_path"`
ControlSocketPath string `json:"control_socket_path"`
}
func wireBuildx(dockerCli *command.DockerCli, name string, use bool, md buildxMetadata) error {
return withStore(dockerCli, func(txn *store.Txn) error {
ng, err := txn.NodeGroupByName(name)
if err != nil {
if !os.IsNotExist(errors.Cause(err)) {
return err
}
}
const driver = "remote"
if ng == nil {
ng = &store.NodeGroup{
Name: name,
Driver: driver,
}
}
for _, p := range md.Instances {
var platforms []string
if p.Platform == "arm64" {
platforms = []string{"linux/arm64"}
}
if err := ng.Update(string(p.Platform), "unix://"+p.SocketPath, platforms, true, true, nil, "", nil); err != nil {
return err
}
}
if use {
ep, err := dockerutil.GetCurrentEndpoint(dockerCli)
if err != nil {
return err
}
if err := txn.SetCurrent(ep, name, false, false); err != nil {
return err
}
}
if err := txn.Save(ng); err != nil {
return err
}
return nil
})
}
func newCleanupBuildxCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cleanup",
Short: "Unregisters Namespace Remote builders from buildx.",
}
stateDir := cmd.Flags().String("state", "", "If set, looks for the remote builder context in this directory.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
dockerCli, err := command.NewDockerCli()
if err != nil {
return err
}
if err := dockerCli.Initialize(cliflags.NewClientOptions()); err != nil {
return err
}
return withStore(dockerCli, func(txn *store.Txn) error {
state, err := ensureStateDir(*stateDir, buildkitProxyPath)
if err != nil {
return err
}
if !proxyAlreadyExists(state) {
console.SetStickyContent(ctx, "build", "State file not found. Nothing to cleanup.")
return nil
}
return doBuildxCleanup(ctx, state, txn)
})
})
return cmd
}
func doBuildxCleanup(ctx context.Context, state string, txn *store.Txn) error {
var md buildxMetadata
if err := files.ReadJson(filepath.Join(state, metadataFile), &md); err != nil {
return err
}
for _, inst := range md.Instances {
if inst.Pid > 0 {
process, err := os.FindProcess(inst.Pid)
if err != nil {
return err
}
if err := process.Signal(os.Interrupt); err != nil && !errors.Is(err, os.ErrProcessDone) {
return err
}
fmt.Fprintf(console.Debug(ctx), "Sent SIGINT to worker handling %s (pid %d).\n", inst.Platform, inst.Pid)
}
}
if err := os.RemoveAll(state); err != nil {
console.SetStickyContent(ctx, "build",
fmt.Sprintf("Warning: deleting state files in %s failed: %v", state, err))
}
fmt.Fprintf(console.Debug(ctx), "Removed local state directory %q.\n", state)
if md.NodeGroupName != "" {
if err := txn.Remove(md.NodeGroupName); err != nil {
return err
}
fmt.Fprintf(console.Stderr(ctx), "Removed buildx node group %q.\n", md.NodeGroupName)
}
fmt.Fprintf(console.Stderr(ctx), "Cleanup complete.\n")
return nil
}
func newWireBuildxCommand(hidden bool) *cobra.Command {
cmd := &cobra.Command{
Use: "wire",
Short: "Wires a previously setup proxy setup.",
Hidden: hidden,
}
name := cmd.Flags().String("name", defaultBuilder, "The name of the builder we setup.")
use := cmd.Flags().Bool("use", false, "If true, changes the current builder to nsc-remote.")
stateDir := cmd.Flags().String("state", "", "Where the proxies live.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
if *stateDir == "" {
return fnerrors.New("--state is required")
}
dockerCli, err := command.NewDockerCli()
if err != nil {
return err
}
if err := dockerCli.Initialize(cliflags.NewClientOptions()); err != nil {
return err
}
var md buildxMetadata
if err := files.ReadJson(filepath.Join(*stateDir, metadataFile), &md); err != nil {
return err
}
return wireBuildx(dockerCli, *name, *use, md)
})
return cmd
}
func determineAvailable(ctx context.Context) ([]api.BuildPlatform, error) {
profile, err := api.GetProfile(ctx, api.Methods)
if err != nil {
return nil, err
}
avail := make([]api.BuildPlatform, len(profile.ClusterPlatform))
for k, x := range profile.ClusterPlatform {
avail[k] = api.BuildPlatform(x)
}
return avail, nil
}
func banner(ctx context.Context, name string, use bool, native []api.BuildPlatform, background bool) string {
w := wordwrap.NewWriter(80)
style := colors.Ctx(ctx)
fmt.Fprint(w, style.Highlight.Apply("docker buildx"), " has been configured to use ",
style.Highlight.Apply("Namespace Remote builders"), ".\n")
fmt.Fprintln(w)
fmt.Fprint(w, "Native building: ", strings.Join(bold(style, native), " and "), ".\n")
if !use {
fmt.Fprint(w, "\nThe default buildx builder was not changed, you can re-run with ", style.Highlight.Apply("--use"), " or run:\n")
fmt.Fprintf(w, "\n docker buildx use %s\n", name)
}
if !background {
fmt.Fprintf(w, "\nStart a new terminal, and start building:\n")
fmt.Fprintf(w, "\n docker buildx build ...\n")
fmt.Fprintln(w)
fmt.Fprintln(w, style.Comment.Apply("Exiting will remove the configuration."))
} else {
fmt.Fprintf(w, "\nStart building:\n")
fmt.Fprintf(w, "\n docker buildx build ...\n")
fmt.Fprintf(w, "\nYour remote builder context is running in the background. You can always clean it up with:\n")
fmt.Fprintf(w, "\n nsc docker buildx cleanup \n")
}
_ = w.Close()
return strings.TrimSpace(w.String())
}
func bold[X any](style colors.Style, values []X) []string {
result := make([]string, len(values))
for k, x := range values {
result[k] = style.Highlight.Apply(fmt.Sprintf("%v", x))
}
return result
}
func withStore(dockerCli *command.DockerCli, f func(*store.Txn) error) error {
txn, release, err := storeutil.GetStore(dockerCli)
if err != nil {
return err
}
defer release()
return f(txn)
}
func buildxContextNotConfigured() string {
return `Docker buildx context is not configured for Namespace remote builders.
Try running:
nsc docker buildx setup --use --background
`
}
func buildxContextNotRunning() string {
return `It seems that Namespace buildx context is not running.
Try running the following to restart it:
nsc docker buildx cleanup && nsc docker buildx setup --use --background
`
}
func makeUnixHTTPClient(unixSockPath string) *http.Client {
unixDial := func(proto, addr string) (conn net.Conn, err error) {
return net.Dial("unix", unixSockPath)
}
tr := &http.Transport{
Dial: unixDial,
}
return &http.Client{Transport: tr}
}
func newStatusBuildxCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Status information for the local Namespace buildx context.",
}
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
stateDir := cmd.Flags().String("state", "", "If set, looks for the remote builder context in this directory.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
state, err := ensureStateDir(*stateDir, buildkitProxyPath)
if err != nil {
return err
}
var md buildxMetadata
if err := files.ReadJson(filepath.Join(state, metadataFile), &md); err != nil {
if errors.Is(err, os.ErrNotExist) {
console.SetStickyContent(ctx, "build", buildxContextNotConfigured())
return nil
}
return err
}
descs := []StatusData{}
for _, proxy := range md.Instances {
client := makeUnixHTTPClient(proxy.ControlSocketPath)
resp, err := client.Get("http://localhost/status")
if err != nil {
console.SetStickyContent(ctx, "build", buildxContextNotRunning())
return err
}
defer resp.Body.Close()
var desc StatusData
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&desc); err != nil {
return err
}
descs = append(descs, desc)
}
stdout := console.Stdout(ctx)
switch *output {
case "json":
enc := json.NewEncoder(console.Stdout(ctx))
enc.SetIndent("", " ")
if err := enc.Encode(descs); err != nil {
return fnerrors.InternalError("failed to encode status as JSON output: %w", err)
}
default:
if *output != "plain" {
fmt.Fprintf(console.Warnings(ctx), "defaulting output to plain\n")
}
fmt.Fprintf(stdout, "\nBuildx context status:\n\n")
for _, desc := range descs {
fmt.Fprintf(stdout, "Platform: %s\n", desc.Platform)
fmt.Fprintf(stdout, " Status: %s\n", desc.Status)
fmt.Fprintf(stdout, " Last Instance ID: %s\n", desc.LastInstanceID)
fmt.Fprintf(stdout, " Last Update: %v\n", desc.LastUpdate)
fmt.Fprintf(stdout, " Last Error: %v\n", desc.LastError)
fmt.Fprintf(stdout, " Requests Handled: %v\n", desc.Requests)
fmt.Fprintf(stdout, " Log Path: %v\n", desc.LogPath)
fmt.Fprintf(stdout, "\n")
}
}
return nil
})
return cmd
}