member_commands.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. hc, err := client.NewHTTPClient(tr, eps)
  58. if err != nil {
  59. fmt.Fprintln(os.Stderr, err.Error())
  60. os.Exit(1)
  61. }
  62. if !c.GlobalBool("no-sync") {
  63. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  64. err := hc.Sync(ctx)
  65. cancel()
  66. if err != nil {
  67. fmt.Fprintln(os.Stderr, err.Error())
  68. os.Exit(1)
  69. }
  70. }
  71. if c.GlobalBool("debug") {
  72. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  73. }
  74. return client.NewMembersAPI(hc)
  75. }
  76. func actionMemberList(c *cli.Context) {
  77. if len(c.Args()) != 0 {
  78. fmt.Fprintln(os.Stderr, "No arguments accepted")
  79. os.Exit(1)
  80. }
  81. mAPI := mustNewMembersAPI(c)
  82. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  83. members, err := mAPI.List(ctx)
  84. cancel()
  85. if err != nil {
  86. fmt.Fprintln(os.Stderr, err.Error())
  87. os.Exit(1)
  88. }
  89. for _, m := range members {
  90. fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
  91. }
  92. }
  93. func actionMemberAdd(c *cli.Context) {
  94. args := c.Args()
  95. if len(args) != 2 {
  96. fmt.Fprintln(os.Stderr, "Provide a name and a single member peerURL")
  97. os.Exit(1)
  98. }
  99. mAPI := mustNewMembersAPI(c)
  100. url := args[1]
  101. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  102. m, err := mAPI.Add(ctx, url)
  103. cancel()
  104. if err != nil {
  105. fmt.Fprintln(os.Stderr, err.Error())
  106. os.Exit(1)
  107. }
  108. newID := m.ID
  109. newName := args[0]
  110. fmt.Printf("Added member named %s with ID %s to cluster\n", newName, newID)
  111. ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  112. members, err := mAPI.List(ctx)
  113. cancel()
  114. if err != nil {
  115. fmt.Fprintln(os.Stderr, err.Error())
  116. os.Exit(1)
  117. }
  118. conf := []string{}
  119. for _, m := range members {
  120. for _, u := range m.PeerURLs {
  121. n := m.Name
  122. if m.ID == newID {
  123. n = newName
  124. }
  125. conf = append(conf, fmt.Sprintf("%s=%s", n, u))
  126. }
  127. }
  128. fmt.Print("\n")
  129. fmt.Printf("ETCD_NAME=%q\n", newName)
  130. fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
  131. fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
  132. }
  133. func actionMemberRemove(c *cli.Context) {
  134. args := c.Args()
  135. if len(args) != 1 {
  136. fmt.Fprintln(os.Stderr, "Provide a single member ID")
  137. os.Exit(1)
  138. }
  139. mAPI := mustNewMembersAPI(c)
  140. mID := args[0]
  141. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  142. err := mAPI.Remove(ctx, mID)
  143. cancel()
  144. if err != nil {
  145. fmt.Fprintln(os.Stderr, err.Error())
  146. os.Exit(1)
  147. }
  148. fmt.Printf("Removed member %s from cluster\n", mID)
  149. }