Use the StatePath to config when loading the state

This commit is contained in:
kaiyou 2020-05-14 12:03:09 +02:00 committed by Leo Antunes
parent a0882dad49
commit 31b8263e96
3 changed files with 18 additions and 11 deletions

View File

@ -23,16 +23,17 @@ type Cluster struct {
mlConfig *memberlist.Config mlConfig *memberlist.Config
localNode *common.Node localNode *common.Node
LocalName string LocalName string
statePath string
state *state state *state
events chan memberlist.NodeEvent events chan memberlist.NodeEvent
} }
// New is used to create a new Cluster instance // New is used to create a new Cluster instance
// The returned instance is ready to be updated with the local node settings then joined // The returned instance is ready to be updated with the local node settings then joined
func New(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool) (*Cluster, error) { func New(init bool, clusterKey []byte, bindAddr string, bindPort int, statePath string, useIPAsName bool) (*Cluster, error) {
state := &state{} state := &state{}
if !init { if !init {
loadState(state) loadState(state, statePath)
} }
clusterKey, err := computeClusterKey(state, clusterKey) clusterKey, err := computeClusterKey(state, clusterKey)
@ -63,6 +64,7 @@ func New(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsNam
// More than this many simultaneous events will deadlock cluster.members() // More than this many simultaneous events will deadlock cluster.members()
events: make(chan memberlist.NodeEvent, 100), events: make(chan memberlist.NodeEvent, 100),
state: state, state: state,
statePath: statePath,
} }
return &cluster, nil return &cluster, nil
} }
@ -94,7 +96,7 @@ func (c *Cluster) Join(addrs []string) error {
// Leave saves the current state before leaving, then leaves the cluster // Leave saves the current state before leaving, then leaves the cluster
func (c *Cluster) Leave() { func (c *Cluster) Leave() {
c.state.save() c.state.save(c.statePath)
c.ml.Leave(10 * time.Second) c.ml.Leave(10 * time.Second)
c.ml.Shutdown() //nolint: errcheck c.ml.Shutdown() //nolint: errcheck
} }
@ -144,7 +146,7 @@ func (c *Cluster) Members() <-chan []common.Node {
} }
c.state.Nodes = nodes c.state.Nodes = nodes
changes <- nodes changes <- nodes
c.state.save() c.state.save(c.statePath)
} }
}() }()
return changes return changes

View File

@ -16,10 +16,12 @@ type state struct {
Nodes []common.Node Nodes []common.Node
} }
// TODO: this should be replaced by a configurable path later const defaultStatePath = "/var/lib/wesher/state.json"
var statePath = "/var/lib/wesher/state.json"
func (s *state) save() error { func (s *state) save(statePath string) error {
if statePath == "" {
statePath = defaultStatePath
}
if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil { if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil {
return err return err
} }
@ -32,7 +34,10 @@ func (s *state) save() error {
return ioutil.WriteFile(statePath, stateOut, 0600) return ioutil.WriteFile(statePath, stateOut, 0600)
} }
func loadState(cs *state) { func loadState(cs *state, statePath string) {
if statePath == "" {
statePath = defaultStatePath
}
content, err := ioutil.ReadFile(statePath) content, err := ioutil.ReadFile(statePath)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {

View File

@ -35,7 +35,7 @@ func main() {
logrus.SetLevel(logLevel) logrus.SetLevel(logLevel)
// Create the wireguard and cluster configuration // Create the wireguard and cluster configuration
cluster, err := cluster.New(config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.UseIPAsName) cluster, err := cluster.New(config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.StatePath, config.UseIPAsName)
if err != nil { if err != nil {
logrus.WithError(err).Fatal("could not create cluster") logrus.WithError(err).Fatal("could not create cluster")
} }