influxdb/node.go

51 lines
849 B
Go
Raw Normal View History

2015-12-23 15:48:25 +00:00
package influxdb
import (
"encoding/json"
"os"
"path/filepath"
)
const nodeFile = "node.json"
2015-12-23 15:48:25 +00:00
type Node struct {
path string
ID uint64
MetaServers []string
2015-12-23 15:48:25 +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))
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
return n, nil
}
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
}
// 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
}