member_command.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/spf13/cobra"
  20. )
  21. var memberPeerURLs string
  22. // NewMemberCommand returns the cobra command for "member".
  23. func NewMemberCommand() *cobra.Command {
  24. mc := &cobra.Command{
  25. Use: "member <subcommand>",
  26. Short: "Membership related commands",
  27. }
  28. mc.AddCommand(NewMemberAddCommand())
  29. mc.AddCommand(NewMemberRemoveCommand())
  30. mc.AddCommand(NewMemberUpdateCommand())
  31. mc.AddCommand(NewMemberListCommand())
  32. return mc
  33. }
  34. // NewMemberAddCommand returns the cobra command for "member add".
  35. func NewMemberAddCommand() *cobra.Command {
  36. cc := &cobra.Command{
  37. Use: "add <memberName> [options]",
  38. Short: "Adds a member into the cluster",
  39. Run: memberAddCommandFunc,
  40. }
  41. cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "comma separated peer URLs for the new member.")
  42. return cc
  43. }
  44. // NewMemberRemoveCommand returns the cobra command for "member remove".
  45. func NewMemberRemoveCommand() *cobra.Command {
  46. cc := &cobra.Command{
  47. Use: "remove <memberID>",
  48. Short: "Removes a member from the cluster",
  49. Run: memberRemoveCommandFunc,
  50. }
  51. return cc
  52. }
  53. // NewMemberUpdateCommand returns the cobra command for "member update".
  54. func NewMemberUpdateCommand() *cobra.Command {
  55. cc := &cobra.Command{
  56. Use: "update <memberID> [options]",
  57. Short: "Updates a member in the cluster",
  58. Run: memberUpdateCommandFunc,
  59. }
  60. cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "comma separated peer URLs for the updated member.")
  61. return cc
  62. }
  63. // NewMemberListCommand returns the cobra command for "member list".
  64. func NewMemberListCommand() *cobra.Command {
  65. cc := &cobra.Command{
  66. Use: "list",
  67. Short: "Lists all members in the cluster",
  68. Long: `When --write-out is set to simple, this command prints out comma-separated member lists for each endpoint.
  69. The items in the lists are ID, Status, Name, Peer Addrs, Client Addrs.
  70. `,
  71. Run: memberListCommandFunc,
  72. }
  73. return cc
  74. }
  75. // memberAddCommandFunc executes the "member add" command.
  76. func memberAddCommandFunc(cmd *cobra.Command, args []string) {
  77. if len(args) != 1 {
  78. ExitWithError(ExitBadArgs, fmt.Errorf("member name not provided."))
  79. }
  80. newMemberName := args[0]
  81. if len(memberPeerURLs) == 0 {
  82. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  83. }
  84. urls := strings.Split(memberPeerURLs, ",")
  85. ctx, cancel := commandCtx(cmd)
  86. cli := mustClientFromCmd(cmd)
  87. resp, err := cli.MemberAdd(ctx, urls)
  88. cancel()
  89. if err != nil {
  90. ExitWithError(ExitError, err)
  91. }
  92. newID := resp.Member.ID
  93. display.MemberAdd(*resp)
  94. if _, ok := (display).(*simplePrinter); ok {
  95. ctx, cancel = commandCtx(cmd)
  96. listResp, err := cli.MemberList(ctx)
  97. // make sure the member who served member list request has the latest member list.
  98. syncedMemberSet := make(map[uint64]struct{})
  99. syncedMemberSet[resp.Header.MemberId] = struct{}{} // the member who served member add is guaranteed to have the latest member list.
  100. for {
  101. if err != nil {
  102. ExitWithError(ExitError, err)
  103. }
  104. if _, ok := syncedMemberSet[listResp.Header.MemberId]; ok {
  105. break
  106. }
  107. // quorum get to sync cluster list
  108. gresp, gerr := cli.Get(ctx, "_")
  109. if gerr != nil {
  110. ExitWithError(ExitError, err)
  111. }
  112. syncedMemberSet[gresp.Header.MemberId] = struct{}{}
  113. listResp, err = cli.MemberList(ctx)
  114. }
  115. cancel()
  116. conf := []string{}
  117. for _, memb := range listResp.Members {
  118. for _, u := range memb.PeerURLs {
  119. n := memb.Name
  120. if memb.ID == newID {
  121. n = newMemberName
  122. }
  123. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  124. }
  125. }
  126. fmt.Print("\n")
  127. fmt.Printf("ETCD_NAME=%q\n", newMemberName)
  128. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  129. fmt.Printf("ETCD_INITIAL_ADVERTISE_PEER_URLS=%q\n", memberPeerURLs)
  130. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  131. }
  132. }
  133. // memberRemoveCommandFunc executes the "member remove" command.
  134. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  135. if len(args) != 1 {
  136. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  137. }
  138. id, err := strconv.ParseUint(args[0], 16, 64)
  139. if err != nil {
  140. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  141. }
  142. ctx, cancel := commandCtx(cmd)
  143. resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
  144. cancel()
  145. if err != nil {
  146. ExitWithError(ExitError, err)
  147. }
  148. display.MemberRemove(id, *resp)
  149. }
  150. // memberUpdateCommandFunc executes the "member update" command.
  151. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  152. if len(args) != 1 {
  153. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  154. }
  155. id, err := strconv.ParseUint(args[0], 16, 64)
  156. if err != nil {
  157. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  158. }
  159. if len(memberPeerURLs) == 0 {
  160. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  161. }
  162. urls := strings.Split(memberPeerURLs, ",")
  163. ctx, cancel := commandCtx(cmd)
  164. resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
  165. cancel()
  166. if err != nil {
  167. ExitWithError(ExitError, err)
  168. }
  169. display.MemberUpdate(id, *resp)
  170. }
  171. // memberListCommandFunc executes the "member list" command.
  172. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  173. ctx, cancel := commandCtx(cmd)
  174. resp, err := mustClientFromCmd(cmd).MemberList(ctx)
  175. cancel()
  176. if err != nil {
  177. ExitWithError(ExitError, err)
  178. }
  179. display.MemberList(*resp)
  180. }