mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	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>
This commit is contained in:
		@@ -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")
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,8 +25,13 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command {
 | 
				
			|||||||
		Use:   "debug-shell",
 | 
							Use:   "debug-shell",
 | 
				
			||||||
		Short: "Start a monitor",
 | 
							Short: "Start a monitor",
 | 
				
			||||||
		RunE: func(cmd *cobra.Command, args []string) error {
 | 
							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()
 | 
								ctx := context.TODO()
 | 
				
			||||||
			c, err := controller.NewController(ctx, options, dockerCli)
 | 
								c, err := controller.NewController(ctx, options, dockerCli, printer)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -40,11 +45,6 @@ func debugShellCmd(dockerCli command.Cli) *cobra.Command {
 | 
				
			|||||||
				return errors.Errorf("failed to configure terminal: %v", err)
 | 
									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{
 | 
								err = monitor.RunMonitor(ctx, "", nil, controllerapi.InvokeConfig{
 | 
				
			||||||
				Tty: true,
 | 
									Tty: true,
 | 
				
			||||||
			}, c, os.Stdin, os.Stdout, os.Stderr, printer)
 | 
								}, c, os.Stdin, os.Stdout, os.Stderr, printer)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,26 +2,35 @@ package controller
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/buildx/controller/control"
 | 
						"github.com/docker/buildx/controller/control"
 | 
				
			||||||
	"github.com/docker/buildx/controller/local"
 | 
						"github.com/docker/buildx/controller/local"
 | 
				
			||||||
	"github.com/docker/buildx/controller/remote"
 | 
						"github.com/docker/buildx/controller/remote"
 | 
				
			||||||
 | 
						"github.com/docker/buildx/util/progress"
 | 
				
			||||||
	"github.com/docker/cli/cli/command"
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
	"github.com/pkg/errors"
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
	"github.com/sirupsen/logrus"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli) (c control.BuildxController, err error) {
 | 
					func NewController(ctx context.Context, opts control.ControlOptions, dockerCli command.Cli, pw progress.Writer) (control.BuildxController, error) {
 | 
				
			||||||
	if !opts.Detach {
 | 
						var name string
 | 
				
			||||||
		logrus.Infof("launching local buildx controller")
 | 
						if opts.Detach {
 | 
				
			||||||
		c = local.NewLocalBuildxController(ctx, dockerCli)
 | 
							name = "remote"
 | 
				
			||||||
		return c, nil
 | 
						} else {
 | 
				
			||||||
 | 
							name = "local"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	logrus.Infof("connecting to buildx server")
 | 
						var c control.BuildxController
 | 
				
			||||||
	c, err = remote.NewRemoteBuildxController(ctx, dockerCli, opts)
 | 
						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 {
 | 
						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
 | 
						return c, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,7 +18,7 @@ import (
 | 
				
			|||||||
	"github.com/pkg/errors"
 | 
						"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{
 | 
						return &localController{
 | 
				
			||||||
		dockerCli: dockerCli,
 | 
							dockerCli: dockerCli,
 | 
				
			||||||
		ref:       "local",
 | 
							ref:       "local",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -54,7 +54,7 @@ type serverConfig struct {
 | 
				
			|||||||
	LogFile string `toml:"log_file"`
 | 
						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
 | 
						rootDir := opts.Root
 | 
				
			||||||
	if rootDir == "" {
 | 
						if rootDir == "" {
 | 
				
			||||||
		rootDir = rootDataDir(dockerCli)
 | 
							rootDir = rootDataDir(dockerCli)
 | 
				
			||||||
@@ -74,27 +74,32 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// start buildx server via subcommand
 | 
						// start buildx server via subcommand
 | 
				
			||||||
	logrus.Info("no buildx server found; launching...")
 | 
						err = logger.Wrap("no buildx server found; launching...", func() error {
 | 
				
			||||||
	launchFlags := []string{}
 | 
							launchFlags := []string{}
 | 
				
			||||||
	if opts.ServerConfig != "" {
 | 
							if opts.ServerConfig != "" {
 | 
				
			||||||
		launchFlags = append(launchFlags, "--config", opts.ServerConfig)
 | 
								launchFlags = append(launchFlags, "--config", opts.ServerConfig)
 | 
				
			||||||
	}
 | 
							}
 | 
				
			||||||
	logFile, err := getLogFilePath(dockerCli, opts.ServerConfig)
 | 
							logFile, err := getLogFilePath(dockerCli, opts.ServerConfig)
 | 
				
			||||||
	if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
		return nil, err
 | 
								return err
 | 
				
			||||||
	}
 | 
							}
 | 
				
			||||||
	wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...)
 | 
							wait, err := launch(ctx, logFile, append([]string{serveCommandName}, launchFlags...)...)
 | 
				
			||||||
	if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
		return nil, err
 | 
								return err
 | 
				
			||||||
	}
 | 
							}
 | 
				
			||||||
	go wait()
 | 
							go wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// wait for buildx server to be ready
 | 
							// wait for buildx server to be ready
 | 
				
			||||||
	ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
 | 
							ctx2, cancel = context.WithTimeout(ctx, 10*time.Second)
 | 
				
			||||||
	c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
 | 
							c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
 | 
				
			||||||
	cancel()
 | 
							cancel()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return errors.Wrap(err, "cannot connect to the buildx server")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, errors.Wrap(err, "cannot connect to the buildx server")
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &buildxController{c, serverRoot}, nil
 | 
						return &buildxController{c, serverRoot}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,12 +6,13 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/docker/buildx/controller/control"
 | 
						"github.com/docker/buildx/controller/control"
 | 
				
			||||||
 | 
						"github.com/docker/buildx/util/progress"
 | 
				
			||||||
	"github.com/docker/cli/cli/command"
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
	"github.com/pkg/errors"
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
						"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")
 | 
						return nil, errors.New("remote buildx unsupported")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user