github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/cluster/utils.go (about)

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