member_command.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2016 The etcd Authors
  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. "errors"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "github.com/spf13/cobra"
  21. "go.etcd.io/etcd/v3/clientv3"
  22. )
  23. var (
  24. memberPeerURLs string
  25. isLearner bool
  26. )
  27. // NewMemberCommand returns the cobra command for "member".
  28. func NewMemberCommand() *cobra.Command {
  29. mc := &cobra.Command{
  30. Use: "member <subcommand>",
  31. Short: "Membership related commands",
  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 <memberName> [options]",
  43. Short: "Adds a member into the cluster",
  44. Run: memberAddCommandFunc,
  45. }
  46. cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "comma separated peer URLs for the new member.")
  47. cc.Flags().BoolVar(&isLearner, "learner", false, "indicates if the new member is raft learner")
  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 <memberID>",
  54. Short: "Removes 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 <memberID> [options]",
  63. Short: "Updates a member in the cluster",
  64. Run: memberUpdateCommandFunc,
  65. }
  66. cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "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: "Lists all members in the cluster",
  74. Long: `When --write-out is set to simple, this command prints out comma-separated member lists for each endpoint.
  75. The items in the lists are ID, Status, Name, Peer Addrs, Client Addrs, Is Learner.
  76. `,
  77. Run: memberListCommandFunc,
  78. }
  79. return cc
  80. }
  81. // memberAddCommandFunc executes the "member add" command.
  82. func memberAddCommandFunc(cmd *cobra.Command, args []string) {
  83. if len(args) < 1 {
  84. ExitWithError(ExitBadArgs, errors.New("member name not provided"))
  85. }
  86. if len(args) > 1 {
  87. ev := "too many arguments"
  88. for _, s := range args {
  89. if strings.HasPrefix(strings.ToLower(s), "http") {
  90. ev += fmt.Sprintf(`, did you mean --peer-urls=%s`, s)
  91. }
  92. }
  93. ExitWithError(ExitBadArgs, errors.New(ev))
  94. }
  95. newMemberName := args[0]
  96. if len(memberPeerURLs) == 0 {
  97. ExitWithError(ExitBadArgs, errors.New("member peer urls not provided"))
  98. }
  99. urls := strings.Split(memberPeerURLs, ",")
  100. ctx, cancel := commandCtx(cmd)
  101. cli := mustClientFromCmd(cmd)
  102. var (
  103. resp *clientv3.MemberAddResponse
  104. err error
  105. )
  106. if isLearner {
  107. resp, err = cli.MemberAddAsLearner(ctx, urls)
  108. } else {
  109. resp, err = cli.MemberAdd(ctx, urls)
  110. }
  111. cancel()
  112. if err != nil {
  113. ExitWithError(ExitError, err)
  114. }
  115. newID := resp.Member.ID
  116. display.MemberAdd(*resp)
  117. if _, ok := (display).(*simplePrinter); ok {
  118. ctx, cancel = commandCtx(cmd)
  119. listResp, err := cli.MemberList(ctx)
  120. // get latest member list; if there's failover new member might have outdated list
  121. for {
  122. if err != nil {
  123. ExitWithError(ExitError, err)
  124. }
  125. if listResp.Header.MemberId == resp.Header.MemberId {
  126. break
  127. }
  128. // quorum get to sync cluster list
  129. gresp, gerr := cli.Get(ctx, "_")
  130. if gerr != nil {
  131. ExitWithError(ExitError, err)
  132. }
  133. resp.Header.MemberId = gresp.Header.MemberId
  134. listResp, err = cli.MemberList(ctx)
  135. }
  136. cancel()
  137. conf := []string{}
  138. for _, memb := range listResp.Members {
  139. for _, u := range memb.PeerURLs {
  140. n := memb.Name
  141. if memb.ID == newID {
  142. n = newMemberName
  143. }
  144. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  145. }
  146. }
  147. fmt.Print("\n")
  148. fmt.Printf("ETCD_NAME=%q\n", newMemberName)
  149. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  150. fmt.Printf("ETCD_INITIAL_ADVERTISE_PEER_URLS=%q\n", memberPeerURLs)
  151. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  152. }
  153. }
  154. // memberRemoveCommandFunc executes the "member remove" command.
  155. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  156. if len(args) != 1 {
  157. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  158. }
  159. id, err := strconv.ParseUint(args[0], 16, 64)
  160. if err != nil {
  161. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  162. }
  163. ctx, cancel := commandCtx(cmd)
  164. resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
  165. cancel()
  166. if err != nil {
  167. ExitWithError(ExitError, err)
  168. }
  169. display.MemberRemove(id, *resp)
  170. }
  171. // memberUpdateCommandFunc executes the "member update" command.
  172. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  173. if len(args) != 1 {
  174. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  175. }
  176. id, err := strconv.ParseUint(args[0], 16, 64)
  177. if err != nil {
  178. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  179. }
  180. if len(memberPeerURLs) == 0 {
  181. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided"))
  182. }
  183. urls := strings.Split(memberPeerURLs, ",")
  184. ctx, cancel := commandCtx(cmd)
  185. resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
  186. cancel()
  187. if err != nil {
  188. ExitWithError(ExitError, err)
  189. }
  190. display.MemberUpdate(id, *resp)
  191. }
  192. // memberListCommandFunc executes the "member list" command.
  193. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  194. ctx, cancel := commandCtx(cmd)
  195. resp, err := mustClientFromCmd(cmd).MemberList(ctx)
  196. cancel()
  197. if err != nil {
  198. ExitWithError(ExitError, err)
  199. }
  200. display.MemberList(*resp)
  201. }