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"
|
|
|
|
|
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"
|
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()
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
txn, release, err := storeutil.GetStore(dockerCli)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
|
|
|
var ng *store.NodeGroup
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
if in.builder != "" {
|
2021-11-04 12:17:27 +08:00
|
|
|
ng, err = storeutil.GetNodeGroup(txn, dockerCli, in.builder)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-04 12:17:27 +08:00
|
|
|
ng, err = storeutil.GetCurrentInstance(txn, dockerCli)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ng == nil {
|
|
|
|
ng = &store.NodeGroup{
|
|
|
|
Name: "default",
|
2019-04-16 01:21:09 +08:00
|
|
|
Nodes: []store.Node{{
|
|
|
|
Name: "default",
|
|
|
|
Endpoint: "default",
|
|
|
|
}},
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 01:21:09 +08:00
|
|
|
ngi := &nginfo{ng: ng}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
err = loadNodeGroupData(timeoutCtx, dockerCli, ngi)
|
|
|
|
|
2020-11-05 06:39:54 +08:00
|
|
|
var bootNgi *nginfo
|
2019-04-16 01:21:09 +08:00
|
|
|
if in.bootstrap {
|
2019-04-16 08:01:00 +08:00
|
|
|
var ok bool
|
2021-08-12 14:45:40 +08:00
|
|
|
ok, err = boot(ctx, ngi)
|
2019-04-16 08:01:00 +08:00
|
|
|
if err != nil {
|
2019-04-16 01:21:09 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-11-05 06:39:54 +08:00
|
|
|
bootNgi = ngi
|
2019-04-16 08:01:00 +08:00
|
|
|
if ok {
|
|
|
|
ngi = &nginfo{ng: ng}
|
|
|
|
err = loadNodeGroupData(ctx, dockerCli, ngi)
|
|
|
|
}
|
2019-04-16 01:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
|
|
|
fmt.Fprintf(w, "Name:\t%s\n", ngi.ng.Name)
|
|
|
|
fmt.Fprintf(w, "Driver:\t%s\n", ngi.ng.Driver)
|
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())
|
|
|
|
} else if ngi.err != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", ngi.err.Error())
|
|
|
|
}
|
2019-04-16 08:01:00 +08:00
|
|
|
if err == nil {
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "Nodes:")
|
|
|
|
|
|
|
|
for i, n := range ngi.ng.Nodes {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-16 08:01:00 +08:00
|
|
|
if err := ngi.drivers[i].di.Err; err != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", err.Error())
|
|
|
|
} else if err := ngi.drivers[i].err; err != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", err.Error())
|
2020-11-05 06:39:54 +08:00
|
|
|
} else if bootNgi != nil && len(bootNgi.drivers) > i && bootNgi.drivers[i].err != nil {
|
|
|
|
fmt.Fprintf(w, "Error:\t%s\n", bootNgi.drivers[i].err.Error())
|
2019-04-16 08:01:00 +08:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Status:\t%s\n", ngi.drivers[i].info.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-08-18 18:03:03 +08:00
|
|
|
if ngi.drivers[i].version != "" {
|
|
|
|
fmt.Fprintf(w, "Buildkit:\t%s\n", ngi.drivers[i].version)
|
|
|
|
}
|
2020-04-25 11:08:43 +08:00
|
|
|
fmt.Fprintf(w, "Platforms:\t%s\n", strings.Join(platformutil.FormatInGroups(n.Platforms, ngi.drivers[i].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
|
|
|
|
}
|