member_command.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // get latest member list; if there's failover new member might have outdated list
  98. for {
  99. if err != nil {
  100. ExitWithError(ExitError, err)
  101. }
  102. if listResp.Header.MemberId == resp.Header.MemberId {
  103. break
  104. }
  105. // quorum get to sync cluster list
  106. gresp, gerr := cli.Get(ctx, "_")
  107. if gerr != nil {
  108. ExitWithError(ExitError, err)
  109. }
  110. resp.Header.MemberId = gresp.Header.MemberId
  111. listResp, err = cli.MemberList(ctx)
  112. }
  113. cancel()
  114. conf := []string{}
  115. for _, memb := range listResp.Members {
  116. for _, u := range memb.PeerURLs {
  117. n := memb.Name
  118. if memb.ID == newID {
  119. n = newMemberName
  120. }
  121. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  122. }
  123. }
  124. fmt.Print("\n")
  125. fmt.Printf("ETCD_NAME=%q\n", newMemberName)
  126. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  127. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  128. }
  129. }
  130. // memberRemoveCommandFunc executes the "member remove" command.
  131. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  132. if len(args) != 1 {
  133. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  134. }
  135. id, err := strconv.ParseUint(args[0], 16, 64)
  136. if err != nil {
  137. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  138. }
  139. ctx, cancel := commandCtx(cmd)
  140. resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
  141. cancel()
  142. if err != nil {
  143. ExitWithError(ExitError, err)
  144. }
  145. display.MemberRemove(id, *resp)
  146. }
  147. // memberUpdateCommandFunc executes the "member update" command.
  148. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  149. if len(args) != 1 {
  150. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  151. }
  152. id, err := strconv.ParseUint(args[0], 16, 64)
  153. if err != nil {
  154. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  155. }
  156. if len(memberPeerURLs) == 0 {
  157. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  158. }
  159. urls := strings.Split(memberPeerURLs, ",")
  160. ctx, cancel := commandCtx(cmd)
  161. resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
  162. cancel()
  163. if err != nil {
  164. ExitWithError(ExitError, err)
  165. }
  166. display.MemberUpdate(id, *resp)
  167. }
  168. // memberListCommandFunc executes the "member list" command.
  169. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  170. ctx, cancel := commandCtx(cmd)
  171. resp, err := mustClientFromCmd(cmd).MemberList(ctx)
  172. cancel()
  173. if err != nil {
  174. ExitWithError(ExitError, err)
  175. }
  176. display.MemberList(*resp)
  177. }