2019-04-12 08:24:24 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-04-16 01:21:09 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
"github.com/docker/buildx/builder"
|
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 01:21:09 +08:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2019-04-12 08:24:24 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type inspectOptions struct {
|
|
|
|
bootstrap bool
|
2020-04-28 05:37:17 +08:00
|
|
|
builder string
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func runInspect(dockerCli command.Cli, in inspectOptions) error {
|
2019-04-16 01:21:09 +08:00
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
b, err := builder.New(dockerCli,
|
|
|
|
builder.WithName(in.builder),
|
|
|
|
builder.WithSkippedValidation(),
|
|
|
|
)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 01:21:09 +08:00
|
|
|
|
2020-07-16 23:51:09 +08:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
2019-04-16 01:21:09 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
nodes, err := b.LoadNodes(timeoutCtx, true)
|
2019-04-16 01:21:09 +08:00
|
|
|
if in.bootstrap {
|
2019-04-16 08:01:00 +08:00
|
|
|
var ok bool
|
2022-12-06 02:57:35 +08:00
|
|
|
ok, err = b.Boot(ctx)
|
2019-04-16 08:01:00 +08:00
|
|
|
if err != nil {
|
2019-04-16 01:21:09 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-04-16 08:01:00 +08:00
|
|
|
if ok {
|
2022-12-06 02:57:35 +08:00
|
|
|
nodes, err = b.LoadNodes(timeoutCtx, true)
|
2019-04-16 08:01:00 +08:00
|
|
|
}
|
2019-04-16 01:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
2022-12-06 02:57:35 +08:00
|
|
|
fmt.Fprintf(w, "Name:\t%s\n", b.Name)
|
|
|
|
fmt.Fprintf(w, "Driver:\t%s\n", b.Driver)
|
2022-12-07 18:44:33 +08:00
|
|
|
if !b.NodeGroup.LastActivity.IsZero() {
|
|
|
|
fmt.Fprintf(w, "Last Activity:\t%v\n", b.NodeGroup.LastActivity)
|
|
|
|
}
|
2022-03-16 02:52:11 +08:00
|
|
|
|
2019-04-16 01:21:09 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", err.Error())
|
2022-12-06 02:57:35 +08:00
|
|
|
} else if b.Err() != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", b.Err().Error())
|
2019-04-16 01:21:09 +08:00
|
|
|
}
|
2019-04-16 08:01:00 +08:00
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "Nodes:")
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
for i, n := range nodes {
|
2019-04-16 08:01:00 +08:00
|
|
|
if i != 0 {
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "Name:\t%s\n", n.Name)
|
|
|
|
fmt.Fprintf(w, "Endpoint:\t%s\n", n.Endpoint)
|
2022-03-16 02:52:11 +08:00
|
|
|
|
|
|
|
var driverOpts []string
|
|
|
|
for k, v := range n.DriverOpts {
|
2022-04-13 02:56:52 +08:00
|
|
|
driverOpts = append(driverOpts, fmt.Sprintf("%s=%q", k, v))
|
2022-03-16 02:52:11 +08:00
|
|
|
}
|
|
|
|
if len(driverOpts) > 0 {
|
2022-04-13 02:56:52 +08:00
|
|
|
fmt.Fprintf(w, "Driver Options:\t%s\n", strings.Join(driverOpts, " "))
|
2022-03-16 02:52:11 +08:00
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
if err := n.Err; err != nil {
|
2019-04-16 08:01:00 +08:00
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", err.Error())
|
|
|
|
} else {
|
2022-12-06 02:57:35 +08:00
|
|
|
fmt.Fprintf(w, "Status:\t%s\n", nodes[i].DriverInfo.Status)
|
2019-07-09 06:06:07 +08:00
|
|
|
if len(n.Flags) > 0 {
|
|
|
|
fmt.Fprintf(w, "Flags:\t%s\n", strings.Join(n.Flags, " "))
|
|
|
|
}
|
2022-12-06 02:57:35 +08:00
|
|
|
if nodes[i].Version != "" {
|
|
|
|
fmt.Fprintf(w, "Buildkit:\t%s\n", nodes[i].Version)
|
2022-08-18 18:03:03 +08:00
|
|
|
}
|
2022-12-06 02:57:35 +08:00
|
|
|
fmt.Fprintf(w, "Platforms:\t%s\n", strings.Join(platformutil.FormatInGroups(n.Node.Platforms, n.Platforms), ", "))
|
2019-04-16 08:01:00 +08:00
|
|
|
}
|
2019-04-16 01:21:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
2019-04-12 08:24:24 +08:00
|
|
|
var options inspectOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "inspect [NAME]",
|
|
|
|
Short: "Inspect current builder instance",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-28 05:37:17 +08:00
|
|
|
options.builder = rootOpts.builder
|
|
|
|
if len(args) > 0 {
|
|
|
|
options.builder = args[0]
|
|
|
|
}
|
|
|
|
return runInspect(dockerCli, options)
|
2019-04-12 08:24:24 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&options.bootstrap, "bootstrap", false, "Ensure builder has booted before inspecting")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|