diff --git a/commands/build.go b/commands/build.go index 5943522a..9f84f091 100644 --- a/commands/build.go +++ b/commands/build.go @@ -279,7 +279,7 @@ func runControllerBuild(ctx context.Context, dockerCli command.Cli, options buil return nil, errors.Errorf("Dockerfile or context from stdin is not supported with invoke") } - c, err := controller.NewController(ctx, options.ControlOptions, dockerCli) + c, err := controller.NewController(ctx, options.ControlOptions, dockerCli, printer) if err != nil { return nil, err } diff --git a/commands/debug-shell.go b/commands/debug-shell.go index 1a9cc6ac..0b091572 100644 --- a/commands/debug-shell.go +++ b/commands/debug-shell.go @@ -25,8 +25,13 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command { Use: "debug-shell", Short: "Start a monitor", RunE: func(cmd *cobra.Command, args []string) error { + printer, err := progress.NewPrinter(context.TODO(), os.Stderr, os.Stderr, progressMode) + if err != nil { + return err + } + ctx := context.TODO() - c, err := controller.NewController(ctx, options, dockerCli) + c, err := controller.NewController(ctx, options, dockerCli, printer) if err != nil { return err } @@ -40,11 +45,6 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command { return errors.Errorf("failed to configure terminal: %v", err) } - printer, err := progress.NewPrinter(context.TODO(), os.Stderr, os.Stderr, progressMode) - if err != nil { - return err - } - err = monitor.RunMonitor(ctx, "", nil, controllerapi.InvokeConfig{ Tty: true, }, c, os.Stdin, os.Stdout, os.Stderr, printer) diff --git a/controller/controller.go b/controller/controller.go index cc054338..635a7a23 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -2,26 +2,35 @@ package controller import ( "context" + "fmt" "github.com/docker/buildx/controller/control" "github.com/docker/buildx/controller/local" "github.com/docker/buildx/controller/remote" + "github.com/docker/buildx/util/progress" "github.com/docker/cli/cli/command" "github.com/pkg/errors" - "github.com/sirupsen/logrus" ) -func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli) (c control.BuildxController, err error) { - if !opts.Detach { - logrus.Infof("launching local buildx controller") - c = local.NewLocalBuildxController(ctx, dockerCli) - return c, nil +func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli, pw progress.Writer) (control.BuildxController, error) { + var name string + if opts.Detach { + name = "remote" + } else { + name = "local" } - logrus.Infof("connecting to buildx server") - c, err = remote.NewRemoteBuildxController(ctx, dockerCli, opts) + var c control.BuildxController + err := progress.Wrap(fmt.Sprintf("[internal] connecting to %s controller", name), pw.Write, func(l progress.SubLogger) (err error) { + if opts.Detach { + c, err = remote.NewRemoteBuildxController(ctx, dockerCli, opts, l) + } else { + c = local.NewLocalBuildxController(ctx, dockerCli, l) + } + return err + }) if err != nil { - return nil, errors.Wrap(err, "failed to use buildx server; use --detach=false") + return nil, errors.Wrap(err, "failed to start buildx controller") } return c, nil } diff --git a/controller/local/controller.go b/controller/local/controller.go index 2bce2b28..9d69412e 100644 --- a/controller/local/controller.go +++ b/controller/local/controller.go @@ -18,7 +18,7 @@ import ( "github.com/pkg/errors" ) -func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli) control.BuildxController { +func NewLocalBuildxController(ctx context.Context, dockerCli command.Cli, logger progress.SubLogger) control.BuildxController { return &localController{ dockerCli: dockerCli, ref: "local", diff --git a/controller/remote/controller.go b/controller/remote/controller.go index 60aaa852..7d7c8496 100644 --- a/controller/remote/controller.go +++ b/controller/remote/controller.go @@ -54,7 +54,7 @@ type serverConfig struct { LogFile string `toml:"log_file"` } -func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions) (control.BuildxController, error) { +func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions, logger progress.SubLogger) (control.BuildxController, error) { rootDir := opts.Root if rootDir == "" { rootDir = rootDataDir(dockerCli) @@ -74,27 +74,32 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts } // start buildx server via subcommand - logrus.Info("no buildx server found; launching...") - launchFlags := []string{} - if opts.ServerConfig != "" { - launchFlags = append(launchFlags, "--config", opts.ServerConfig) - } - logFile, err := getLogFilePath(dockerCli, opts.ServerConfig) - if err != nil { - return nil, err - } - wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...) - if err != nil { - return nil, err - } - go wait() + err = logger.Wrap("no buildx server found; launching...", func() error { + launchFlags := []string{} + if opts.ServerConfig != "" { + launchFlags = append(launchFlags, "--config", opts.ServerConfig) + } + logFile, err := getLogFilePath(dockerCli, opts.ServerConfig) + if err != nil { + return err + } + wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...) + if err != nil { + return err + } + go wait() - // wait for buildx server to be ready - ctx2, cancel = context.WithTimeout(ctx, 10*time.Second) - c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename)) - cancel() + // wait for buildx server to be ready + ctx2, cancel = context.WithTimeout(ctx, 10*time.Second) + c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename)) + cancel() + if err != nil { + return errors.Wrap(err, "cannot connect to the buildx server") + } + return nil + }) if err != nil { - return nil, errors.Wrap(err, "cannot connect to the buildx server") + return nil, err } return &buildxController{c, serverRoot}, nil } diff --git a/controller/remote/controller_nolinux.go b/controller/remote/controller_nolinux.go index fe5200f7..07c1c3b2 100644 --- a/controller/remote/controller_nolinux.go +++ b/controller/remote/controller_nolinux.go @@ -6,12 +6,13 @@ import ( "context" "github.com/docker/buildx/controller/control" + "github.com/docker/buildx/util/progress" "github.com/docker/cli/cli/command" "github.com/pkg/errors" "github.com/spf13/cobra" ) -func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions) (control.BuildxController, error) { +func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts control.ControlOptions, logger progress.SubLogger) (control.BuildxController, error) { return nil, errors.New("remote buildx unsupported") }