machines.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. // machineNum returns the number of machines in the cluster
  15. func machineNum() int {
  16. response, _ := etcdStore.RawGet("_etcd/machines")
  17. return len(response)
  18. }
  19. // getMachines gets the current machines in the cluster
  20. func getMachines(toURL func(string) (string, bool)) []string {
  21. peers := r.Peers()
  22. machines := make([]string, len(peers)+1)
  23. leader, ok := toURL(r.Leader())
  24. self, _ := toURL(r.Name())
  25. i := 1
  26. if ok {
  27. machines[0] = leader
  28. if leader != self {
  29. machines[1] = self
  30. i = 2
  31. }
  32. } else {
  33. machines[0] = self
  34. }
  35. // Add all peers to the slice
  36. for peerName, _ := range peers {
  37. if machine, ok := toURL(peerName); ok {
  38. // do not add leader twice
  39. if machine != leader {
  40. machines[i] = machine
  41. i++
  42. }
  43. }
  44. }
  45. return machines
  46. }