Skip to content

Commit

Permalink
cli: add --all-inactive for rm command
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Dec 17, 2021
1 parent 33c121d commit 2f54a85
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
91 changes: 83 additions & 8 deletions commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@ package commands

import (
"context"
"sort"
"time"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/appcontext"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

type rmOptions struct {
builder string
keepState bool
keepDaemon bool
builder string
keepState bool
keepDaemon bool
allInactive bool
}

const (
rmInactiveWarning = `WARNING! This will remove all builders that are not in running state. Are you sure you want to continue?`
)

func runRm(dockerCli command.Cli, in rmOptions) error {
ctx := appcontext.Context()

if in.allInactive && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), rmInactiveWarning) {
return nil
}

txn, release, err := storeutil.GetStore(dockerCli)
if err != nil {
return err
}
defer release()

if in.allInactive {
return rmAllInactive(ctx, txn, dockerCli, in)
}

if in.builder != "" {
ng, err := storeutil.GetNodeGroup(txn, dockerCli, in.builder)
if err != nil {
return err
}
err1 := rm(ctx, dockerCli, ng, in.keepState, in.keepDaemon)
err1 := rm(ctx, dockerCli, in, ng)
if err := txn.Remove(ng.Name); err != nil {
return err
}
Expand All @@ -43,7 +61,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
return err
}
if ng != nil {
err1 := rm(ctx, dockerCli, ng, in.keepState, in.keepDaemon)
err1 := rm(ctx, dockerCli, in, ng)
if err := txn.Remove(ng.Name); err != nil {
return err
}
Expand All @@ -63,6 +81,9 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder
if len(args) > 0 {
if options.allInactive {
return errors.New("cannot specify builder name when --all-inactive is set")
}
options.builder = args[0]
}
return runRm(dockerCli, options)
Expand All @@ -72,11 +93,12 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
flags := cmd.Flags()
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
flags.BoolVar(&options.keepDaemon, "keep-daemon", false, "Keep the buildkitd daemon running")
flags.BoolVar(&options.allInactive, "all-inactive", false, "Remove all inactive builders")

return cmd
}

func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepState, keepDaemon bool) error {
func rm(ctx context.Context, dockerCli command.Cli, in rmOptions, ng *store.NodeGroup) error {
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
if err != nil {
return err
Expand All @@ -86,12 +108,12 @@ func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepSta
continue
}
// Do not stop the buildkitd daemon when --keep-daemon is provided
if !keepDaemon {
if !in.keepDaemon {
if err := di.Driver.Stop(ctx, true); err != nil {
return err
}
}
if err := di.Driver.Rm(ctx, true, !keepState, !keepDaemon); err != nil {
if err := di.Driver.Rm(ctx, true, !in.keepState, !in.keepDaemon); err != nil {
return err
}
if di.Err != nil {
Expand All @@ -100,3 +122,56 @@ func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepSta
}
return err
}

func rmAllInactive(ctx context.Context, txn *store.Txn, dockerCli command.Cli, in rmOptions) error {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

ll, err := txn.List()
if err != nil {
return err
}

builders := make([]*nginfo, len(ll))
for i, ng := range ll {
builders[i] = &nginfo{ng: ng}
}

eg, _ := errgroup.WithContext(ctx)
for _, b := range builders {
func(b *nginfo) {
eg.Go(func() error {
if err := loadNodeGroupData(ctx, dockerCli, b); err != nil {
return errors.Wrapf(err, "cannot load %s", b.ng.Name)
}
if b.ng.Dynamic {
return nil
}
var kn []store.Node
for idx, n := range b.ng.Nodes {
d := b.drivers[idx]
if d.info == nil || d.info.Status != driver.Running {
_ = d.di.Driver.Rm(ctx, true, !in.keepState, !in.keepDaemon)
continue
}
kn = append(kn, n)
}
b.ng.Nodes = kn
sort.Slice(b.ng.Nodes, func(i, j int) bool {
return b.ng.Nodes[i].Name < b.ng.Nodes[j].Name
})
if err := txn.Save(b.ng); err != nil {
return err
}
if len(b.ng.Nodes) == 0 {
if err := txn.Remove(b.ng.Name); err != nil {
return errors.Wrapf(err, "cannot remove %s", b.ng.Name)
}
}
return nil
})
}(b)
}

return eg.Wait()
}
1 change: 1 addition & 0 deletions docs/reference/buildx_rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Remove a builder instance

| Name | Description |
| --- | --- |
| `--all-inactive` | Remove all inactive builders |
| [`--builder string`](#builder) | Override the configured builder instance |
| [`--keep-daemon`](#keep-daemon) | Keep the buildkitd daemon running |
| [`--keep-state`](#keep-state) | Keep BuildKit state |
Expand Down

0 comments on commit 2f54a85

Please sign in to comment.