freifunkmanager/runtime/yanic.go

102 lines
2.3 KiB
Go
Raw Normal View History

2018-06-30 01:45:51 +02:00
package runtime
import (
"time"
2019-06-10 02:57:36 +02:00
"github.com/bdlm/log"
2018-07-26 14:14:23 +02:00
"github.com/jinzhu/gorm"
2018-06-30 01:45:51 +02:00
databaseYanic "github.com/FreifunkBremen/yanic/database"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/freifunkmanager/ssh"
2018-06-30 01:45:51 +02:00
)
type YanicDB struct {
databaseYanic.Connection
2018-08-10 17:17:00 +02:00
blacklistFor time.Duration
db *gorm.DB
ssh *ssh.Manager
sendNode func(*Node)
sendStats func(*runtimeYanic.GlobalStats)
prefix string
2018-06-30 01:45:51 +02:00
}
2018-08-10 17:17:00 +02:00
func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, blacklistFor time.Duration, sendNode func(*Node), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB {
2018-06-30 01:45:51 +02:00
return &YanicDB{
2018-08-10 17:17:00 +02:00
db: db,
ssh: ssh,
blacklistFor: blacklistFor,
sendNode: sendNode,
sendStats: sendStats,
prefix: prefix,
2018-06-30 01:45:51 +02:00
}
}
func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
2018-08-10 17:17:00 +02:00
if n.Nodeinfo == nil {
return
}
2018-08-10 17:17:00 +02:00
now := time.Now()
logger := log.WithField("method", "LearnNode").WithField("node_id", n.Nodeinfo.NodeID)
2018-07-26 14:14:23 +02:00
lNode := Node{
2018-08-10 17:17:00 +02:00
NodeID: n.Nodeinfo.NodeID,
}
2018-07-26 14:14:23 +02:00
if conn.db.First(&lNode).Error == nil {
2018-08-10 13:46:18 +02:00
lNode.Update(n, conn.prefix)
conn.db.Model(&lNode).Update(map[string]interface{}{
"address": lNode.Address,
"lastseen": now,
})
2018-08-10 17:17:00 +02:00
2019-06-05 23:39:13 +02:00
if lNode.TimeFilter(conn.blacklistFor) {
2018-07-26 14:14:23 +02:00
logger.Debug("on blacklist")
return
}
2018-08-10 13:46:18 +02:00
if !lNode.CheckRespondd() {
2018-08-10 17:17:00 +02:00
if !lNode.SSHUpdate(conn.ssh) {
conn.db.Model(&lNode).Update(map[string]interface{}{"blacklist": &now})
logger.Warn("yanic trigger sshupdate failed - set blacklist")
return
2018-08-10 17:17:00 +02:00
}
logger.Debug("yanic trigger sshupdate again")
2018-07-27 13:40:04 +02:00
} else {
logger.Debug("yanic update")
2018-07-26 14:14:23 +02:00
}
conn.sendNode(&lNode)
return
}
2018-08-10 13:46:18 +02:00
node := NewNode(n, conn.prefix)
if node == nil {
return
}
2018-07-26 14:14:23 +02:00
_, err := conn.ssh.RunOn(node.GetAddress(), "uptime")
if err != nil {
2018-08-10 13:46:18 +02:00
logger.Debugf("set on blacklist: %s", err.Error())
2018-08-10 17:17:00 +02:00
node.Blacklist = &now
}
2018-07-26 14:14:23 +02:00
conn.db.Create(&node)
2018-08-10 17:17:00 +02:00
if node.Blacklist == nil {
2018-08-10 13:46:18 +02:00
conn.sendNode(node)
}
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 {
2018-07-26 14:14:23 +02:00
conn.sendStats(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() {
}