2015-12-23 15:48:25 +00:00
|
|
|
package influxdb
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const nodeFile = "node.json"
|
2015-12-23 15:48:25 +00:00
|
|
|
|
|
|
|
type Node struct {
|
2015-12-30 13:15:00 +00:00
|
|
|
path string
|
|
|
|
ID uint64
|
|
|
|
MetaServers []string
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
// NewNode will load the node information from disk if present
|
|
|
|
func NewNode(path string) (*Node, error) {
|
|
|
|
n := &Node{
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filepath.Join(path, nodeFile))
|
2016-01-06 22:34:34 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return n, nil
|
2015-12-30 13:15:00 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 13:15:00 +00:00
|
|
|
// Save will save the node file to disk and replace the existing one if present
|
|
|
|
func (n *Node) Save() error {
|
|
|
|
tmpFile := filepath.Join(n.path, nodeFile, "tmp")
|
|
|
|
|
|
|
|
f, err := os.Open(tmpFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewEncoder(f).Encode(n)
|
2015-12-23 15:48:25 +00:00
|
|
|
}
|