member_command.go 6.0 KB

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