2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := newNodeOptions()
|
2016-09-08 13:11:39 -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 {
|
|
|
|
return runUpdate(dockerCli, cmd.Flags(), args[0])
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.StringVar(&options.role, flagRole, "", `Role of the node ("worker"|"manager")`)
|
|
|
|
flags.StringVar(&options.availability, flagAvailability, "", `Availability of the node ("active"|"pause"|"drain")`)
|
|
|
|
flags.Var(&options.annotations.labels, flagLabelAdd, "Add or update a node label (key=value)")
|
2016-09-08 13:11:39 -04:00
|
|
|
labelKeys := opts.NewListOpts(nil)
|
|
|
|
flags.Var(&labelKeys, flagLabelRemove, "Remove a node label if exists")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, nodeID string) error {
|
2016-09-08 13:11:39 -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-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, nodeID := range nodes {
|
|
|
|
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mergeNode(&node)
|
|
|
|
if err != nil {
|
|
|
|
if err == errNoRoleChange {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
success(nodeID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeNodeUpdate(flags *pflag.FlagSet) func(*swarm.Node) error {
|
|
|
|
return func(node *swarm.Node) error {
|
|
|
|
spec := &node.Spec
|
|
|
|
|
|
|
|
if flags.Changed(flagRole) {
|
|
|
|
str, err := flags.GetString(flagRole)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
spec.Role = swarm.NodeRole(str)
|
|
|
|
}
|
|
|
|
if flags.Changed(flagAvailability) {
|
|
|
|
str, err := flags.GetString(flagAvailability)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
spec.Availability = swarm.NodeAvailability(str)
|
|
|
|
}
|
|
|
|
if spec.Annotations.Labels == nil {
|
|
|
|
spec.Annotations.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
if flags.Changed(flagLabelAdd) {
|
|
|
|
labels := flags.Lookup(flagLabelAdd).Value.(*opts.ListOpts).GetAll()
|
2017-06-05 18:23:21 -04:00
|
|
|
for k, v := range opts.ConvertKVStringsToMap(labels) {
|
2016-09-08 13:11:39 -04:00
|
|
|
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 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("key %s doesn't exist in node's labels", k)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
delete(spec.Annotations.Labels, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagRole = "role"
|
|
|
|
flagAvailability = "availability"
|
|
|
|
flagLabelAdd = "label-add"
|
|
|
|
flagLabelRemove = "label-rm"
|
|
|
|
)
|