member_commands.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 actionMemberList(c *cli.Context) {
  47. if len(c.Args()) != 0 {
  48. fmt.Fprintln(os.Stderr, "No arguments accepted")
  49. os.Exit(1)
  50. }
  51. mAPI := mustNewMembersAPI(c)
  52. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  53. members, err := mAPI.List(ctx)
  54. cancel()
  55. if err != nil {
  56. fmt.Fprintln(os.Stderr, err.Error())
  57. os.Exit(1)
  58. }
  59. for _, m := range members {
  60. if len(m.Name) == 0 {
  61. fmt.Printf("%s[unstarted]: peerURLs=%s\n", m.ID, strings.Join(m.PeerURLs, ","))
  62. } else {
  63. fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  64. }
  65. }
  66. }
  67. func actionMemberAdd(c *cli.Context) {
  68. args := c.Args()
  69. if len(args) != 2 {
  70. fmt.Fprintln(os.Stderr, "Provide a name and a single member peerURL")
  71. os.Exit(1)
  72. }
  73. mAPI := mustNewMembersAPI(c)
  74. url := args[1]
  75. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  76. m, err := mAPI.Add(ctx, url)
  77. cancel()
  78. if err != nil {
  79. fmt.Fprintln(os.Stderr, err.Error())
  80. os.Exit(1)
  81. }
  82. newID := m.ID
  83. newName := args[0]
  84. fmt.Printf("Added member named %s with ID %s to cluster\n", newName, newID)
  85. ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  86. members, err := mAPI.List(ctx)
  87. cancel()
  88. if err != nil {
  89. fmt.Fprintln(os.Stderr, err.Error())
  90. os.Exit(1)
  91. }
  92. conf := []string{}
  93. for _, memb := range members {
  94. for _, u := range memb.PeerURLs {
  95. n := memb.Name
  96. if memb.ID == newID {
  97. n = newName
  98. }
  99. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  100. }
  101. }
  102. fmt.Print("\n")
  103. fmt.Printf("ETCD_NAME=%q\n", newName)
  104. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  105. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  106. }
  107. func actionMemberRemove(c *cli.Context) {
  108. args := c.Args()
  109. if len(args) != 1 {
  110. fmt.Fprintln(os.Stderr, "Provide a single member ID")
  111. os.Exit(1)
  112. }
  113. removalID := args[0]
  114. mAPI := mustNewMembersAPI(c)
  115. // Get the list of members.
  116. listctx, listCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  117. members, err := mAPI.List(listctx)
  118. listCancel()
  119. if err != nil {
  120. fmt.Fprintln(os.Stderr, "Error while verifying ID against known members:", err.Error())
  121. os.Exit(1)
  122. }
  123. // Sanity check the input.
  124. foundID := false
  125. for _, m := range members {
  126. if m.ID == removalID {
  127. foundID = true
  128. }
  129. if m.Name == removalID {
  130. // Note that, so long as it's not ambiguous, we *could* do the right thing by name here.
  131. 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)
  132. 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")
  133. }
  134. }
  135. if !foundID {
  136. fmt.Fprintf(os.Stderr, "Couldn't find a member in the cluster with an ID of %s.\n", removalID)
  137. os.Exit(1)
  138. }
  139. // Actually attempt to remove the member.
  140. ctx, removeCancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  141. err = mAPI.Remove(ctx, removalID)
  142. removeCancel()
  143. if err != nil {
  144. fmt.Fprintf(os.Stderr, "Recieved an error trying to remove member %s: %s", removalID, err.Error())
  145. os.Exit(1)
  146. }
  147. fmt.Printf("Removed member %s from cluster\n", removalID)
  148. }