member_command.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. )
  22. var (
  23. memberID uint64
  24. memberPeerURLs string
  25. )
  26. // NewMemberCommand returns the cobra command for "member".
  27. func NewMemberCommand() *cobra.Command {
  28. mc := &cobra.Command{
  29. Use: "member",
  30. Short: "member is used to manage membership in an etcd cluster.",
  31. }
  32. mc.AddCommand(NewMemberAddCommand())
  33. mc.AddCommand(NewMemberRemoveCommand())
  34. mc.AddCommand(NewMemberUpdateCommand())
  35. mc.AddCommand(NewMemberListCommand())
  36. return mc
  37. }
  38. // NewMemberAddCommand returns the cobra command for "member add".
  39. func NewMemberAddCommand() *cobra.Command {
  40. cc := &cobra.Command{
  41. Use: "add",
  42. Short: "add is used to add a member into the cluster",
  43. Run: memberAddCommandFunc,
  44. }
  45. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "comma separated peer URLs for the new member.")
  46. return cc
  47. }
  48. // NewMemberRemoveCommand returns the cobra command for "member remove".
  49. func NewMemberRemoveCommand() *cobra.Command {
  50. cc := &cobra.Command{
  51. Use: "remove",
  52. Short: "remove is used to remove a member from the cluster",
  53. Run: memberRemoveCommandFunc,
  54. }
  55. return cc
  56. }
  57. // NewMemberUpdateCommand returns the cobra command for "member update".
  58. func NewMemberUpdateCommand() *cobra.Command {
  59. cc := &cobra.Command{
  60. Use: "update",
  61. Short: "update is used to update a member in the cluster",
  62. Run: memberUpdateCommandFunc,
  63. }
  64. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "comma separated peer URLs for the updated member.")
  65. return cc
  66. }
  67. // NewMemberListCommand returns the cobra command for "member list".
  68. func NewMemberListCommand() *cobra.Command {
  69. cc := &cobra.Command{
  70. Use: "list",
  71. Short: "list is used to list all members in the cluster",
  72. Run: memberListCommandFunc,
  73. }
  74. return cc
  75. }
  76. // memberAddCommandFunc executes the "member add" command.
  77. func memberAddCommandFunc(cmd *cobra.Command, args []string) {
  78. if len(args) != 1 {
  79. ExitWithError(ExitBadArgs, fmt.Errorf("member name not provided."))
  80. }
  81. if len(memberPeerURLs) == 0 {
  82. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  83. }
  84. urls := strings.Split(memberPeerURLs, ",")
  85. resp, err := mustClientFromCmd(cmd).MemberAdd(context.TODO(), urls)
  86. if err != nil {
  87. ExitWithError(ExitError, err)
  88. }
  89. fmt.Printf("Member %16x added to cluster %16x\n", resp.Member.ID, resp.Header.ClusterId)
  90. }
  91. // memberRemoveCommandFunc executes the "member remove" command.
  92. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  93. if len(args) != 1 {
  94. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  95. }
  96. id, err := strconv.ParseUint(args[0], 16, 64)
  97. if err != nil {
  98. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  99. }
  100. resp, err := mustClientFromCmd(cmd).MemberRemove(context.TODO(), id)
  101. if err != nil {
  102. ExitWithError(ExitError, err)
  103. }
  104. fmt.Printf("Member %16x removed from cluster %16x\n", id, resp.Header.ClusterId)
  105. }
  106. // memberUpdateCommandFunc executes the "member update" command.
  107. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  108. if len(args) != 1 {
  109. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  110. }
  111. id, err := strconv.ParseUint(args[0], 16, 64)
  112. if err != nil {
  113. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  114. }
  115. if len(memberPeerURLs) == 0 {
  116. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  117. }
  118. urls := strings.Split(memberPeerURLs, ",")
  119. resp, err := mustClientFromCmd(cmd).MemberUpdate(context.TODO(), id, urls)
  120. if err != nil {
  121. ExitWithError(ExitError, err)
  122. }
  123. fmt.Printf("Member %16x updated in cluster %16x\n", id, resp.Header.ClusterId)
  124. }
  125. // memberListCommandFunc executes the "member list" command.
  126. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  127. resp, err := mustClientFromCmd(cmd).MemberList(context.TODO())
  128. if err != nil {
  129. ExitWithError(ExitError, err)
  130. }
  131. // use https://github.com/olekukonko/tablewriter to print out a pretty table?
  132. for _, m := range resp.Members {
  133. if len(m.Name) == 0 {
  134. fmt.Printf("%16x[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  135. } else {
  136. fmt.Printf("%16x: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  137. }
  138. }
  139. }