mirror of
https://github.com/postmannen/ctrl.git
synced 2024-12-14 12:37:31 +00:00
Creating config file, and storing flag values
This commit is contained in:
parent
35e0c12109
commit
069bf5a639
5 changed files with 135 additions and 21 deletions
18
cmd/main.go
18
cmd/main.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -12,25 +11,18 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
nodeName := flag.String("node", "0", "some unique string to identify this Edge unit")
|
||||
brokerAddress := flag.String("brokerAddress", "0", "the address of the message broker")
|
||||
profilingPort := flag.String("profilingPort", "", "The number of the profiling port")
|
||||
promHostAndPort := flag.String("promHostAndPort", ":2112", "host and port for prometheus listener, e.g. localhost:2112")
|
||||
centralErrorLogger := flag.Bool("centralErrorLogger", false, "set to true if this is the node that should receive the error log's from other nodes")
|
||||
defaultMessageTimeout := flag.Int("defaultMessageTimeout", 10, "default message timeout in seconds. This can be overridden on the message level")
|
||||
defaultMessageRetries := flag.Int("defaultMessageRetries", 0, "default amount of retries that will be done before a message is thrown away, and out of the system")
|
||||
publisherServiceSayhello := flag.Int("publisherServiceSayhello", 0, "Make the current node send hello messages to central at given interval in seconds")
|
||||
flag.Parse()
|
||||
c := steward.NewConfiguration()
|
||||
c.CheckFlags()
|
||||
|
||||
// Start profiling if profiling port is specified
|
||||
if *profilingPort != "" {
|
||||
if c.ProfilingPort != "" {
|
||||
go func() {
|
||||
http.ListenAndServe("localhost:"+*profilingPort, nil)
|
||||
http.ListenAndServe("localhost:"+c.ProfilingPort, nil)
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
s, err := steward.NewServer(*brokerAddress, *nodeName, *promHostAndPort, *centralErrorLogger, *defaultMessageTimeout, *defaultMessageRetries, *publisherServiceSayhello)
|
||||
s, err := steward.NewServer(c)
|
||||
if err != nil {
|
||||
log.Printf("error: failed to connect to broker: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
|
109
configurationAndFlags.go
Normal file
109
configurationAndFlags.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package steward
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
toml "github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
// The configuration folder on disk
|
||||
ConfigFolder string
|
||||
// some unique string to identify this Edge unit
|
||||
NodeName string
|
||||
// the address of the message broker
|
||||
BrokerAddress string
|
||||
// The number of the profiling port
|
||||
ProfilingPort string
|
||||
// host and port for prometheus listener, e.g. localhost:2112
|
||||
PromHostAndPort string
|
||||
// set to true if this is the node that should receive the error log's from other nodes
|
||||
CentralErrorLogger bool
|
||||
// default message timeout in seconds. This can be overridden on the message level")
|
||||
DefaultMessageTimeout int
|
||||
// default amount of retries that will be done before a message is thrown away, and out of the system
|
||||
DefaultMessageRetries int
|
||||
// Make the current node send hello messages to central at given interval in seconds
|
||||
PublisherServiceSayhello int
|
||||
}
|
||||
|
||||
func NewConfiguration() *Configuration {
|
||||
c := Configuration{}
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *Configuration) CheckFlags() {
|
||||
// read file config
|
||||
fc, err := c.ReadConfigFile()
|
||||
if err != nil {
|
||||
log.Printf("%v\n", err)
|
||||
}
|
||||
|
||||
//fmt.Printf("---\nContent of file\n---\n%#v\n", tmpC)
|
||||
|
||||
flag.StringVar(&c.ConfigFolder, "configFolder", fc.ConfigFolder, "")
|
||||
flag.StringVar(&c.NodeName, "node", fc.NodeName, "some unique string to identify this Edge unit")
|
||||
flag.StringVar(&c.BrokerAddress, "brokerAddress", fc.BrokerAddress, "the address of the message broker")
|
||||
flag.StringVar(&c.ProfilingPort, "profilingPort", fc.ProfilingPort, "The number of the profiling port")
|
||||
flag.StringVar(&c.PromHostAndPort, "promHostAndPort", fc.PromHostAndPort, "host and port for prometheus listener, e.g. localhost:2112")
|
||||
flag.BoolVar(&c.CentralErrorLogger, "centralErrorLogger", fc.CentralErrorLogger, "set to true if this is the node that should receive the error log's from other nodes")
|
||||
flag.IntVar(&c.DefaultMessageTimeout, "defaultMessageTimeout", fc.DefaultMessageTimeout, "default message timeout in seconds. This can be overridden on the message level")
|
||||
flag.IntVar(&c.DefaultMessageRetries, "defaultMessageRetries", fc.DefaultMessageRetries, "default amount of retries that will be done before a message is thrown away, and out of the system")
|
||||
flag.IntVar(&c.PublisherServiceSayhello, "publisherServiceSayhello", fc.PublisherServiceSayhello, "Make the current node send hello messages to central at given interval in seconds")
|
||||
flag.Parse()
|
||||
|
||||
if err := c.WriteConfigFile(); err != nil {
|
||||
log.Printf("error: checkFlags: failed writing config file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) ReadConfigFile() (Configuration, error) {
|
||||
fp := filepath.Join("./etc/", "config.toml")
|
||||
|
||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||
return Configuration{}, fmt.Errorf("error: no config file found %v: %v", fp, err)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(fp, os.O_RDONLY, 0600)
|
||||
if err != nil {
|
||||
return Configuration{}, fmt.Errorf("error: failed to open file %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var conf Configuration
|
||||
dec := toml.NewDecoder(f)
|
||||
err = dec.Decode(&conf)
|
||||
if err != nil {
|
||||
return Configuration{}, fmt.Errorf("error: decode toml file %v: %v", fp, err)
|
||||
}
|
||||
|
||||
fmt.Printf("%+v\n", c)
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func (c *Configuration) WriteConfigFile() error {
|
||||
if _, err := os.Stat(c.ConfigFolder); os.IsNotExist(err) {
|
||||
err := os.Mkdir(c.ConfigFolder, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error: failed to create directory %v: %v", c.ConfigFolder, err)
|
||||
}
|
||||
}
|
||||
|
||||
fp := filepath.Join(c.ConfigFolder, "config.toml")
|
||||
|
||||
f, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error: failed to open file %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
enc := toml.NewEncoder(f)
|
||||
enc.Encode(c)
|
||||
|
||||
return nil
|
||||
}
|
9
etc/config.toml
Normal file
9
etc/config.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
BrokerAddress = "0"
|
||||
CentralErrorLogger = true
|
||||
ConfigFolder = "./etc"
|
||||
DefaultMessageRetries = 3
|
||||
DefaultMessageTimeout = 5
|
||||
NodeName = "central"
|
||||
ProfilingPort = ""
|
||||
PromHostAndPort = ":2112"
|
||||
PublisherServiceSayhello = 0
|
Binary file not shown.
20
server.go
20
server.go
|
@ -23,6 +23,9 @@ func processNameGet(sn subjectName, pk processKind) processName {
|
|||
// server is the structure that will hold the state about spawned
|
||||
// processes on a local instance.
|
||||
type server struct {
|
||||
// Configuration options used for running the server
|
||||
configuration *Configuration
|
||||
// The nats connection to the broker
|
||||
natsConn *nats.Conn
|
||||
// TODO: sessions should probably hold a slice/map of processes ?
|
||||
processes map[processName]process
|
||||
|
@ -65,8 +68,8 @@ type server struct {
|
|||
}
|
||||
|
||||
// newServer will prepare and return a server type
|
||||
func NewServer(brokerAddress string, nodeName string, promHostAndPort string, centralErrorLogger bool, defaultMessageTimeout int, defaultMessageRetries int, sayHelloInterval int) (*server, error) {
|
||||
conn, err := nats.Connect(brokerAddress, nil)
|
||||
func NewServer(c *Configuration) (*server, error) {
|
||||
conn, err := nats.Connect(c.BrokerAddress, nil)
|
||||
if err != nil {
|
||||
log.Printf("error: nats.Connect failed: %v\n", err)
|
||||
}
|
||||
|
@ -75,18 +78,19 @@ func NewServer(brokerAddress string, nodeName string, promHostAndPort string, ce
|
|||
var coe CommandOrEvent
|
||||
|
||||
s := &server{
|
||||
nodeName: nodeName,
|
||||
configuration: c,
|
||||
nodeName: c.NodeName,
|
||||
natsConn: conn,
|
||||
processes: make(map[processName]process),
|
||||
newMessagesCh: make(chan []subjectAndMessage),
|
||||
methodsAvailable: m.GetMethodsAvailable(),
|
||||
commandOrEventAvailable: coe.GetCommandOrEventAvailable(),
|
||||
metrics: newMetrics(promHostAndPort),
|
||||
metrics: newMetrics(c.PromHostAndPort),
|
||||
subscriberServices: newSubscriberServices(),
|
||||
publisherServices: newPublisherServices(sayHelloInterval),
|
||||
centralErrorLogger: centralErrorLogger,
|
||||
defaultMessageTimeout: defaultMessageTimeout,
|
||||
defaultMessageRetries: defaultMessageRetries,
|
||||
publisherServices: newPublisherServices(c.PublisherServiceSayhello),
|
||||
centralErrorLogger: c.CentralErrorLogger,
|
||||
defaultMessageTimeout: c.DefaultMessageTimeout,
|
||||
defaultMessageRetries: c.DefaultMessageRetries,
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
|
Loading…
Reference in a new issue