2019-04-12 08:24:24 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-04-16 06:10:50 +08:00
|
|
|
"context"
|
2019-04-12 08:24:24 +08:00
|
|
|
"fmt"
|
2019-04-16 06:10:50 +08:00
|
|
|
"io"
|
2022-05-15 14:30:31 +08:00
|
|
|
"sort"
|
2019-04-16 06:10:50 +08:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/store"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/docker/buildx/store/storeutil"
|
2021-11-05 01:52:11 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/util/platformutil"
|
2019-04-12 08:24:24 +08:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2019-04-16 06:10:50 +08:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2019-04-12 08:24:24 +08:00
|
|
|
"github.com/spf13/cobra"
|
2019-04-16 06:10:50 +08:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-04-12 08:24:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type lsOptions struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLs(dockerCli command.Cli, in lsOptions) error {
|
2019-04-16 06:10:50 +08:00
|
|
|
ctx := appcontext.Context()
|
2019-04-13 07:39:06 +08:00
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
txn, release, err := storeutil.GetStore(dockerCli)
|
2019-04-16 06:10:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
2020-07-16 23:49:52 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
2019-04-16 06:10:50 +08:00
|
|
|
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}
|
|
|
|
}
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2022-05-15 14:30:31 +08:00
|
|
|
contexts, err := dockerCli.ContextStore().List()
|
2019-04-12 08:24:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-15 14:30:31 +08:00
|
|
|
sort.Slice(contexts, func(i, j int) bool {
|
|
|
|
return contexts[i].Name < contexts[j].Name
|
|
|
|
})
|
|
|
|
for _, c := range contexts {
|
|
|
|
ngi := &nginfo{ng: &store.NodeGroup{
|
|
|
|
Name: c.Name,
|
2019-04-16 06:10:50 +08:00
|
|
|
Nodes: []store.Node{{
|
2022-05-15 14:30:31 +08:00
|
|
|
Name: c.Name,
|
|
|
|
Endpoint: c.Name,
|
2019-04-16 06:10:50 +08:00
|
|
|
}},
|
|
|
|
}}
|
2022-05-15 14:30:31 +08:00
|
|
|
// if a context has the same name as an instance from the store, do not
|
|
|
|
// add it to the builders list. An instance from the store takes
|
|
|
|
// precedence over context builders.
|
|
|
|
if hasNodeGroup(builders, ngi) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
builders = append(builders, ngi)
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 06:10:50 +08:00
|
|
|
eg, _ := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
for _, b := range builders {
|
|
|
|
func(b *nginfo) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
err = loadNodeGroupData(ctx, dockerCli, b)
|
|
|
|
if b.err == nil && err != nil {
|
|
|
|
b.err = err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
2019-04-13 07:39:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:10:50 +08:00
|
|
|
currentName := "default"
|
2021-11-04 12:17:27 +08:00
|
|
|
current, err := storeutil.GetCurrentInstance(txn, dockerCli)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 06:10:50 +08:00
|
|
|
if current != nil {
|
|
|
|
currentName = current.Name
|
|
|
|
if current.Name == "default" {
|
|
|
|
currentName = current.Nodes[0].Endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 04:36:55 +08:00
|
|
|
w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0)
|
2022-04-09 07:22:33 +08:00
|
|
|
fmt.Fprintf(w, "NAME/NODE\tDRIVER/ENDPOINT\tSTATUS\tBUILDKIT\tPLATFORMS\n")
|
2019-04-16 06:10:50 +08:00
|
|
|
|
|
|
|
currentSet := false
|
2022-05-25 04:36:55 +08:00
|
|
|
printErr := false
|
2019-04-16 06:10:50 +08:00
|
|
|
for _, b := range builders {
|
|
|
|
if !currentSet && b.ng.Name == currentName {
|
|
|
|
b.ng.Name += " *"
|
|
|
|
currentSet = true
|
|
|
|
}
|
2022-05-25 04:36:55 +08:00
|
|
|
if ok := printngi(w, b); !ok {
|
|
|
|
printErr = true
|
|
|
|
}
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 06:10:50 +08:00
|
|
|
w.Flush()
|
|
|
|
|
2022-05-25 04:36:55 +08:00
|
|
|
if printErr {
|
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "\n")
|
|
|
|
for _, b := range builders {
|
|
|
|
if b.err != nil {
|
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Cannot load builder %s: %s\n", b.ng.Name, strings.TrimSpace(b.err.Error()))
|
|
|
|
} else {
|
|
|
|
for idx, n := range b.ng.Nodes {
|
|
|
|
d := b.drivers[idx]
|
|
|
|
var nodeErr string
|
|
|
|
if d.err != nil {
|
|
|
|
nodeErr = d.err.Error()
|
|
|
|
} else if d.di.Err != nil {
|
|
|
|
nodeErr = d.di.Err.Error()
|
|
|
|
}
|
|
|
|
if nodeErr != "" {
|
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Failed to get status for %s (%s): %s\n", b.ng.Name, n.Name, strings.TrimSpace(nodeErr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 04:36:55 +08:00
|
|
|
func printngi(w io.Writer, ngi *nginfo) (ok bool) {
|
|
|
|
ok = true
|
2019-04-16 06:10:50 +08:00
|
|
|
var err string
|
|
|
|
if ngi.err != nil {
|
2022-05-25 04:36:55 +08:00
|
|
|
ok = false
|
|
|
|
err = "error"
|
2019-04-16 06:10:50 +08:00
|
|
|
}
|
2022-04-09 07:22:33 +08:00
|
|
|
fmt.Fprintf(w, "%s\t%s\t%s\t\t\n", ngi.ng.Name, ngi.ng.Driver, err)
|
2019-04-16 06:10:50 +08:00
|
|
|
if ngi.err == nil {
|
|
|
|
for idx, n := range ngi.ng.Nodes {
|
|
|
|
d := ngi.drivers[idx]
|
|
|
|
var status string
|
|
|
|
if d.info != nil {
|
|
|
|
status = d.info.Status.String()
|
|
|
|
}
|
2022-05-25 04:36:55 +08:00
|
|
|
if d.err != nil || d.di.Err != nil {
|
|
|
|
ok = false
|
|
|
|
fmt.Fprintf(w, " %s\t%s\t%s\t\t\n", n.Name, n.Endpoint, "error")
|
2019-04-16 06:10:50 +08:00
|
|
|
} else {
|
2022-04-09 07:22:33 +08:00
|
|
|
fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\n", n.Name, n.Endpoint, status, d.version, strings.Join(platformutil.FormatInGroups(n.Platforms, d.platforms), ", "))
|
2019-04-16 06:10:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 04:36:55 +08:00
|
|
|
return
|
2019-04-16 06:10:50 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
func lsCmd(dockerCli command.Cli) *cobra.Command {
|
|
|
|
var options lsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Short: "List builder instances",
|
|
|
|
Args: cli.ExactArgs(0),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runLs(dockerCli, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-05 01:52:11 +08:00
|
|
|
// hide builder persistent flag for this command
|
|
|
|
cobrautil.HideInheritedFlags(cmd, "builder")
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
return cmd
|
|
|
|
}
|