mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 10:03:42 +08:00 
			
		
		
		
	rm: support removing multiple builders at once
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
		@@ -9,7 +9,6 @@ import (
 | 
				
			|||||||
	"github.com/docker/buildx/store"
 | 
						"github.com/docker/buildx/store"
 | 
				
			||||||
	"github.com/docker/buildx/store/storeutil"
 | 
						"github.com/docker/buildx/store/storeutil"
 | 
				
			||||||
	"github.com/docker/buildx/util/cobrautil/completion"
 | 
						"github.com/docker/buildx/util/cobrautil/completion"
 | 
				
			||||||
	"github.com/docker/cli/cli"
 | 
					 | 
				
			||||||
	"github.com/docker/cli/cli/command"
 | 
						"github.com/docker/cli/cli/command"
 | 
				
			||||||
	"github.com/moby/buildkit/util/appcontext"
 | 
						"github.com/moby/buildkit/util/appcontext"
 | 
				
			||||||
	"github.com/pkg/errors"
 | 
						"github.com/pkg/errors"
 | 
				
			||||||
@@ -18,7 +17,7 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type rmOptions struct {
 | 
					type rmOptions struct {
 | 
				
			||||||
	builder     string
 | 
						builders    []string
 | 
				
			||||||
	keepState   bool
 | 
						keepState   bool
 | 
				
			||||||
	keepDaemon  bool
 | 
						keepDaemon  bool
 | 
				
			||||||
	allInactive bool
 | 
						allInactive bool
 | 
				
			||||||
@@ -46,33 +45,52 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
 | 
				
			|||||||
		return rmAllInactive(ctx, txn, dockerCli, in)
 | 
							return rmAllInactive(ctx, txn, dockerCli, in)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	b, err := builder.New(dockerCli,
 | 
						eg, _ := errgroup.WithContext(ctx)
 | 
				
			||||||
		builder.WithName(in.builder),
 | 
						for _, name := range in.builders {
 | 
				
			||||||
		builder.WithStore(txn),
 | 
							func(name string) {
 | 
				
			||||||
		builder.WithSkippedValidation(),
 | 
								eg.Go(func() (err error) {
 | 
				
			||||||
	)
 | 
									defer func() {
 | 
				
			||||||
	if err != nil {
 | 
										if err == nil {
 | 
				
			||||||
		return err
 | 
											_, _ = fmt.Fprintf(dockerCli.Err(), "%s removed\n", name)
 | 
				
			||||||
 | 
										} else {
 | 
				
			||||||
 | 
											_, _ = fmt.Fprintf(dockerCli.Err(), "failed to remove %s: %v\n", name, err)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									b, err := builder.New(dockerCli,
 | 
				
			||||||
 | 
										builder.WithName(name),
 | 
				
			||||||
 | 
										builder.WithStore(txn),
 | 
				
			||||||
 | 
										builder.WithSkippedValidation(),
 | 
				
			||||||
 | 
									)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									nodes, err := b.LoadNodes(ctx)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if cb := b.ContextName(); cb != "" {
 | 
				
			||||||
 | 
										return errors.Errorf("context builder cannot be removed, run `docker context rm %s` to remove this context", cb)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									err1 := rm(ctx, nodes, in)
 | 
				
			||||||
 | 
									if err := txn.Remove(b.Name); err != nil {
 | 
				
			||||||
 | 
										return err
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									if err1 != nil {
 | 
				
			||||||
 | 
										return err1
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							}(name)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	nodes, err := b.LoadNodes(ctx)
 | 
						if err := eg.Wait(); err != nil {
 | 
				
			||||||
	if err != nil {
 | 
							return errors.New("failed to remove one or more builders")
 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if cb := b.ContextName(); cb != "" {
 | 
					 | 
				
			||||||
		return errors.Errorf("context builder cannot be removed, run `docker context rm %s` to remove this context", cb)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	err1 := rm(ctx, nodes, in)
 | 
					 | 
				
			||||||
	if err := txn.Remove(b.Name); err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if err1 != nil {
 | 
					 | 
				
			||||||
		return err1
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	_, _ = fmt.Fprintf(dockerCli.Err(), "%s removed\n", b.Name)
 | 
					 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -80,16 +98,15 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
 | 
				
			|||||||
	var options rmOptions
 | 
						var options rmOptions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cmd := &cobra.Command{
 | 
						cmd := &cobra.Command{
 | 
				
			||||||
		Use:   "rm [NAME]",
 | 
							Use:   "rm [OPTIONS] [NAME] [NAME...]",
 | 
				
			||||||
		Short: "Remove a builder instance",
 | 
							Short: "Remove one or more builder instances",
 | 
				
			||||||
		Args:  cli.RequiresMaxArgs(1),
 | 
					 | 
				
			||||||
		RunE: func(cmd *cobra.Command, args []string) error {
 | 
							RunE: func(cmd *cobra.Command, args []string) error {
 | 
				
			||||||
			options.builder = rootOpts.builder
 | 
								options.builders = []string{rootOpts.builder}
 | 
				
			||||||
			if len(args) > 0 {
 | 
								if len(args) > 0 {
 | 
				
			||||||
				if options.allInactive {
 | 
									if options.allInactive {
 | 
				
			||||||
					return errors.New("cannot specify builder name when --all-inactive is set")
 | 
										return errors.New("cannot specify builder name when --all-inactive is set")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				options.builder = args[0]
 | 
									options.builders = args
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return runRm(dockerCli, options)
 | 
								return runRm(dockerCli, options)
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,7 @@ Extended build capabilities with BuildKit
 | 
				
			|||||||
| [`inspect`](buildx_inspect.md)       | Inspect current builder instance       |
 | 
					| [`inspect`](buildx_inspect.md)       | Inspect current builder instance       |
 | 
				
			||||||
| [`ls`](buildx_ls.md)                 | List builder instances                 |
 | 
					| [`ls`](buildx_ls.md)                 | List builder instances                 |
 | 
				
			||||||
| [`prune`](buildx_prune.md)           | Remove build cache                     |
 | 
					| [`prune`](buildx_prune.md)           | Remove build cache                     |
 | 
				
			||||||
| [`rm`](buildx_rm.md)                 | Remove a builder instance              |
 | 
					| [`rm`](buildx_rm.md)                 | Remove one or more builder instances   |
 | 
				
			||||||
| [`stop`](buildx_stop.md)             | Stop builder instance                  |
 | 
					| [`stop`](buildx_stop.md)             | Stop builder instance                  |
 | 
				
			||||||
| [`use`](buildx_use.md)               | Set the current builder instance       |
 | 
					| [`use`](buildx_use.md)               | Set the current builder instance       |
 | 
				
			||||||
| [`version`](buildx_version.md)       | Show buildx version information        |
 | 
					| [`version`](buildx_version.md)       | Show buildx version information        |
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,11 @@
 | 
				
			|||||||
# buildx rm
 | 
					# buildx rm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```text
 | 
					```text
 | 
				
			||||||
docker buildx rm [NAME]
 | 
					docker buildx rm [OPTIONS] [NAME] [NAME...]
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<!---MARKER_GEN_START-->
 | 
					<!---MARKER_GEN_START-->
 | 
				
			||||||
Remove a builder instance
 | 
					Remove one or more builder instances
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Options
 | 
					### Options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,7 @@ func TestIntegration(t *testing.T) {
 | 
				
			|||||||
	tests = append(tests, imagetoolsTests...)
 | 
						tests = append(tests, imagetoolsTests...)
 | 
				
			||||||
	tests = append(tests, versionTests...)
 | 
						tests = append(tests, versionTests...)
 | 
				
			||||||
	tests = append(tests, createTests...)
 | 
						tests = append(tests, createTests...)
 | 
				
			||||||
 | 
						tests = append(tests, rmTests...)
 | 
				
			||||||
	testIntegration(t, tests...)
 | 
						testIntegration(t, tests...)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										48
									
								
								tests/rm.go
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								tests/rm.go
									
									
									
									
									
								
							@@ -1,7 +1,11 @@
 | 
				
			|||||||
package tests
 | 
					package tests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/moby/buildkit/util/testutil/integration"
 | 
						"github.com/moby/buildkit/util/testutil/integration"
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/require"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func rmCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
 | 
					func rmCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
 | 
				
			||||||
@@ -10,3 +14,47 @@ func rmCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
 | 
				
			|||||||
	out, err := cmd.CombinedOutput()
 | 
						out, err := cmd.CombinedOutput()
 | 
				
			||||||
	return string(out), err
 | 
						return string(out), err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var rmTests = []func(t *testing.T, sb integration.Sandbox){
 | 
				
			||||||
 | 
						testRm,
 | 
				
			||||||
 | 
						testRmMulti,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func testRm(t *testing.T, sb integration.Sandbox) {
 | 
				
			||||||
 | 
						if sb.Name() != "docker-container" {
 | 
				
			||||||
 | 
							t.Skip("only testing for docker-container driver")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						out, err := rmCmd(sb, withArgs("default"))
 | 
				
			||||||
 | 
						require.Error(t, err, out) // can't remove a docker builder
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						out, err = createCmd(sb, withArgs("--driver", "docker-container"))
 | 
				
			||||||
 | 
						require.NoError(t, err, out)
 | 
				
			||||||
 | 
						builderName := strings.TrimSpace(out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						out, err = inspectCmd(sb, withArgs(builderName, "--bootstrap"))
 | 
				
			||||||
 | 
						require.NoError(t, err, out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						out, err = rmCmd(sb, withArgs(builderName))
 | 
				
			||||||
 | 
						require.NoError(t, err, out)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func testRmMulti(t *testing.T, sb integration.Sandbox) {
 | 
				
			||||||
 | 
						if sb.Name() != "docker-container" {
 | 
				
			||||||
 | 
							t.Skip("only testing for docker-container driver")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var builderNames []string
 | 
				
			||||||
 | 
						for i := 0; i < 3; i++ {
 | 
				
			||||||
 | 
							out, err := createCmd(sb, withArgs("--driver", "docker-container"))
 | 
				
			||||||
 | 
							require.NoError(t, err, out)
 | 
				
			||||||
 | 
							builderName := strings.TrimSpace(out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							out, err = inspectCmd(sb, withArgs(builderName, "--bootstrap"))
 | 
				
			||||||
 | 
							require.NoError(t, err, out)
 | 
				
			||||||
 | 
							builderNames = append(builderNames, builderName)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						out, err := rmCmd(sb, withArgs(builderNames...))
 | 
				
			||||||
 | 
						require.NoError(t, err, out)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user