-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
359 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ _testmain.go | |
*.prof | ||
webroot | ||
/config.toml | ||
/config-respondd.toml | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/naoina/toml" | ||
|
||
"github.com/FreifunkBremen/yanic/database" | ||
"github.com/FreifunkBremen/yanic/respond" | ||
"github.com/FreifunkBremen/yanic/runtime" | ||
"github.com/FreifunkBremen/yanic/webserver" | ||
) | ||
|
||
// Config represents the whole configuration | ||
type Config struct { | ||
Respondd respond.Config | ||
Webserver webserver.Config | ||
Nodes runtime.NodesConfig | ||
Database database.Config | ||
} | ||
|
||
var ( | ||
configPath string | ||
collector *respond.Collector | ||
nodes *runtime.Nodes | ||
) | ||
|
||
func loadConfig() *Config { | ||
config, err := ReadConfigFile(configPath) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "unable to load config file:", err) | ||
os.Exit(2) | ||
} | ||
return config | ||
} | ||
|
||
// ReadConfigFile reads a config model from path of a yml file | ||
func ReadConfigFile(path string) (config *Config, err error) { | ||
config = &Config{} | ||
|
||
func ReadConfigFile(path string, config interface{}) error { | ||
file, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
err = toml.Unmarshal(file, config) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
return | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/bdlm/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/FreifunkBremen/yanic/respond/daemon" | ||
) | ||
|
||
// serveCmd represents the serve command | ||
var responddCMD = &cobra.Command{ | ||
Use: "respondd", | ||
Short: "Runs a respond daemon", | ||
Example: "yanic respondd --config /etc/respondd.toml", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
daemon := &respondd.Daemon{} | ||
if err := ReadConfigFile(configPath, daemon); err != nil { | ||
log.Panicf("unable to load config file: %s", err) | ||
} | ||
|
||
go daemon.Start() | ||
|
||
log.Info("respondd daemon started") | ||
// Wait for INT/TERM | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
sig := <-sigs | ||
log.Infof("received %s", sig) | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(responddCMD) | ||
responddCMD.Flags().StringVarP(&configPath, "config", "c", "config-respondd.toml", "Path to configuration file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# how ofter the cache respond of a respondd request is calculated | ||
data_interval = "3m" | ||
|
||
# if set true, respond will contain data from batman interface | ||
multi_instance = false | ||
|
||
[[listen]] | ||
address = "ff02::2:1001" | ||
interface = "" | ||
port = 1001 | ||
|
||
# manuelle data for respond | ||
[data.nodeinfo.location] | ||
latitude = 53.112446246 | ||
longitude = 8.734087944 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Unit] | ||
Description=yanic | ||
|
||
[Service] | ||
Type=simple | ||
User=yanic | ||
ExecStart=/opt/go/bin/yanic respondd --config /etc/respondd.conf | ||
Restart=always | ||
RestartSec=5s | ||
Environment=PATH=/usr/bin:/usr/local/bin | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package respondd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/FreifunkBremen/yanic/data" | ||
) | ||
|
||
func trim(s string) string { | ||
return strings.TrimSpace(strings.Trim(s, "\n")) | ||
} | ||
|
||
func (d *Daemon) updateData() { | ||
nodeID := "" | ||
// Nodeinfo | ||
if d.Data.Nodeinfo == nil { | ||
d.Data.Nodeinfo = &data.Nodeinfo{} | ||
} else { | ||
nodeID = d.Data.Nodeinfo.NodeID | ||
} | ||
if d.Data.Nodeinfo.Hostname == "" { | ||
d.Data.Nodeinfo.Hostname, _ = os.Hostname() | ||
} | ||
|
||
// Statistics | ||
if d.Data.Statistics == nil { | ||
d.Data.Statistics = &data.Statistics{} | ||
} else if nodeID == "" { | ||
nodeID = d.Data.Statistics.NodeID | ||
} | ||
|
||
// Neighbours | ||
if d.Data.Neighbours == nil { | ||
d.Data.Neighbours = &data.Neighbours{} | ||
} else if nodeID == "" { | ||
nodeID = d.Data.Neighbours.NodeID | ||
} | ||
|
||
if nodeID == "" && !d.MultiInstance { | ||
if v, err := ioutil.ReadFile("/etc/machine-id"); err == nil { | ||
nodeID = trim(string(v))[:12] | ||
} | ||
} | ||
d.Data.Nodeinfo.NodeID = nodeID | ||
d.Data.Statistics.NodeID = nodeID | ||
d.Data.Neighbours.NodeID = nodeID | ||
|
||
for _, data := range d.dataByInterface { | ||
data.Nodeinfo = d.Data.Nodeinfo | ||
} | ||
} | ||
|
||
func (d *Daemon) getData(iface string) *data.ResponseData { | ||
if !d.MultiInstance { | ||
return d.Data | ||
} | ||
if data, ok := d.dataByInterface[iface]; ok { | ||
return data | ||
} | ||
d.dataByInterface[iface] = &data.ResponseData{} | ||
d.updateData() | ||
return d.dataByInterface[iface] | ||
} |
Oops, something went wrong.