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"
|
2018-06-30 16:20:54 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2018-06-30 01:45:51 +02:00
|
|
|
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-07-26 14:14:23 +02:00
|
|
|
db *gorm.DB
|
|
|
|
ssh *ssh.Manager
|
|
|
|
sendNode func(*Node, bool)
|
|
|
|
sendStats func(*runtimeYanic.GlobalStats)
|
|
|
|
prefix string
|
2018-06-30 01:45:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node, bool), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB {
|
2018-06-30 01:45:51 +02:00
|
|
|
return &YanicDB{
|
2018-07-26 14:14:23 +02:00
|
|
|
db: db,
|
|
|
|
ssh: ssh,
|
|
|
|
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-07-24 15:07:11 +02:00
|
|
|
node := NewNode(n, conn.prefix)
|
2018-06-30 16:20:54 +02:00
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node.Lastseen = jsontime.Now()
|
|
|
|
logger := log.WithField("method", "LearnNode").WithField("node_id", node.NodeID)
|
2018-07-26 14:14:23 +02:00
|
|
|
lNode := Node{
|
|
|
|
NodeID: node.NodeID,
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|
2018-07-26 14:14:23 +02:00
|
|
|
if conn.db.First(&lNode).Error == nil {
|
|
|
|
conn.db.Model(&lNode).Update(map[string]interface{}{
|
|
|
|
"Lastseen": jsontime.Now(),
|
|
|
|
//"StatsWireless": node.StatsWireless,
|
|
|
|
//"StatsClients": node.StatsClients,
|
|
|
|
"Address": node.Address,
|
|
|
|
})
|
|
|
|
if lNode.Blacklist {
|
|
|
|
logger.Debug("on blacklist")
|
|
|
|
return
|
|
|
|
}
|
2018-07-27 13:40:04 +02:00
|
|
|
conn.sendNode(node, false)
|
2018-07-26 14:14:23 +02:00
|
|
|
if !node.IsEqual(&lNode) {
|
|
|
|
lNode.SSHUpdate(conn.ssh, node)
|
2018-07-27 13:40:04 +02:00
|
|
|
logger.Debug("yanic trigger sshupdate again")
|
|
|
|
} else {
|
|
|
|
logger.Debug("yanic update")
|
2018-07-26 14:14:23 +02:00
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
return
|
|
|
|
}
|
2018-07-26 14:14:23 +02:00
|
|
|
|
|
|
|
node.Lastseen = jsontime.Now()
|
|
|
|
|
|
|
|
_, err := conn.ssh.RunOn(node.GetAddress(), "uptime")
|
2018-06-30 16:20:54 +02:00
|
|
|
if err != nil {
|
2018-07-26 14:14:23 +02:00
|
|
|
logger.Debug("set on blacklist")
|
|
|
|
node.Blacklist = true
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|
2018-07-26 14:14:23 +02:00
|
|
|
conn.db.Create(&node)
|
|
|
|
conn.sendNode(node, true)
|
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() {
|
|
|
|
}
|