starting to reconfig config
parent
c4fecc7eeb
commit
ebd87863bc
@ -1,17 +1,16 @@
|
||||
server:
|
||||
name: "Rack Server"
|
||||
db-url: "http://192.168.100.2:8086"
|
||||
db-org: "ForeLight"
|
||||
db-token: ""
|
||||
ports:
|
||||
lis: 2022
|
||||
reactor: 2023
|
||||
tui: 2024
|
||||
db: 8086
|
||||
db:
|
||||
org: ForeLight
|
||||
token: ""
|
||||
url: http://192.168.100.2:8086
|
||||
name: Rack Server
|
||||
ports:
|
||||
db: 8086
|
||||
lis: 20000
|
||||
reactor: 2023
|
||||
tui: 2024
|
||||
reactors:
|
||||
10002123:
|
||||
name: "Beaglebone Black"
|
||||
db-token: ""
|
||||
db-bucket: ""
|
||||
|
||||
|
||||
"10002123":
|
||||
db:
|
||||
bucket: test
|
||||
token: ""
|
||||
name: Beaglebone Black
|
||||
|
@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
/*
|
||||
Package provides a way to update current config based on values passed in flags
|
||||
*/
|
@ -0,0 +1,173 @@
|
||||
package config
|
||||
|
||||
/*
|
||||
Load.go contains methods to load values from config, flags and env.
|
||||
*/
|
||||
|
||||
import (
|
||||
"FRMS/internal/pkg/logging"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
//"os"
|
||||
//"log"
|
||||
//"os/exec"
|
||||
//"bytes"
|
||||
)
|
||||
|
||||
func NewConfig(name string) (Config, error) {
|
||||
// returns a Config Structure of assocaited name
|
||||
return C, Load(name)
|
||||
}
|
||||
|
||||
type ConfigStruct interface {
|
||||
// structure to do demarshall config into
|
||||
LoadFile(string) error
|
||||
}
|
||||
|
||||
func LoadConfigFile(fname string, strct ConfigStruct) error {
|
||||
// Demarshalls a given filename into the struct
|
||||
// returns nil if successful
|
||||
logging.Debug(logging.DStart, "Loading config for %s", fname)
|
||||
viper.SetConfigName(fname)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath("/etc/frms/config")
|
||||
|
||||
// unmarshalling
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logging.Debug(logging.DStart, "CON Loaded configs from %v", viper.ConfigFileUsed())
|
||||
if err = viper.Unmarshal(strct); err != nil {
|
||||
logging.Debug(logging.DError, "Cannot unmarshall %v", err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Outcome: %#v\n \n", C)
|
||||
// unmarshalled at this point
|
||||
}
|
||||
|
||||
////
|
||||
|
||||
func LoadConfig() Config {
|
||||
return C
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetURL() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return C.Server.URL, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetOrg() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Orginization, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetPort(port string) (int, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
portString, ok := c.Server.Ports[port]
|
||||
if !ok {
|
||||
portEnv := strings.ToUpper(port) + "_PORT"
|
||||
return 0, fmt.Errorf("%s port doesnt exist! Please set using env %s=####", port, portEnv)
|
||||
}
|
||||
// returns int, err
|
||||
//return strconv.Atoi(portString)
|
||||
return portString, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetServerToken() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Token, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetReactorClient(id uint32) (string, string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
idString := strconv.FormatUint(uint64(id), 10)
|
||||
if r, ok := c.Reactors[idString]; ok {
|
||||
return r.Bucket, r.Token, nil
|
||||
}
|
||||
return "", "", fmt.Errorf("reactor %v config doesnt exist", id)
|
||||
}
|
||||
|
||||
// setters
|
||||
func (c *ServerConf) UpdateURL(url string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if url == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.URL = url
|
||||
viper.Set("server.db-url", url)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateOrg(org string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if org == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.Orginization = org
|
||||
viper.Set("server.db-org", org)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdatePort(pName string, port int) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if port < 1024 || port > 65535 {
|
||||
// OOB
|
||||
return fmt.Errorf("Port %d out of bounds! [1024,65535]", port)
|
||||
}
|
||||
if c.Server.Ports == nil {
|
||||
c.Server.Ports = make(map[string]int)
|
||||
}
|
||||
c.Server.Ports[pName] = port
|
||||
pname := fmt.Sprintf("server.ports.%s", pName)
|
||||
viper.Set(pname, port)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateServerToken(token string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if token == "" {
|
||||
return errors.New("String cannot be empty!")
|
||||
}
|
||||
c.Server.Token = token
|
||||
viper.Set("server.token", token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateReactorClient(id uint32, bucket, token string) error {
|
||||
c.Lock()
|
||||
c.Unlock()
|
||||
sid := strconv.FormatUint(uint64(id), 10)
|
||||
if token == "" || bucket == "" {
|
||||
return errors.New("String cannot be empty!")
|
||||
}
|
||||
if reactor, ok := c.Reactors[sid]; !ok {
|
||||
c.Reactors[sid] = ReactorConfig{Token: token, Bucket: bucket, Id: id}
|
||||
} else {
|
||||
reactor.Bucket = bucket
|
||||
reactor.Token = token
|
||||
c.Reactors[sid] = reactor
|
||||
}
|
||||
reactorbucket := fmt.Sprintf("%s.db-bucket", id)
|
||||
reactortoken := fmt.Sprintf("%s.db-token", id)
|
||||
viper.Set(reactorbucket, bucket)
|
||||
viper.Set(reactortoken, token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) Store() error {
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
@ -0,0 +1,180 @@
|
||||
package config
|
||||
|
||||
// package serves to implement config interface for server
|
||||
|
||||
import (
|
||||
"FRMS/internal/pkg/logging"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
//"os"
|
||||
//"log"
|
||||
//"os/exec"
|
||||
//"bytes"
|
||||
)
|
||||
|
||||
type Config interface {
|
||||
Store() error
|
||||
}
|
||||
|
||||
var C Config
|
||||
|
||||
func Load(fname string) error {
|
||||
// read stored configs
|
||||
//C = &ServerConf{}
|
||||
switch fname {
|
||||
case "server":
|
||||
C = &ServerConf{}
|
||||
case "reactor":
|
||||
C = &ReactorConf{}
|
||||
default:
|
||||
return fmt.Errorf("%s not recognized", fname)
|
||||
}
|
||||
logging.Debug(logging.DStart, "Loading config for %s", fname)
|
||||
viper.SetConfigName(fname)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath("/etc/frms/config")
|
||||
// defaults which we don't want to set nessecarily
|
||||
//viper.SetDefault("server.db-org", "ForeLight")
|
||||
//viper.SetDefault("server.db-url", "http://192.168.100.2:8086")
|
||||
//viper.SetDefault("reactor.db-url", "http://192.168.100.2:8086")
|
||||
// unmarshalling
|
||||
err := viper.ReadInConfig() // the fact i never did this is infuriating
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logging.Debug(logging.DStart, "CON Loaded configs from %v", viper.ConfigFileUsed())
|
||||
err = viper.Unmarshal(C)
|
||||
if err != nil {
|
||||
logging.Debug(logging.DError, "Cannot unmarshall Server! %v", err)
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("Outcome: %#v\n \n", C)
|
||||
//fmt.Printf("%v\n",C)
|
||||
// unmarshalled at this point
|
||||
}
|
||||
|
||||
func LoadConfig() Config {
|
||||
return C
|
||||
}
|
||||
|
||||
func (c *ServerConf) Load()
|
||||
|
||||
func (c *ServerConf) GetURL() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return C.Server.URL, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetOrg() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Orginization, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetPort(port string) (int, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
portString, ok := c.Server.Ports[port]
|
||||
if !ok {
|
||||
portEnv := strings.ToUpper(port) + "_PORT"
|
||||
return 0, fmt.Errorf("%s port doesnt exist! Please set using env %s=####", port, portEnv)
|
||||
}
|
||||
// returns int, err
|
||||
//return strconv.Atoi(portString)
|
||||
return portString, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetServerToken() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Token, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetReactorClient(id uint32) (string, string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
idString := strconv.FormatUint(uint64(id), 10)
|
||||
if r, ok := c.Reactors[idString]; ok {
|
||||
return r.Bucket, r.Token, nil
|
||||
}
|
||||
return "", "", fmt.Errorf("reactor %v config doesnt exist", id)
|
||||
}
|
||||
|
||||
// setters
|
||||
func (c *ServerConf) UpdateURL(url string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if url == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.URL = url
|
||||
viper.Set("server.db-url", url)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateOrg(org string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if org == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.Orginization = org
|
||||
viper.Set("server.db-org", org)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdatePort(pName string, port int) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if port < 1024 || port > 65535 {
|
||||
// OOB
|
||||
return fmt.Errorf("Port %d out of bounds! [1024,65535]", port)
|
||||
}
|
||||
if c.Server.Ports == nil {
|
||||
c.Server.Ports = make(map[string]int)
|
||||
}
|
||||
c.Server.Ports[pName] = port
|
||||
pname := fmt.Sprintf("server.ports.%s", pName)
|
||||
viper.Set(pname, port)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateServerToken(token string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if token == "" {
|
||||
return errors.New("String cannot be empty!")
|
||||
}
|
||||
c.Server.Token = token
|
||||
viper.Set("server.token", token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateReactorClient(id uint32, bucket, token string) error {
|
||||
c.Lock()
|
||||
c.Unlock()
|
||||
sid := strconv.FormatUint(uint64(id), 10)
|
||||
if token == "" || bucket == "" {
|
||||
return errors.New("String cannot be empty!")
|
||||
}
|
||||
if reactor, ok := c.Reactors[sid]; !ok {
|
||||
c.Reactors[sid] = ReactorConfig{Token: token, Bucket: bucket, Id: id}
|
||||
} else {
|
||||
reactor.Bucket = bucket
|
||||
reactor.Token = token
|
||||
c.Reactors[sid] = reactor
|
||||
}
|
||||
reactorbucket := fmt.Sprintf("%s.db-bucket", id)
|
||||
reactortoken := fmt.Sprintf("%s.db-token", id)
|
||||
viper.Set(reactorbucket, bucket)
|
||||
viper.Set(reactortoken, token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) Store() error {
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
@ -1,46 +1,53 @@
|
||||
package influxdb
|
||||
|
||||
import (
|
||||
_ "fmt"
|
||||
_ "github.com/influxdata/influxdb-client-go/v2"
|
||||
"errors"
|
||||
_ "fmt"
|
||||
|
||||
_ "github.com/influxdata/influxdb-client-go/v2"
|
||||
)
|
||||
|
||||
type DBClient struct {
|
||||
URL string
|
||||
Org string
|
||||
Bucket string
|
||||
Token string
|
||||
// Client *influxdb2.Client
|
||||
URL string
|
||||
Org string
|
||||
Bucket string
|
||||
Token string
|
||||
// Client *influxdb2.Client
|
||||
}
|
||||
|
||||
type DBAdmin struct {
|
||||
// struct for admin methods
|
||||
*DBClient
|
||||
// struct for admin methods
|
||||
*DBClient
|
||||
}
|
||||
|
||||
func NewDBClient(url, org, token string) *DBClient {
|
||||
db := &DBClient{URL:url, Org:org, Token:token}
|
||||
return db
|
||||
db := &DBClient{URL: url, Org: org, Token: token}
|
||||
return db
|
||||
}
|
||||
|
||||
func NewDBAdmin(url, org, token string) *DBAdmin {
|
||||
admin := &DBAdmin{}
|
||||
admin.DBClient = NewDBClient(url, org, token)
|
||||
return admin
|
||||
admin := &DBAdmin{}
|
||||
admin.DBClient = NewDBClient(url, org, token)
|
||||
return admin
|
||||
}
|
||||
|
||||
// base level funcs
|
||||
func (d *DBClient) Start() {
|
||||
// connect to DB
|
||||
// connect to DB
|
||||
}
|
||||
|
||||
func (d *DBAdmin) GetReactorClient(id string) (string, string, error) {
|
||||
// given an id returns associated token and bucket
|
||||
client := influxdb2.NewClient(d.URL,d.Token)
|
||||
defer client.Close()
|
||||
bucket, err := client.BucketsAPI().FindBucketByName(context.Background(),id)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if d.ReactorExists(id) {
|
||||
// get corresponding reactor token and bucket
|
||||
/*
|
||||
client := influxdb2.NewClient(d.URL, d.Token)
|
||||
defer client.Close()
|
||||
bucket, err := client.BucketsAPI().FindBucketByName(context.Background(), id)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if d.ReactorExists(id) {
|
||||
// get corresponding reactor token and bucket
|
||||
}
|
||||
*/
|
||||
return "", "", errors.New("Unimpl")
|
||||
}
|
||||
|
Loading…
Reference in New Issue