Skip to content
Snippets Groups Projects
Commit f75a0547 authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

include spf13/viper for config

parent e9246fb5
No related branches found
No related tags found
3 merge requests!90Develop,!88Use SPF Viper for configuration,!53V.0.1.0 Codename Threadbare
CliSocket = "localhost:55055" CliSocket = "localhost:55055"
DatabaseSocket = "bolt://172.17.0.4:7687" db.socket = "bolt://172.17.0.4:7687"
DatabaseUser = "" db.user = ""
DatabasePassword = "" db.password = ""
DatabaseCrypto = false db.crypto = false
ConfigPath = "./configs/gosdn.toml"
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/neo4j/neo4j-go-driver/neo4j" "github.com/neo4j/neo4j-go-driver/neo4j"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
) )
//Database is a database //Database is a database
...@@ -19,7 +20,11 @@ type PND struct { ...@@ -19,7 +20,11 @@ type PND struct {
} }
//NewDatabaseClient creates a database ciena //NewDatabaseClient creates a database ciena
func NewDatabaseClient(uri, username, password string, encrypted bool) Database { func NewDatabaseClient() Database {
uri := viper.GetString("db.socket")
username := viper.GetString("db.user")
password := viper.GetString("db.password")
encrypted := viper.GetBool("db.crypto")
driver := createDriver(uri, username, password, encrypted) driver := createDriver(uri, username, password, encrypted)
return Database{ return Database{
......
goarista @ 09b37ed2
Subproject commit 09b37ed24462f5dfc3c5e47abecd790c5540f327
...@@ -5,23 +5,23 @@ go 1.14 ...@@ -5,23 +5,23 @@ go 1.14
require ( require (
code.fbi.h-da.de/cocsn/swagger/apis v0.0.0-20200924152423-61030cab7b88 code.fbi.h-da.de/cocsn/swagger/apis v0.0.0-20200924152423-61030cab7b88
code.fbi.h-da.de/cocsn/yang-models v0.0.3 code.fbi.h-da.de/cocsn/yang-models v0.0.3
github.com/BurntSushi/toml v0.3.1 github.com/aristanetworks/goarista v0.0.0-20201120222254-94a892eb0c6a // indirect
github.com/aristanetworks/goarista v0.0.0-20201120222254-94a892eb0c6a
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591 github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591
github.com/go-openapi/runtime v0.19.22 github.com/go-openapi/runtime v0.19.22
github.com/go-openapi/strfmt v0.19.5 github.com/go-openapi/strfmt v0.19.5
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/protobuf v1.4.2 github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.2 github.com/google/uuid v1.1.2
github.com/neo4j/neo4j-go-driver v1.8.3 github.com/neo4j/neo4j-go-driver v1.8.3
github.com/onsi/ginkgo v1.13.0 // indirect github.com/onsi/ginkgo v1.13.0 // indirect
github.com/openconfig/gnmi v0.0.0-20200617225440-d2b4e6a45802 github.com/openconfig/gnmi v0.0.0-20200617225440-d2b4e6a45802
github.com/openconfig/reference v0.0.0-20190727015836-8dfd928c9696 github.com/openconfig/reference v0.0.0-20190727015836-8dfd928c9696 // indirect
github.com/openconfig/ygot v0.8.11 github.com/openconfig/ygot v0.8.11
github.com/rivo/tview v0.0.0-20201018122409-d551c850a743 github.com/rivo/tview v0.0.0-20201018122409-d551c850a743
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2
github.com/spf13/viper v1.7.1
github.com/tidwall/gjson v1.6.3 github.com/tidwall/gjson v1.6.3
golang.org/x/net v0.0.0-20200904194848-62affa334b73 golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
google.golang.org/grpc v1.29.1 google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.25.0 google.golang.org/protobuf v1.25.0
) )
This diff is collapsed.
...@@ -2,9 +2,10 @@ package nucleus ...@@ -2,9 +2,10 @@ package nucleus
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/database" "code.fbi.h-da.de/cocsn/gosdn/database"
"github.com/BurntSushi/toml" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os" "os"
) )
...@@ -27,15 +28,19 @@ type Core struct { ...@@ -27,15 +28,19 @@ type Core struct {
} }
//Initialize does start-up housekeeping like reading controller config files //Initialize does start-up housekeeping like reading controller config files
func (c *Core) Initialize(socket, configFileController, configFileClient string, IsRunningChannel chan bool) { func (c *Core) Initialize(IsRunningChannel chan bool) {
if err := c.readControllerConfig(configFileController); err != nil { // Set config defaults
log.Fatal(err) viper.SetDefault("socket", "localhost:55055")
}
if socket != "localhost:55055" { // Set config path and read config
c.config.CliSocket = socket viper.SetConfigName("gosdn")
viper.SetConfigType("toml")
viper.AddConfigPath("/usr/local/etc/gosdn/")
viper.AddConfigPath("./configs/")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(fmt.Errorf("Fatal error config file: %s \n", err))
} }
c.AttachDatabase() c.AttachDatabase()
c.IsRunning = IsRunningChannel c.IsRunning = IsRunningChannel
...@@ -43,7 +48,7 @@ func (c *Core) Initialize(socket, configFileController, configFileClient string, ...@@ -43,7 +48,7 @@ func (c *Core) Initialize(socket, configFileController, configFileClient string,
// AttachDatabase connects to the database and passes the connectio to the controller core // AttachDatabase connects to the database and passes the connectio to the controller core
func (c *Core) AttachDatabase() { func (c *Core) AttachDatabase() {
c.database = database.NewDatabaseClient(c.config.DatabaseSocket, c.config.DatabaseUser, c.config.DatabasePassword, c.config.DatabaseCrypto) c.database = database.NewDatabaseClient()
} }
// Shutdown waits for the shutdown signal and gracefully shuts down once it arrived // Shutdown waits for the shutdown signal and gracefully shuts down once it arrived
...@@ -51,33 +56,10 @@ func (c *Core) Shutdown() { ...@@ -51,33 +56,10 @@ func (c *Core) Shutdown() {
<-c.IsRunning <-c.IsRunning
log.Info("Received shutdown signal. Shutting down") log.Info("Received shutdown signal. Shutting down")
f, err := os.Create(c.config.ConfigPath) err := viper.WriteConfig()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
enc := toml.NewEncoder(f)
if err := enc.Encode(c.config); err != nil {
log.Fatal(err)
}
log.Info("Shutdown complete") log.Info("Shutdown complete")
os.Exit(0) os.Exit(0)
} }
\ No newline at end of file
func (c *Core) readControllerConfig(configFileController string) error {
if configFileController == "" {
configFileController = "configs/gosdn.toml"
}
if _, err := os.Stat(configFileController); err != nil {
return err
}
c.config = controllerConfig{}
if _, err := toml.DecodeFile(configFileController, &c.config); err != nil {
return err
}
if c.config.ConfigPath == "" {
c.config.ConfigPath = configFileController
}
return nil
}
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
) )
//StartAndRun is used to start the core of the controller and any auxiliary services. //StartAndRun is used to start the core of the controller and any auxiliary services.
func StartAndRun(socket, filename string, IsRunningChannel chan bool) { func StartAndRun(IsRunningChannel chan bool) {
log.Info("This is the network superintendent...") log.Info("This is the network superintendent...")
log.Info("Starting my ducks") log.Info("Starting my ducks")
...@@ -15,7 +15,7 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) { ...@@ -15,7 +15,7 @@ func StartAndRun(socket, filename string, IsRunningChannel chan bool) {
core := Core{ core := Core{
database: database.Database{}, database: database.Database{},
} }
core.Initialize(socket, filename, "", IsRunningChannel) core.Initialize(IsRunningChannel)
// Start the GRCP CLI // Start the GRCP CLI
go getCLIGoing(&core) go getCLIGoing(&core)
go core.Shutdown() go core.Shutdown()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment