2016-06-13 22:56:23 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2016-08-05 18:51:32 -04:00
|
|
|
"errors"
|
2016-06-13 22:56:23 -04:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"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-30 20:33:43 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-08-05 18:51:32 -04:00
|
|
|
var (
|
|
|
|
errNoRoleChange = errors.New("role was already set to the requested value")
|
|
|
|
)
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
|
2016-06-30 20:33:43 -04:00
|
|
|
nodeOpts := newNodeOptions()
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "update [OPTIONS] NODE",
|
|
|
|
Short: "Update a node",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-17 11:26:53 -04:00
|
|
|
return runUpdate(dockerCli, cmd.Flags(), args[0])
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-17 11:26:53 -04:00
|
|
|
flags := cmd.Flags()
|
2016-06-30 20:33:43 -04:00
|
|
|
flags.StringVar(&nodeOpts.role, flagRole, "", "Role of the node (worker/manager)")
|
|
|
|
flags.StringVar(&nodeOpts.availability, flagAvailability, "", "Availability of the node (active/pause/drain)")
|
|
|
|
flags.Var(&nodeOpts.annotations.labels, flagLabelAdd, "Add or update a node label (key=value)")
|
|
|
|
labelKeys := opts.NewListOpts(nil)
|
|
|
|
flags.Var(&labelKeys, flagLabelRemove, "Remove a node label if exists")
|
2016-06-13 22:56:23 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
|
2016-06-17 11:26:53 -04:00
|
|
|
success := func(_ string) {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), nodeID)
|
|
|
|
}
|
|
|
|
return updateNodes(dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func updateNodes(dockerCli command.Cli, nodes []string, mergeNode func(node *swarm.Node) error, success func(nodeID string)) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-17 11:26:53 -04:00
|
|
|
for _, nodeID := range nodes {
|
2016-06-30 17:04:02 -04:00
|
|
|
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
|
2016-06-17 11:26:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-30 20:33:43 -04:00
|
|
|
err = mergeNode(&node)
|
|
|
|
if err != nil {
|
2016-08-05 18:51:32 -04:00
|
|
|
if err == errNoRoleChange {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-30 20:33:43 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-06-17 11:26:53 -04:00
|
|
|
err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
success(nodeID)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 20:33:43 -04:00
|
|
|
func mergeNodeUpdate(flags *pflag.FlagSet) func(*swarm.Node) error {
|
|
|
|
return func(node *swarm.Node) error {
|
2016-06-17 11:26:53 -04:00
|
|
|
spec := &node.Spec
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-17 11:26:53 -04:00
|
|
|
if flags.Changed(flagRole) {
|
2016-06-30 20:33:43 -04:00
|
|
|
str, err := flags.GetString(flagRole)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 11:26:53 -04:00
|
|
|
spec.Role = swarm.NodeRole(str)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
2016-06-17 11:26:53 -04:00
|
|
|
if flags.Changed(flagAvailability) {
|
2016-06-30 20:33:43 -04:00
|
|
|
str, err := flags.GetString(flagAvailability)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 11:26:53 -04:00
|
|
|
spec.Availability = swarm.NodeAvailability(str)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
2016-06-30 20:33:43 -04:00
|
|
|
if spec.Annotations.Labels == nil {
|
|
|
|
spec.Annotations.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
if flags.Changed(flagLabelAdd) {
|
|
|
|
labels := flags.Lookup(flagLabelAdd).Value.(*opts.ListOpts).GetAll()
|
|
|
|
for k, v := range runconfigopts.ConvertKVStringsToMap(labels) {
|
|
|
|
spec.Annotations.Labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flags.Changed(flagLabelRemove) {
|
|
|
|
keys := flags.Lookup(flagLabelRemove).Value.(*opts.ListOpts).GetAll()
|
|
|
|
for _, k := range keys {
|
|
|
|
// if a key doesn't exist, fail the command explicitly
|
|
|
|
if _, exists := spec.Annotations.Labels[k]; !exists {
|
|
|
|
return fmt.Errorf("key %s doesn't exist in node's labels", k)
|
|
|
|
}
|
|
|
|
delete(spec.Annotations.Labels, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-17 11:26:53 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
flagRole = "role"
|
|
|
|
flagAvailability = "availability"
|
2016-06-30 20:33:43 -04:00
|
|
|
flagLabelAdd = "label-add"
|
|
|
|
flagLabelRemove = "label-rm"
|
2016-06-17 11:26:53 -04:00
|
|
|
)
|