member_command.go 6.1 KB

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