member_command.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. resp, err := mustClientFromCmd(cmd).MemberAdd(ctx, urls)
  87. cancel()
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. newID := resp.Member.ID
  92. display.MemberAdd(*resp)
  93. if _, ok := (display).(*simplePrinter); ok {
  94. ctx, cancel = commandCtx(cmd)
  95. listResp, err := mustClientFromCmd(cmd).MemberList(ctx)
  96. cancel()
  97. if err != nil {
  98. ExitWithError(ExitError, err)
  99. }
  100. conf := []string{}
  101. for _, memb := range listResp.Members {
  102. for _, u := range memb.PeerURLs {
  103. n := memb.Name
  104. if memb.ID == newID {
  105. n = newMemberName
  106. }
  107. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  108. }
  109. }
  110. fmt.Print("\n")
  111. fmt.Printf("ETCD_NAME=%q\n", newMemberName)
  112. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  113. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  114. }
  115. }
  116. // memberRemoveCommandFunc executes the "member remove" command.
  117. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  118. if len(args) != 1 {
  119. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  120. }
  121. id, err := strconv.ParseUint(args[0], 16, 64)
  122. if err != nil {
  123. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  124. }
  125. ctx, cancel := commandCtx(cmd)
  126. resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
  127. cancel()
  128. if err != nil {
  129. ExitWithError(ExitError, err)
  130. }
  131. display.MemberRemove(id, *resp)
  132. }
  133. // memberUpdateCommandFunc executes the "member update" command.
  134. func memberUpdateCommandFunc(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. if len(memberPeerURLs) == 0 {
  143. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  144. }
  145. urls := strings.Split(memberPeerURLs, ",")
  146. ctx, cancel := commandCtx(cmd)
  147. resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
  148. cancel()
  149. if err != nil {
  150. ExitWithError(ExitError, err)
  151. }
  152. display.MemberUpdate(id, *resp)
  153. }
  154. // memberListCommandFunc executes the "member list" command.
  155. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  156. ctx, cancel := commandCtx(cmd)
  157. resp, err := mustClientFromCmd(cmd).MemberList(ctx)
  158. cancel()
  159. if err != nil {
  160. ExitWithError(ExitError, err)
  161. }
  162. display.MemberList(*resp)
  163. }