member_commands.go 5.3 KB

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