utils.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // rather than delete the entire swarm directory, delete the contents in order to preserve the inode
  37. // (for example, allowing it to be bind-mounted)
  38. files, err := ioutil.ReadDir(root)
  39. if err != nil {
  40. return err
  41. }
  42. for _, f := range files {
  43. if err := os.RemoveAll(filepath.Join(root, f.Name())); err != nil {
  44. return err
  45. }
  46. }
  47. return nil
  48. }
  49. func removingManagerCausesLossOfQuorum(reachable, unreachable int) bool {
  50. return reachable-2 <= unreachable
  51. }
  52. func isLastManager(reachable, unreachable int) bool {
  53. return reachable == 1 && unreachable == 0
  54. }