2018-02-05 21:05:59 +00:00
|
|
|
package cluster // import "github.com/docker/docker/daemon/cluster"
|
2016-11-16 22:17:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loadPersistentState(root string) (*nodeStartConfig, error) {
|
2021-08-24 10:10:50 +00:00
|
|
|
dt, err := os.ReadFile(filepath.Join(root, stateFile))
|
2016-11-16 22:17:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// missing certificate means no actual state to restore from
|
|
|
|
if _, err := os.Stat(filepath.Join(root, "certificates/swarm-node.crt")); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
clearPersistentState(root)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var st nodeStartConfig
|
|
|
|
if err := json.Unmarshal(dt, &st); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &st, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func savePersistentState(root string, config nodeStartConfig) error {
|
|
|
|
dt, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(root, stateFile), dt, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearPersistentState(root string) error {
|
|
|
|
// todo: backup this data instead of removing?
|
2017-02-22 21:26:41 +00:00
|
|
|
// rather than delete the entire swarm directory, delete the contents in order to preserve the inode
|
|
|
|
// (for example, allowing it to be bind-mounted)
|
2021-08-24 10:10:50 +00:00
|
|
|
files, err := os.ReadDir(root)
|
2017-02-22 21:26:41 +00:00
|
|
|
if err != nil {
|
2016-11-16 22:17:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-22 21:26:41 +00:00
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
if err := os.RemoveAll(filepath.Join(root, f.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-16 22:17:18 +00:00
|
|
|
}
|
2017-02-22 21:26:41 +00:00
|
|
|
|
2016-11-16 22:17:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removingManagerCausesLossOfQuorum(reachable, unreachable int) bool {
|
|
|
|
return reachable-2 <= unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLastManager(reachable, unreachable int) bool {
|
|
|
|
return reachable == 1 && unreachable == 0
|
|
|
|
}
|