[TASK] extract seperate config to packages

This commit is contained in:
Martin Geno 2018-01-07 21:00:56 +01:00 committed by Julian Kornberger
parent ba95194dec
commit d892d1693d
35 changed files with 137 additions and 115 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/yanic/runtime/config"
)
var (
@ -16,8 +17,8 @@ var (
nodes *runtime.Nodes
)
func loadConfig() *runtime.Config {
config, err := runtime.ReadConfigFile(configPath)
func loadConfig() *config.Config {
config, err := config.ReadConfigFile(configPath)
if err != nil {
fmt.Fprintln(os.Stderr, "unable to load config file:", err)
os.Exit(2)

View File

@ -24,7 +24,7 @@ var queryCmd = &cobra.Command{
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)
defer collector.Close()

View File

@ -29,7 +29,7 @@ var serveCmd = &cobra.Command{
}
defer allDatabase.Close()
nodes = runtime.NewNodes(config)
nodes = runtime.NewNodes(&config.Nodes)
nodes.Start()
err = allOutput.Start(nodes, config.Nodes)

View File

@ -1,27 +1,30 @@
package all
import (
"sync"
"time"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
)
var conn database.Connection
var wg = sync.WaitGroup{}
var quit chan struct{}
func Start(config runtime.DatabaseConfig) (err error) {
func Start(config database.Config) (err error) {
conn, err = Connect(config.Connection)
if err != nil {
return
}
quit = make(chan struct{})
wg.Add(1)
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
return
}
func Close() {
close(quit)
wg.Wait()
conn.Close()
quit = nil
}
@ -35,6 +38,7 @@ func deleteWorker(deleteInterval time.Duration, deleteAfter time.Duration) {
conn.PruneNodes(deleteAfter)
case <-quit:
ticker.Stop()
wg.Done()
return
}
}

View File

@ -6,7 +6,7 @@ import (
"time"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/yanic/lib/duration"
"github.com/stretchr/testify/assert"
)
@ -21,8 +21,8 @@ func TestStart(t *testing.T) {
})
// Test for PruneNodes (by start)
assert.Nil(quit)
err := Start(runtime.DatabaseConfig{
DeleteInterval: runtime.Duration{Duration: time.Millisecond},
err := Start(database.Config{
DeleteInterval: duration.Duration{Duration: time.Millisecond},
Connection: map[string]interface{}{
"a": []map[string]interface{}{
map[string]interface{}{
@ -54,6 +54,7 @@ func TestStart(t *testing.T) {
assert.NoError(err)
assert.NotNil(quit)
// connection type not found
_, err = Connect(map[string]interface{}{
"e": []map[string]interface{}{
map[string]interface{}{},
@ -61,9 +62,14 @@ func TestStart(t *testing.T) {
})
assert.Error(err)
// test close
Close()
// wrong format
_, err = Connect(map[string]interface{}{
err = Start(database.Config{
Connection: map[string]interface{}{
"e": true,
},
})
assert.Error(err)
}

9
database/config.go Normal file
View 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{}
}

View File

@ -88,7 +88,7 @@ func TestGlobalStats(t *testing.T) {
}
func createTestNodes() *runtime.Nodes {
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
nodeData := &runtime.Node{
Online: true,

View File

@ -177,7 +177,7 @@ func testPoints(nodes ...*runtime.Node) (points []*client.Point) {
panic(err)
}
nodesList := runtime.NewNodes(&runtime.Config{})
nodesList := runtime.NewNodes(&runtime.NodesConfig{})
// Create dummy connection
conn := &Connection{

View File

@ -1,4 +1,4 @@
package runtime
package duration
import (
"fmt"

View File

@ -1,4 +1,4 @@
package runtime
package duration
import (
"testing"

View File

@ -1,6 +1,8 @@
package all
import "github.com/FreifunkBremen/yanic/runtime"
import (
"github.com/FreifunkBremen/yanic/runtime"
)
// Config Filter
type filterConfig map[string]interface{}
@ -13,7 +15,7 @@ func noFilter(node *runtime.Node) *runtime.Node {
// Create Filter
func (f filterConfig) filtering(nodesOrigin *runtime.Nodes) *runtime.Nodes {
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
filterfuncs := []filterFunc{
f.HasLocation(),
f.Blacklist(),

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

View File

@ -11,7 +11,7 @@ import (
func TestTransform(t *testing.T) {
assert := assert.New(t)
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
nodes.AddNode(&runtime.Node{
Online: true,
Nodeinfo: &data.NodeInfo{

View File

@ -3,7 +3,7 @@ package meshviewerFFRGB
import (
"time"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

View File

@ -10,7 +10,7 @@ import (
func TestRegister(t *testing.T) {
assert := assert.New(t)
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
node := NewNode(nodes, &runtime.Node{
Nodeinfo: &data.NodeInfo{
Owner: &data.Owner{

View File

@ -33,7 +33,7 @@ func TestGenerateGraph(t *testing.T) {
func testGetNodesByFile(files ...string) *runtime.Nodes {
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
for _, file := range files {
node := testGetNodeByFile(file)

View File

@ -2,7 +2,7 @@ package meshviewer
import (
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
)
// Node struct

View File

@ -23,7 +23,7 @@ func TestNodesV2(t *testing.T) {
}
func createTestNodes() *runtime.Nodes {
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
nodeData := &runtime.Node{
Statistics: &data.Statistics{

View File

@ -1,7 +1,7 @@
package meshviewer
import (
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

View File

@ -1,7 +1,7 @@
package meshviewer
import (
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

View File

@ -1,7 +1,7 @@
package nodelist
import (
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)

View File

@ -17,7 +17,7 @@ func TestTransform(t *testing.T) {
}
func createTestNodes() *runtime.Nodes {
nodes := runtime.NewNodes(&runtime.Config{})
nodes := runtime.NewNodes(&runtime.NodesConfig{})
nodeData := &runtime.Node{
Statistics: &data.Statistics{

View File

@ -11,7 +11,7 @@ import (
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
"github.com/FreifunkBremen/yanic/runtime"
)
@ -30,7 +30,7 @@ type 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{
db: db,

View File

@ -12,7 +12,7 @@ import (
const SITE_TEST = "ffxx"
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.Start(time.Millisecond)

View File

@ -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
View 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
}

View File

@ -1,4 +1,4 @@
package runtime
package config
import (
"testing"
@ -10,7 +10,7 @@ import (
func TestReadConfig(t *testing.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.NotNil(config)

View File

@ -4,7 +4,7 @@ import (
"net"
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
)
// Node struct

View File

@ -8,26 +8,26 @@ import (
"time"
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
)
// Nodes struct: cache DB of Node's structs
type Nodes struct {
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
ifaceToNodeID map[string]string // mapping from MAC address to NodeID
config *Config
config *NodesConfig
sync.RWMutex
}
// NewNodes create Nodes structs
func NewNodes(config *Config) *Nodes {
func NewNodes(config *NodesConfig) *Nodes {
nodes := &Nodes{
List: make(map[string]*Node),
ifaceToNodeID: make(map[string]string),
config: config,
}
if config.Nodes.StatePath != "" {
if config.StatePath != "" {
nodes.load()
}
@ -130,7 +130,7 @@ func (nodes *Nodes) NodeLinks(node *Node) (result []Link) {
// Periodically saves the cached DB to json file
func (nodes *Nodes) worker() {
c := time.Tick(nodes.config.Nodes.SaveInterval.Duration)
c := time.Tick(nodes.config.SaveInterval.Duration)
for range c {
nodes.expire()
@ -143,14 +143,14 @@ func (nodes *Nodes) expire() {
now := jsontime.Now()
// Nodes last seen before expireAfter will be removed
prunePeriod := nodes.config.Nodes.PruneAfter.Duration
prunePeriod := nodes.config.PruneAfter.Duration
if prunePeriod == 0 {
prunePeriod = time.Hour * 24 * 7 // our default
}
pruneAfter := now.Add(-prunePeriod)
// 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
nodes.Lock()
@ -194,7 +194,7 @@ func (nodes *Nodes) readIfaces(nodeinfo *data.NodeInfo) {
}
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 err = json.NewDecoder(f).Decode(nodes); err == nil {
@ -222,7 +222,7 @@ func (nodes *Nodes) save() {
defer nodes.RUnlock()
// serialize nodes
SaveJSON(nodes, nodes.config.Nodes.StatePath)
SaveJSON(nodes, nodes.config.StatePath)
}
// SaveJSON to path

11
runtime/nodes_config.go Normal file
View 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{}
}

View File

@ -9,15 +9,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/jsontime"
"github.com/FreifunkBremen/yanic/lib/jsontime"
)
func TestExpire(t *testing.T) {
assert := assert.New(t)
config := &Config{}
config.Nodes.OfflineAfter.Duration = time.Minute * 10
config := &NodesConfig{}
config.OfflineAfter.Duration = time.Minute * 10
// to get default (100%) path of testing
// config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6
// config.PruneAfter.Duration = time.Hour * 24 * 6
nodes := &Nodes{
config: config,
List: make(map[string]*Node),
@ -51,22 +51,22 @@ func TestExpire(t *testing.T) {
func TestLoadAndSave(t *testing.T) {
assert := assert.New(t)
config := &Config{}
config := &NodesConfig{}
// not autoload without StatePath
NewNodes(config)
// Test unmarshalable /dev/null - autolead with StatePath
config.Nodes.StatePath = "/dev/null"
config.StatePath = "/dev/null"
nodes := NewNodes(config)
// Test unopen able
config.Nodes.StatePath = "/root/nodes.json"
config.StatePath = "/root/nodes.json"
nodes.load()
// works ;)
config.Nodes.StatePath = "testdata/nodes.json"
config.StatePath = "testdata/nodes.json"
nodes.load()
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
config.Nodes.StatePath = tmpfile.Name()
config.StatePath = tmpfile.Name()
nodes.save()
os.Remove(tmpfile.Name())
@ -113,8 +113,8 @@ func TestUpdateNodes(t *testing.T) {
func TestSelectNodes(t *testing.T) {
assert := assert.New(t)
config := &Config{}
config.Nodes.StatePath = "testdata/nodes.json"
config := &NodesConfig{}
config.StatePath = "testdata/nodes.json"
nodes := NewNodes(config)
@ -139,7 +139,7 @@ func TestSelectNodes(t *testing.T) {
func TestAddNode(t *testing.T) {
assert := assert.New(t)
nodes := NewNodes(&Config{})
nodes := NewNodes(&NodesConfig{})
nodes.AddNode(&Node{})
assert.Len(nodes.List, 0)

View File

@ -54,7 +54,7 @@ func TestGlobalStats(t *testing.T) {
}
func createTestNodes() *Nodes {
nodes := NewNodes(&Config{})
nodes := NewNodes(&NodesConfig{})
nodeData := &Node{
Online: true,