2018-06-30 01:45:51 +02:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2018-06-30 16:20:54 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
databaseYanic "github.com/FreifunkBremen/yanic/database"
|
|
|
|
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
|
2018-06-30 16:20:54 +02:00
|
|
|
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
|
2018-08-10 17:17:00 +02:00
|
|
|
if n.Nodeinfo == nil {
|
2018-06-30 16:20:54 +02:00
|
|
|
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-06-30 16:20:54 +02:00
|
|
|
}
|
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)
|
2018-08-10 17:17:00 +02:00
|
|
|
conn.db.Model(&lNode).Update(map[string]interface{}{"address": lNode.Address})
|
|
|
|
|
|
|
|
if lNode.Blacklist != nil && lNode.Blacklist.After(now.Add(-conn.blacklistFor)) {
|
2018-07-26 14:14:23 +02:00
|
|
|
logger.Debug("on blacklist")
|
|
|
|
return
|
|
|
|
}
|
2018-08-10 13:46:18 +02:00
|
|
|
conn.sendNode(&lNode)
|
|
|
|
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")
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
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")
|
2018-06-30 16:20:54 +02:00
|
|
|
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-06-30 16:20:54 +02:00
|
|
|
}
|
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) {
|
2018-06-30 16:20:54 +02:00
|
|
|
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 16:20:54 +02:00
|
|
|
|
2018-06-30 01:45:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *YanicDB) PruneNodes(deleteAfter time.Duration) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *YanicDB) Close() {
|
|
|
|
}
|