member_command.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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",
  26. Short: "member is used to manage membership in an etcd cluster.",
  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>",
  38. Short: "add is used to add a member into the cluster",
  39. Run: memberAddCommandFunc,
  40. }
  41. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "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: "remove is used to remove 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>",
  57. Short: "update is used to update a member in the cluster",
  58. Run: memberUpdateCommandFunc,
  59. }
  60. cc.Flags().StringVar(&memberPeerURLs, "peerURLs", "", "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: "list is used to list 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. if len(memberPeerURLs) == 0 {
  81. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  82. }
  83. urls := strings.Split(memberPeerURLs, ",")
  84. ctx, cancel := commandCtx(cmd)
  85. resp, err := mustClientFromCmd(cmd).MemberAdd(ctx, urls)
  86. cancel()
  87. if err != nil {
  88. ExitWithError(ExitError, err)
  89. }
  90. fmt.Printf("Member %16x added to cluster %16x\n", resp.Member.ID, resp.Header.ClusterId)
  91. }
  92. // memberRemoveCommandFunc executes the "member remove" command.
  93. func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
  94. if len(args) != 1 {
  95. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  96. }
  97. id, err := strconv.ParseUint(args[0], 16, 64)
  98. if err != nil {
  99. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  100. }
  101. ctx, cancel := commandCtx(cmd)
  102. resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
  103. cancel()
  104. if err != nil {
  105. ExitWithError(ExitError, err)
  106. }
  107. fmt.Printf("Member %16x removed from cluster %16x\n", id, resp.Header.ClusterId)
  108. }
  109. // memberUpdateCommandFunc executes the "member update" command.
  110. func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
  111. if len(args) != 1 {
  112. ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
  113. }
  114. id, err := strconv.ParseUint(args[0], 16, 64)
  115. if err != nil {
  116. ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
  117. }
  118. if len(memberPeerURLs) == 0 {
  119. ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
  120. }
  121. urls := strings.Split(memberPeerURLs, ",")
  122. ctx, cancel := commandCtx(cmd)
  123. resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
  124. cancel()
  125. if err != nil {
  126. ExitWithError(ExitError, err)
  127. }
  128. fmt.Printf("Member %16x updated in cluster %16x\n", id, resp.Header.ClusterId)
  129. }
  130. // memberListCommandFunc executes the "member list" command.
  131. func memberListCommandFunc(cmd *cobra.Command, args []string) {
  132. ctx, cancel := commandCtx(cmd)
  133. resp, err := mustClientFromCmd(cmd).MemberList(ctx)
  134. cancel()
  135. if err != nil {
  136. ExitWithError(ExitError, err)
  137. }
  138. display.MemberList(*resp)
  139. }