mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	controller: use unique files per buildx version
This ensures that we should never accidentally connect to a server with a mismatched version, while also allowing us to run multiple buildx servers at a time. Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
		@@ -35,6 +35,12 @@ const (
 | 
				
			|||||||
	serveCommandName = "_INTERNAL_SERVE"
 | 
						serveCommandName = "_INTERNAL_SERVE"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						defaultLogFilename    = fmt.Sprintf("buildx.%s.log", version.Revision)
 | 
				
			||||||
 | 
						defaultSocketFilename = fmt.Sprintf("buildx.%s.sock", version.Revision)
 | 
				
			||||||
 | 
						defaultPIDFilename    = fmt.Sprintf("buildx.%s.pid", version.Revision)
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type serverConfig struct {
 | 
					type serverConfig struct {
 | 
				
			||||||
	// Specify buildx server root
 | 
						// Specify buildx server root
 | 
				
			||||||
	Root string `toml:"root"`
 | 
						Root string `toml:"root"`
 | 
				
			||||||
@@ -55,7 +61,7 @@ func NewRemoteBuildxController(ctx context.Context, dockerCli command.Cli, opts
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// connect to buildx server if it is already running
 | 
						// connect to buildx server if it is already running
 | 
				
			||||||
	ctx2, cancel := context.WithTimeout(ctx, 1*time.Second)
 | 
						ctx2, cancel := context.WithTimeout(ctx, 1*time.Second)
 | 
				
			||||||
	c, err := newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, "buildx.sock"))
 | 
						c, err := newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
 | 
				
			||||||
	cancel()
 | 
						cancel()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		if !errors.Is(err, context.DeadlineExceeded) {
 | 
							if !errors.Is(err, context.DeadlineExceeded) {
 | 
				
			||||||
@@ -66,28 +72,28 @@ 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...")
 | 
						logrus.Info("no buildx server found; launching...")
 | 
				
			||||||
		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 nil, 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 nil, 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, "buildx.sock"))
 | 
						c, err = newBuildxClientAndCheck(ctx2, filepath.Join(serverRoot, defaultSocketFilename))
 | 
				
			||||||
	cancel()
 | 
						cancel()
 | 
				
			||||||
		if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
			return nil, errors.Wrap(err, "cannot connect to the buildx server")
 | 
							return nil, errors.Wrap(err, "cannot connect to the buildx server")
 | 
				
			||||||
		}
 | 
						}
 | 
				
			||||||
	return &buildxController{c, serverRoot}, nil
 | 
						return &buildxController{c, serverRoot}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -124,7 +130,7 @@ func serveCmd(dockerCli command.Cli) *cobra.Command {
 | 
				
			|||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			pidF := filepath.Join(root, "pid")
 | 
								pidF := filepath.Join(root, defaultPIDFilename)
 | 
				
			||||||
			if err := os.WriteFile(pidF, []byte(fmt.Sprintf("%d", os.Getpid())), 0600); err != nil {
 | 
								if err := os.WriteFile(pidF, []byte(fmt.Sprintf("%d", os.Getpid())), 0600); err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -141,7 +147,7 @@ func serveCmd(dockerCli command.Cli) *cobra.Command {
 | 
				
			|||||||
			defer b.Close()
 | 
								defer b.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// serve server
 | 
								// serve server
 | 
				
			||||||
			addr := filepath.Join(root, "buildx.sock")
 | 
								addr := filepath.Join(root, defaultSocketFilename)
 | 
				
			||||||
			if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { // avoid EADDRINUSE
 | 
								if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { // avoid EADDRINUSE
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -199,7 +205,7 @@ func getLogFilePath(dockerCli command.Cli, configPath string) (string, error) {
 | 
				
			|||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return "", err
 | 
								return "", err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return filepath.Join(root, "log"), nil
 | 
							return filepath.Join(root, defaultLogFilename), nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return config.LogFile, nil
 | 
						return config.LogFile, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -267,7 +273,7 @@ type buildxController struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *buildxController) Kill(ctx context.Context) error {
 | 
					func (c *buildxController) Kill(ctx context.Context) error {
 | 
				
			||||||
	pidB, err := os.ReadFile(filepath.Join(c.serverRoot, "pid"))
 | 
						pidB, err := os.ReadFile(filepath.Join(c.serverRoot, defaultPIDFilename))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user