2020-04-07 08:31:18 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2020-04-07 10:03:29 +08:00
|
|
|
"github.com/docker/buildx/build"
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/opts"
|
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"
|
2020-04-07 08:31:18 +08:00
|
|
|
"github.com/tonistiigi/units"
|
2020-04-07 10:03:29 +08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-03-26 07:32:46 +08:00
|
|
|
dis, err := getInstanceOrDefault(ctx, dockerCli, opts.builder, "")
|
2020-04-07 10:03:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, di := range dis {
|
|
|
|
if di.Err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([][]*client.UsageInfo, len(dis))
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for i, di := range dis {
|
|
|
|
func(i int, di build.DriverInfo) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
if di.Driver != nil {
|
|
|
|
c, err := di.Driver.Client(ctx)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}(i, di)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if di.Parent != "" {
|
|
|
|
printKV(tw, "Parent", di.Parent)
|
|
|
|
}
|
|
|
|
printKV(tw, "Created at", di.CreatedAt)
|
|
|
|
printKV(tw, "Mutable", di.Mutable)
|
|
|
|
printKV(tw, "Reclaimable", !di.InUse)
|
|
|
|
printKV(tw, "Shared", di.Shared)
|
|
|
|
printKV(tw, "Size", fmt.Sprintf("%.2f", units.Bytes(di.Size)))
|
|
|
|
if di.Description != "" {
|
|
|
|
printKV(tw, "Description", di.Description)
|
|
|
|
}
|
|
|
|
printKV(tw, "Usage count", di.UsageCount)
|
|
|
|
if di.LastUsedAt != nil {
|
|
|
|
printKV(tw, "Last used", di.LastUsedAt)
|
|
|
|
}
|
|
|
|
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 += "*"
|
|
|
|
}
|
|
|
|
size := fmt.Sprintf("%.2f", units.Bytes(di.Size))
|
|
|
|
if di.Shared {
|
|
|
|
size += "*"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(tw, "%-71s\t%-11v\t%s\t\n", id, !di.InUse, size)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fmt.Fprintf(tw, "Shared:\t%.2f\n", units.Bytes(shared))
|
|
|
|
fmt.Fprintf(tw, "Private:\t%.2f\n", units.Bytes(total-shared))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(tw, "Reclaimable:\t%.2f\n", units.Bytes(reclaimable))
|
|
|
|
fmt.Fprintf(tw, "Total:\t%.2f\n", units.Bytes(total))
|
|
|
|
tw.Flush()
|
|
|
|
}
|