2020-04-07 08:31:18 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-12-16 14:09:13 +08:00
|
|
|
"strings"
|
2020-04-07 08:31:18 +08:00
|
|
|
"text/tabwriter"
|
2021-11-29 23:21:35 +08:00
|
|
|
"time"
|
2020-04-07 08:31:18 +08:00
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
"github.com/docker/buildx/builder"
|
2023-04-10 03:20:18 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil/completion"
|
2020-04-07 10:03:29 +08:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/opts"
|
2021-11-29 23:21:35 +08:00
|
|
|
"github.com/docker/go-units"
|
2020-04-07 08:31:18 +08:00
|
|
|
"github.com/moby/buildkit/client"
|
2020-04-07 10:03:29 +08:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2020-04-07 08:31:18 +08:00
|
|
|
)
|
|
|
|
|
2020-04-07 10:03:29 +08:00
|
|
|
type duOptions struct {
|
2020-04-28 05:37:17 +08:00
|
|
|
builder string
|
2020-04-07 10:03:29 +08:00
|
|
|
filter opts.FilterOpt
|
|
|
|
verbose bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
|
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
|
|
|
pi, err := toBuildkitPruneInfo(opts.filter.Value())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
b, err := builder.New(dockerCli, builder.WithName(opts.builder))
|
2020-04-07 10:03:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
nodes, err := b.LoadNodes(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Err != nil {
|
|
|
|
return node.Err
|
2020-04-07 10:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
out := make([][]*client.UsageInfo, len(nodes))
|
2020-04-07 10:03:29 +08:00
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2022-12-06 02:57:35 +08:00
|
|
|
for i, node := range nodes {
|
|
|
|
func(i int, node builder.Node) {
|
2020-04-07 10:03:29 +08:00
|
|
|
eg.Go(func() error {
|
2022-12-06 02:57:35 +08:00
|
|
|
if node.Driver != nil {
|
|
|
|
c, err := node.Driver.Client(ctx)
|
2020-04-07 10:03:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
du, err := c.DiskUsage(ctx, client.WithFilter(pi.Filter))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out[i] = du
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-12-06 02:57:35 +08:00
|
|
|
}(i, node)
|
2020-04-07 10:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
|
|
|
|
first := true
|
|
|
|
for _, du := range out {
|
|
|
|
if du == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if opts.verbose {
|
|
|
|
printVerbose(tw, du)
|
|
|
|
} else {
|
|
|
|
if first {
|
|
|
|
printTableHeader(tw)
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
for _, di := range du {
|
|
|
|
printTableRow(tw, di)
|
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.filter.Value().Len() == 0 {
|
|
|
|
printSummary(tw, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
2020-04-07 10:03:29 +08:00
|
|
|
options := duOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "du",
|
|
|
|
Short: "Disk usage",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-28 05:37:17 +08:00
|
|
|
options.builder = rootOpts.builder
|
2020-04-07 10:03:29 +08:00
|
|
|
return runDiskUsage(dockerCli, options)
|
|
|
|
},
|
2023-04-10 03:20:18 +08:00
|
|
|
ValidArgsFunction: completion.Disable,
|
2020-04-07 10:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.Var(&options.filter, "filter", "Provide filter values")
|
|
|
|
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:31:18 +08:00
|
|
|
func printKV(w io.Writer, k string, v interface{}) {
|
|
|
|
fmt.Fprintf(w, "%s:\t%v\n", k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printVerbose(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
|
|
|
for _, di := range du {
|
|
|
|
printKV(tw, "ID", di.ID)
|
2021-12-16 14:09:13 +08:00
|
|
|
if len(di.Parents) != 0 {
|
|
|
|
printKV(tw, "Parent", strings.Join(di.Parents, ","))
|
2020-04-07 08:31:18 +08:00
|
|
|
}
|
|
|
|
printKV(tw, "Created at", di.CreatedAt)
|
|
|
|
printKV(tw, "Mutable", di.Mutable)
|
|
|
|
printKV(tw, "Reclaimable", !di.InUse)
|
|
|
|
printKV(tw, "Shared", di.Shared)
|
2021-11-29 23:21:35 +08:00
|
|
|
printKV(tw, "Size", units.HumanSize(float64(di.Size)))
|
2020-04-07 08:31:18 +08:00
|
|
|
if di.Description != "" {
|
|
|
|
printKV(tw, "Description", di.Description)
|
|
|
|
}
|
|
|
|
printKV(tw, "Usage count", di.UsageCount)
|
|
|
|
if di.LastUsedAt != nil {
|
2021-11-29 23:21:35 +08:00
|
|
|
printKV(tw, "Last used", units.HumanDuration(time.Since(*di.LastUsedAt))+" ago")
|
2020-04-07 08:31:18 +08:00
|
|
|
}
|
|
|
|
if di.RecordType != "" {
|
|
|
|
printKV(tw, "Type", di.RecordType)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(tw, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTableHeader(tw *tabwriter.Writer) {
|
|
|
|
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tLAST ACCESSED")
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTableRow(tw *tabwriter.Writer, di *client.UsageInfo) {
|
|
|
|
id := di.ID
|
|
|
|
if di.Mutable {
|
|
|
|
id += "*"
|
|
|
|
}
|
2021-11-29 23:21:35 +08:00
|
|
|
size := units.HumanSize(float64(di.Size))
|
2020-04-07 08:31:18 +08:00
|
|
|
if di.Shared {
|
|
|
|
size += "*"
|
|
|
|
}
|
2021-11-29 23:21:35 +08:00
|
|
|
lastAccessed := ""
|
|
|
|
if di.LastUsedAt != nil {
|
|
|
|
lastAccessed = units.HumanDuration(time.Since(*di.LastUsedAt)) + " ago"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(tw, "%-40s\t%-5v\t%-10s\t%s\n", id, !di.InUse, size, lastAccessed)
|
2020-04-07 08:31:18 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 10:03:29 +08:00
|
|
|
func printSummary(tw *tabwriter.Writer, dus [][]*client.UsageInfo) {
|
2020-04-07 08:31:18 +08:00
|
|
|
total := int64(0)
|
|
|
|
reclaimable := int64(0)
|
|
|
|
shared := int64(0)
|
|
|
|
|
2020-04-07 10:03:29 +08:00
|
|
|
for _, du := range dus {
|
|
|
|
for _, di := range du {
|
|
|
|
if di.Size > 0 {
|
|
|
|
total += di.Size
|
|
|
|
if !di.InUse {
|
|
|
|
reclaimable += di.Size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if di.Shared {
|
|
|
|
shared += di.Size
|
2020-04-07 08:31:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shared > 0 {
|
2021-11-29 23:21:35 +08:00
|
|
|
fmt.Fprintf(tw, "Shared:\t%s\n", units.HumanSize(float64(shared)))
|
|
|
|
fmt.Fprintf(tw, "Private:\t%s\n", units.HumanSize(float64(total-shared)))
|
2020-04-07 08:31:18 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 23:21:35 +08:00
|
|
|
fmt.Fprintf(tw, "Reclaimable:\t%s\n", units.HumanSize(float64(reclaimable)))
|
|
|
|
fmt.Fprintf(tw, "Total:\t%s\n", units.HumanSize(float64(total)))
|
2020-04-07 08:31:18 +08:00
|
|
|
tw.Flush()
|
|
|
|
}
|