server launches with proper config for itself and grafana, added storing config on exit or changes

main
Keegan 2 years ago
parent 429ce92207
commit 1c2868daf8

@ -1,17 +1,20 @@
package main package main
import ( import (
"fmt"
_ "net/http" _ "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os/signal"
"strconv" "strconv"
"strings" "strings"
"syscall"
//"flag" //"flag"
//"log" //"log"
"os"
"fmt"
"FRMS/internal/pkg/logging"
"FRMS/internal/pkg/config" "FRMS/internal/pkg/config"
"FRMS/internal/pkg/logging"
"FRMS/internal/pkg/server" "FRMS/internal/pkg/server"
"os"
) )
type coordinator interface { type coordinator interface {
@ -29,6 +32,7 @@ func LoadConfig(fname string) Config {
type Config interface { type Config interface {
UpdatePort(string, int) error UpdatePort(string, int) error
Store() error
} }
func main() { func main() {
@ -37,8 +41,10 @@ func main() {
// go func() { // go func() {
// fmt.Println(http.ListenAndServe("localhost:6060",nil)) // fmt.Println(http.ListenAndServe("localhost:6060",nil))
// }() // }()
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
conf := LoadConfig("server") conf := LoadConfig("server")
ch := make(chan error) errCh := make(chan error)
// checking env // checking env
envVars := os.Environ() envVars := os.Environ()
for _, envString := range envVars { for _, envString := range envVars {
@ -61,10 +67,18 @@ func main() {
} }
//fmt.Printf("Listening on %v\n", lport) //fmt.Printf("Listening on %v\n", lport)
c := NewCoordinator(ch) c := NewCoordinator(errCh)
go c.Start() go c.Start()
fmt.Println("Server Active!")
logging.Debug(logging.DStart, "CCO 01 Server started") logging.Debug(logging.DStart, "CCO 01 Server started")
err := <-ch // blocking to wait for any errors and keep alive otherwise select {
case err := <-errCh: // blocking to wait for any errors and keep alive otherwise
panic(err)
case <-gracefulShutdown:
err := conf.Store()
if err != nil {
panic(err) panic(err)
} }
fmt.Println("Stored config successfully. Exiting...")
os.Exit(0)
}
}

@ -8,12 +8,16 @@ services:
ports: ports:
- "2022:2022" - "2022:2022"
- "2023:2023" - "2023:2023"
- "2024:2024"
volumes: volumes:
- ./logs:/log - ./logs:/log
- server-config:/configs - server-config:/etc/frms/config
environment: environment:
- LOGTYPE=SERVER - LOGTYPE=SERVER
- VERBOSE=1 - VERBOSE=1
- LIS_PORT=2022
- REACTOR_PORT=2023
- TUI_PORT=2024
depends_on: depends_on:
- db - db
db: db:
@ -32,7 +36,7 @@ services:
- DOCKER_INFLUXDB_INIT_USERNAME=admin - DOCKER_INFLUXDB_INIT_USERNAME=admin
- DOCKER_INFLUXDB_INIT_PASSWORD=F0r3l1ght - DOCKER_INFLUXDB_INIT_PASSWORD=F0r3l1ght
- DOCKER_INFLUXDB_INIT_ORG=ForeLight - DOCKER_INFLUXDB_INIT_ORG=ForeLight
- DOCKER_INFLUXDB_INIT_BUCKET=default - DOCKER_INFLUXDB_INIT_BUCKET=test
grafana: grafana:
image: grafana/grafana-oss:latest image: grafana/grafana-oss:latest
ports: ports:
@ -40,6 +44,8 @@ services:
volumes: volumes:
- grafana-provisioning:/etc/grafana/provisioning - grafana-provisioning:/etc/grafana/provisioning
- grafana-data:/var/lib/grafana - grafana-data:/var/lib/grafana
depends_on:
- db
volumes: volumes:
grafana-data: grafana-data:
grafana-provisioning: grafana-provisioning:

@ -1,12 +1,14 @@
#!/bin/bash #!/bin/bash
#DB_URL=$(cat "$INFLUX_CONFIGS_PATH" | awk '/url/ {print $3}' | head -n 1)
DB_URL="frms-db-1:8086"
TOKEN=$(influx auth list --user ${DOCKER_INFLUXDB_INIT_USER_ID} --hide-headers | cut -f 3) TOKEN=$(influx auth list --user ${DOCKER_INFLUXDB_INIT_USER_ID} --hide-headers | cut -f 3)
ORG=$(influx org list | grep ${DOCKER_INFLUXDB_INIT_ORG_ID} | awk '{print $2}') ORG=$(influx org list | grep ${DOCKER_INFLUXDB_INIT_ORG_ID} | awk '{print $2}')
# creating starting server YAML # creating starting server YAML
echo -e "server:\n\tdb-url:${INFLUX_HOST}\n\tdb-org:${ORG}\n\tdb-token:${TOKEN}" >/configs/server.yaml; echo -e "server:\n db-url: ${DB_URL}\n db-org: ${ORG}\n db-token: ${TOKEN}" >/configs/server.yaml;
# creating grafana yaml # creating grafana yaml
influx user create -n grafana -o ${ORG} influx user create -n grafana -o ${ORG}
GRAFANA_USER_ID=$(influx user list --hide-headers --name grafana) GRAFANA_TOKEN=$(influx auth list --user grafana --hide-headers | cut -f 3)
GRAFANA_TOKEN=$(influx auth list --user ${GRAFANA_USER_ID} --hide-headers | cut -f 3) echo -e "apiVersion: 1\n\ndeleteDatasources:\n\ndatasources:\n - name: INFLUXDB\n type: influxdb\n access: proxy\n url: ${DB_URL}\n jsonData:\n httpMode: GET\n httpHeaderName1: 'Authorization'\n secureJsonData:\n httpHeaderValue1: 'Token ${GRAFANA_TOKEN}'" >/grafana/datasources/datasource.yaml
echo -e "datasources:\n\t- name: INFLUXDB\n\ttype: influxdb\n\turl:${INFLUX_HOST}\n\tdatabase: test\n\t jsonData:\n\t\thttpMode: GET\n\t\thttpHeaderName1: 'Authorization'\n\tsecureJsonData:\n\t\thttpHeaderValue1: 'Token ${GRAFANA_TOKEN}'"

@ -3,13 +3,14 @@ package config
// package serves to store/load config files for server // package serves to store/load config files for server
import ( import (
"fmt"
"strconv"
"github.com/spf13/viper"
"FRMS/internal/pkg/logging" "FRMS/internal/pkg/logging"
"errors" "errors"
"sync" "fmt"
"strconv"
"strings" "strings"
"sync"
"github.com/spf13/viper"
//"os" //"os"
//"log" //"log"
//"os/exec" //"os/exec"
@ -44,15 +45,17 @@ func Load(fname string) {
C = &Config{} C = &Config{}
viper.SetConfigName(fname) viper.SetConfigName(fname)
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.AddConfigPath("./configs") viper.AddConfigPath("/etc/frms/config") //
//viper.AddConfigPath("../../internal/configs")
// defaults // defaults
viper.SetDefault("server.db-org", "ForeLight") viper.SetDefault("server.db-org", "ForeLight")
viper.SetDefault("server.db-url", "http://192.168.100.2:8086") viper.SetDefault("server.db-url", "http://192.168.100.2:8086")
// unmarshalling // unmarshalling
viper.ReadInConfig() // the fact i never did this is infuriating 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()) logging.Debug(logging.DStart, "CON Loaded configs from %v", viper.ConfigFileUsed())
err := viper.Unmarshal(C) err = viper.Unmarshal(C)
if err != nil { if err != nil {
logging.Debug(logging.DError, "Cannot unmarshall Server! %v", err) logging.Debug(logging.DError, "Cannot unmarshall Server! %v", err)
panic(err) panic(err)
@ -115,7 +118,8 @@ func (c *Config) UpdateURL(url string) error {
return errors.New("String cannot be empty!") return errors.New("String cannot be empty!")
} }
c.Server.URL = url c.Server.URL = url
return viper.WriteConfig() viper.Set("server.db-url", url)
return viper.WriteConfigAs(viper.ConfigFileUsed())
} }
func (c *Config) UpdateOrg(org string) error { func (c *Config) UpdateOrg(org string) error {
@ -125,7 +129,8 @@ func (c *Config) UpdateOrg(org string) error {
return errors.New("String cannot be empty!") return errors.New("String cannot be empty!")
} }
c.Server.Orginization = org c.Server.Orginization = org
return viper.WriteConfig() viper.Set("server.db-org", org)
return viper.WriteConfigAs(viper.ConfigFileUsed())
} }
func (c *Config) UpdatePort(pName string, port int) error { func (c *Config) UpdatePort(pName string, port int) error {
@ -135,8 +140,13 @@ func (c *Config) UpdatePort(pName string, port int) error {
// OOB // OOB
return fmt.Errorf("Port %d out of bounds! [1024,65535]", port) 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 c.Server.Ports[pName] = port
return nil pname := fmt.Sprintf("server.ports.%s", pName)
viper.Set(pname, port)
return viper.WriteConfigAs(viper.ConfigFileUsed())
} }
func (c *Config) UpdateServerToken(token string) error { func (c *Config) UpdateServerToken(token string) error {
@ -146,7 +156,8 @@ func (c *Config) UpdateServerToken(token string) error {
return errors.New("String cannot be empty!") return errors.New("String cannot be empty!")
} }
c.Server.Token = token c.Server.Token = token
return viper.WriteConfig() viper.Set("server.token", token)
return viper.WriteConfigAs(viper.ConfigFileUsed())
} }
func (c *Config) UpdateReactorClient(id uint32, bucket, token string) error { func (c *Config) UpdateReactorClient(id uint32, bucket, token string) error {
@ -163,6 +174,13 @@ func (c *Config) UpdateReactorClient(id uint32, bucket, token string) error {
reactor.Token = token reactor.Token = token
c.Reactors[sid] = reactor c.Reactors[sid] = reactor
} }
return viper.WriteConfig() 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 *Config) Store() error {
return viper.WriteConfigAs(viper.ConfigFileUsed())
}

@ -7,6 +7,7 @@ import (
type DBClient struct { type DBClient struct {
URL string URL string
Org string
Bucket string Bucket string
Token string Token string
// Client *influxdb2.Client // Client *influxdb2.Client
@ -17,18 +18,29 @@ type DBAdmin struct {
*DBClient *DBClient
} }
func NewDBClient(url, bucket, token string) *DBClient { func NewDBClient(url, org, token string) *DBClient {
db := &DBClient{URL:url, Bucket:bucket, Token:token} db := &DBClient{URL:url, Org:org, Token:token}
return db return db
} }
func NewDBAdmin(url, bucket, token string) *DBAdmin { func NewDBAdmin(url, org, token string) *DBAdmin {
admin := &DBAdmin{} admin := &DBAdmin{}
admin.DBClient = NewDBClient(url, bucket, token) admin.DBClient = NewDBClient(url, org, token)
return admin return admin
} }
// base level funcs // base level funcs
func (d *DBClient) Start() { func (d *DBClient) Start() {
// connect to DB // connect to DB
// d.Client = influxdb2.NewClient(d.URL,d.Token)
} }
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

@ -1,17 +1,18 @@
package server package server
import ( import (
"sync"
"fmt"
"net"
"context"
"errors"
"FRMS/internal/pkg/logging"
"google.golang.org/grpc"
pb "FRMS/internal/pkg/grpc"
"FRMS/internal/pkg/config" "FRMS/internal/pkg/config"
pb "FRMS/internal/pkg/grpc"
"FRMS/internal/pkg/influxdb"
_ "FRMS/internal/pkg/influxdb" _ "FRMS/internal/pkg/influxdb"
"FRMS/internal/pkg/logging"
"context"
"errors"
"fmt"
"net"
"sync"
"google.golang.org/grpc"
) )
// this package creates the central coordiantor and sub coordiantors for clients // this package creates the central coordiantor and sub coordiantors for clients
@ -33,26 +34,25 @@ type Config interface { // PROPOSED RENAMING: ServerConfig to avoid confusion w/
GetReactorClient(uint32) (string, string, error) // ret (bucket, token, err) GetReactorClient(uint32) (string, string, error) // ret (bucket, token, err)
// setters // setters
// save on write // save on write
UpdateURL(string) error //UpdateURL(string) error
UpdateOrg(string) error //UpdateOrg(string) error
UpdateServerToken(string) error //UpdateServerToken(string) error
UpdateReactorClient(uint32, string, string) error // call (id, bucket, token) UpdateReactorClient(uint32, string, string) error // call (id, bucket, token)
} }
// db client interface // db client interface
type DB interface { type DB interface {
// getters (all create if doesnt exist) // getters (all create if doesnt exist)
GetToken() (string, error) // returns admin token (Creates if it doesnt exist) //GetToken() (string, error) // returns admin token (Creates if it doesnt exist)
GetReactorClient(uint32) (string, string, error) // returns (bucket, token, err) GetReactorClient(string) (string, string, error) // returns (bucket, token, err)
// delete // delete
DeleteReactorClient(uint32) error // removes client token but maintains bucket DeleteReactorClient(string) error // removes client token but maintains bucket
PurgeReactorClientData(uint32) error // perm deletes all assocaited reactor data (token, bucket etc) PurgeReactorClientData(string) error // perm deletes all assocaited reactor data (token, bucket etc)
} }
/*func NewDBClient() DBClient { func NewDBAdmin(url, org, token string) DB {
return influxdb.NewServerClient() return influxdb.NewDBAdmin(token, org, url)
}*/ }
type CentralCoordinator struct { type CentralCoordinator struct {
ClientConnections *ClientPacket ClientConnections *ClientPacket
@ -102,23 +102,11 @@ func (c *CentralCoordinator) LoadCfg() {
if err != nil { if err != nil {
logging.Debug(logging.DError, "CCO 01 Err: %v", err) logging.Debug(logging.DError, "CCO 01 Err: %v", err)
c.Err <- err c.Err <- err
} else if token == "" {
if token, err = c.DB.GetToken(); err != nil {
logging.Debug(logging.DError,"CCO 01 Err: %v", err)
c.Err <-err
}
c.Config.UpdateServerToken(token)
} }
org, err := c.Config.GetOrg() org, err := c.Config.GetOrg()
if err != nil { if err != nil {
logging.Debug(logging.DError, "CCO 01 Err: %v", err) logging.Debug(logging.DError, "CCO 01 Err: %v", err)
c.Err <- err c.Err <- err
} else if token == "" {
if token, err = c.DB.GetToken(); err != nil {
logging.Debug(logging.DError,"CCO 01 Err: %v", err)
c.Err <-err
}
c.Config.UpdateOrg(org)
} }
} }
@ -170,7 +158,6 @@ type ManagerInterface interface {
Register() Register()
} }
type GeneralManager interface { type GeneralManager interface {
// used by sub coordinator to interact with manager // used by sub coordinator to interact with manager
Start() Start()
@ -354,4 +341,3 @@ func (t *tuiCoordinator) DeleteReactorDevice(ctx context.Context, req *pb.Delete
// TODO // TODO
return &pb.DeleteReactorDeviceResponse{}, nil return &pb.DeleteReactorDeviceResponse{}, nil
} }

Loading…
Cancel
Save