2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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{
|
2017-12-06 05:15:36 -05:00
|
|
|
Use: "node",
|
|
|
|
Short: "Manage Swarm nodes",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: command.ShowHelp(dockerCli.Err()),
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"version": "1.24",
|
|
|
|
"swarm": "",
|
|
|
|
},
|
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
|
|
|
|
}
|