Skip to content
Snippets Groups Projects
Commit 8425282d authored by Andre Sterba's avatar Andre Sterba
Browse files

Add environment to decide which config should be loaded

See merge request !275
parent b9dc38cf
No related branches found
No related tags found
2 merge requests!275Add environment to decide which config should be loaded,!264WIP: Develop
Pipeline #98851 passed
...@@ -13,3 +13,9 @@ clab-gosdn_csbi_arista_base/ ...@@ -13,3 +13,9 @@ clab-gosdn_csbi_arista_base/
coverage.out coverage.out
report.xml report.xml
# artifacts
controller/gosdn
cli/gosdnc
# testing
controller/configs/testing-gosdn.toml
...@@ -29,16 +29,16 @@ clean: ...@@ -29,16 +29,16 @@ clean:
rm -f $(BINARY_NAME) rm -f $(BINARY_NAME)
start: clean build start: clean build
./$(BINARY_NAME) -l debug ENVIRONMENT=development ./$(BINARY_NAME) -l debug
start-insecure: clean build start-insecure: clean build
./$(BINARY_NAME) -l debug -s insecure ENVIRONMENT=development ./$(BINARY_NAME) -l debug -s insecure
unit-test: install-tools unit-test: install-tools
./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out ENVIRONMENT=testing ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out
controller-test: install-tools controller-test: install-tools
./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun ENVIRONMENT=testing ./$(TOOLS_DIR)/gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun
lint: install-tools lint: install-tools
./$(TOOLS_DIR)/golangci-lint run --config .gitlab/ci/.golangci-config/.golangci.yml ./$(TOOLS_DIR)/golangci-lint run --config .gitlab/ci/.golangci-config/.golangci.yml
...@@ -50,12 +50,13 @@ ci-lint: ...@@ -50,12 +50,13 @@ ci-lint:
golangci-lint run --config ../.gitlab/ci/.golangci-config/.golangci.yml --out-format code-climate | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"' golangci-lint run --config ../.gitlab/ci/.golangci-config/.golangci.yml --out-format code-climate | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
ci-unit-test: ci-install-tools ci-unit-test: ci-install-tools
gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out ENVIRONMENT=testing gotestsum --junitfile report.xml --format testname -- -short -race $$( go list ./... | grep -v /forks/ | grep -v /mocks ) -v -coverprofile=coverage.out
ci-controller-test: ci-install-tools ci-controller-test: ci-install-tools
gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun ENVIRONMENT=testing gotestsum --junitfile report.xml --format testname -- -race -v -run TestRun
integration-test-nucleus: integration-test-nucleus:
ENVIRONMENT=testing &&\
cd ./test/integration &&\ cd ./test/integration &&\
go test -race -v -run TestGnmi_SetIntegration &&\ go test -race -v -run TestGnmi_SetIntegration &&\
go test -race -v -run TestGnmi_GetIntegration &&\ go test -race -v -run TestGnmi_GetIntegration &&\
...@@ -63,5 +64,6 @@ integration-test-nucleus: ...@@ -63,5 +64,6 @@ integration-test-nucleus:
go test -race -v -run TestGnmi_CapabilitiesIntegration go test -race -v -run TestGnmi_CapabilitiesIntegration
integration-test-api: integration-test-api:
ENVIRONMENT=testing &&\
cd ./api &&\ cd ./api &&\
go test -race -v -run TestApiIntegration go test -race -v -run TestApiIntegration
...@@ -33,10 +33,12 @@ package cmd ...@@ -33,10 +33,12 @@ package cmd
import ( import (
"context" "context"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"code.fbi.h-da.de/danet/gosdn/controller" "code.fbi.h-da.de/danet/gosdn/controller"
"code.fbi.h-da.de/danet/gosdn/controller/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
...@@ -96,10 +98,14 @@ func initConfig() { ...@@ -96,10 +98,14 @@ func initConfig() {
// Use config file from the flag. // Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
env := config.DetermineConfigEnvironment()
log.Infof("environment is %s\n", env)
viper.AddConfigPath(configHome) viper.AddConfigPath(configHome)
viper.AddConfigPath("/usr/local/etc/gosdn/") viper.AddConfigPath("/usr/local/etc/gosdn/")
viper.SetConfigType(configType) viper.SetConfigType(configType)
viper.SetConfigName(configName) viper.SetConfigName(fmt.Sprintf("%s-%s", env, configName))
} }
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match
......
package config
import (
"os"
"strings"
)
// Environment is used to determine the environment the controller is running in.
type Environment int64
const (
// Development is used for local development
Development Environment = iota
// Production is used for production deployments
Production
// Testing is used for tests
Testing
)
func (e Environment) String() string {
switch e {
case Development:
return "development"
case Production:
return "production"
case Testing:
return "testing"
}
return "development"
}
// DetermineConfigEnvironment returns the environment mode the controller should use.
func DetermineConfigEnvironment() Environment {
env := os.Getenv("ENVIRONMENT")
switch strings.ToLower(env) {
case "development":
return Development
case "production":
return Production
case "testing":
return Testing
default:
return Development
}
}
basepnduuid = "d4dd9fcb-7fe3-4657-aa33-f9071b2ae257"
basesouthboundtype = 1
basesouthbounduuid = "a325693a-aa16-45fc-965d-08c986c476c1"
basepnduuid = "5f20f34b-cbd0-4511-9ddc-c50cf6a3b49d"
basesouthboundtype = 1
basesouthbounduuid = "ca29311a-3b17-4385-96f8-515b602a97ac"
cli_pnd = "0455b241-5863-4660-ad15-dfde7617738e"
cli_sbi = "a249f2d2-f7da-481d-8a99-b7f11471e0af"
config = ""
csbi-orchestrator = "localhost:55056"
grpc-port = ""
help = false
log-level = "debug"
pnduuid = "bf8160d4-4659-4a1b-98fd-f409a04111ec"
socket = ":55055"
databaseConnection = "mongodb://root:example@localhost:27017"
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
const ( const (
configHome string = "./configs" configHome string = "./configs"
configName string = "gosdn" configName string = "ci-testing-gosdn"
configType string = "toml" configType string = "toml"
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment