member_command.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. )
  24. var (
  25. memberID uint64
  26. memberPeerURLs string
  27. )
  28. // NewMemberCommand returns the cobra command for "member".
  29. func NewMemberCommand() *cobra.Command {
  30. mc := &cobra.Command{
  31. Use: "member",
  32. Short: "member is used to manage membership in an etcd cluster.",
  33. }
  34. mc.AddCommand(NewMemberAddCommand())
  35. mc.AddCommand(NewMemberRemoveCommand())
  36. mc.AddCommand(NewMemberUpdateCommand())
  37. mc.AddCommand(NewMemberListCommand())
  38. return mc
  39. }
  40. // NewMemberAddCommand returns the cobra command for "member add".
  41. func NewMemberAddCommand() *cobra.Command {
  42. cc := &cobra.Command{
  43. Use: "add",
  44. Short: "add is used to add a member into the cluster",
  45. Run: memberAddCommandFunc,
  46. }
  47. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "comma separated peer URLs for the new member.")
  48. return cc
  49. }
  50. // NewMemberRemoveCommand returns the cobra command for "member remove".
  51. func NewMemberRemoveCommand() *cobra.Command {
  52. cc := &cobra.Command{
  53. Use: "remove",
  54. Short: "remove is used to remove a member from the cluster",
  55. Run: memberRemoveCommandFunc,
  56. }
  57. return cc
  58. }
  59. // NewMemberUpdateCommand returns the cobra command for "member update".
  60. func NewMemberUpdateCommand() *cobra.Command {
  61. cc := &cobra.Command{
  62. Use: "update",
  63. Short: "update is used to update a member in the cluster",
  64. Run: memberUpdateCommandFunc,
  65. }
  66. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "comma separated peer URLs for the updated member.")
  67. return cc
  68. }
  69. // NewMemberListCommand returns the cobra command for "member list".
  70. func NewMemberListCommand() *cobra.Command {
  71. cc := &cobra.Command{
  72. Use: "list",
  73. Short: "list is used to list all members in the cluster",
  74. Run: memberListCommandFunc,
  75. }
  76. return cc
  77. }
  78. // memberAddCommandFunc executes the "member add" command.
  79. func memberAddCommandFunc(cmd *cobra.Command, args []string) {
  80. if len(args) != 1 {
  81. ExitWithError(ExitBadArgs, fmt.Errorf("member name not provided."))
  82. }
  83. if len(memberPeerURLs) == 0 {
  84. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  85. }
  86. urls := strings.Split(memberPeerURLs, ",")
  87. endpoint, err := cmd.Flags().GetString("endpoint")
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. conn, err := grpc.Dial(endpoint)
  92. if err != nil {
  93. ExitWithError(ExitBadConnection, err)
  94. }
  95. mc := pb.NewClusterClient(conn)
  96. resp, err := mc.MemberAdd(context.TODO(), &pb.MemberAddRequest{PeerURLs: urls})
  97. if err != nil {
  98. ExitWithError(ExitError, err)
  99. }
  100. fmt.Printf("Member %16x added to cluster %16x\n", args[0], resp.Member.ID, resp.Header.ClusterId)
  101. }
  102. // memberRemoveCommandFunc executes the "member remove" command.
  103. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  104. if len(args) != 1 {
  105. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  106. }
  107. id, err := strconv.ParseUint(args[0], 16, 64)
  108. if err != nil {
  109. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  110. }
  111. endpoint, err := cmd.Flags().GetString("endpoint")
  112. if err != nil {
  113. ExitWithError(ExitError, err)
  114. }
  115. conn, err := grpc.Dial(endpoint)
  116. if err != nil {
  117. ExitWithError(ExitBadConnection, err)
  118. }
  119. mc := pb.NewClusterClient(conn)
  120. resp, err := mc.MemberRemove(context.TODO(), &pb.MemberRemoveRequest{ID: uint64(id)})
  121. if err != nil {
  122. ExitWithError(ExitError, err)
  123. }
  124. fmt.Printf("Member %16x removed from cluster %16x\n", id, resp.Header.ClusterId)
  125. }
  126. // memberUpdateCommandFunc executes the "member update" command.
  127. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  128. if len(args) != 1 {
  129. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  130. }
  131. id, err := strconv.ParseUint(args[0], 16, 64)
  132. if err != nil {
  133. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  134. }
  135. if len(memberPeerURLs) == 0 {
  136. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  137. }
  138. urls := strings.Split(memberPeerURLs, ",")
  139. endpoint, err := cmd.Flags().GetString("endpoint")
  140. if err != nil {
  141. ExitWithError(ExitError, err)
  142. }
  143. conn, err := grpc.Dial(endpoint)
  144. if err != nil {
  145. ExitWithError(ExitBadConnection, err)
  146. }
  147. mc := pb.NewClusterClient(conn)
  148. resp, err := mc.MemberUpdate(context.TODO(), &pb.MemberUpdateRequest{ID: uint64(id), PeerURLs: urls})
  149. if err != nil {
  150. ExitWithError(ExitError, err)
  151. }
  152. fmt.Printf("Member %16x updated in cluster %16x\n", id, resp.Header.ClusterId)
  153. }
  154. // memberListCommandFunc executes the "member list" command.
  155. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  156. endpoint, err := cmd.Flags().GetString("endpoint")
  157. if err != nil {
  158. ExitWithError(ExitError, err)
  159. }
  160. conn, err := grpc.Dial(endpoint)
  161. if err != nil {
  162. ExitWithError(ExitBadConnection, err)
  163. }
  164. mc := pb.NewClusterClient(conn)
  165. resp, err := mc.MemberList(context.TODO(), &pb.MemberListRequest{})
  166. if err != nil {
  167. ExitWithError(ExitError, err)
  168. }
  169. // use https://github.com/olekukonko/tablewriter to print out a pretty table?
  170. for _, m := range resp.Members {
  171. if len(m.Name) == 0 {
  172. fmt.Printf("%16x[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  173. } else {
  174. fmt.Printf("%16x: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  175. }
  176. }
  177. }