member_command.go 5.2 KB

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