standby.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 migrate
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "os"
  20. )
  21. type StandbyInfo4 struct {
  22. Running bool
  23. Cluster []*MachineMessage
  24. SyncInterval float64
  25. }
  26. // MachineMessage represents information about a peer or standby in the registry.
  27. type MachineMessage struct {
  28. Name string `json:"name"`
  29. State string `json:"state"`
  30. ClientURL string `json:"clientURL"`
  31. PeerURL string `json:"peerURL"`
  32. }
  33. func (si *StandbyInfo4) ClientURLs() []string {
  34. var urls []string
  35. for _, m := range si.Cluster {
  36. urls = append(urls, m.ClientURL)
  37. }
  38. return urls
  39. }
  40. func (si *StandbyInfo4) InitialCluster() string {
  41. b := &bytes.Buffer{}
  42. first := true
  43. for _, m := range si.Cluster {
  44. if !first {
  45. fmt.Fprintf(b, ",")
  46. }
  47. first = false
  48. fmt.Fprintf(b, "%s=%s", m.Name, m.PeerURL)
  49. }
  50. return b.String()
  51. }
  52. func DecodeStandbyInfo4FromFile(path string) (*StandbyInfo4, error) {
  53. var info StandbyInfo4
  54. file, err := os.OpenFile(path, os.O_RDONLY, 0600)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer file.Close()
  59. if err = json.NewDecoder(file).Decode(&info); err != nil {
  60. return nil, err
  61. }
  62. return &info, nil
  63. }