freifunkmanager/runtime/yanic.go

79 lines
1.9 KiB
Go
Raw Normal View History

2018-06-30 01:45:51 +02:00
package runtime
import (
"time"
log "github.com/sirupsen/logrus"
2018-06-30 01:45:51 +02:00
databaseYanic "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/lib/jsontime"
2018-06-30 01:45:51 +02:00
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/freifunkmanager/ssh"
2018-06-30 01:45:51 +02:00
)
type YanicDB struct {
databaseYanic.Connection
nodes *Nodes
2018-06-30 01:45:51 +02:00
}
func NewYanicDB(nodes *Nodes) *YanicDB {
return &YanicDB{
nodes: nodes,
}
}
func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
node := NewNode(n)
if node == nil {
return
}
node.Lastseen = jsontime.Now()
logger := log.WithField("method", "LearnNode").WithField("node_id", node.NodeID)
conn.nodes.Lock()
defer conn.nodes.Unlock()
if lNode := conn.nodes.List[node.NodeID]; lNode != nil {
lNode.Lastseen = jsontime.Now()
lNode.Stats = node.Stats
} else {
conn.nodes.List[node.NodeID] = node
conn.nodes.notifyNode(node, true)
}
if _, ok := conn.nodes.Current[node.NodeID]; ok {
conn.nodes.Current[node.NodeID] = node
conn.nodes.notifyNode(node, false)
return
}
// session := nodes.ssh.ConnectTo(node.Address)
result, err := conn.nodes.ssh.RunOn(node.GetAddress(conn.nodes.iface), "uptime")
if err != nil {
logger.Debug("init ssh command not run", err)
return
}
uptime := ssh.SSHResultToString(result)
logger.Infof("new node with uptime: %s", uptime)
conn.nodes.Current[node.NodeID] = node
if lNode := conn.nodes.List[node.NodeID]; lNode != nil {
lNode.Address = node.Address
go lNode.SSHUpdate(conn.nodes.ssh, conn.nodes.iface, node)
}
conn.nodes.notifyNode(node, false)
2018-06-30 01:45:51 +02:00
}
func (conn *YanicDB) InsertLink(link *runtimeYanic.Link, time time.Time) {
}
func (conn *YanicDB) InsertGlobals(stats *runtimeYanic.GlobalStats, time time.Time, site string, domain string) {
if runtimeYanic.GLOBAL_SITE == site && runtimeYanic.GLOBAL_DOMAIN == domain {
conn.nodes.notifyStats(stats)
2018-06-30 01:45:51 +02:00
}
2018-06-30 01:45:51 +02:00
}
func (conn *YanicDB) PruneNodes(deleteAfter time.Duration) {
}
func (conn *YanicDB) Close() {
}