member_commands.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2015 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. "os"
  18. "strings"
  19. "github.com/urfave/cli"
  20. )
  21. func NewMemberCommand() cli.Command {
  22. return cli.Command{
  23. Name: "member",
  24. Usage: "member add, remove and list subcommands",
  25. Subcommands: []cli.Command{
  26. {
  27. Name: "list",
  28. Usage: "enumerate existing cluster members",
  29. ArgsUsage: " ",
  30. Action: actionMemberList,
  31. },
  32. {
  33. Name: "add",
  34. Usage: "add a new member to the etcd cluster",
  35. ArgsUsage: "<name> <peerURL>",
  36. Action: actionMemberAdd,
  37. },
  38. {
  39. Name: "remove",
  40. Usage: "remove an existing member from the etcd cluster",
  41. ArgsUsage: "<memberID>",
  42. Action: actionMemberRemove,
  43. },
  44. {
  45. Name: "update",
  46. Usage: "update an existing member in the etcd cluster",
  47. ArgsUsage: "<memberID> <peerURLs>",
  48. Action: actionMemberUpdate,
  49. },
  50. },
  51. }
  52. }
  53. func actionMemberList(c *cli.Context) error {
  54. if len(c.Args()) != 0 {
  55. fmt.Fprintln(os.Stderr, "No arguments accepted")
  56. os.Exit(1)
  57. }
  58. mAPI := mustNewMembersAPI(c)
  59. ctx, cancel := contextWithTotalTimeout(c)
  60. defer cancel()
  61. members, err := mAPI.List(ctx)
  62. if err != nil {
  63. fmt.Fprintln(os.Stderr, err.Error())
  64. os.Exit(1)
  65. }
  66. leader, err := mAPI.Leader(ctx)
  67. if err != nil {
  68. fmt.Fprintln(os.Stderr, "Failed to get leader: ", err)
  69. os.Exit(1)
  70. }
  71. for _, m := range members {
  72. isLeader := false
  73. if m.ID == leader.ID {
  74. isLeader = true
  75. }
  76. if len(m.Name) == 0 {
  77. fmt.Printf("%s[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  78. } else {
  79. fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s isLeader=%v\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","), isLeader)
  80. }
  81. }
  82. return nil
  83. }
  84. func actionMemberAdd(c *cli.Context) error {
  85. args := c.Args()
  86. if len(args) != 2 {
  87. fmt.Fprintln(os.Stderr, "Provide a name and a single member peerURL")
  88. os.Exit(1)
  89. }
  90. mAPI := mustNewMembersAPI(c)
  91. url := args[1]
  92. ctx, cancel := contextWithTotalTimeout(c)
  93. defer cancel()
  94. m, err := mAPI.Add(ctx, url)
  95. if err != nil {
  96. fmt.Fprintln(os.Stderr, err.Error())
  97. os.Exit(1)
  98. }
  99. newID := m.ID
  100. newName := args[0]
  101. fmt.Printf("Added member named %s with ID %s to cluster\n", newName, newID)
  102. members, err := mAPI.List(ctx)
  103. if err != nil {
  104. fmt.Fprintln(os.Stderr, err.Error())
  105. os.Exit(1)
  106. }
  107. conf := []string{}
  108. for _, memb := range members {
  109. for _, u := range memb.PeerURLs {
  110. n := memb.Name
  111. if memb.ID == newID {
  112. n = newName
  113. }
  114. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  115. }
  116. }
  117. fmt.Print("\n")
  118. fmt.Printf("ETCD_NAME=%q\n", newName)
  119. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  120. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  121. return nil
  122. }
  123. func actionMemberRemove(c *cli.Context) error {
  124. args := c.Args()
  125. if len(args) != 1 {
  126. fmt.Fprintln(os.Stderr, "Provide a single member ID")
  127. os.Exit(1)
  128. }
  129. removalID := args[0]
  130. mAPI := mustNewMembersAPI(c)
  131. ctx, cancel := contextWithTotalTimeout(c)
  132. defer cancel()
  133. // Get the list of members.
  134. members, err := mAPI.List(ctx)
  135. if err != nil {
  136. fmt.Fprintln(os.Stderr, "Error while verifying ID against known members:", err.Error())
  137. os.Exit(1)
  138. }
  139. // Sanity check the input.
  140. foundID := false
  141. for _, m := range members {
  142. if m.ID == removalID {
  143. foundID = true
  144. }
  145. if m.Name == removalID {
  146. // Note that, so long as it's not ambiguous, we *could* do the right thing by name here.
  147. fmt.Fprintf(os.Stderr, "Found a member named %s; if this is correct, please use its ID, eg:\n\tetcdctl member remove %s\n", m.Name, m.ID)
  148. fmt.Fprintf(os.Stderr, "For more details, read the documentation at https://github.com/etcd-io/etcd/blob/master/Documentation/runtime-configuration.md#remove-a-member\n\n")
  149. }
  150. }
  151. if !foundID {
  152. fmt.Fprintf(os.Stderr, "Couldn't find a member in the cluster with an ID of %s.\n", removalID)
  153. os.Exit(1)
  154. }
  155. // Actually attempt to remove the member.
  156. err = mAPI.Remove(ctx, removalID)
  157. if err != nil {
  158. fmt.Fprintf(os.Stderr, "Received an error trying to remove member %s: %s", removalID, err.Error())
  159. os.Exit(1)
  160. }
  161. fmt.Printf("Removed member %s from cluster\n", removalID)
  162. return nil
  163. }
  164. func actionMemberUpdate(c *cli.Context) error {
  165. args := c.Args()
  166. if len(args) != 2 {
  167. fmt.Fprintln(os.Stderr, "Provide an ID and a list of comma separated peerURL (0xabcd http://example.com,http://example1.com)")
  168. os.Exit(1)
  169. }
  170. mAPI := mustNewMembersAPI(c)
  171. mid := args[0]
  172. urls := args[1]
  173. ctx, cancel := contextWithTotalTimeout(c)
  174. err := mAPI.Update(ctx, mid, strings.Split(urls, ","))
  175. cancel()
  176. if err != nil {
  177. fmt.Fprintln(os.Stderr, err.Error())
  178. os.Exit(1)
  179. }
  180. fmt.Printf("Updated member with ID %s in cluster\n", mid)
  181. return nil
  182. }