2016-06-13 22:56:23 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-07-20 14:15:08 -04:00
|
|
|
listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-13 22:56:23 -04:00
|
|
|
opts := listOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "ls [OPTIONS]",
|
2016-06-13 22:56:23 -04:00
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List nodes in the swarm",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
|
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
nodes, err := client.NodeList(
|
|
|
|
ctx,
|
|
|
|
types.NodeListOptions{Filter: opts.filter.Value()})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := client.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := dockerCli.Out()
|
|
|
|
if opts.quiet {
|
|
|
|
printQuiet(out, nodes)
|
|
|
|
} else {
|
|
|
|
printTable(out, nodes, info)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTable(out io.Writer, nodes []swarm.Node, info types.Info) {
|
|
|
|
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
fmt.Fprintf(writer, listItemFmt, "ID", "HOSTNAME", "STATUS", "AVAILABILITY", "MANAGER STATUS")
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, node := range nodes {
|
2016-06-29 22:48:53 -04:00
|
|
|
name := node.Description.Hostname
|
2016-06-13 22:56:23 -04:00
|
|
|
availability := string(node.Spec.Availability)
|
|
|
|
|
|
|
|
reachability := ""
|
|
|
|
if node.ManagerStatus != nil {
|
2016-06-14 20:50:02 -04:00
|
|
|
if node.ManagerStatus.Leader {
|
|
|
|
reachability = "Leader"
|
|
|
|
} else {
|
|
|
|
reachability = string(node.ManagerStatus.Reachability)
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ID := node.ID
|
|
|
|
if node.ID == info.Swarm.NodeID {
|
|
|
|
ID = ID + " *"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
listItemFmt,
|
|
|
|
ID,
|
|
|
|
name,
|
2016-09-08 13:11:39 -04:00
|
|
|
command.PrettyPrint(string(node.Status.State)),
|
|
|
|
command.PrettyPrint(availability),
|
|
|
|
command.PrettyPrint(reachability))
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printQuiet(out io.Writer, nodes []swarm.Node) {
|
|
|
|
for _, node := range nodes {
|
|
|
|
fmt.Fprintln(out, node.ID)
|
|
|
|
}
|
|
|
|
}
|