2017-05-06 19:44:37 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
2017-05-12 21:32:10 +02:00
|
|
|
"sync"
|
2017-05-06 19:44:37 +02:00
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
|
2018-06-30 01:45:51 +02:00
|
|
|
"github.com/genofire/golang-lib/file"
|
2017-05-06 19:44:37 +02:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/freifunkmanager/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Nodes struct {
|
2018-06-30 16:20:54 +02:00
|
|
|
List map[string]*Node `json:"nodes"`
|
|
|
|
Current map[string]*Node `json:"-"`
|
|
|
|
Statistics *runtimeYanic.GlobalStats `json:"-"`
|
|
|
|
ssh *ssh.Manager
|
|
|
|
statePath string
|
|
|
|
iface string
|
|
|
|
notifyNodeFunc []func(*Node, bool)
|
|
|
|
notifyStatsFunc []func(*runtimeYanic.GlobalStats)
|
2017-05-12 21:32:10 +02:00
|
|
|
sync.Mutex
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|
|
|
|
|
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),
|
2017-05-30 02:16:46 +02:00
|
|
|
Current: make(map[string]*Node),
|
2017-05-07 03:37:30 +02:00
|
|
|
ssh: mgmt,
|
|
|
|
statePath: path,
|
|
|
|
iface: iface,
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|
2018-06-30 01:45:51 +02:00
|
|
|
file.ReadJSON(path, nodes)
|
2017-05-07 03:37:30 +02:00
|
|
|
return nodes
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (nodes *Nodes) AddNotifyNode(f func(*Node, bool)) {
|
|
|
|
nodes.notifyNodeFunc = append(nodes.notifyNodeFunc, f)
|
|
|
|
}
|
|
|
|
func (nodes *Nodes) notifyNode(node *Node, system bool) {
|
|
|
|
for _, f := range nodes.notifyNodeFunc {
|
|
|
|
f(node, system)
|
2017-07-06 08:03:37 +02:00
|
|
|
}
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (nodes *Nodes) AddNotifyStats(f func(stats *runtimeYanic.GlobalStats)) {
|
|
|
|
nodes.notifyStatsFunc = append(nodes.notifyStatsFunc, f)
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
func (nodes *Nodes) notifyStats(stats *runtimeYanic.GlobalStats) {
|
|
|
|
nodes.Statistics = stats
|
|
|
|
for _, f := range nodes.notifyStatsFunc {
|
|
|
|
f(stats)
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
2017-05-07 03:37:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 20:00:18 +02:00
|
|
|
func (nodes *Nodes) UpdateNode(node *Node) bool {
|
2017-05-08 19:13:29 +02:00
|
|
|
if node == nil {
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Warn("no new node to update")
|
2018-07-15 20:00:18 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if GetChannel(node.Wireless.Channel24) == nil {
|
|
|
|
log.Warnf("wrong wifi24 channel for '%s'", node.NodeID)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if GetChannel(node.Wireless.Channel5) == nil {
|
|
|
|
log.Warnf("wrong wifi5 channel for '%s'", node.NodeID)
|
|
|
|
return false
|
2017-05-08 19:13:29 +02:00
|
|
|
}
|
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)
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Info("update node", node.NodeID)
|
2017-05-07 03:37:30 +02:00
|
|
|
}
|
2017-05-30 02:16:46 +02:00
|
|
|
nodes.List[node.NodeID] = node
|
2018-06-30 16:20:54 +02:00
|
|
|
nodes.notifyNode(node, true)
|
2018-07-15 20:00:18 +02:00
|
|
|
return 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()
|
2017-05-30 02:16:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Info("updater per ssh")
|
2017-05-07 03:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nodes *Nodes) Saver() {
|
2017-05-12 21:32:10 +02:00
|
|
|
nodes.Lock()
|
2018-06-30 01:45:51 +02:00
|
|
|
file.SaveJSON(nodes.statePath, nodes)
|
2017-05-12 21:32:10 +02:00
|
|
|
nodes.Unlock()
|
2018-06-30 01:45:51 +02:00
|
|
|
log.Debug("saved state file")
|
2017-05-06 19:44:37 +02:00
|
|
|
}
|