cmd.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package node
  2. import (
  3. "errors"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. apiclient "github.com/docker/docker/client"
  8. "github.com/spf13/cobra"
  9. "golang.org/x/net/context"
  10. )
  11. // NewNodeCommand returns a cobra command for `node` subcommands
  12. func NewNodeCommand(dockerCli *command.DockerCli) *cobra.Command {
  13. cmd := &cobra.Command{
  14. Use: "node",
  15. Short: "Manage Swarm nodes",
  16. Args: cli.NoArgs,
  17. RunE: dockerCli.ShowHelp,
  18. Tags: map[string]string{"version": "1.24"},
  19. }
  20. cmd.AddCommand(
  21. newDemoteCommand(dockerCli),
  22. newInspectCommand(dockerCli),
  23. newListCommand(dockerCli),
  24. newPromoteCommand(dockerCli),
  25. newRemoveCommand(dockerCli),
  26. newPsCommand(dockerCli),
  27. newUpdateCommand(dockerCli),
  28. )
  29. return cmd
  30. }
  31. // Reference returns the reference of a node. The special value "self" for a node
  32. // reference is mapped to the current node, hence the node ID is retrieved using
  33. // the `/info` endpoint.
  34. func Reference(ctx context.Context, client apiclient.APIClient, ref string) (string, error) {
  35. if ref == "self" {
  36. info, err := client.Info(ctx)
  37. if err != nil {
  38. return "", err
  39. }
  40. if info.Swarm.NodeID == "" {
  41. // If there's no node ID in /info, the node probably
  42. // isn't a manager. Call a swarm-specific endpoint to
  43. // get a more specific error message.
  44. _, err = client.NodeList(ctx, types.NodeListOptions{})
  45. if err != nil {
  46. return "", err
  47. }
  48. return "", errors.New("node ID not found in /info")
  49. }
  50. return info.Swarm.NodeID, nil
  51. }
  52. return ref, nil
  53. }