member_commands.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. cli.Command{
  29. Name: "list",
  30. Usage: "enumerate existing cluster members",
  31. Action: actionMemberList,
  32. },
  33. cli.Command{
  34. Name: "add",
  35. Usage: "add a new member to the etcd cluster",
  36. Action: actionMemberAdd,
  37. },
  38. cli.Command{
  39. Name: "remove",
  40. Usage: "remove an existing member from the etcd cluster",
  41. Action: actionMemberRemove,
  42. },
  43. },
  44. }
  45. }
  46. func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
  47. eps, err := getEndpoints(c)
  48. if err != nil {
  49. fmt.Fprintln(os.Stderr, err.Error())
  50. os.Exit(1)
  51. }
  52. tr, err := getTransport(c)
  53. if err != nil {
  54. fmt.Fprintln(os.Stderr, err.Error())
  55. os.Exit(1)
  56. }
  57. cfg := client.Config{
  58. Transport: tr,
  59. Endpoints: eps,
  60. }
  61. hc, err := client.New(cfg)
  62. if err != nil {
  63. fmt.Fprintln(os.Stderr, err.Error())
  64. os.Exit(1)
  65. }
  66. if !c.GlobalBool("no-sync") {
  67. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  68. err := hc.Sync(ctx)
  69. cancel()
  70. if err != nil {
  71. fmt.Fprintln(os.Stderr, err.Error())
  72. os.Exit(1)
  73. }
  74. }
  75. if c.GlobalBool("debug") {
  76. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  77. }
  78. return client.NewMembersAPI(hc)
  79. }
  80. func actionMemberList(c *cli.Context) {
  81. if len(c.Args()) != 0 {
  82. fmt.Fprintln(os.Stderr, "No arguments accepted")
  83. os.Exit(1)
  84. }
  85. mAPI := mustNewMembersAPI(c)
  86. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  87. members, err := mAPI.List(ctx)
  88. cancel()
  89. if err != nil {
  90. fmt.Fprintln(os.Stderr, err.Error())
  91. os.Exit(1)
  92. }
  93. for _, m := range members {
  94. if len(m.Name) == 0 {
  95. fmt.Printf("%s[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  96. } else {
  97. fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  98. }
  99. }
  100. }
  101. func actionMemberAdd(c *cli.Context) {
  102. args := c.Args()
  103. if len(args) != 2 {
  104. fmt.Fprintln(os.Stderr, "Provide a name and a single member peerURL")
  105. os.Exit(1)
  106. }
  107. mAPI := mustNewMembersAPI(c)
  108. url := args[1]
  109. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  110. m, err := mAPI.Add(ctx, url)
  111. cancel()
  112. if err != nil {
  113. fmt.Fprintln(os.Stderr, err.Error())
  114. os.Exit(1)
  115. }
  116. newID := m.ID
  117. newName := args[0]
  118. fmt.Printf("Added member named %s with ID %s to cluster\n", newName, newID)
  119. ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  120. members, err := mAPI.List(ctx)
  121. cancel()
  122. if err != nil {
  123. fmt.Fprintln(os.Stderr, err.Error())
  124. os.Exit(1)
  125. }
  126. conf := []string{}
  127. for _, memb := range members {
  128. for _, u := range memb.PeerURLs {
  129. n := memb.Name
  130. if memb.ID == newID {
  131. n = newName
  132. }
  133. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  134. }
  135. }
  136. fmt.Print("\n")
  137. fmt.Printf("ETCD_NAME=%q\n", newName)
  138. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  139. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  140. }
  141. func actionMemberRemove(c *cli.Context) {
  142. args := c.Args()
  143. if len(args) != 1 {
  144. fmt.Fprintln(os.Stderr, "Provide a single member ID")
  145. os.Exit(1)
  146. }
  147. removalID := args[0]
  148. mAPI := mustNewMembersAPI(c)
  149. // Get the list of members.
  150. listctx, listCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  151. members, err := mAPI.List(listctx)
  152. listCancel()
  153. if err != nil {
  154. fmt.Fprintln(os.Stderr, "Error while verifying ID against known members:", err.Error())
  155. os.Exit(1)
  156. }
  157. // Sanity check the input.
  158. foundID := false
  159. for _, m := range members {
  160. if m.ID == removalID {
  161. foundID = true
  162. }
  163. if m.Name == removalID {
  164. // Note that, so long as it's not ambiguous, we *could* do the right thing by name here.
  165. 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)
  166. 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")
  167. }
  168. }
  169. if !foundID {
  170. fmt.Fprintf(os.Stderr, "Couldn't find a member in the cluster with an ID of %s.\n", removalID)
  171. os.Exit(1)
  172. }
  173. // Actually attempt to remove the member.
  174. ctx, removeCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  175. err = mAPI.Remove(ctx, removalID)
  176. removeCancel()
  177. if err != nil {
  178. fmt.Fprintf(os.Stderr, "Recieved an error trying to remove member %s: %s", removalID, err.Error())
  179. os.Exit(1)
  180. }
  181. fmt.Printf("Removed member %s from cluster\n", removalID)
  182. }