freifunkmanager/runtime/nodes.go

128 lines
3.0 KiB
Go
Raw Normal View History

package runtime
import (
2017-05-07 03:37:30 +02:00
"encoding/json"
"os"
2017-05-12 21:32:10 +02:00
"sync"
2017-05-12 21:32:10 +02:00
"github.com/FreifunkBremen/yanic/jsontime"
yanic "github.com/FreifunkBremen/yanic/runtime"
2017-05-29 22:55:38 +02:00
"github.com/genofire/golang-lib/log"
"github.com/FreifunkBremen/freifunkmanager/ssh"
)
type Nodes struct {
2017-05-08 19:13:29 +02:00
List map[string]*Node `json:"nodes"`
Current map[string]*Node `json:"-"`
2017-05-08 19:13:29 +02:00
ssh *ssh.Manager
statePath string
iface string
notifyFunc []func(*Node, bool)
2017-05-12 21:32:10 +02:00
sync.Mutex
}
2017-05-07 03:37:30 +02:00
func NewNodes(path string, iface string, mgmt *ssh.Manager) *Nodes {
nodes := &Nodes{
List: make(map[string]*Node),
Current: make(map[string]*Node),
2017-05-07 03:37:30 +02:00
ssh: mgmt,
statePath: path,
iface: iface,
}
2017-05-07 03:37:30 +02:00
nodes.load()
return nodes
}
func (nodes *Nodes) LearnNode(n *yanic.Node) {
2017-05-07 03:37:30 +02:00
node := NewNode(n)
if node == nil {
return
}
2017-05-12 21:32:10 +02:00
node.Lastseen = jsontime.Now()
logger := log.Log.WithField("method", "LearnNode").WithField("node_id", node.NodeID)
2017-05-12 21:32:10 +02:00
nodes.Lock()
defer nodes.Unlock()
if lNode := nodes.List[node.NodeID]; lNode != nil {
lNode.Lastseen = jsontime.Now()
lNode.Stats = node.Stats
} else {
nodes.List[node.NodeID] = node
nodes.notify(node, true)
}
if _, ok := nodes.Current[node.NodeID]; ok {
nodes.Current[node.NodeID] = node
nodes.notify(node, false)
return
}
2017-05-07 03:37:30 +02:00
// session := nodes.ssh.ConnectTo(node.Address)
result, err := nodes.ssh.RunOn(node.GetAddress(nodes.iface), "uptime")
if err != nil {
logger.Error("init ssh command not run", err)
return
}
uptime := ssh.SSHResultToString(result)
logger.Infof("new node with uptime: %s", uptime)
2017-05-07 03:37:30 +02:00
nodes.Current[node.NodeID] = node
if lNode := nodes.List[node.NodeID]; lNode != nil {
lNode.Address = node.Address
go lNode.SSHUpdate(nodes.ssh, nodes.iface, node)
}
nodes.notify(node, false)
2017-05-08 19:13:29 +02:00
}
func (nodes *Nodes) AddNotify(f func(*Node, bool)) {
nodes.notifyFunc = append(nodes.notifyFunc, f)
}
func (nodes *Nodes) notify(node *Node, system bool) {
2017-05-08 19:13:29 +02:00
for _, f := range nodes.notifyFunc {
f(node, system)
2017-05-08 19:13:29 +02:00
}
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) UpdateNode(node *Node) {
2017-05-08 19:13:29 +02:00
if node == nil {
log.Log.Warn("no new node to update")
return
}
2017-05-12 21:32:10 +02:00
nodes.Lock()
defer nodes.Unlock()
2017-05-07 03:37:30 +02:00
if n, ok := nodes.List[node.NodeID]; ok {
2017-07-06 09:17:19 +02:00
node.Address = n.Address
2017-05-07 03:37:30 +02:00
go node.SSHUpdate(nodes.ssh, nodes.iface, n)
}
nodes.List[node.NodeID] = node
nodes.notify(node, true)
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) Updater() {
2017-05-12 21:32:10 +02:00
nodes.Lock()
defer nodes.Unlock()
for nodeid, node := range nodes.List {
if n, ok := nodes.Current[nodeid]; ok {
2017-07-06 09:17:19 +02:00
go node.SSHUpdate(nodes.ssh, nodes.iface, n)
2017-05-07 03:37:30 +02:00
}
}
2017-05-08 19:13:29 +02:00
log.Log.Debug("updater per ssh runs")
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) load() {
if f, err := os.Open(nodes.statePath); err == nil { // transform data to legacy meshviewer
if err = json.NewDecoder(f).Decode(nodes); err == nil {
2017-05-08 19:13:29 +02:00
log.Log.Infof("loaded %d nodes", len(nodes.List))
2017-05-07 03:37:30 +02:00
} else {
log.Log.Error("failed to unmarshal nodes:", err)
}
} else {
log.Log.Error("failed to load cached nodes:", err)
}
}
func (nodes *Nodes) Saver() {
2017-05-12 21:32:10 +02:00
nodes.Lock()
2017-05-07 03:37:30 +02:00
yanic.SaveJSON(nodes, nodes.statePath)
2017-05-12 21:32:10 +02:00
nodes.Unlock()
2017-05-08 19:13:29 +02:00
log.Log.Debug("saved state file")
}