[TASK] extract seperate config to packages
This commit is contained in:
parent
ba95194dec
commit
d892d1693d
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/FreifunkBremen/yanic/database"
|
"github.com/FreifunkBremen/yanic/database"
|
||||||
"github.com/FreifunkBremen/yanic/respond"
|
"github.com/FreifunkBremen/yanic/respond"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
|
"github.com/FreifunkBremen/yanic/runtime/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -16,8 +17,8 @@ var (
|
|||||||
nodes *runtime.Nodes
|
nodes *runtime.Nodes
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfig() *runtime.Config {
|
func loadConfig() *config.Config {
|
||||||
config, err := runtime.ReadConfigFile(configPath)
|
config, err := config.ReadConfigFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "unable to load config file:", err)
|
fmt.Fprintln(os.Stderr, "unable to load config file:", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
|
@ -24,7 +24,7 @@ var queryCmd = &cobra.Command{
|
|||||||
|
|
||||||
log.Printf("Sending request address=%s iface=%s", dstAddress, iface)
|
log.Printf("Sending request address=%s iface=%s", dstAddress, iface)
|
||||||
|
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
collector := respond.NewCollector(nil, nodes, []string{}, []string{iface}, 0)
|
collector := respond.NewCollector(nil, nodes, []string{}, []string{iface}, 0)
|
||||||
defer collector.Close()
|
defer collector.Close()
|
||||||
|
@ -29,7 +29,7 @@ var serveCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
defer allDatabase.Close()
|
defer allDatabase.Close()
|
||||||
|
|
||||||
nodes = runtime.NewNodes(config)
|
nodes = runtime.NewNodes(&config.Nodes)
|
||||||
nodes.Start()
|
nodes.Start()
|
||||||
|
|
||||||
err = allOutput.Start(nodes, config.Nodes)
|
err = allOutput.Start(nodes, config.Nodes)
|
||||||
|
@ -1,27 +1,30 @@
|
|||||||
package all
|
package all
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/database"
|
"github.com/FreifunkBremen/yanic/database"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var conn database.Connection
|
var conn database.Connection
|
||||||
|
var wg = sync.WaitGroup{}
|
||||||
var quit chan struct{}
|
var quit chan struct{}
|
||||||
|
|
||||||
func Start(config runtime.DatabaseConfig) (err error) {
|
func Start(config database.Config) (err error) {
|
||||||
conn, err = Connect(config.Connection)
|
conn, err = Connect(config.Connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
quit = make(chan struct{})
|
quit = make(chan struct{})
|
||||||
|
wg.Add(1)
|
||||||
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
|
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Close() {
|
func Close() {
|
||||||
close(quit)
|
close(quit)
|
||||||
|
wg.Wait()
|
||||||
conn.Close()
|
conn.Close()
|
||||||
quit = nil
|
quit = nil
|
||||||
}
|
}
|
||||||
@ -35,6 +38,7 @@ func deleteWorker(deleteInterval time.Duration, deleteAfter time.Duration) {
|
|||||||
conn.PruneNodes(deleteAfter)
|
conn.PruneNodes(deleteAfter)
|
||||||
case <-quit:
|
case <-quit:
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
wg.Done()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/database"
|
"github.com/FreifunkBremen/yanic/database"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/lib/duration"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,8 +21,8 @@ func TestStart(t *testing.T) {
|
|||||||
})
|
})
|
||||||
// Test for PruneNodes (by start)
|
// Test for PruneNodes (by start)
|
||||||
assert.Nil(quit)
|
assert.Nil(quit)
|
||||||
err := Start(runtime.DatabaseConfig{
|
err := Start(database.Config{
|
||||||
DeleteInterval: runtime.Duration{Duration: time.Millisecond},
|
DeleteInterval: duration.Duration{Duration: time.Millisecond},
|
||||||
Connection: map[string]interface{}{
|
Connection: map[string]interface{}{
|
||||||
"a": []map[string]interface{}{
|
"a": []map[string]interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
@ -54,6 +54,7 @@ func TestStart(t *testing.T) {
|
|||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.NotNil(quit)
|
assert.NotNil(quit)
|
||||||
|
|
||||||
|
// connection type not found
|
||||||
_, err = Connect(map[string]interface{}{
|
_, err = Connect(map[string]interface{}{
|
||||||
"e": []map[string]interface{}{
|
"e": []map[string]interface{}{
|
||||||
map[string]interface{}{},
|
map[string]interface{}{},
|
||||||
@ -61,9 +62,14 @@ func TestStart(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
|
|
||||||
|
// test close
|
||||||
|
Close()
|
||||||
|
|
||||||
// wrong format
|
// wrong format
|
||||||
_, err = Connect(map[string]interface{}{
|
err = Start(database.Config{
|
||||||
|
Connection: map[string]interface{}{
|
||||||
"e": true,
|
"e": true,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
}
|
}
|
||||||
|
9
database/config.go
Normal file
9
database/config.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import "github.com/FreifunkBremen/yanic/lib/duration"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
DeleteInterval duration.Duration `toml:"delete_interval"` // Delete stats of nodes every n minutes
|
||||||
|
DeleteAfter duration.Duration `toml:"delete_after"` // Delete stats of nodes till now-deletetill n minutes
|
||||||
|
Connection map[string]interface{}
|
||||||
|
}
|
@ -88,7 +88,7 @@ func TestGlobalStats(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTestNodes() *runtime.Nodes {
|
func createTestNodes() *runtime.Nodes {
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
nodeData := &runtime.Node{
|
nodeData := &runtime.Node{
|
||||||
Online: true,
|
Online: true,
|
||||||
|
@ -177,7 +177,7 @@ func testPoints(nodes ...*runtime.Node) (points []*client.Point) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nodesList := runtime.NewNodes(&runtime.Config{})
|
nodesList := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
// Create dummy connection
|
// Create dummy connection
|
||||||
conn := &Connection{
|
conn := &Connection{
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package runtime
|
package duration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
@ -1,4 +1,4 @@
|
|||||||
package runtime
|
package duration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
@ -1,6 +1,8 @@
|
|||||||
package all
|
package all
|
||||||
|
|
||||||
import "github.com/FreifunkBremen/yanic/runtime"
|
import (
|
||||||
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
// Config Filter
|
// Config Filter
|
||||||
type filterConfig map[string]interface{}
|
type filterConfig map[string]interface{}
|
||||||
@ -13,7 +15,7 @@ func noFilter(node *runtime.Node) *runtime.Node {
|
|||||||
|
|
||||||
// Create Filter
|
// Create Filter
|
||||||
func (f filterConfig) filtering(nodesOrigin *runtime.Nodes) *runtime.Nodes {
|
func (f filterConfig) filtering(nodesOrigin *runtime.Nodes) *runtime.Nodes {
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
filterfuncs := []filterFunc{
|
filterfuncs := []filterFunc{
|
||||||
f.HasLocation(),
|
f.HasLocation(),
|
||||||
f.Blacklist(),
|
f.Blacklist(),
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
func TestTransform(t *testing.T) {
|
func TestTransform(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
nodes.AddNode(&runtime.Node{
|
nodes.AddNode(&runtime.Node{
|
||||||
Online: true,
|
Online: true,
|
||||||
Nodeinfo: &data.NodeInfo{
|
Nodeinfo: &data.NodeInfo{
|
||||||
|
@ -3,7 +3,7 @@ package meshviewerFFRGB
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func TestRegister(t *testing.T) {
|
func TestRegister(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
node := NewNode(nodes, &runtime.Node{
|
node := NewNode(nodes, &runtime.Node{
|
||||||
Nodeinfo: &data.NodeInfo{
|
Nodeinfo: &data.NodeInfo{
|
||||||
Owner: &data.Owner{
|
Owner: &data.Owner{
|
||||||
|
@ -33,7 +33,7 @@ func TestGenerateGraph(t *testing.T) {
|
|||||||
|
|
||||||
func testGetNodesByFile(files ...string) *runtime.Nodes {
|
func testGetNodesByFile(files ...string) *runtime.Nodes {
|
||||||
|
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
node := testGetNodeByFile(file)
|
node := testGetNodeByFile(file)
|
||||||
|
@ -2,7 +2,7 @@ package meshviewer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Node struct
|
// Node struct
|
||||||
|
@ -23,7 +23,7 @@ func TestNodesV2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTestNodes() *runtime.Nodes {
|
func createTestNodes() *runtime.Nodes {
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
nodeData := &runtime.Node{
|
nodeData := &runtime.Node{
|
||||||
Statistics: &data.Statistics{
|
Statistics: &data.Statistics{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package meshviewer
|
package meshviewer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package meshviewer
|
package meshviewer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package nodelist
|
package nodelist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ func TestTransform(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTestNodes() *runtime.Nodes {
|
func createTestNodes() *runtime.Nodes {
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
nodeData := &runtime.Node{
|
nodeData := &runtime.Node{
|
||||||
Statistics: &data.Statistics{
|
Statistics: &data.Statistics{
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/database"
|
"github.com/FreifunkBremen/yanic/database"
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ type Collector struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCollector creates a Collector struct
|
// NewCollector creates a Collector struct
|
||||||
func NewCollector(db database.Connection, nodes *runtime.Nodes, sites []string , ifaces []string, port int) *Collector {
|
func NewCollector(db database.Connection, nodes *runtime.Nodes, sites []string, ifaces []string, port int) *Collector {
|
||||||
|
|
||||||
coll := &Collector{
|
coll := &Collector{
|
||||||
db: db,
|
db: db,
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
const SITE_TEST = "ffxx"
|
const SITE_TEST = "ffxx"
|
||||||
|
|
||||||
func TestCollector(t *testing.T) {
|
func TestCollector(t *testing.T) {
|
||||||
nodes := runtime.NewNodes(&runtime.Config{})
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
||||||
|
|
||||||
collector := NewCollector(nil, nodes, []string{SITE_TEST}, []string{}, 10001)
|
collector := NewCollector(nil, nodes, []string{SITE_TEST}, []string{}, 10001)
|
||||||
collector.Start(time.Millisecond)
|
collector.Start(time.Millisecond)
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
package runtime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Config the config File of this daemon
|
|
||||||
type Config struct {
|
|
||||||
Respondd struct {
|
|
||||||
Enable bool `toml:"enable"`
|
|
||||||
Synchronize Duration `toml:"synchronize"`
|
|
||||||
Interfaces []string `toml:"interfaces"`
|
|
||||||
Sites []string `toml:"sites"`
|
|
||||||
Port int `toml:"port"`
|
|
||||||
CollectInterval Duration `toml:"collect_interval"`
|
|
||||||
}
|
|
||||||
Webserver struct {
|
|
||||||
Enable bool `toml:"enable"`
|
|
||||||
Bind string `toml:"bind"`
|
|
||||||
Webroot string `toml:"webroot"`
|
|
||||||
}
|
|
||||||
Nodes NodesConfig
|
|
||||||
Database DatabaseConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
type NodesConfig struct {
|
|
||||||
StatePath string `toml:"state_path"`
|
|
||||||
SaveInterval Duration `toml:"save_interval"` // Save nodes periodically
|
|
||||||
OfflineAfter Duration `toml:"offline_after"` // Set node to offline if not seen within this period
|
|
||||||
PruneAfter Duration `toml:"prune_after"` // Remove nodes after n days of inactivity
|
|
||||||
Output map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type DatabaseConfig struct {
|
|
||||||
DeleteInterval Duration `toml:"delete_interval"` // Delete stats of nodes every n minutes
|
|
||||||
DeleteAfter Duration `toml:"delete_after"` // Delete stats of nodes till now-deletetill n minutes
|
|
||||||
Connection map[string]interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadConfigFile reads a config model from path of a yml file
|
|
||||||
func ReadConfigFile(path string) (config *Config, err error) {
|
|
||||||
config = &Config{}
|
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = toml.Unmarshal(file, config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
46
runtime/config/config.go
Normal file
46
runtime/config/config.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/FreifunkBremen/yanic/database"
|
||||||
|
"github.com/FreifunkBremen/yanic/lib/duration"
|
||||||
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Config the config File of this daemon
|
||||||
|
type Config struct {
|
||||||
|
Respondd struct {
|
||||||
|
Enable bool `toml:"enable"`
|
||||||
|
Synchronize duration.Duration `toml:"synchronize"`
|
||||||
|
Interfaces []string `toml:"interfaces"`
|
||||||
|
Sites []string `toml:"sites"`
|
||||||
|
Port int `toml:"port"`
|
||||||
|
CollectInterval duration.Duration `toml:"collect_interval"`
|
||||||
|
}
|
||||||
|
Webserver struct {
|
||||||
|
Enable bool `toml:"enable"`
|
||||||
|
Bind string `toml:"bind"`
|
||||||
|
Webroot string `toml:"webroot"`
|
||||||
|
}
|
||||||
|
Nodes runtime.NodesConfig
|
||||||
|
Database database.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadConfigFile reads a config model from path of a yml file
|
||||||
|
func ReadConfigFile(path string) (config *Config, err error) {
|
||||||
|
config = &Config{}
|
||||||
|
|
||||||
|
file, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = toml.Unmarshal(file, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package runtime
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -10,7 +10,7 @@ import (
|
|||||||
func TestReadConfig(t *testing.T) {
|
func TestReadConfig(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
config, err := ReadConfigFile("../config_example.toml")
|
config, err := ReadConfigFile("../../config_example.toml")
|
||||||
assert.NoError(err, "no error during reading")
|
assert.NoError(err, "no error during reading")
|
||||||
assert.NotNil(config)
|
assert.NotNil(config)
|
||||||
|
|
@ -4,7 +4,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Node struct
|
// Node struct
|
||||||
|
@ -8,26 +8,26 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Nodes struct: cache DB of Node's structs
|
// Nodes struct: cache DB of Node's structs
|
||||||
type Nodes struct {
|
type Nodes struct {
|
||||||
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
|
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
|
||||||
ifaceToNodeID map[string]string // mapping from MAC address to NodeID
|
ifaceToNodeID map[string]string // mapping from MAC address to NodeID
|
||||||
config *Config
|
config *NodesConfig
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNodes create Nodes structs
|
// NewNodes create Nodes structs
|
||||||
func NewNodes(config *Config) *Nodes {
|
func NewNodes(config *NodesConfig) *Nodes {
|
||||||
nodes := &Nodes{
|
nodes := &Nodes{
|
||||||
List: make(map[string]*Node),
|
List: make(map[string]*Node),
|
||||||
ifaceToNodeID: make(map[string]string),
|
ifaceToNodeID: make(map[string]string),
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Nodes.StatePath != "" {
|
if config.StatePath != "" {
|
||||||
nodes.load()
|
nodes.load()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +130,7 @@ func (nodes *Nodes) NodeLinks(node *Node) (result []Link) {
|
|||||||
|
|
||||||
// Periodically saves the cached DB to json file
|
// Periodically saves the cached DB to json file
|
||||||
func (nodes *Nodes) worker() {
|
func (nodes *Nodes) worker() {
|
||||||
c := time.Tick(nodes.config.Nodes.SaveInterval.Duration)
|
c := time.Tick(nodes.config.SaveInterval.Duration)
|
||||||
|
|
||||||
for range c {
|
for range c {
|
||||||
nodes.expire()
|
nodes.expire()
|
||||||
@ -143,14 +143,14 @@ func (nodes *Nodes) expire() {
|
|||||||
now := jsontime.Now()
|
now := jsontime.Now()
|
||||||
|
|
||||||
// Nodes last seen before expireAfter will be removed
|
// Nodes last seen before expireAfter will be removed
|
||||||
prunePeriod := nodes.config.Nodes.PruneAfter.Duration
|
prunePeriod := nodes.config.PruneAfter.Duration
|
||||||
if prunePeriod == 0 {
|
if prunePeriod == 0 {
|
||||||
prunePeriod = time.Hour * 24 * 7 // our default
|
prunePeriod = time.Hour * 24 * 7 // our default
|
||||||
}
|
}
|
||||||
pruneAfter := now.Add(-prunePeriod)
|
pruneAfter := now.Add(-prunePeriod)
|
||||||
|
|
||||||
// Nodes last seen within OfflineAfter are changed to 'offline'
|
// Nodes last seen within OfflineAfter are changed to 'offline'
|
||||||
offlineAfter := now.Add(-nodes.config.Nodes.OfflineAfter.Duration)
|
offlineAfter := now.Add(-nodes.config.OfflineAfter.Duration)
|
||||||
|
|
||||||
// Locking foo
|
// Locking foo
|
||||||
nodes.Lock()
|
nodes.Lock()
|
||||||
@ -194,7 +194,7 @@ func (nodes *Nodes) readIfaces(nodeinfo *data.NodeInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (nodes *Nodes) load() {
|
func (nodes *Nodes) load() {
|
||||||
path := nodes.config.Nodes.StatePath
|
path := nodes.config.StatePath
|
||||||
|
|
||||||
if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer
|
if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer
|
||||||
if err = json.NewDecoder(f).Decode(nodes); err == nil {
|
if err = json.NewDecoder(f).Decode(nodes); err == nil {
|
||||||
@ -222,7 +222,7 @@ func (nodes *Nodes) save() {
|
|||||||
defer nodes.RUnlock()
|
defer nodes.RUnlock()
|
||||||
|
|
||||||
// serialize nodes
|
// serialize nodes
|
||||||
SaveJSON(nodes, nodes.config.Nodes.StatePath)
|
SaveJSON(nodes, nodes.config.StatePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveJSON to path
|
// SaveJSON to path
|
||||||
|
11
runtime/nodes_config.go
Normal file
11
runtime/nodes_config.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package runtime
|
||||||
|
|
||||||
|
import "github.com/FreifunkBremen/yanic/lib/duration"
|
||||||
|
|
||||||
|
type NodesConfig struct {
|
||||||
|
StatePath string `toml:"state_path"`
|
||||||
|
SaveInterval duration.Duration `toml:"save_interval"` // Save nodes periodically
|
||||||
|
OfflineAfter duration.Duration `toml:"offline_after"` // Set node to offline if not seen within this period
|
||||||
|
PruneAfter duration.Duration `toml:"prune_after"` // Remove nodes after n days of inactivity
|
||||||
|
Output map[string]interface{}
|
||||||
|
}
|
@ -9,15 +9,15 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExpire(t *testing.T) {
|
func TestExpire(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
config := &Config{}
|
config := &NodesConfig{}
|
||||||
config.Nodes.OfflineAfter.Duration = time.Minute * 10
|
config.OfflineAfter.Duration = time.Minute * 10
|
||||||
// to get default (100%) path of testing
|
// to get default (100%) path of testing
|
||||||
// config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6
|
// config.PruneAfter.Duration = time.Hour * 24 * 6
|
||||||
nodes := &Nodes{
|
nodes := &Nodes{
|
||||||
config: config,
|
config: config,
|
||||||
List: make(map[string]*Node),
|
List: make(map[string]*Node),
|
||||||
@ -51,22 +51,22 @@ func TestExpire(t *testing.T) {
|
|||||||
func TestLoadAndSave(t *testing.T) {
|
func TestLoadAndSave(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
config := &Config{}
|
config := &NodesConfig{}
|
||||||
// not autoload without StatePath
|
// not autoload without StatePath
|
||||||
NewNodes(config)
|
NewNodes(config)
|
||||||
|
|
||||||
// Test unmarshalable /dev/null - autolead with StatePath
|
// Test unmarshalable /dev/null - autolead with StatePath
|
||||||
config.Nodes.StatePath = "/dev/null"
|
config.StatePath = "/dev/null"
|
||||||
nodes := NewNodes(config)
|
nodes := NewNodes(config)
|
||||||
// Test unopen able
|
// Test unopen able
|
||||||
config.Nodes.StatePath = "/root/nodes.json"
|
config.StatePath = "/root/nodes.json"
|
||||||
nodes.load()
|
nodes.load()
|
||||||
// works ;)
|
// works ;)
|
||||||
config.Nodes.StatePath = "testdata/nodes.json"
|
config.StatePath = "testdata/nodes.json"
|
||||||
nodes.load()
|
nodes.load()
|
||||||
|
|
||||||
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
|
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
|
||||||
config.Nodes.StatePath = tmpfile.Name()
|
config.StatePath = tmpfile.Name()
|
||||||
nodes.save()
|
nodes.save()
|
||||||
os.Remove(tmpfile.Name())
|
os.Remove(tmpfile.Name())
|
||||||
|
|
||||||
@ -113,8 +113,8 @@ func TestUpdateNodes(t *testing.T) {
|
|||||||
func TestSelectNodes(t *testing.T) {
|
func TestSelectNodes(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
config := &Config{}
|
config := &NodesConfig{}
|
||||||
config.Nodes.StatePath = "testdata/nodes.json"
|
config.StatePath = "testdata/nodes.json"
|
||||||
|
|
||||||
nodes := NewNodes(config)
|
nodes := NewNodes(config)
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ func TestSelectNodes(t *testing.T) {
|
|||||||
|
|
||||||
func TestAddNode(t *testing.T) {
|
func TestAddNode(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
nodes := NewNodes(&Config{})
|
nodes := NewNodes(&NodesConfig{})
|
||||||
|
|
||||||
nodes.AddNode(&Node{})
|
nodes.AddNode(&Node{})
|
||||||
assert.Len(nodes.List, 0)
|
assert.Len(nodes.List, 0)
|
||||||
|
@ -54,7 +54,7 @@ func TestGlobalStats(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createTestNodes() *Nodes {
|
func createTestNodes() *Nodes {
|
||||||
nodes := NewNodes(&Config{})
|
nodes := NewNodes(&NodesConfig{})
|
||||||
|
|
||||||
nodeData := &Node{
|
nodeData := &Node{
|
||||||
Online: true,
|
Online: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user