mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Remove dead code from node update.
Also share mode code between update commands
and use flag constants
Signed-off-by: Daniel Nephin <dnephin@docker.com>
(cherry picked from commit cacaeab9db
)
This commit is contained in:
parent
0641b8ba9c
commit
8a93ff1ffb
5 changed files with 70 additions and 118 deletions
|
@ -7,34 +7,25 @@ import (
|
|||
"github.com/docker/docker/cli"
|
||||
"github.com/docker/engine-api/types/swarm"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func newAcceptCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||
var flags *pflag.FlagSet
|
||||
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "accept NODE [NODE...]",
|
||||
Short: "Accept a node in the swarm",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runAccept(dockerCli, flags, args)
|
||||
return runAccept(dockerCli, args)
|
||||
},
|
||||
}
|
||||
|
||||
flags = cmd.Flags()
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runAccept(dockerCli *client.DockerCli, flags *pflag.FlagSet, args []string) error {
|
||||
for _, id := range args {
|
||||
if err := runUpdate(dockerCli, id, func(node *swarm.Node) {
|
||||
node.Spec.Membership = swarm.NodeMembershipAccepted
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(dockerCli.Out(), "Node %s accepted in the swarm.\n", id)
|
||||
func runAccept(dockerCli *client.DockerCli, nodes []string) error {
|
||||
accept := func(node *swarm.Node) {
|
||||
node.Spec.Membership = swarm.NodeMembershipAccepted
|
||||
}
|
||||
|
||||
return nil
|
||||
success := func(nodeID string) {
|
||||
fmt.Fprintf(dockerCli.Out(), "Node %s accepted in the swarm.\n", nodeID)
|
||||
}
|
||||
return updateNodes(dockerCli, nodes, accept, success)
|
||||
}
|
||||
|
|
|
@ -7,34 +7,25 @@ import (
|
|||
"github.com/docker/docker/cli"
|
||||
"github.com/docker/engine-api/types/swarm"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func newDemoteCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||
var flags *pflag.FlagSet
|
||||
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "demote NODE [NODE...]",
|
||||
Short: "Demote a node from manager in the swarm",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runDemote(dockerCli, flags, args)
|
||||
return runDemote(dockerCli, args)
|
||||
},
|
||||
}
|
||||
|
||||
flags = cmd.Flags()
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runDemote(dockerCli *client.DockerCli, flags *pflag.FlagSet, args []string) error {
|
||||
for _, id := range args {
|
||||
if err := runUpdate(dockerCli, id, func(node *swarm.Node) {
|
||||
node.Spec.Role = swarm.NodeRoleWorker
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", id)
|
||||
func runDemote(dockerCli *client.DockerCli, nodes []string) error {
|
||||
demote := func(node *swarm.Node) {
|
||||
node.Spec.Role = swarm.NodeRoleWorker
|
||||
}
|
||||
|
||||
return nil
|
||||
success := func(nodeID string) {
|
||||
fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", nodeID)
|
||||
}
|
||||
return updateNodes(dockerCli, nodes, demote, success)
|
||||
}
|
||||
|
|
|
@ -7,34 +7,25 @@ import (
|
|||
"github.com/docker/docker/cli"
|
||||
"github.com/docker/engine-api/types/swarm"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func newPromoteCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||
var flags *pflag.FlagSet
|
||||
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "promote NODE [NODE...]",
|
||||
Short: "Promote a node to a manager in the swarm",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runPromote(dockerCli, flags, args)
|
||||
return runPromote(dockerCli, args)
|
||||
},
|
||||
}
|
||||
|
||||
flags = cmd.Flags()
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runPromote(dockerCli *client.DockerCli, flags *pflag.FlagSet, args []string) error {
|
||||
for _, id := range args {
|
||||
if err := runUpdate(dockerCli, id, func(node *swarm.Node) {
|
||||
node.Spec.Role = swarm.NodeRoleManager
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(dockerCli.Out(), "Node %s promoted to a manager in the swarm.\n", id)
|
||||
func runPromote(dockerCli *client.DockerCli, nodes []string) error {
|
||||
promote := func(node *swarm.Node) {
|
||||
node.Spec.Role = swarm.NodeRoleManager
|
||||
}
|
||||
|
||||
return nil
|
||||
success := func(nodeID string) {
|
||||
fmt.Fprintf(dockerCli.Out(), "Node %s promoted to a manager in the swarm.\n", nodeID)
|
||||
}
|
||||
return updateNodes(dockerCli, nodes, promote, success)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
"github.com/docker/docker/api/client"
|
||||
"github.com/docker/docker/cli"
|
||||
runconfigopts "github.com/docker/docker/runconfig/opts"
|
||||
"github.com/docker/engine-api/types/swarm"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
@ -14,90 +13,71 @@ import (
|
|||
|
||||
func newUpdateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||
var opts nodeOptions
|
||||
var flags *pflag.FlagSet
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "update [OPTIONS] NODE",
|
||||
Short: "Update a node",
|
||||
Args: cli.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := runUpdate(dockerCli, args[0], mergeNodeUpdate(flags)); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(dockerCli.Out(), args[0])
|
||||
return nil
|
||||
return runUpdate(dockerCli, cmd.Flags(), args[0])
|
||||
},
|
||||
}
|
||||
|
||||
flags = cmd.Flags()
|
||||
flags.StringVar(&opts.role, "role", "", "Role of the node (worker/manager)")
|
||||
flags.StringVar(&opts.membership, "membership", "", "Membership of the node (accepted/rejected)")
|
||||
flags.StringVar(&opts.availability, "availability", "", "Availability of the node (active/pause/drain)")
|
||||
flags := cmd.Flags()
|
||||
flags.StringVar(&opts.role, flagRole, "", "Role of the node (worker/manager)")
|
||||
flags.StringVar(&opts.membership, flagMembership, "", "Membership of the node (accepted/rejected)")
|
||||
flags.StringVar(&opts.availability, flagAvailability, "", "Availability of the node (active/pause/drain)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runUpdate(dockerCli *client.DockerCli, nodeID string, mergeNode func(node *swarm.Node)) error {
|
||||
func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, nodeID string) error {
|
||||
success := func(_ string) {
|
||||
fmt.Fprintln(dockerCli.Out(), nodeID)
|
||||
}
|
||||
return updateNodes(dockerCli, []string{nodeID}, mergeNodeUpdate(flags), success)
|
||||
}
|
||||
|
||||
func updateNodes(dockerCli *client.DockerCli, nodes []string, mergeNode func(node *swarm.Node), success func(nodeID string)) error {
|
||||
client := dockerCli.Client()
|
||||
ctx := context.Background()
|
||||
|
||||
node, err := client.NodeInspect(ctx, nodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, nodeID := range nodes {
|
||||
node, err := client.NodeInspect(ctx, nodeID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mergeNode(&node)
|
||||
err = client.NodeUpdate(ctx, node.ID, node.Version, node.Spec)
|
||||
if err != nil {
|
||||
return err
|
||||
mergeNode(&node)
|
||||
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) {
|
||||
return func(node *swarm.Node) {
|
||||
mergeString := func(flag string, field *string) {
|
||||
if flags.Changed(flag) {
|
||||
*field, _ = flags.GetString(flag)
|
||||
}
|
||||
}
|
||||
|
||||
mergeRole := func(flag string, field *swarm.NodeRole) {
|
||||
if flags.Changed(flag) {
|
||||
str, _ := flags.GetString(flag)
|
||||
*field = swarm.NodeRole(str)
|
||||
}
|
||||
}
|
||||
|
||||
mergeMembership := func(flag string, field *swarm.NodeMembership) {
|
||||
if flags.Changed(flag) {
|
||||
str, _ := flags.GetString(flag)
|
||||
*field = swarm.NodeMembership(str)
|
||||
}
|
||||
}
|
||||
|
||||
mergeAvailability := func(flag string, field *swarm.NodeAvailability) {
|
||||
if flags.Changed(flag) {
|
||||
str, _ := flags.GetString(flag)
|
||||
*field = swarm.NodeAvailability(str)
|
||||
}
|
||||
}
|
||||
|
||||
mergeLabels := func(flag string, field *map[string]string) {
|
||||
if flags.Changed(flag) {
|
||||
values, _ := flags.GetStringSlice(flag)
|
||||
for key, value := range runconfigopts.ConvertKVStringsToMap(values) {
|
||||
(*field)[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spec := &node.Spec
|
||||
mergeString("name", &spec.Name)
|
||||
// TODO: setting labels is not working
|
||||
mergeLabels("label", &spec.Labels)
|
||||
mergeRole("role", &spec.Role)
|
||||
mergeMembership("membership", &spec.Membership)
|
||||
mergeAvailability("availability", &spec.Availability)
|
||||
|
||||
if flags.Changed(flagRole) {
|
||||
str, _ := flags.GetString(flagRole)
|
||||
spec.Role = swarm.NodeRole(str)
|
||||
}
|
||||
if flags.Changed(flagMembership) {
|
||||
str, _ := flags.GetString(flagMembership)
|
||||
spec.Membership = swarm.NodeMembership(str)
|
||||
}
|
||||
if flags.Changed(flagAvailability) {
|
||||
str, _ := flags.GetString(flagAvailability)
|
||||
spec.Availability = swarm.NodeAvailability(str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
flagRole = "role"
|
||||
flagMembership = "membership"
|
||||
flagAvailability = "availability"
|
||||
)
|
||||
|
|
|
@ -212,7 +212,6 @@ func anyChanged(flags *pflag.FlagSet, fields ...string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
|
||||
// TODO: should this override by destination path, or does swarm handle that?
|
||||
func updateMounts(flags *pflag.FlagSet, mounts *[]swarm.Mount) {
|
||||
if !flags.Changed(flagMount) {
|
||||
|
|
Loading…
Reference in a new issue