member_command.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // TODO: enable grpc.WithTransportCredentials(creds)
  92. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  93. if err != nil {
  94. ExitWithError(ExitBadConnection, err)
  95. }
  96. mc := pb.NewClusterClient(conn)
  97. resp, err := mc.MemberAdd(context.TODO(), &pb.MemberAddRequest{PeerURLs: urls})
  98. if err != nil {
  99. ExitWithError(ExitError, err)
  100. }
  101. fmt.Printf("Member %16x added to cluster %16x\n", args[0], resp.Member.ID, resp.Header.ClusterId)
  102. }
  103. // memberRemoveCommandFunc executes the "member remove" command.
  104. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  105. if len(args) != 1 {
  106. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  107. }
  108. id, err := strconv.ParseUint(args[0], 16, 64)
  109. if err != nil {
  110. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  111. }
  112. endpoint, err := cmd.Flags().GetString("endpoint")
  113. if err != nil {
  114. ExitWithError(ExitError, err)
  115. }
  116. // TODO: enable grpc.WithTransportCredentials(creds)
  117. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  118. if err != nil {
  119. ExitWithError(ExitBadConnection, err)
  120. }
  121. mc := pb.NewClusterClient(conn)
  122. resp, err := mc.MemberRemove(context.TODO(), &pb.MemberRemoveRequest{ID: uint64(id)})
  123. if err != nil {
  124. ExitWithError(ExitError, err)
  125. }
  126. fmt.Printf("Member %16x removed from cluster %16x\n", id, resp.Header.ClusterId)
  127. }
  128. // memberUpdateCommandFunc executes the "member update" command.
  129. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  130. if len(args) != 1 {
  131. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  132. }
  133. id, err := strconv.ParseUint(args[0], 16, 64)
  134. if err != nil {
  135. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  136. }
  137. if len(memberPeerURLs) == 0 {
  138. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  139. }
  140. urls := strings.Split(memberPeerURLs, ",")
  141. endpoint, err := cmd.Flags().GetString("endpoint")
  142. if err != nil {
  143. ExitWithError(ExitError, err)
  144. }
  145. // TODO: enable grpc.WithTransportCredentials(creds)
  146. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  147. if err != nil {
  148. ExitWithError(ExitBadConnection, err)
  149. }
  150. mc := pb.NewClusterClient(conn)
  151. resp, err := mc.MemberUpdate(context.TODO(), &pb.MemberUpdateRequest{ID: uint64(id), PeerURLs: urls})
  152. if err != nil {
  153. ExitWithError(ExitError, err)
  154. }
  155. fmt.Printf("Member %16x updated in cluster %16x\n", id, resp.Header.ClusterId)
  156. }
  157. // memberListCommandFunc executes the "member list" command.
  158. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  159. endpoint, err := cmd.Flags().GetString("endpoint")
  160. if err != nil {
  161. ExitWithError(ExitError, err)
  162. }
  163. // TODO: enable grpc.WithTransportCredentials(creds)
  164. conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
  165. if err != nil {
  166. ExitWithError(ExitBadConnection, err)
  167. }
  168. mc := pb.NewClusterClient(conn)
  169. resp, err := mc.MemberList(context.TODO(), &pb.MemberListRequest{})
  170. if err != nil {
  171. ExitWithError(ExitError, err)
  172. }
  173. // use https://github.com/olekukonko/tablewriter to print out a pretty table?
  174. for _, m := range resp.Members {
  175. if len(m.Name) == 0 {
  176. fmt.Printf("%16x[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  177. } else {
  178. fmt.Printf("%16x: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  179. }
  180. }
  181. }