Skip to content

Commit 2b8410e

Browse files
committed
controller: replace logrus status messages with progress messages
logrus info messages aren't particularly in-theme with the rest of the progress output (and are also frustratingly racy). The progress output is a lot neater, so we refactor it into that. Signed-off-by: Justin Chadwell <me@jedevc.com>
1 parent e826141 commit 2b8410e

File tree

6 files changed

+52
-38
lines changed

6 files changed

+52
-38
lines changed

commands/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func runControllerBuild(ctx context.Context, dockerCli command.Cli, options buil
279279
return nil, errors.Errorf("Dockerfile or context from stdin is not supported with invoke")
280280
}
281281

282-
c, err := controller.NewController(ctx, options.ControlOptions, dockerCli)
282+
c, err := controller.NewController(ctx, options.ControlOptions, dockerCli, printer)
283283
if err != nil {
284284
return nil, err
285285
}

commands/debug-shell.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command {
2525
Use: "debug-shell",
2626
Short: "Start a monitor",
2727
RunE: func(cmd *cobra.Command, args []string) error {
28+
printer, err := progress.NewPrinter(context.TODO(), os.Stderr, os.Stderr, progressMode)
29+
if err != nil {
30+
return err
31+
}
32+
2833
ctx := context.TODO()
29-
c, err := controller.NewController(ctx, options, dockerCli)
34+
c, err := controller.NewController(ctx, options, dockerCli, printer)
3035
if err != nil {
3136
return err
3237
}
@@ -40,11 +45,6 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command {
4045
return errors.Errorf("failed to configure terminal: %v", err)
4146
}
4247

43-
printer, err := progress.NewPrinter(context.TODO(), os.Stderr, os.Stderr, progressMode)
44-
if err != nil {
45-
return err
46-
}
47-
4848
err = monitor.RunMonitor(ctx, "", nil, controllerapi.InvokeConfig{
4949
Tty: true,
5050
}, c, os.Stdin, os.Stdout, os.Stderr, printer)

controller/controller.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@ package controller
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/docker/buildx/controller/control"
78
"github.com/docker/buildx/controller/local"
89
"github.com/docker/buildx/controller/remote"
10+
"github.com/docker/buildx/util/progress"
911
"github.com/docker/cli/cli/command"
1012
"github.com/pkg/errors"
11-
"github.com/sirupsen/logrus"
1213
)
1314

14-
func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli) (c control.BuildxController, err error) {
15-
if !opts.Detach {
16-
logrus.Infof("launching local buildx controller")
17-
c = local.NewLocalBuildxController(ctx, dockerCli)
18-
return c, nil
15+
func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli, pw progress.Writer) (control.BuildxController, error) {
16+
var name string
17+
if opts.Detach {
18+
name = "remote"
19+
} else {
20+
name = "local"
1921
}
2022

21-
logrus.Infof("connecting to buildx server")
22-
c, err = remote.NewRemoteBuildxController(ctx, dockerCli, opts)
23+
var c control.BuildxController
24+
err := progress.Wrap(fmt.Sprintf("[internal] connecting to %s controller", name), pw.Write, func(l progress.SubLogger) (err error) {
25+
if opts.Detach {
26+
c, err = remote.NewRemoteBuildxController(ctx, dockerCli, opts, l)
27+
} else {
28+
c = local.NewLocalBuildxController(ctx, dockerCli, l)
29+
}
30+
return err
31+
})
2332
if err != nil {
24-
return nil, errors.Wrap(err, "failed to use buildx server; use --detach=false")
33+
return nil, errors.Wrap(err, "failed to start buildx controller")
2534
}
2635
return c, nil
2736
}

controller/local/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/pkg/errors"
1919
)
2020

21-
func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli) control.BuildxController {
21+
func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli, logger progress.SubLogger) control.BuildxController {
2222
return &localController{
2323
dockerCli: dockerCli,
2424
ref: "local",

controller/remote/controller.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type serverConfig struct {
5454
LogFile string `toml:"log_file"`
5555
}
5656

57-
func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions) (control.BuildxController, error) {
57+
func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions, logger progress.SubLogger) (control.BuildxController, error) {
5858
rootDir := opts.Root
5959
if rootDir == "" {
6060
rootDir = rootDataDir(dockerCli)
@@ -74,27 +74,32 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts
7474
}
7575

7676
// start buildx server via subcommand
77-
logrus.Info("no buildx server found; launching...")
78-
launchFlags := []string{}
79-
if opts.ServerConfig != "" {
80-
launchFlags = append(launchFlags, "--config", opts.ServerConfig)
81-
}
82-
logFile, err := getLogFilePath(dockerCli, opts.ServerConfig)
83-
if err != nil {
84-
return nil, err
85-
}
86-
wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...)
87-
if err != nil {
88-
return nil, err
89-
}
90-
go wait()
77+
err = logger.Wrap("no buildx server found; launching...", func() error {
78+
launchFlags := []string{}
79+
if opts.ServerConfig != "" {
80+
launchFlags = append(launchFlags, "--config", opts.ServerConfig)
81+
}
82+
logFile, err := getLogFilePath(dockerCli, opts.ServerConfig)
83+
if err != nil {
84+
return err
85+
}
86+
wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...)
87+
if err != nil {
88+
return err
89+
}
90+
go wait()
9191

92-
// wait for buildx server to be ready
93-
ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
94-
c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
95-
cancel()
92+
// wait for buildx server to be ready
93+
ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
94+
c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
95+
cancel()
96+
if err != nil {
97+
return errors.Wrap(err, "cannot connect to the buildx server")
98+
}
99+
return nil
100+
})
96101
if err != nil {
97-
return nil, errors.Wrap(err, "cannot connect to the buildx server")
102+
return nil, err
98103
}
99104
return &buildxController{c, serverRoot}, nil
100105
}

controller/remote/controller_nolinux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions) (control.BuildxController, error) {
14+
func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions, logger progress.SubLogger) (control.BuildxController, error) {
1515
return nil, errors.New("remote buildx unsupported")
1616
}
1717

0 commit comments

Comments
 (0)