utils.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package cluster
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "github.com/docker/docker/pkg/ioutils"
  8. )
  9. func loadPersistentState(root string) (*nodeStartConfig, error) {
  10. dt, err := ioutil.ReadFile(filepath.Join(root, stateFile))
  11. if err != nil {
  12. return nil, err
  13. }
  14. // missing certificate means no actual state to restore from
  15. if _, err := os.Stat(filepath.Join(root, "certificates/swarm-node.crt")); err != nil {
  16. if os.IsNotExist(err) {
  17. clearPersistentState(root)
  18. }
  19. return nil, err
  20. }
  21. var st nodeStartConfig
  22. if err := json.Unmarshal(dt, &st); err != nil {
  23. return nil, err
  24. }
  25. return &st, nil
  26. }
  27. func savePersistentState(root string, config nodeStartConfig) error {
  28. dt, err := json.Marshal(config)
  29. if err != nil {
  30. return err
  31. }
  32. return ioutils.AtomicWriteFile(filepath.Join(root, stateFile), dt, 0600)
  33. }
  34. func clearPersistentState(root string) error {
  35. // todo: backup this data instead of removing?
  36. if err := os.RemoveAll(root); err != nil {
  37. return err
  38. }
  39. if err := os.MkdirAll(root, 0700); err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func removingManagerCausesLossOfQuorum(reachable, unreachable int) bool {
  45. return reachable-2 <= unreachable
  46. }
  47. func isLastManager(reachable, unreachable int) bool {
  48. return reachable == 1 && unreachable == 0
  49. }