2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2017-03-21 19:19:59 -04:00
|
|
|
"errors"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-03-21 19:19:59 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-08 13:51:30 -04:00
|
|
|
apiclient "github.com/docker/docker/client"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewNodeCommand returns a cobra command for `node` subcommands
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewNodeCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "node",
|
2016-09-12 11:37:00 -04:00
|
|
|
Short: "Manage Swarm nodes",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: cli.NoArgs,
|
2017-05-03 17:58:52 -04:00
|
|
|
RunE: command.ShowHelp(dockerCli.Err()),
|
2017-03-03 07:42:43 -05:00
|
|
|
Tags: map[string]string{"version": "1.24"},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
cmd.AddCommand(
|
|
|
|
newDemoteCommand(dockerCli),
|
|
|
|
newInspectCommand(dockerCli),
|
|
|
|
newListCommand(dockerCli),
|
|
|
|
newPromoteCommand(dockerCli),
|
|
|
|
newRemoveCommand(dockerCli),
|
|
|
|
newPsCommand(dockerCli),
|
|
|
|
newUpdateCommand(dockerCli),
|
|
|
|
)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reference returns the reference of a node. The special value "self" for a node
|
|
|
|
// reference is mapped to the current node, hence the node ID is retrieved using
|
|
|
|
// the `/info` endpoint.
|
|
|
|
func Reference(ctx context.Context, client apiclient.APIClient, ref string) (string, error) {
|
|
|
|
if ref == "self" {
|
|
|
|
info, err := client.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-03-21 19:19:59 -04:00
|
|
|
if info.Swarm.NodeID == "" {
|
|
|
|
// If there's no node ID in /info, the node probably
|
|
|
|
// isn't a manager. Call a swarm-specific endpoint to
|
|
|
|
// get a more specific error message.
|
|
|
|
_, err = client.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", errors.New("node ID not found in /info")
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
return info.Swarm.NodeID, nil
|
|
|
|
}
|
|
|
|
return ref, nil
|
|
|
|
}
|