cluster.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 main
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "net"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. etcdclient "github.com/coreos/etcd/client"
  23. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  24. )
  25. type cluster struct {
  26. agentEndpoints []string
  27. datadir string
  28. Size int
  29. Agents []client.Agent
  30. Names []string
  31. ClientURLs []string
  32. }
  33. // newCluster starts and returns a new cluster. The caller should call Terminate when finished, to shut it down.
  34. func newCluster(agentEndpoints []string, datadir string) (*cluster, error) {
  35. c := &cluster{
  36. agentEndpoints: agentEndpoints,
  37. datadir: datadir,
  38. }
  39. if err := c.Bootstrap(); err != nil {
  40. return nil, err
  41. }
  42. return c, nil
  43. }
  44. func (c *cluster) Bootstrap() error {
  45. size := len(c.agentEndpoints)
  46. agents := make([]client.Agent, size)
  47. names := make([]string, size)
  48. clientURLs := make([]string, size)
  49. peerURLs := make([]string, size)
  50. members := make([]string, size)
  51. for i, u := range c.agentEndpoints {
  52. var err error
  53. agents[i], err = client.NewAgent(u)
  54. if err != nil {
  55. return err
  56. }
  57. names[i] = fmt.Sprintf("etcd-%d", i)
  58. host, _, err := net.SplitHostPort(u)
  59. if err != nil {
  60. return err
  61. }
  62. clientURLs[i] = fmt.Sprintf("http://%s:2379", host)
  63. peerURLs[i] = fmt.Sprintf("http://%s:2380", host)
  64. members[i] = fmt.Sprintf("%s=%s", names[i], peerURLs[i])
  65. }
  66. clusterStr := strings.Join(members, ",")
  67. token := fmt.Sprint(rand.Int())
  68. for i, a := range agents {
  69. _, err := a.Start(
  70. "-name", names[i],
  71. "-data-dir", c.datadir,
  72. "-advertise-client-urls", clientURLs[i],
  73. "-listen-client-urls", clientURLs[i],
  74. "-initial-advertise-peer-urls", peerURLs[i],
  75. "-listen-peer-urls", peerURLs[i],
  76. "-initial-cluster-token", token,
  77. "-initial-cluster", clusterStr,
  78. "-initial-cluster-state", "new",
  79. )
  80. if err != nil {
  81. // cleanup
  82. for j := 0; j < i; j++ {
  83. agents[j].Terminate()
  84. }
  85. return err
  86. }
  87. }
  88. c.Size = size
  89. c.Agents = agents
  90. c.Names = names
  91. c.ClientURLs = clientURLs
  92. return nil
  93. }
  94. func (c *cluster) WaitHealth() error {
  95. var err error
  96. for i := 0; i < 10; i++ {
  97. err = setHealthKey(c.ClientURLs)
  98. if err == nil {
  99. return nil
  100. }
  101. time.Sleep(time.Second)
  102. }
  103. return err
  104. }
  105. func (c *cluster) Cleanup() error {
  106. for _, a := range c.Agents {
  107. if err := a.Cleanup(); err != nil {
  108. return err
  109. }
  110. }
  111. return nil
  112. }
  113. func (c *cluster) Terminate() {
  114. for _, a := range c.Agents {
  115. a.Terminate()
  116. }
  117. }
  118. // setHealthKey sets health key on all given urls.
  119. func setHealthKey(us []string) error {
  120. for _, u := range us {
  121. cfg := etcdclient.Config{
  122. Endpoints: []string{u},
  123. }
  124. c, err := etcdclient.New(cfg)
  125. if err != nil {
  126. return err
  127. }
  128. kapi := etcdclient.NewKeysAPI(c)
  129. _, err = kapi.Set(context.TODO(), "health", "good", nil)
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. return nil
  135. }